|
1 | 1 | import csv |
2 | 2 | import os |
3 | 3 | import re |
| 4 | +from unittest import mock |
4 | 5 | from unittest.mock import patch |
5 | 6 | import pytest |
6 | 7 | import yaml |
@@ -55,6 +56,17 @@ def test_scan_file_negative(tmp_path): |
55 | 56 | results = scan_file(str(test_file), rules) |
56 | 57 | assert len(results) == 0 |
57 | 58 |
|
| 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 | + |
58 | 70 | # Test directory scan simulation |
59 | 71 | def test_directory_scan(tmp_path): |
60 | 72 | # Create dummy files |
@@ -316,6 +328,55 @@ def test_load_ignore_file(tmp_path): |
316 | 328 | assert ignore_dirs == ["dir1", "dir2"] |
317 | 329 |
|
318 | 330 |
|
| 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 | + |
319 | 380 | def test_regex_examples_from_config(): |
320 | 381 | """Test that examples in config match at least one rule in the group.""" |
321 | 382 | config_path = "regex_config.yaml" |
|
0 commit comments