mirror of
https://github.com/unclebob/crapper.git
synced 2026-11-26 18:18:49 +00:00
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:
co-authored by
Robert C. Martin
parent
ccbac08c2b
commit
98cddf8c4a
+13
-3
@@ -70,6 +70,11 @@ def _clean_dir(path: Path) -> None:
|
||||
shutil.rmtree(path)
|
||||
|
||||
|
||||
def _remove_file(path: Path) -> None:
|
||||
if path.is_file():
|
||||
path.unlink()
|
||||
|
||||
|
||||
_COVERAGE_DIRS = {"typescript", "rust", "go", "python"}
|
||||
|
||||
|
||||
@@ -718,16 +723,19 @@ def _export_swift_package(root: Path, package: Path) -> None:
|
||||
profdata = swift_profdata(package)
|
||||
binary = swift_test_binary(package)
|
||||
tool = llvm_cov_tool()
|
||||
report = _swift_report(root, package)
|
||||
if profdata is None or binary is None or tool is None:
|
||||
_remove_file(report)
|
||||
_warn(f"Swift coverage data is missing in {package}. Swift coverage will score 0%.")
|
||||
return
|
||||
report = _swift_report(root, package)
|
||||
code = write_command_output(swift_export_command(tool, binary, profdata), package, report)
|
||||
if code != 0:
|
||||
_remove_file(report)
|
||||
_warn(f"llvm-cov export exited {code} in {package}. Swift coverage will score 0%.")
|
||||
|
||||
|
||||
def _cover_swift_package(root: Path, package: Path) -> None:
|
||||
_remove_file(_swift_report(root, package))
|
||||
if shutil.which("swift") is None:
|
||||
_warn(f"swift is not installed. Swift coverage will score 0% in {package}.")
|
||||
return
|
||||
@@ -739,6 +747,8 @@ def _cover_swift_package(root: Path, package: Path) -> None:
|
||||
|
||||
|
||||
def _cover_xcode(root: Path, project: Path) -> None:
|
||||
exported = _swift_report(root, project).parent / "xccov.json"
|
||||
_remove_file(exported)
|
||||
if shutil.which("xcodebuild") is None:
|
||||
_warn(f"xcodebuild is not installed. Swift coverage will score 0% for {project.name}.")
|
||||
return
|
||||
@@ -746,16 +756,16 @@ def _cover_xcode(root: Path, project: Path) -> None:
|
||||
if not schemes:
|
||||
_warn(f"No shared Xcode scheme in {project}. Swift coverage will score 0%.")
|
||||
return
|
||||
result = _swift_report(root, project).parent / "Test.xcresult"
|
||||
result = exported.parent / "Test.xcresult"
|
||||
if result.exists():
|
||||
shutil.rmtree(result)
|
||||
code = run_shell(xcode_test_command(project, schemes[0], result), project.parent)
|
||||
if code != 0:
|
||||
_warn(f"Xcode coverage exited {code} in {project}. Swift coverage will score 0%.")
|
||||
return
|
||||
exported = result.parent / "xccov.json"
|
||||
code = write_command_output(xcode_export_command(result), project.parent, exported)
|
||||
if code != 0:
|
||||
_remove_file(exported)
|
||||
_warn(f"xccov exited {code} in {project}. Swift coverage will score 0%.")
|
||||
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user