Skip to content

Commit 4787bcd

Browse files
committed
finish tests for main report
1 parent 4515268 commit 4787bcd

File tree

3 files changed

+135
-14
lines changed

3 files changed

+135
-14
lines changed

src/rattlesnake/cicd/reports_main_page.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import argparse
6-
import datetime
76
import sys
87
from typing import Final
98

@@ -82,17 +81,6 @@
8281
"""
8382

8483

85-
# def get_timestamp() -> str:
86-
# """
87-
# Get a formatted timestamp string.
88-
#
89-
# Returns:
90-
# Formatted timestamp string.
91-
# """
92-
# now: datetime.datetime = datetime.datetime.now(datetime.timezone.utc)
93-
# return now.strftime("%Y-%m-%d %H:%M:%S %Z")
94-
95-
9684
def get_report_html(github_repo: str, pylint_score: str) -> str:
9785
"""Generates the HTML for the reports landing page.
9886
@@ -190,7 +178,7 @@ def main() -> int:
190178
pylint_score=args.pylint_score,
191179
output_file=args.output_file,
192180
)
193-
print(f"✅ GitHub Pages main reports page generated: {args.output_file}")
181+
print(f"✅ GitHub Pages reports main page generated: {args.output_file}")
194182
except IOError as e:
195183
print(f"❌ I/O error occurred: {e}", file=sys.stderr)
196184
return 1

tests/test_report_lint.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import re
1515

16-
# import sys # unused import
1716
import types
1817
from pathlib import Path
1918
from typing import Final

tests/test_reports_main_page.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)