ratchets

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

commit e360cbbf46cd04f550946028e513aeca72cbdf99
parent 082e64eee9509a18741e9dc835348f2e6ac55f48
Author: Andrew Laack <andrew@laack.co>
Date:   Thu, 19 Jun 2025 23:43:22 -0500

Added description to .toml file and added it to exceptions thrown when running pytest

Diffstat:
Msrc/ratchets/abstracted_tests.py | 19+++++++++++--------
Msrc/ratchets/run_tests.py | 14++++++++++----
Msrc/ratchets/validate.py | 1-
Mtests.toml | 6++++++
Mtests/test_files/test_files.py | 1+
5 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/ratchets/abstracted_tests.py b/src/ratchets/abstracted_tests.py @@ -107,12 +107,13 @@ def check_regex_rule(test_name: str, rule: Dict[str, Any]) -> None: baseline_counts = get_baseline_counts() baseline_count = baseline_counts.get(test_name, 0) if current_count > baseline_count: - details = "\n".join( - f"{r.get('file')}:{r.get('line')} — {r.get('content')}" for r in matches - ) + description = rule.get('description') + if description is None: + description = "" + raise Exception( - f"Regex violations for '{test_name}' increased: baseline={baseline_count}, current={current_count}\n" - + details + f"Regex infractions for '{test_name}' increased: baseline={baseline_count}, current={current_count}" + + ". " + str(description) ) @@ -126,8 +127,10 @@ def check_shell_rule(test_name: str, test_dict: Dict[str, Any]) -> None: baseline_counts = get_baseline_counts() baseline_count = baseline_counts.get(test_name, 0) if current_count > baseline_count: - details = "\n".join(f"{r.get('file')} — {r.get('content')}" for r in matches) + description = test_dict.get('description') + if description is None: + description = "" raise Exception( - f"shell violations for '{test_name}' increased: baseline={baseline_count}, current={current_count}\n" - + details + f"Shell infractions for '{test_name}' increased: baseline={baseline_count}, current={current_count}" + + ". " + str(description) ) diff --git a/src/ratchets/run_tests.py b/src/ratchets/run_tests.py @@ -365,7 +365,9 @@ def print_issues_with_blames( for test_name, matches in issues_dict.items(): if matches: sorted_matches = sorted( - matches, key=lambda m: _parse_time(m.get("blame_time")), reverse=True + matches, + key=lambda m: _parse_time(m.get("blame_time")), + reverse=True, ) print() print( @@ -487,7 +489,6 @@ def add_blames( return test_issues, shell_issues - def expand_paths(file_args: Optional[List[str]]) -> Optional[List[str]]: """Expands glob patterns and directories into a list of file paths.""" if not file_args: @@ -506,9 +507,10 @@ def expand_paths(file_args: Optional[List[str]]) -> Optional[List[str]]: expanded_paths.append(str(path)) else: print(f"Warning: '{item}' does not exist or is not valid.") - + return expanded_paths if expanded_paths else None + def cli(): """Primary entry point for CLI usage, providing parsing and function calls.""" parser = argparse.ArgumentParser(description="Python ratchet testing") @@ -574,7 +576,11 @@ def cli(): paths = expand_paths(path_files) if paths is not None: - paths = [path for path in paths if Path(path).suffix == ".py" and Path(path).is_file()] + paths = [ + path + for path in paths + if Path(path).suffix == ".py" and Path(path).is_file() + ] excludes_path = get_excludes_path() diff --git a/src/ratchets/validate.py b/src/ratchets/validate.py @@ -62,4 +62,3 @@ if __name__ == "__main__": file: Optional[str] = args.toml_file if validate(file): print("Your .toml file is valid!") - diff --git a/tests.toml b/tests.toml @@ -24,11 +24,15 @@ 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." + [ratchet.regex.lightning] regex = "import pytorch_lightning|from pytorch_lightning" valid = ["import torch", "from my_project import Trainer"] invalid = ["import pytorch_lightning", "from pytorch_lightning import LightningModule"] +description = "The standard PyTorch library should be used in lieu of the PyTorch Lightning package." + [ratchet.regex.tabs] regex = "\\t" @@ -42,6 +46,7 @@ invalid = [ """def bar(): \treturn True""" ] +description = "As per the PEP 8 style guide for Python code, spaces are to be used instead of tabs for indentation. To mitigate this issue, run 'black FILENAME' to reformat the file with black, or manually fix the issue and update your editor to replace tabs with space." # printed text is assumed to be failures # each evaluation **must** accept a file path as input @@ -50,3 +55,4 @@ invalid = [ [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/tests/test_files/test_files.py b/tests/test_files/test_files.py @@ -48,6 +48,7 @@ def test_files(): if exception1_sum != 6: raise Exception(f"Incorrect number of infractions counted for {filtered1_file}") + if __name__ == "__main__": """Invoke all tests in the file when called directly.""" test_files()