Run swift test or xcodebuild, and keep Swift build output private.

A SwiftPM package is tested with swift test from the directory that
holds Package.swift. An Xcode or iOS project is tested with xcodebuild
test. The scheme is --xcode-scheme, otherwise the first shared scheme,
otherwise the project name. --xcode-destination is one argument, so a
simulator name with spaces is not split. A file inside a package is not
also sent to xcodebuild. iOS still needs a Mac with Xcode; the command
is only constructed here.

Workers no longer link .build, .swiftpm, or DerivedData. A symlink would
make swift test and xcodebuild write the real tree's build products,
and parallel workers would share them.

Proved by tests/test_swift.py. The command tests failed first because
Swift returned false, xcode_destination was not a parameter, and a
worker linked .build. A run with fake swift and xcodebuild on PATH then
showed swift test and xcodebuild test -destination with the scheme.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 00:00:21 +00:00
co-authored by Robert C. Martin
parent 23b7d47936
commit 7c72d9deba
5 changed files with 279 additions and 4 deletions
+174
View File
@@ -1,10 +1,12 @@
"""Swift mutation sites join the functions crapper reports."""
import os
import sys
from pathlib import Path
from mutator.cli import parse_args, run, select_files
from mutator.functions import sites_in_file
from mutator.runner import test_plan as plan_command
SOURCE = """func place(x: Int, ready: Bool, name: String?) -> Int {
guard x > 0, ready else { return 0 }
@@ -156,6 +158,178 @@ def test_swift_test_files_are_skipped_and_production_sources_are_kept(tmp_path,
assert "BoxTests.swift" in captured.err
def _write(path: Path, text: str) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
return path
def test_xcode_options_require_a_value():
options = parse_args(
["--xcode-scheme", "App", "--xcode-destination", "platform=iOS Simulator,name=iPhone 16"]
)
assert options.xcode_scheme == "App"
assert options.xcode_destination == "platform=iOS Simulator,name=iPhone 16"
help_text = parse_args(["--help"]).message
assert "--xcode-scheme" in help_text
assert "--xcode-destination" in help_text
assert "swift test" in help_text
assert "xcodebuild" in help_text
assert parse_args(["--xcode-scheme"]).exit_code == 1
def test_a_swift_package_runs_swift_test(tmp_path):
root = tmp_path / "pkg"
_write(root / "Package.swift", "// swift-tools-version:5.9\n")
source = _write(root / "Sources" / "Demo" / "Box.swift", "func open() -> Int { 1 }\n")
command, directory = plan_command(root, source, "swift", None)
assert command == ["swift", "test"]
assert directory == root.resolve()
override, override_dir = plan_command(root, source, "swift", "swift test --filter open")
assert override == "swift test --filter open"
assert override_dir == root.resolve()
def test_an_xcode_project_runs_xcodebuild_with_scheme_and_destination(tmp_path):
root = tmp_path / "ios"
project = root / "App.xcodeproj"
_write(project / "xcshareddata" / "xcschemes" / "App.xcscheme", "<Scheme/>\n")
_write(project / "xcshareddata" / "xcschemes" / "Zed.xcscheme", "<Scheme/>\n")
source = _write(root / "App" / "ContentView.swift", "func body() -> Int { 1 }\n")
destination = "platform=iOS Simulator,name=iPhone 16"
command, directory = plan_command(root, source, "swift", None, xcode_destination=destination)
assert command == [
"xcodebuild",
"test",
"-project",
str(project.resolve()),
"-scheme",
"App",
"-destination",
destination,
]
assert directory == root.resolve()
chosen, _directory = plan_command(
root, source, "swift", None, xcode_scheme="Zed", xcode_destination=destination
)
assert chosen[chosen.index("-scheme") + 1] == "Zed"
def test_a_workspace_uses_its_flag_and_a_missing_scheme_uses_the_project_name(tmp_path):
root = tmp_path / "ios"
workspace = root / "App.xcworkspace"
_write(workspace / "contents.xcworkspacedata", "<Workspace/>\n")
_write(root / "App.xcodeproj" / "project.pbxproj", "")
_write(workspace / "xcshareddata" / "xcschemes" / "App.xcscheme", "<Scheme/>\n")
source = _write(root / "App" / "ContentView.swift", "func body() -> Int { 1 }\n")
command, directory = plan_command(root, source, "swift", None)
assert command[:4] == ["xcodebuild", "test", "-workspace", str(workspace.resolve())]
assert "-destination" not in command
assert directory == root.resolve()
bare = tmp_path / "bare"
project = bare / "Solo.xcodeproj"
_write(project / "project.pbxproj", "")
solo = _write(bare / "Solo" / "View.swift", "func body() -> Int { 1 }\n")
bare_command, _bare_dir = plan_command(bare, solo, "swift", None)
assert bare_command[bare_command.index("-scheme") + 1] == "Solo"
def test_a_package_is_tested_with_swift_and_an_app_outside_it_with_xcodebuild(tmp_path):
root = tmp_path / "repo"
_write(root / "libs" / "Demo" / "Package.swift", "// swift-tools-version:5.9\n")
package_source = _write(
root / "libs" / "Demo" / "Sources" / "Demo" / "Box.swift",
"func open() -> Int { 1 }\n",
)
project = root / "App.xcodeproj"
_write(project / "xcshareddata" / "xcschemes" / "App.xcscheme", "<Scheme/>\n")
app_source = _write(root / "App" / "ContentView.swift", "func body() -> Int { 1 }\n")
package_command, package_dir = plan_command(root, package_source, "swift", None)
app_command, app_dir = plan_command(root, app_source, "swift", None)
assert package_command == ["swift", "test"]
assert package_dir == (root / "libs" / "Demo").resolve()
assert app_command[2:6] == ["-project", str(project.resolve()), "-scheme", "App"]
assert app_dir == root.resolve()
def test_a_worker_does_not_share_swift_build_directories(tmp_path):
from mutator.workers import create_workers, new_run_dir
root = tmp_path / "pkg"
source = _write(root / "Sources" / "Demo" / "Box.swift", "func open() -> Int { 1 }\n")
_write(root / "Package.swift", "// swift-tools-version:5.9\n")
for name in (".build", ".swiftpm", "DerivedData"):
_write(root / name / "marker", "x")
worker = create_workers(
new_run_dir(root), root, "Sources/Demo/Box.swift", source.read_bytes(), 1
)[0]
for name in (".build", ".swiftpm", "DerivedData"):
assert not (worker / name).exists()
assert (worker / "Package.swift").is_file()
copy = worker / "Sources" / "Demo" / "Box.swift"
assert copy.is_file() and not copy.is_symlink()
def _fake_tool(directory: Path, name: str, log: Path) -> None:
directory.mkdir(parents=True, exist_ok=True)
script = directory / name
script.write_text(
"#!/bin/sh\n"
f"printf '%s\\n' \"$PWD\" >> '{log}'\n"
f"printf '%s\\n' \"$@\" >> '{log}'\n"
f"printf '%s\\n' --- >> '{log}'\n"
"exit 0\n",
encoding="utf-8",
)
script.chmod(0o755)
def test_a_run_invokes_swift_test_and_xcodebuild_with_the_destination(tmp_path, monkeypatch):
bin_dir = tmp_path / "bin"
swift_log = tmp_path / "swift.log"
xcode_log = tmp_path / "xcode.log"
_fake_tool(bin_dir, "swift", swift_log)
_fake_tool(bin_dir, "xcodebuild", xcode_log)
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}")
package = tmp_path / "pkg"
_write(package / "Package.swift", "// swift-tools-version:5.9\n")
_write(package / "Sources" / "Demo" / "Box.swift", "func open() -> Int { 1 }\n")
swift_code = run(["--no-coverage", "--root", str(package), "--max-workers", "1"])
swift_text = swift_log.read_text(encoding="utf-8")
assert swift_code == 3
assert "test\n" in swift_text
assert str(package.resolve()) in swift_text
app = tmp_path / "ios"
project = app / "My App.xcodeproj"
_write(project / "xcshareddata" / "xcschemes" / "App.xcscheme", "<Scheme/>\n")
_write(app / "App" / "ContentView.swift", "func body() -> Int { 1 }\n")
destination = "platform=iOS Simulator,name=iPhone 16"
xcode_code = run(
[
"--no-coverage",
"--root",
str(app),
"--max-workers",
"1",
"--xcode-scheme",
"App",
"--xcode-destination",
destination,
]
)
xcode_text = xcode_log.read_text(encoding="utf-8")
assert xcode_code == 3
assert "xcodebuild" not in xcode_text
assert "-project" in xcode_text
assert str(project.resolve()) in xcode_text
assert "-scheme" in xcode_text
assert "App" in xcode_text.splitlines()
assert destination in xcode_text.splitlines()
def test_a_swift_run_records_killed_and_survived_on_the_crapper_namespace(tmp_path, capsys):
relative = "Sources/Demo/Place.swift"
path = tmp_path / relative