Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/unit/basic/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ unit_test(
contains = ["core.DivideZero"],
data = [":basic_codechecker"],
excludes = ["Text not in file"],
expected_missing_files = "this/file/definitely/does/not.exists",
files = "test/unit/basic/basic_codechecker/codechecker.log",
regex_patterns = ["[a-z]"],
)
Expand All @@ -62,6 +63,7 @@ unit_test(
contains = ["Division by zero"],
data = [":basic_per_file"],
excludes = ["Text not in file"],
expected_missing_files = "this/file/definitely/does/not.exists",
files = "test/unit/basic/basic_per_file/**/*.plist",
regex_patterns = ["[a-z]"],
require_patterns_in_each_file = False,
Expand Down
69 changes: 54 additions & 15 deletions test/unit/grep_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Copy link
Copy Markdown
Collaborator

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?

nargs="+",
required=False,
help="Path or glob pattern to the file(s) that shouldn't exists.",
)
parser.add_argument(
"--contains",
nargs="+",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def assert_files_missing(files):
def assert_expectedly_missing_files(files):

"""
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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".

@furtib furtib Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed it to expected_missing_file.


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:
Expand All @@ -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__":
Expand Down
9 changes: 9 additions & 0 deletions test/unit/unit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Example:
def unit_test(
name,
files,
expected_missing_files = [],
contains = None,
excludes = None,
regex_patterns = None,
Expand All @@ -43,6 +44,9 @@ def unit_test(
Args:
name: Test name.
files: Path or glob to the files to be checked.
expected_missing_files: Path or glob that should not exists.
If these are in the `files` list too,
disregard the missing file error.
contains: Text that should be inside the files.
excludes: Text that shouldn't be inside the files.
regex_patterns: Regex patterns that should be found inside the files.
Expand All @@ -53,6 +57,8 @@ def unit_test(
"""
if type(files) == "string":
files = [files]
if type(expected_missing_files) == "string":
expected_missing_files = [expected_missing_files]
if type(contains) == "string":
contains = [contains]
if type(excludes) == "string":
Expand All @@ -61,6 +67,9 @@ def unit_test(
regex_patterns = [regex_patterns]

python_args = ["--files"] + files
if expected_missing_files:
python_args.append("--no_files")
python_args.extend(expected_missing_files)
if contains:
python_args.append("--contains")
python_args.extend(["\"{}\"".format(pat) for pat in contains])
Expand Down
Loading