Skip to content

Commit d652dbf

Browse files
committed
feat(scanner): add filename scanning support
1 parent 94174bb commit d652dbf

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

scripts/version_scanner/tests/unit/test_version_scanner.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ def test_scan_file_ignores_pragma(tmp_path):
6767
results = scan_file(str(test_file), rules)
6868
assert len(results) == 0
6969

70+
def test_scan_file_ignores_next_line(tmp_path):
71+
test_file = tmp_path / "test.py"
72+
test_file.write_text("# version-scanner: ignore-next-line\npython_requires = '>=3.7'\n")
73+
74+
rules = [
75+
{"name": "python_requires_check", "pattern": re.compile(r"python_requires\s*=\s*['\"]>=3\.7['\"]")}
76+
]
77+
78+
results = scan_file(str(test_file), rules)
79+
assert len(results) == 0
80+
81+
def test_scan_repository_flags_filename(tmp_path):
82+
test_file = tmp_path / "test-3.9.txt"
83+
test_file.write_text("clean content\n")
84+
85+
rules = []
86+
87+
from version_scanner import scan_repository
88+
results = scan_repository(str(tmp_path), rules, version_string="3.9")
89+
90+
assert len(results) == 1
91+
assert results[0]["rule_name"] == "filename_match"
92+
assert results[0]["matched_string"] == "3.9"
93+
7094
# Test directory scan simulation
7195
def test_directory_scan(tmp_path):
7296
# Create dummy files

scripts/version_scanner/version_scanner.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,14 @@ def scan_file(file_path: str, compiled_rules: List[Dict[str, re.Pattern]]) -> Li
106106

107107
try:
108108
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
109+
skip_next = False
109110
for line_num, line in enumerate(f, 1):
111+
if skip_next:
112+
skip_next = False
113+
continue
114+
if "version-scanner: ignore-next-line" in line:
115+
skip_next = True
116+
continue
110117
if "version-scanner: ignore" in line:
111118
continue
112119
for rule in compiled_rules:
@@ -339,7 +346,8 @@ def scan_repository(
339346
root_path: str,
340347
rules: List[Dict[str, str]],
341348
target_packages: List[str] = None,
342-
ignore_dirs: List[str] = None
349+
ignore_dirs: List[str] = None,
350+
version_string: str = None
343351
) -> List[Dict[str, str]]:
344352
"""
345353
Scan repository for matching patterns.
@@ -398,6 +406,15 @@ def scan_repository(
398406
file_path = os.path.join(root, file)
399407
matches = scan_file(file_path, compiled_rules)
400408

409+
# Add filename match if applicable
410+
if version_string and version_string in file:
411+
matches.append({
412+
"rule_name": "filename_match",
413+
"line_number": 0,
414+
"matched_string": version_string,
415+
"context_line": f"Filename contains {version_string}"
416+
})
417+
401418
# Compute display path and package name
402419
rel_file_path = os.path.relpath(file_path, root_path)
403420

@@ -528,7 +545,7 @@ def main():
528545
print(f"Loaded {len(ignore_dirs)} ignore patterns from {ignore_file_path}")
529546

530547
# Scan repository
531-
all_matches = scan_repository(args.path, rules, target_packages, ignore_dirs)
548+
all_matches = scan_repository(args.path, rules, target_packages, ignore_dirs, version_string=args.version)
532549

533550
print(f"\nFound {len(all_matches)} matches.")
534551
for m in all_matches[:10]: # Show first 10

0 commit comments

Comments
 (0)