|
| 1 | +""" |
| 2 | +Unit tests for generate_reports_landing_page.py — GitHub Pages Landing Page Generator. |
| 3 | +
|
| 4 | +This test suite verifies the correctness of the functions used to generate |
| 5 | +the main landing page for code quality reports. |
| 6 | +""" |
| 7 | + |
| 8 | +import types |
| 9 | + |
| 10 | +import pytest |
| 11 | +from rattlesnake.cicd.reports_main_page import ( |
| 12 | + get_report_html, |
| 13 | + main, |
| 14 | + run_reports_main_page, |
| 15 | + write_report, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def test_write_report_success(tmp_path): |
| 20 | + """Test that write_report successfully writes to a file.""" |
| 21 | + content = "<html></html>" |
| 22 | + file_path = tmp_path / "report.html" |
| 23 | + write_report(content, str(file_path)) |
| 24 | + assert file_path.read_text() == content |
| 25 | + |
| 26 | + |
| 27 | +def test_write_report_io_error(monkeypatch): |
| 28 | + """Test that write_report raises IOError when file writing fails.""" |
| 29 | + |
| 30 | + def mock_open_raises_io_error(*args, **kwargs): |
| 31 | + raise IOError("Permission denied") |
| 32 | + |
| 33 | + monkeypatch.setattr("builtins.open", mock_open_raises_io_error) |
| 34 | + with pytest.raises(IOError) as excinfo: |
| 35 | + write_report("<html></html>", "protected_file.html") |
| 36 | + assert 'Error writing output file "protected_file.html"' in str(excinfo.value) |
| 37 | + assert "Permission denied" in str(excinfo.value) |
| 38 | + |
| 39 | + |
| 40 | +def test_get_report_html(): |
| 41 | + """Test get_report_html with valid inputs.""" |
| 42 | + report = get_report_html( |
| 43 | + github_repo="test/repo", |
| 44 | + pylint_score="9.5", |
| 45 | + ) |
| 46 | + assert "<!DOCTYPE html>" in report |
| 47 | + assert "Rattlesnake Code Quality Reports" in report |
| 48 | + assert "https://github.com/test/repo" in report |
| 49 | + assert "<strong>Latest Score:</strong> 9.5/10" in report |
| 50 | + |
| 51 | + |
| 52 | +def test_run_generate_landing_page(tmp_path): |
| 53 | + """Tests the main report creation function.""" |
| 54 | + output_file = tmp_path / "index.html" |
| 55 | + run_reports_main_page( |
| 56 | + github_repo="owner/repo", |
| 57 | + pylint_score="8.8", |
| 58 | + output_file=str(output_file), |
| 59 | + ) |
| 60 | + assert output_file.is_file() |
| 61 | + content = output_file.read_text() |
| 62 | + assert "<h1>Rattlesnake Code Quality Reports Summary</h1>" in content |
| 63 | + assert "<strong>Latest Score:</strong> 8.8/10" in content |
| 64 | + assert 'href="https://github.com/owner/repo"' in content |
| 65 | + |
| 66 | + |
| 67 | +def test_main_success(monkeypatch, capsys): |
| 68 | + """Test the main function for a successful run.""" |
| 69 | + mock_args = types.SimpleNamespace( |
| 70 | + github_repo="owner/repo", |
| 71 | + pylint_score="9.9", |
| 72 | + output_file="landing.html", |
| 73 | + ) |
| 74 | + |
| 75 | + monkeypatch.setattr("argparse.ArgumentParser.parse_args", lambda self: mock_args) |
| 76 | + |
| 77 | + def mock_run(*args, **kwargs): |
| 78 | + pass |
| 79 | + |
| 80 | + monkeypatch.setattr( |
| 81 | + "rattlesnake.cicd.reports_main_page.run_reports_main_page", |
| 82 | + mock_run, |
| 83 | + ) |
| 84 | + |
| 85 | + exit_code = main() |
| 86 | + assert exit_code == 0 |
| 87 | + captured = capsys.readouterr() |
| 88 | + assert "✅ GitHub Pages reports main page generated: landing.html" in captured.out |
| 89 | + |
| 90 | + |
| 91 | +def test_main_io_error(monkeypatch, capsys): |
| 92 | + """Test the main function when an IOError occurs.""" |
| 93 | + mock_args = types.SimpleNamespace( |
| 94 | + github_repo="owner/repo", |
| 95 | + pylint_score="9.9", |
| 96 | + output_file="landing.html", |
| 97 | + ) |
| 98 | + monkeypatch.setattr("argparse.ArgumentParser.parse_args", lambda self: mock_args) |
| 99 | + |
| 100 | + def mock_run_raises_io_error(*args, **kwargs): |
| 101 | + raise IOError("Disk full") |
| 102 | + |
| 103 | + monkeypatch.setattr( |
| 104 | + "rattlesnake.cicd.reports_main_page.run_reports_main_page", |
| 105 | + mock_run_raises_io_error, |
| 106 | + ) |
| 107 | + |
| 108 | + exit_code = main() |
| 109 | + assert exit_code == 1 |
| 110 | + captured = capsys.readouterr() |
| 111 | + assert "❌ I/O error occurred: Disk full" in captured.err |
| 112 | + |
| 113 | + |
| 114 | +def test_main_unexpected_error(monkeypatch, capsys): |
| 115 | + """Test the main function for an unexpected error.""" |
| 116 | + mock_args = types.SimpleNamespace( |
| 117 | + github_repo="owner/repo", |
| 118 | + pylint_score="9.9", |
| 119 | + output_file="landing.html", |
| 120 | + ) |
| 121 | + monkeypatch.setattr("argparse.ArgumentParser.parse_args", lambda self: mock_args) |
| 122 | + |
| 123 | + def mock_run_raises_exception(*args, **kwargs): |
| 124 | + raise Exception("Something bad happened") |
| 125 | + |
| 126 | + monkeypatch.setattr( |
| 127 | + "rattlesnake.cicd.reports_main_page.run_reports_main_page", |
| 128 | + mock_run_raises_exception, |
| 129 | + ) |
| 130 | + |
| 131 | + exit_code = main() |
| 132 | + assert exit_code == 1 |
| 133 | + captured = capsys.readouterr() |
| 134 | + assert "❌ An unexpected error occurred: Something bad happened" in captured.err |
0 commit comments