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 <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 00:03:03 +00:00
co-authored by Robert C. Martin
parent 95c17c42ab
commit ccbac08c2b
3 changed files with 84 additions and 2 deletions
+1 -1
View File
@@ -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
+24 -1
View File
@@ -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:
+59
View File
@@ -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"