mirror of
https://github.com/unclebob/crapper.git
synced 2026-11-26 18:18:49 +00:00
Find SwiftPM test binaries in both toolchain layouts.
Current Xcode puts NameTests inside NameTests.xctest/Contents/MacOS. Older toolchains use PackageTests, and on Linux the .xctest file is the executable. swift_test_binary searches both, and prefers the directory from `swift build --show-bin-path` when that command prints one. Proved by the xctest bundle, Linux .xctest, show-bin-path preference, relative path, and fallback tests. The dSYM is still ignored. Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
co-authored by
Robert C. Martin
parent
18d7a4c57d
commit
ec077c3793
+64
-18
@@ -587,31 +587,80 @@ def swift_profdata(package: Path) -> Path | None:
|
||||
return _first_file(package, ".build/**/codecov/default.profdata")
|
||||
|
||||
|
||||
def _part_ends_with(path: Path, suffix: str) -> bool:
|
||||
return any(part.endswith(suffix) for part in path.parts)
|
||||
|
||||
|
||||
def _in_dsym(path: Path) -> bool:
|
||||
return any(part.endswith(".dSYM") for part in path.parts)
|
||||
return _part_ends_with(path, ".dSYM")
|
||||
|
||||
|
||||
def _swift_binary(path: Path) -> bool:
|
||||
def _in_xctest(path: Path) -> bool:
|
||||
return _part_ends_with(path, ".xctest")
|
||||
|
||||
|
||||
def _named_test_file(path: Path) -> bool:
|
||||
name = path.name
|
||||
if name.endswith("PackageTests") or name.endswith("Tests.xctest"):
|
||||
return True
|
||||
return name.endswith("Tests") and _in_xctest(path)
|
||||
|
||||
|
||||
def _test_executable(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")
|
||||
return _named_test_file(path)
|
||||
|
||||
|
||||
def _first_test_binary(base: Path) -> Path | None:
|
||||
if not base.is_dir():
|
||||
return None
|
||||
found = sorted(path for path in base.rglob("*") if _test_executable(path))
|
||||
if not found:
|
||||
return None
|
||||
return found[0]
|
||||
|
||||
|
||||
def _tool_stdout(name: str, args: list[str], cwd: Path | None = None) -> str | None:
|
||||
tool = shutil.which(name)
|
||||
if not tool:
|
||||
return None
|
||||
return _command_stdout([tool, *args], cwd)
|
||||
|
||||
|
||||
def _shown_directory(package: Path, text: str) -> Path | None:
|
||||
stripped = text.strip()
|
||||
if stripped == "":
|
||||
return None
|
||||
path = Path(stripped)
|
||||
if not path.is_absolute():
|
||||
path = package / path
|
||||
return path
|
||||
|
||||
|
||||
def _swift_bin_path(package: Path) -> Path | None:
|
||||
text = _tool_stdout("swift", ["build", "--show-bin-path"], package)
|
||||
if text is None:
|
||||
return None
|
||||
return _shown_directory(package, text)
|
||||
|
||||
|
||||
def swift_test_binary(package: Path) -> Path | None:
|
||||
"""The test executable. macOS nests it in the .xctest bundle; Linux names the file .xctest.
|
||||
"""The test executable.
|
||||
|
||||
A dSYM stores another file with the same name. That is debug info, not the executable.
|
||||
Prefer the directory `swift build --show-bin-path` prints. Otherwise
|
||||
search `.build`. A current toolchain puts `NameTests` inside
|
||||
`NameTests.xctest/Contents/MacOS`. On Linux the `.xctest` file is the
|
||||
executable. Older toolchains call that file `PackageTests`. A dSYM 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]
|
||||
base = _swift_bin_path(package)
|
||||
if base is not None:
|
||||
found = _first_test_binary(base)
|
||||
if found is not None:
|
||||
return found
|
||||
return _first_test_binary(package / ".build")
|
||||
|
||||
|
||||
def _command_stdout(command: list[str], cwd: Path | None = None) -> str | None:
|
||||
@@ -643,10 +692,7 @@ def _llvm_cov_beside_swift() -> str | None:
|
||||
|
||||
|
||||
def _llvm_cov_from_xcrun() -> str | None:
|
||||
xcrun = shutil.which("xcrun")
|
||||
if xcrun is None:
|
||||
return None
|
||||
text = _command_stdout([xcrun, "--find", "llvm-cov"])
|
||||
text = _tool_stdout("xcrun", ["--find", "llvm-cov"])
|
||||
if text is None:
|
||||
return None
|
||||
path = Path(text.strip())
|
||||
|
||||
+110
-1
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
from crapper.cli import run
|
||||
from crapper.discover import is_test_file, iter_source_files
|
||||
from crapper.languages.swift import functions_in_source
|
||||
from crapper.runners import llvm_cov_tool, run_coverage, swift_export_command
|
||||
from crapper.runners import llvm_cov_tool, run_coverage, swift_export_command, swift_test_binary
|
||||
|
||||
_FIXTURES = Path(__file__).parent / "fixtures" / "swift"
|
||||
_BOX = (_FIXTURES / "Box.swift").read_text(encoding="utf-8")
|
||||
@@ -690,6 +690,115 @@ def test_a_missing_or_failing_xcrun_leaves_llvm_cov_unset(tmp_path, monkeypatch)
|
||||
assert llvm_cov_tool() is None
|
||||
|
||||
|
||||
def _xctest_binary(package: Path, directory: str, name: str) -> Path:
|
||||
binary = package / directory / f"{name}.xctest" / "Contents" / "MacOS" / name
|
||||
binary.parent.mkdir(parents=True, exist_ok=True)
|
||||
binary.write_text("bin", encoding="utf-8")
|
||||
return binary
|
||||
|
||||
|
||||
def _swift_stub(path: Path) -> Path:
|
||||
return _program(
|
||||
path,
|
||||
"#!/bin/sh\n"
|
||||
'if [ "$1" = "build" ] && [ "$2" = "--show-bin-path" ]; then\n'
|
||||
' printf "%s\\n" "$SHOW_BIN_PATH"\n'
|
||||
' exit "${SHOW_BIN_STATUS:-0}"\n'
|
||||
"fi\n"
|
||||
"exit 1\n",
|
||||
)
|
||||
|
||||
|
||||
def test_the_current_toolchain_test_binary_is_inside_the_xctest_bundle(tmp_path):
|
||||
binary = _xctest_binary(tmp_path, ".build/out/Products/Debug", "DemoTests")
|
||||
dsym = tmp_path / ".build/out/Products/Debug/DemoTests.dSYM/Contents/MacOS/DemoTests"
|
||||
dsym.parent.mkdir(parents=True)
|
||||
dsym.write_text("dwarf", encoding="utf-8")
|
||||
plist = binary.parents[1] / "Info.plist"
|
||||
plist.write_text("plist", encoding="utf-8")
|
||||
assert swift_test_binary(tmp_path) == binary
|
||||
|
||||
|
||||
def test_a_linux_tests_xctest_file_is_the_test_binary(tmp_path):
|
||||
binary = tmp_path / ".build" / "debug" / "DemoTests.xctest"
|
||||
binary.parent.mkdir(parents=True)
|
||||
binary.write_text("bin", encoding="utf-8")
|
||||
assert swift_test_binary(tmp_path) == binary
|
||||
|
||||
|
||||
def test_the_earliest_test_executable_is_chosen(tmp_path):
|
||||
first = _xctest_binary(tmp_path, ".build/out/Products/Debug", "AAATests")
|
||||
_xctest_binary(tmp_path, ".build/out/Products/Debug", "ZZZTests")
|
||||
assert swift_test_binary(tmp_path) == first
|
||||
|
||||
|
||||
def test_a_bare_tests_file_is_not_the_test_binary(tmp_path):
|
||||
bare = tmp_path / ".build" / "debug" / "AAATests"
|
||||
bare.parent.mkdir(parents=True)
|
||||
bare.write_text("bin", encoding="utf-8")
|
||||
packaged = tmp_path / ".build" / "debug" / "DemoPackageTests"
|
||||
packaged.write_text("bin", encoding="utf-8")
|
||||
assert swift_test_binary(tmp_path) == packaged
|
||||
|
||||
|
||||
def test_show_bin_path_is_preferred_to_another_build_product(tmp_path, monkeypatch):
|
||||
swift = _swift_stub(tmp_path / "swift")
|
||||
chosen = _xctest_binary(tmp_path, "products", "ZedTests")
|
||||
decoy = tmp_path / ".build" / "debug" / "AAAPackageTests"
|
||||
decoy.parent.mkdir(parents=True)
|
||||
decoy.write_text("bin", encoding="utf-8")
|
||||
monkeypatch.setattr("crapper.runners.shutil.which", _which({"swift": str(swift)}))
|
||||
monkeypatch.setenv("SHOW_BIN_PATH", str(tmp_path / "products"))
|
||||
monkeypatch.setenv("SHOW_BIN_STATUS", "0")
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
|
||||
def test_a_relative_show_bin_path_is_inside_the_package(tmp_path, monkeypatch):
|
||||
swift = _swift_stub(tmp_path / "swift")
|
||||
chosen = _xctest_binary(tmp_path, "products", "ZedTests")
|
||||
decoy = tmp_path / ".build" / "debug" / "AAAPackageTests"
|
||||
decoy.parent.mkdir(parents=True)
|
||||
decoy.write_text("bin", encoding="utf-8")
|
||||
monkeypatch.setattr("crapper.runners.shutil.which", _which({"swift": str(swift)}))
|
||||
monkeypatch.setenv("SHOW_BIN_PATH", "products")
|
||||
monkeypatch.setenv("SHOW_BIN_STATUS", "0")
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
|
||||
def test_show_bin_path_falls_back_when_it_does_not_name_a_product(tmp_path, monkeypatch):
|
||||
swift = _swift_stub(tmp_path / "swift")
|
||||
chosen = tmp_path / ".build" / "debug" / "DemoPackageTests"
|
||||
chosen.parent.mkdir(parents=True)
|
||||
chosen.write_text("bin", encoding="utf-8")
|
||||
_xctest_binary(tmp_path, "elsewhere", "AAATests")
|
||||
monkeypatch.setattr("crapper.runners.shutil.which", _which({"swift": str(swift)}))
|
||||
|
||||
monkeypatch.setenv("SHOW_BIN_PATH", "")
|
||||
monkeypatch.setenv("SHOW_BIN_STATUS", "0")
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
monkeypatch.setenv("SHOW_BIN_PATH", str(tmp_path / "elsewhere"))
|
||||
monkeypatch.setenv("SHOW_BIN_STATUS", "1")
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
noted = tmp_path / "not-a-directory"
|
||||
noted.write_text("notes", encoding="utf-8")
|
||||
monkeypatch.setenv("SHOW_BIN_PATH", str(noted))
|
||||
monkeypatch.setenv("SHOW_BIN_STATUS", "0")
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
empty = tmp_path / "empty-products"
|
||||
empty.mkdir()
|
||||
monkeypatch.setenv("SHOW_BIN_PATH", str(empty))
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
monkeypatch.setattr(
|
||||
"crapper.runners.shutil.which",
|
||||
_which({"swift": str(tmp_path / "missing-swift")}),
|
||||
)
|
||||
assert swift_test_binary(tmp_path) == chosen
|
||||
|
||||
|
||||
def test_command_output_is_the_report_file(tmp_path, monkeypatch):
|
||||
from crapper.runners import write_command_output
|
||||
|
||||
|
||||
Reference in New Issue
Block a user