|
26 | 26 | _truncate_context, |
27 | 27 | _wrap_sheet_hyperlink, |
28 | 28 | _wrap_sheet_string, |
| 29 | + _safe_int, |
29 | 30 | format_for_raw_csv, |
30 | 31 | format_for_spreadsheet, |
31 | 32 | format_for_console |
@@ -475,6 +476,42 @@ def test_main_stdout(capsys): |
475 | 476 | assert "test.py:1 [test] 3.7" in captured.out |
476 | 477 |
|
477 | 478 |
|
| 479 | +def test_main_without_stdout_limits_output(capsys): |
| 480 | + """Test that main() without --stdout prints only 10 matches and shows a suffix.""" |
| 481 | + test_args = ['version_scanner.py', '-d', 'python', '-v', '3.7'] |
| 482 | + matches = [{'file_path': f'test_{i}.py', 'line_number': i, 'matched_string': '3.7', 'rule_name': 'test'} for i in range(15)] |
| 483 | + with mock.patch('sys.argv', test_args): |
| 484 | + from version_scanner import main |
| 485 | + with mock.patch('version_scanner.scan_repository', return_value=matches): |
| 486 | + with pytest.raises(SystemExit): |
| 487 | + main() |
| 488 | + |
| 489 | + captured = capsys.readouterr() |
| 490 | + # Should only print first 10 matches |
| 491 | + for i in range(10): |
| 492 | + assert f"test_{i}.py:{i} [test] 3.7" in captured.out |
| 493 | + for i in range(10, 15): |
| 494 | + assert f"test_{i}.py:{i} [test] 3.7" not in captured.out |
| 495 | + assert "... and 5 more matches." in captured.out |
| 496 | + |
| 497 | + |
| 498 | +def test_main_with_stdout_prints_all(capsys): |
| 499 | + """Test that main() with --stdout prints all matches without limiting.""" |
| 500 | + test_args = ['version_scanner.py', '-d', 'python', '-v', '3.7', '--stdout'] |
| 501 | + matches = [{'file_path': f'test_{i}.py', 'line_number': i, 'matched_string': '3.7', 'rule_name': 'test'} for i in range(15)] |
| 502 | + with mock.patch('sys.argv', test_args): |
| 503 | + from version_scanner import main |
| 504 | + with mock.patch('version_scanner.scan_repository', return_value=matches): |
| 505 | + with pytest.raises(SystemExit): |
| 506 | + main() |
| 507 | + |
| 508 | + captured = capsys.readouterr() |
| 509 | + # Should print all 15 matches |
| 510 | + for i in range(15): |
| 511 | + assert f"test_{i}.py:{i} [test] 3.7" in captured.out |
| 512 | + assert "... and 5 more matches." not in captured.out |
| 513 | + |
| 514 | + |
478 | 515 | def test_main_does_not_print_rules(capsys): |
479 | 516 | """Test that main() does not print the list of loaded rules to stdout.""" |
480 | 517 | test_args = ['version_scanner.py', '-d', 'python', '-v', '3.7'] |
@@ -576,9 +613,29 @@ def test_wrap_sheet_hyperlink(): |
576 | 613 |
|
577 | 614 | def test_wrap_sheet_string(): |
578 | 615 | assert _wrap_sheet_string("3.10") == '="3.10"' |
| 616 | + assert _wrap_sheet_string('python_requires = ">=3.7"') == '="python_requires = "">=3.7"""' |
579 | 617 | assert _wrap_sheet_string("") == "" |
580 | 618 | assert _wrap_sheet_string(None) == "" |
581 | 619 |
|
| 620 | +def test_safe_int(): |
| 621 | + assert _safe_int("123") == 123 |
| 622 | + assert _safe_int("") == 0 |
| 623 | + assert _safe_int(None) == 0 |
| 624 | + assert _safe_int("abc") == 0 |
| 625 | + |
| 626 | +def test_format_for_raw_csv_handles_empty_line_number(): |
| 627 | + match = { |
| 628 | + "file_path": "google-cloud-python/main/packages/pkg_a/setup.py", |
| 629 | + "repo_path": "packages/pkg_a/setup.py", |
| 630 | + "package_name": "pkg_a", |
| 631 | + "rule_name": "python_requires_check", |
| 632 | + "line_number": "", |
| 633 | + "matched_string": "3.7", |
| 634 | + "context_line": "python_requires = '>=3.7'" |
| 635 | + } |
| 636 | + formatted = format_for_raw_csv(match) |
| 637 | + assert formatted["line_number"] == 0 |
| 638 | + |
582 | 639 | def test_format_for_raw_csv(): |
583 | 640 | match = { |
584 | 641 | "file_path": "google-cloud-python/main/packages/pkg_a/setup.py", |
|
0 commit comments