|
| 1 | +import errno |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from ifixai.cli.reports import save_reports |
| 7 | +from ifixai.core.types import TestRunResult |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "report_format,suffix", [("json", ".json"), ("markdown", "-summary.md")] |
| 12 | +) |
| 13 | +def test_failed_report_write_preserves_the_previous_artifact( |
| 14 | + tmp_path, monkeypatch, report_format, suffix |
| 15 | +): |
| 16 | + result = TestRunResult(system_name="agent", fixture_name="support") |
| 17 | + target = tmp_path / f"ifixai-agent-support{suffix}" |
| 18 | + old = b"previous successful report\n" |
| 19 | + target.write_bytes(old) |
| 20 | + original = Path.write_text |
| 21 | + writes = [] |
| 22 | + |
| 23 | + def fail_during_actual_write(path, data, *args, **kwargs): |
| 24 | + writes.append(path) |
| 25 | + with path.open("w", encoding="utf-8") as handle: |
| 26 | + handle.write(data[:10]) |
| 27 | + handle.flush() |
| 28 | + raise OSError(errno.ENOSPC, "owned injected disk-full failure") |
| 29 | + |
| 30 | + monkeypatch.setattr(Path, "write_text", fail_during_actual_write) |
| 31 | + with pytest.raises(OSError) as exc: |
| 32 | + save_reports(result, str(tmp_path), report_format) |
| 33 | + assert exc.value.errno == errno.ENOSPC |
| 34 | + assert writes |
| 35 | + assert target.read_bytes() == old |
| 36 | + assert list(tmp_path.iterdir()) == [target] |
| 37 | + monkeypatch.setattr(Path, "write_text", original) |
| 38 | + |
| 39 | + |
| 40 | +def test_successful_json_report_still_publishes_normally(tmp_path): |
| 41 | + import json |
| 42 | + |
| 43 | + save_reports( |
| 44 | + TestRunResult(system_name="agent", fixture_name="support"), |
| 45 | + str(tmp_path), |
| 46 | + "json", |
| 47 | + ) |
| 48 | + assert ( |
| 49 | + json.loads((tmp_path / "ifixai-agent-support.json").read_text())["metadata"][ |
| 50 | + "system_name" |
| 51 | + ] |
| 52 | + == "agent" |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def test_successful_replacement_preserves_existing_report_permissions(tmp_path): |
| 57 | + import os |
| 58 | + import stat |
| 59 | + |
| 60 | + if os.name == "nt": |
| 61 | + pytest.skip("POSIX permission bits") |
| 62 | + target = tmp_path / "ifixai-agent-support.json" |
| 63 | + target.write_text("old") |
| 64 | + target.chmod(0o640) |
| 65 | + save_reports( |
| 66 | + TestRunResult(system_name="agent", fixture_name="support"), |
| 67 | + str(tmp_path), |
| 68 | + "json", |
| 69 | + ) |
| 70 | + assert stat.S_IMODE(target.stat().st_mode) == 0o640 |
| 71 | + assert len(list(tmp_path.iterdir())) == 1 |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize("mask", ["022", "027", "077"]) |
| 75 | +def test_new_reports_follow_normal_file_creation_umask(tmp_path, mask): |
| 76 | + import os |
| 77 | + import subprocess |
| 78 | + import sys |
| 79 | + |
| 80 | + if os.name == "nt": |
| 81 | + pytest.skip("POSIX permission bits") |
| 82 | + # Change the umask only in this owned child, never the pytest process. |
| 83 | + script = """ |
| 84 | +import os |
| 85 | +import stat |
| 86 | +import sys |
| 87 | +from pathlib import Path |
| 88 | +from ifixai.cli.reports import save_reports |
| 89 | +from ifixai.core.types import TestRunResult |
| 90 | +mask = int(sys.argv[2], 8) |
| 91 | +os.umask(mask) |
| 92 | +root = Path(sys.argv[1]) |
| 93 | +save_reports(TestRunResult(system_name="agent", fixture_name="support"), str(root), "both") |
| 94 | +reports = list(root.iterdir()) |
| 95 | +assert len(reports) == 3 |
| 96 | +for report in reports: |
| 97 | + mode = stat.S_IMODE(report.stat().st_mode) |
| 98 | + assert mode == 0o666 & ~mask, (report.name, oct(mode), oct(0o666 & ~mask)) |
| 99 | +""" |
| 100 | + result = subprocess.run( |
| 101 | + [sys.executable, "-c", script, str(tmp_path), mask], |
| 102 | + env={"PATH": os.environ.get("PATH", ""), |
| 103 | + "PYTHONPATH": os.environ.get("PYTHONPATH", "")}, |
| 104 | + capture_output=True, text=True, check=False, |
| 105 | + ) |
| 106 | + assert result.returncode == 0, result.stdout + result.stderr |
0 commit comments