|
1 | 1 | """Unit tests verifying CLI argument handling and dispatch.""" |
2 | 2 |
|
3 | 3 | import importlib |
| 4 | +import json |
4 | 5 | from unittest.mock import MagicMock |
5 | 6 |
|
| 7 | +import pytest |
| 8 | + |
6 | 9 | from rsmetacheck.config import AnalysisConfig |
7 | 10 |
|
8 | 11 | cli_module = importlib.import_module("rsmetacheck.cli") |
@@ -539,3 +542,125 @@ def test_cli_config_load_error_stops_execution(monkeypatch, tmp_path, capsys): |
539 | 542 | captured = capsys.readouterr() |
540 | 543 | assert "Error loading config" in captured.out |
541 | 544 | run_analysis_mock.assert_not_called() |
| 545 | + |
| 546 | + |
| 547 | +def test_exit_on_findings_pitfalls_cause_exit(tmp_path, capsys): |
| 548 | + """Exit code 1 when pitfalls detected and fail_on_pitfalls=True.""" |
| 549 | + analysis_file = tmp_path / "analysis.json" |
| 550 | + analysis_file.write_text(json.dumps({ |
| 551 | + "summary": { |
| 552 | + "total_pitfalls_detected": 3, |
| 553 | + "total_warnings_detected": 0, |
| 554 | + } |
| 555 | + })) |
| 556 | + config = AnalysisConfig(fail_on_pitfalls=True, fail_on_warnings=False) |
| 557 | + |
| 558 | + with pytest.raises(SystemExit) as exc_info: |
| 559 | + cli_module._exit_on_findings(str(analysis_file), config) |
| 560 | + |
| 561 | + assert exc_info.value.code == 1 |
| 562 | + captured = capsys.readouterr() |
| 563 | + assert "CI gate: 3 pitfall(s) detected" in captured.out |
| 564 | + |
| 565 | + |
| 566 | +def test_exit_on_findings_warnings_cause_exit(tmp_path, capsys): |
| 567 | + """Exit code 1 when warnings detected and fail_on_warnings=True.""" |
| 568 | + analysis_file = tmp_path / "analysis.json" |
| 569 | + analysis_file.write_text(json.dumps({ |
| 570 | + "summary": { |
| 571 | + "total_pitfalls_detected": 0, |
| 572 | + "total_warnings_detected": 2, |
| 573 | + } |
| 574 | + })) |
| 575 | + config = AnalysisConfig(fail_on_pitfalls=False, fail_on_warnings=True) |
| 576 | + |
| 577 | + with pytest.raises(SystemExit) as exc_info: |
| 578 | + cli_module._exit_on_findings(str(analysis_file), config) |
| 579 | + |
| 580 | + assert exc_info.value.code == 1 |
| 581 | + captured = capsys.readouterr() |
| 582 | + assert "CI gate: 2 warning(s) detected" in captured.out |
| 583 | + |
| 584 | + |
| 585 | +def test_exit_on_findings_both_cause_exit(tmp_path, capsys): |
| 586 | + """Exit code 1 when both pitfalls and warnings detected with both flags True.""" |
| 587 | + analysis_file = tmp_path / "analysis.json" |
| 588 | + analysis_file.write_text(json.dumps({ |
| 589 | + "summary": { |
| 590 | + "total_pitfalls_detected": 1, |
| 591 | + "total_warnings_detected": 1, |
| 592 | + } |
| 593 | + })) |
| 594 | + config = AnalysisConfig(fail_on_pitfalls=True, fail_on_warnings=True) |
| 595 | + |
| 596 | + with pytest.raises(SystemExit) as exc_info: |
| 597 | + cli_module._exit_on_findings(str(analysis_file), config) |
| 598 | + |
| 599 | + assert exc_info.value.code == 1 |
| 600 | + captured = capsys.readouterr() |
| 601 | + assert "CI gate: 1 pitfall(s) detected" in captured.out |
| 602 | + assert "CI gate: 1 warning(s) detected" in captured.out |
| 603 | + |
| 604 | + |
| 605 | +def test_exit_on_findings_no_exit_when_flags_false(tmp_path): |
| 606 | + """No exit when fail flags are False even with findings.""" |
| 607 | + analysis_file = tmp_path / "analysis.json" |
| 608 | + analysis_file.write_text(json.dumps({ |
| 609 | + "summary": { |
| 610 | + "total_pitfalls_detected": 5, |
| 611 | + "total_warnings_detected": 5, |
| 612 | + } |
| 613 | + })) |
| 614 | + config = AnalysisConfig(fail_on_pitfalls=False, fail_on_warnings=False) |
| 615 | + |
| 616 | + # Should not raise SystemExit |
| 617 | + cli_module._exit_on_findings(str(analysis_file), config) |
| 618 | + |
| 619 | + |
| 620 | +def test_exit_on_findings_no_exit_with_zero_findings(tmp_path): |
| 621 | + """No exit when zero pitfalls and warnings even with flags True.""" |
| 622 | + analysis_file = tmp_path / "analysis.json" |
| 623 | + analysis_file.write_text(json.dumps({ |
| 624 | + "summary": { |
| 625 | + "total_pitfalls_detected": 0, |
| 626 | + "total_warnings_detected": 0, |
| 627 | + } |
| 628 | + })) |
| 629 | + config = AnalysisConfig(fail_on_pitfalls=True, fail_on_warnings=True) |
| 630 | + |
| 631 | + # Should not raise SystemExit |
| 632 | + cli_module._exit_on_findings(str(analysis_file), config) |
| 633 | + |
| 634 | + |
| 635 | +def test_exit_on_findings_missing_file_prints_warning(tmp_path, capsys): |
| 636 | + """Missing analysis file should print warning and not exit.""" |
| 637 | + config = AnalysisConfig(fail_on_pitfalls=True, fail_on_warnings=True) |
| 638 | + |
| 639 | + cli_module._exit_on_findings(str(tmp_path / "nonexistent.json"), config) |
| 640 | + |
| 641 | + captured = capsys.readouterr() |
| 642 | + assert "Warning: Could not read analysis output" in captured.out |
| 643 | + |
| 644 | + |
| 645 | +def test_exit_on_findings_malformed_json_prints_warning(tmp_path, capsys): |
| 646 | + """Malformed analysis file should print warning and not exit.""" |
| 647 | + analysis_file = tmp_path / "analysis.json" |
| 648 | + analysis_file.write_text("not valid json") |
| 649 | + |
| 650 | + config = AnalysisConfig(fail_on_pitfalls=True, fail_on_warnings=True) |
| 651 | + |
| 652 | + cli_module._exit_on_findings(str(analysis_file), config) |
| 653 | + |
| 654 | + captured = capsys.readouterr() |
| 655 | + assert "Warning: Could not read analysis output" in captured.out |
| 656 | + |
| 657 | + |
| 658 | +def test_exit_on_findings_missing_summary_fields_treated_as_zero(tmp_path): |
| 659 | + """Missing summary fields should be treated as 0 (no exit).""" |
| 660 | + analysis_file = tmp_path / "analysis.json" |
| 661 | + analysis_file.write_text(json.dumps({"summary": {}})) |
| 662 | + |
| 663 | + config = AnalysisConfig(fail_on_pitfalls=True, fail_on_warnings=True) |
| 664 | + |
| 665 | + # Should not raise SystemExit |
| 666 | + cli_module._exit_on_findings(str(analysis_file), config) |
0 commit comments