ratchets

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 0ac5f34d9f609c11a9fff4edff64dad10d2ce954
parent 156f4855d0615e191c49811f913b6f03324228b6
Author: Andrew Laack <andrew@laack.co>
Date:   Fri, 20 Jun 2025 00:18:34 -0500

Improved messages to be readable in short summary view with PyTest, and in their entirety with a full description

Diffstat:
MREADME.md | 6+++++-
Mexamples/example_test_ratchet.py | 23++++++++++++++---------
Msrc/ratchets/abstracted_tests.py | 4++--
3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md @@ -16,7 +16,7 @@ pip install ratchets ## Optional -This is only required if you plan to use Ratchets with PyTest. +**Note:** This is only required if you plan to use Ratchets with PyTest. ```bash pip install pytest @@ -57,11 +57,14 @@ except: except: recover()""" ] +description = "Bare except clauses catch all exceptions indiscriminately. This can hide bugs and important exceptions. To mitigate this, explicitly state the exception types that will be handled in the except clause." ``` The valid and invalid entries are not necessary, but we provide a CLI utility, executable with ```python3 -m ratchets.validate```, to verify the regular expressions don't exist in the valid string and do exist in the invalid string. If you are testing a .toml file that is not the repository default, specify it with ```python3 -m ratchets.validate -t FILENAME```. +The description entry is also optional, but if provided, it will be included in the output of failing PyTest tests. + ## ratchet.shell These are tests that run against each file where each evaluation is of the form: @@ -79,6 +82,7 @@ The standard output of the command is assumed to describe infractions, and the n [ratchet.shell.line_too_long] command = "xargs -n1 awk 'length($0) > 88'" +description = "Black sets the max line-width to 88 to help with the readability of code. Ensure all lines have <89 characters. You can run 'black FILENAME' to fix this issue." ``` diff --git a/examples/example_test_ratchet.py b/examples/example_test_ratchet.py @@ -22,25 +22,30 @@ def test_shell_rule(test_name: str, test_dict: dict) -> None: # def test_all_regex_rules(): # """Runs a test for all regex rules.""" # errors = [] +# descriptions = [] # for test_name, rule in get_regex_tests().items(): # try: # check_regex_rule(test_name, rule) # except Exception as e: -# errors.append(f"{test_name}: {e}") -# except Exception as e: -# errors.append(f"{test_name}: unexpected error: {e!r}") +# desc = rule['description'] +# if desc is not None: +# descriptions.append(test_name + " - " + desc) +# errors.append(f"{test_name}") # if errors: -# pytest.fail("Some regex rules failed:\n" + "\n".join(errors)) -# +# pytest.fail(" - ".join(errors) + "\n\n" + "\n\n".join(descriptions)) +# # def test_all_shell_rules(): # """Runs a test for all shell rules.""" # errors = [] +# descriptions = [] # for test_name, test_dict in get_shell_tests().items(): # try: # check_shell_rule(test_name, test_dict) # except Exception as e: -# errors.append(f"{test_name}: {e}") -# except Exception as e: -# errors.append(f"{test_name}: unexpected error: {e!r}") +# desc = test_dict['description'] +# if not desc is None: +# desc = "(" + desc + ")" +# descriptions.append(test_name + " - " + desc) +# errors.append(test_name) # if errors: -# pytest.fail("Some shell rules failed:\n" + "\n".join(errors)) +# pytest.fail(" - ".join(errors) + "\n\n" + "\n\n".join(descriptions)) diff --git a/src/ratchets/abstracted_tests.py b/src/ratchets/abstracted_tests.py @@ -112,7 +112,7 @@ def check_regex_rule(test_name: str, rule: Dict[str, Any]) -> None: description = "" raise Exception( - f"Regex infractions for '{test_name}' increased: baseline={baseline_count}, current={current_count}" + f"'{test_name}' increased from {baseline_count} to {current_count}" + ". " + str(description) ) @@ -131,6 +131,6 @@ def check_shell_rule(test_name: str, test_dict: Dict[str, Any]) -> None: if description is None: description = "" raise Exception( - f"Shell infractions for '{test_name}' increased: baseline={baseline_count}, current={current_count}" + f"'{test_name}' increased from {baseline_count} to {current_count}" + ". " + str(description) )