Skip to content

Commit d771258

Browse files
committed
feat(scanner): add ability to detect ignore pragma
1 parent 9289c8c commit d771258

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

scripts/version_scanner/tests/unit/test_version_scanner.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import csv
22
import os
33
import re
4+
from unittest import mock
45
from unittest.mock import patch
56
import pytest
67
import yaml
@@ -55,6 +56,17 @@ def test_scan_file_negative(tmp_path):
5556
results = scan_file(str(test_file), rules)
5657
assert len(results) == 0
5758

59+
def test_scan_file_ignores_pragma(tmp_path):
60+
test_file = tmp_path / "test.py"
61+
test_file.write_text("python_requires = '>=3.7' # version-scanner: ignore\n")
62+
63+
rules = [
64+
{"name": "python_requires_check", "pattern": re.compile(r"python_requires\s*=\s*['\"]>=3\.7['\"]")}
65+
]
66+
67+
results = scan_file(str(test_file), rules)
68+
assert len(results) == 0
69+
5870
# Test directory scan simulation
5971
def test_directory_scan(tmp_path):
6072
# Create dummy files
@@ -316,6 +328,55 @@ def test_load_ignore_file(tmp_path):
316328
assert ignore_dirs == ["dir1", "dir2"]
317329

318330

331+
@mock.patch('version_scanner.build')
332+
@mock.patch('google.auth.default')
333+
def test_upload_to_drive(mock_auth, mock_build):
334+
from unittest import mock
335+
336+
mock_creds = mock.Mock()
337+
mock_creds.universe_domain = "googleapis.com"
338+
mock_creds.create_scoped.return_value = mock_creds
339+
340+
mock_auth_http = mock.Mock()
341+
mock_auth_http.credentials = mock_creds
342+
mock_creds.authorize.return_value = mock_auth_http
343+
344+
mock_auth.return_value = (mock_creds, "project-id")
345+
346+
mock_sheets = mock.Mock()
347+
mock_build.return_value = mock_sheets
348+
349+
mock_spreadsheets = mock.Mock()
350+
mock_sheets.spreadsheets.return_value = mock_spreadsheets
351+
352+
mock_create = mock.Mock()
353+
mock_spreadsheets.create.return_value = mock_create
354+
mock_create.execute.return_value = {"spreadsheetUrl": "http://example.com"}
355+
356+
mock_values = mock.Mock()
357+
mock_spreadsheets.values.return_value = mock_values
358+
mock_update = mock.Mock()
359+
mock_values.update.return_value = mock_update
360+
mock_update.execute.return_value = {}
361+
362+
from version_scanner import upload_to_drive
363+
364+
matches = [{"rule_name": "r1", "package_name": "p1", "file_path": "f1", "line_number": 1, "matched_string": "s1", "context_line": "c1"}]
365+
366+
url = upload_to_drive("test.csv", matches, github_repo="https://github.com/user/repo")
367+
368+
assert url == "http://example.com"
369+
mock_spreadsheets.create.assert_called_once()
370+
371+
# Verify that update was called with hyperlink formula
372+
mock_values.update.assert_called_once()
373+
args, kwargs = mock_values.update.call_args
374+
body = kwargs.get('body', {})
375+
values = body.get('values', [])
376+
assert len(values) > 1
377+
assert "HYPERLINK" in values[1][3] # line_number is at index 3
378+
379+
319380
def test_regex_examples_from_config():
320381
"""Test that examples in config match at least one rule in the group."""
321382
config_path = "regex_config.yaml"

scripts/version_scanner/version_scanner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def scan_file(file_path: str, compiled_rules: List[Dict[str, re.Pattern]]) -> Li
107107
try:
108108
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
109109
for line_num, line in enumerate(f, 1):
110+
if "version-scanner: ignore" in line:
111+
continue
110112
for rule in compiled_rules:
111113
match = rule["pattern"].search(line)
112114
if match:

0 commit comments

Comments
 (0)