From ccbac08c2b0b85985354f68cebec2b8a56de0e51 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 00:03:03 +0000 Subject: [PATCH] Export llvm-cov against the Swift test executable. The first sorted `*PackageTests` file is the Mach-O inside a .dSYM, and the Linux executable is named `*PackageTests.xctest`, so llvm-cov was pointed at debug info or at nothing. Paths under a `.dSYM` bundle are skipped, and the `.xctest` file is accepted. One test expects the Contents/MacOS executable when a dSYM is beside it; the other expects the Linux file. Co-authored-by: Robert C. Martin --- README.md | 2 +- src/crapper/runners.py | 25 +++++++++++++++++- tests/test_swift.py | 59 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e082d3..a015d4b 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Swift package: swift test --enable-code-coverage bin="$(dirname "$(command -v swift)")/llvm-cov" profdata="$(find .build -path '*/codecov/default.profdata' -print -quit)" -binary="$(find .build -type f -name '*PackageTests' -print -quit)" +binary="$(find .build -type f \( -name '*PackageTests' -o -name '*PackageTests.xctest' \) ! -path '*.dSYM/*' -print -quit)" mkdir -p target/coverage/swift "$bin" export -format=json -instr-profile="$profdata" "$binary" \ > target/coverage/swift/llvm-cov.json diff --git a/src/crapper/runners.py b/src/crapper/runners.py index 2df41e8..ac97d7a 100644 --- a/src/crapper/runners.py +++ b/src/crapper/runners.py @@ -576,8 +576,31 @@ def swift_profdata(package: Path) -> Path | None: return _first_file(package, ".build/**/codecov/default.profdata") +def _in_dsym(path: Path) -> bool: + return any(part.endswith(".dSYM") for part in path.parts) + + +def _swift_binary(path: Path) -> bool: + if not path.is_file() or _in_dsym(path): + return False + return path.name.endswith("PackageTests") or path.name.endswith("PackageTests.xctest") + + def swift_test_binary(package: Path) -> Path | None: - return _first_file(package, ".build/**/*PackageTests") + """The test executable. macOS nests it in the .xctest bundle; Linux names the file .xctest. + + A dSYM stores another file with the same name. That is debug info, not the executable. + """ + + found = [ + path + for pattern in (".build/**/*PackageTests", ".build/**/*PackageTests.xctest") + for path in package.glob(pattern) + if _swift_binary(path) + ] + if not found: + return None + return sorted(set(found))[0] def llvm_cov_tool() -> str | None: diff --git a/tests/test_swift.py b/tests/test_swift.py index b72fc63..4183007 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -479,6 +479,65 @@ def test_xcode_without_a_shared_scheme_does_not_build(tmp_path, monkeypatch, cap assert "No shared Xcode scheme" in capsys.readouterr().err +def _plant_profdata(package: Path) -> None: + profdata = package / ".build" / "debug" / "codecov" / "default.profdata" + profdata.parent.mkdir(parents=True, exist_ok=True) + profdata.write_text("prof", encoding="utf-8") + + +def test_llvm_cov_exports_the_test_binary_not_the_dsym(tmp_path, monkeypatch): + _plant(tmp_path, "Package.swift", "") + source = _plant(tmp_path, "Sources/Demo/Box.swift", "func open() {}\n") + _plant_profdata(tmp_path) + dsym = tmp_path / ".build" / "debug" / "DemoPackageTests.dSYM" / "Contents" / "Resources" / "DWARF" / "DemoPackageTests" + binary = tmp_path / ".build" / "debug" / "DemoPackageTests.xctest" / "Contents" / "MacOS" / "DemoPackageTests" + dsym.parent.mkdir(parents=True) + binary.parent.mkdir(parents=True) + dsym.write_text("dwarf", encoding="utf-8") + binary.write_text("bin", encoding="utf-8") + exports = [] + + def export(command, cwd, destination): + exports.append(list(command)) + destination.parent.mkdir(parents=True, exist_ok=True) + destination.write_text("{}", encoding="utf-8") + return 0 + + 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 exports[0][-1] == str(binary) + + +def test_a_linux_xctest_file_is_the_swift_test_binary(tmp_path, monkeypatch): + _plant(tmp_path, "Package.swift", "") + source = _plant(tmp_path, "Sources/Demo/Box.swift", "func open() {}\n") + _plant_profdata(tmp_path) + binary = tmp_path / ".build" / "x86_64-unknown-linux-gnu" / "debug" / "DemoPackageTests.xctest" + binary.parent.mkdir(parents=True) + binary.write_text("bin", encoding="utf-8") + exports = [] + + def export(command, cwd, destination): + exports.append(list(command)) + destination.parent.mkdir(parents=True, exist_ok=True) + destination.write_text("{}", encoding="utf-8") + return 0 + + 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 exports[0][-1] == str(binary) + + def test_llvm_cov_beside_swift_is_used_when_it_is_not_on_the_path(tmp_path, monkeypatch): swift = tmp_path / "tool" / "swift" llvm = tmp_path / "tool" / "llvm-cov"