-
Notifications
You must be signed in to change notification settings - Fork 7
Extend unit test with the ability to assert certain files not existing #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,12 @@ def parse_args() -> argparse.Namespace: | |||||
| required=True, | ||||||
| help="Path or glob pattern to the file(s) to search within.", | ||||||
| ) | ||||||
| parser.add_argument( | ||||||
| "--no_files", | ||||||
| nargs="+", | ||||||
| required=False, | ||||||
| help="Path or glob pattern to the file(s) that shouldn't exists.", | ||||||
| ) | ||||||
| parser.add_argument( | ||||||
| "--contains", | ||||||
| nargs="+", | ||||||
|
|
@@ -75,8 +81,7 @@ def parse_args() -> argparse.Namespace: | |||||
| def check_args(args): | ||||||
| """Checks wether the arguments are correct, aborts if not""" | ||||||
| if not args.contains and not args.excludes and not args.regex_patterns: | ||||||
| print(" [ERROR] Must define at least one pattern or negative pattern.") | ||||||
| sys.exit(1) | ||||||
| fail(" [ERROR] Must define at least one pattern or negative pattern.") | ||||||
|
|
||||||
|
|
||||||
| def exact_match(pattern: str, content: str) -> bool: | ||||||
|
|
@@ -143,25 +148,60 @@ def check_file(content: str, args) -> tuple[bool, set[str], set[str]]: | |||||
| return all_passed, found_patterns, missing_patterns | ||||||
|
|
||||||
|
|
||||||
| def main() -> None: | ||||||
| """Entry point for the pattern-matching test.""" | ||||||
| args = parse_args() | ||||||
| check_args(args) | ||||||
| def assert_files_missing(files): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| """ | ||||||
| Asserts that none of the files exist in `files`. | ||||||
| """ | ||||||
| for file_pattern in files: | ||||||
| matched_files = glob.glob(file_pattern, recursive=True) | ||||||
| if matched_files: | ||||||
| print(f"\n{file_pattern} unexpectedly exists.") | ||||||
| sys.exit(1) | ||||||
|
|
||||||
|
|
||||||
| def fail(msg: str, exit_code: int = 1): | ||||||
| """ | ||||||
| Exits the program, with a message | ||||||
| """ | ||||||
| print(msg) | ||||||
| sys.exit(exit_code) | ||||||
|
|
||||||
| all_passed = True | ||||||
| found_patterns = set() | ||||||
| missing_patterns = set() | ||||||
|
|
||||||
| def collect_file_paths(glob_list: list[str]) -> list[str]: | ||||||
| """Convert the glob patterns into a list of file paths""" | ||||||
| file_paths = [] | ||||||
| for file_pattern in args.files: | ||||||
| for file_pattern in glob_list: | ||||||
| matched_files = glob.glob(file_pattern, recursive=True) | ||||||
| if not matched_files: | ||||||
| print(f" [WARN] No files matched pattern/path: '{file_pattern}'") | ||||||
| # Each file pattern must match at least a file | ||||||
| fail(f" [WARN] No files matched pattern/path: '{file_pattern}'") | ||||||
| file_paths.extend(matched_files) | ||||||
|
|
||||||
| if not file_paths: | ||||||
| print(" [ERR] No file collected to be checked.") | ||||||
| sys.exit(1) | ||||||
| fail(" [ERR] No file collected to be checked.") | ||||||
| return file_paths | ||||||
|
|
||||||
|
|
||||||
| def main() -> None: | ||||||
| """Entry point for the pattern-matching test.""" | ||||||
| args = parse_args() | ||||||
| check_args(args) | ||||||
|
|
||||||
| assert_files_missing(args.no_files) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been looking at this PR for a bit, and this is the point that convinced me that "absent" isn't the correct phrase. You write "missing" here, but those files are "absent", but that doesn't really telegraph whats desiren, which is "expectedly missing" or "should be missing".
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed it to |
||||||
|
|
||||||
| files_to_check = [x for x in args.files if x not in args.no_files] | ||||||
| if not files_to_check: | ||||||
| print( | ||||||
| "There is no files to check, " | ||||||
| "since all files are expected to not exits" | ||||||
| ) | ||||||
| sys.exit(0) | ||||||
|
|
||||||
| file_paths = collect_file_paths(files_to_check) | ||||||
|
|
||||||
| all_passed = True | ||||||
| found_patterns = set() | ||||||
| missing_patterns = set() | ||||||
|
|
||||||
| for file in file_paths: | ||||||
| with open(file, "r", encoding="utf-8") as f: | ||||||
|
|
@@ -186,8 +226,7 @@ def main() -> None: | |||||
| if not all_passed: | ||||||
| for file, pattern in missing_patterns: | ||||||
| print(f"Missing pattern {pattern} in file {file}") | ||||||
| print("\nOne or more patterns missing. Test FAILED.") | ||||||
| sys.exit(1) | ||||||
| fail("\nOne or more patterns missing. Test FAILED.") | ||||||
|
|
||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we adjust this name as well?