Drop a Swift coverage report when the run fails.

A failed swift test, llvm-cov export, or xcodebuild left the previous
JSON in place, so the next score reused it. The warning says that
language scores 0%. The report is removed before the command and again
when export exits non-zero. Each failure test starts from the fixture
that scores choose at 50% and then sees 0%.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 00:05:40 +00:00
co-authored by Robert C. Martin
parent ccbac08c2b
commit 98cddf8c4a
2 changed files with 67 additions and 3 deletions
+54
View File
@@ -631,6 +631,60 @@ def test_xcode_failure_paths_do_not_export(tmp_path, monkeypatch, capsys):
assert "xccov exited 3" in capsys.readouterr().err
def test_a_failed_swift_run_forgets_the_previous_report(tmp_path, monkeypatch):
_plant(tmp_path, "Package.swift", "// swift-tools-version: 5.9\n")
_swift_project(tmp_path, "llvm-cov.json")
source = tmp_path / "Sources" / "Demo" / "Box.swift"
monkeypatch.setattr(
"crapper.runners.shutil.which",
lambda name: "/usr/bin/swift" if name == "swift" else None,
)
monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 1)
assert run_coverage(tmp_path, [source], None) == 0
assert ':name "choose", :namespace "Demo", :complexity 2, :coverage 0.0, :crap 6.0' in _snapshot(tmp_path)
def test_a_failed_llvm_cov_export_is_not_scored(tmp_path, monkeypatch):
_plant(tmp_path, "Package.swift", "")
_swift_project(tmp_path, "llvm-cov.json")
_plant_profdata(tmp_path)
binary = tmp_path / ".build" / "debug" / "DemoPackageTests"
binary.write_text("bin", encoding="utf-8")
source = tmp_path / "Sources" / "Demo" / "Box.swift"
def export(command, cwd, destination):
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text((_FIXTURES / "llvm-cov.json").read_text(encoding="utf-8"), encoding="utf-8")
return 1
monkeypatch.setattr(
"crapper.runners.shutil.which",
lambda name: f"/usr/bin/{name}" if name in {"swift", "llvm-cov"} else None,
)
monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 0)
monkeypatch.setattr("crapper.runners.write_command_output", export)
assert run_coverage(tmp_path, [source], None) == 0
assert ':name "choose", :namespace "Demo", :complexity 2, :coverage 0.0, :crap 6.0' in _snapshot(tmp_path)
def test_a_failed_xcode_run_forgets_the_previous_report(tmp_path, monkeypatch):
project = tmp_path / "App.xcodeproj"
_plant(project, "xcshareddata/xcschemes/App.xcscheme", "<Scheme/>\n")
source = tmp_path / "Sources" / "Demo" / "Box.swift"
source.parent.mkdir(parents=True)
source.write_text(_BOX, encoding="utf-8")
report = tmp_path / "target" / "coverage" / "swift" / "App.xcodeproj" / "xccov.json"
report.parent.mkdir(parents=True)
report.write_text((_FIXTURES / "xccov.json").read_text(encoding="utf-8"), encoding="utf-8")
monkeypatch.setattr(
"crapper.runners.shutil.which",
lambda name: "/usr/bin/xcodebuild" if name == "xcodebuild" else None,
)
monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 1)
assert run_coverage(tmp_path, [source], None) == 0
assert ':name "choose", :namespace "Demo", :complexity 2, :coverage 0.0, :crap 6.0' in _snapshot(tmp_path)
def test_swift_without_a_toolchain_does_not_run_a_command(tmp_path, monkeypatch, capsys):
_plant(tmp_path, "Package.swift", "")
source = _plant(tmp_path, "Sources/Demo/Box.swift", "func open() {}\n")