1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
//===--------------- JobExecutorTests.swift - Swift Execution Tests -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import XCTest
import TSCBasic
@_spi(Testing) import SwiftDriver
import SwiftDriverExecution
import TestUtilities
extension Job.ArgTemplate: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self = .flag(value)
}
}
class JobCollectingDelegate: JobExecutionDelegate {
struct StubProcess: ProcessProtocol {
static func launchProcess(arguments: [String], env: [String : String]) throws -> StubProcess {
return .init()
}
static func launchProcessAndWriteInput(arguments: [String], env: [String : String],
inputFileHandle: FileHandle) throws -> StubProcess {
return .init()
}
var processID: TSCBasic.Process.ProcessID { .init(-1) }
func waitUntilExit() throws -> ProcessResult {
return ProcessResult(
arguments: [],
environment: [:],
exitStatus: .terminated(code: EXIT_SUCCESS),
output: Result.success(ByteString("test").contents),
stderrOutput: Result.success([])
)
}
}
var started: [Job] = []
var finished: [(Job, ProcessResult)] = []
func jobFinished(job: Job, result: ProcessResult, pid: Int) {
finished.append((job, result))
}
func jobStarted(job: Job, arguments: [String], pid: Int) {
started.append(job)
}
func jobSkipped(job: Job) {}
}
extension DarwinToolchain {
/// macOS SDK path, for testing only.
var sdk: Result<AbsolutePath, Swift.Error> {
Result {
let result = try executor.checkNonZeroExit(
args: "xcrun", "-sdk", "macosx", "--show-sdk-path",
environment: env
).spm_chomp()
return try AbsolutePath(validating: result)
}
}
/// macOS resource directory, for testing only.
var resourcesDirectory: Result<AbsolutePath, Swift.Error> {
return Result {
try AbsolutePath(validating: "../../lib/swift/macosx",
relativeTo: getToolPath(.swiftCompiler))
}
}
var clangRT: Result<AbsolutePath, Error> {
resourcesDirectory.map { try! AbsolutePath(validating: "../clang/lib/darwin/libclang_rt.osx.a",
relativeTo: $0) }
}
var compatibility50: Result<AbsolutePath, Error> {
get throws {
resourcesDirectory.map { $0.appending(component: "libswiftCompatibility50.a") }
}
}
var compatibilityDynamicReplacements: Result<AbsolutePath, Error> {
get throws {
resourcesDirectory.map { $0.appending(component: "libswiftCompatibilityDynamicReplacements.a") }
}
}
}
final class JobExecutorTests: XCTestCase {
func testDarwinBasic() throws {
#if os(macOS)
let hostTriple = try Driver(args: ["swiftc", "test.swift"]).hostTriple
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let toolchain = DarwinToolchain(env: ProcessEnv.vars, executor: executor)
try withTemporaryDirectory { path in
let foo = path.appending(component: "foo.swift")
let main = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(foo, bytes: "let foo = 5")
try localFileSystem.writeFileContents(main, bytes: "print(foo)")
let exec = path.appending(component: "main")
let resolver = try ArgsResolver(fileSystem: localFileSystem)
resolver.pathMapping = [
.relative(try .init(validating: "foo.swift")): foo.pathString,
.relative(try .init(validating: "main.swift")): main.pathString,
.relative(try .init(validating: "main")): exec.pathString,
]
let inputs: [String: TypedVirtualPath] = [
"foo" : .init(file: VirtualPath.relative(try .init(validating: "foo.swift")).intern(), type: .swift),
"main": .init(file: VirtualPath.relative(try .init(validating: "main.swift")).intern(), type: .swift)
]
let compileFoo = Job(
moduleName: "main",
kind: .compile,
tool: try toolchain.resolvedTool(.swiftCompiler),
commandLine: [
"-frontend",
"-c",
"-primary-file",
.path(inputs[ "foo"]!.file),
.path(inputs["main"]!.file),
"-target", .flag(hostTriple.triple),
"-enable-objc-interop",
"-sdk",
.path(.absolute(try toolchain.sdk.get())),
"-module-name", "main",
"-o", .path(.temporary(try RelativePath(validating: "foo.o"))),
],
inputs: Array(inputs.values),
primaryInputs: [inputs["foo"]!],
outputs: [.init(file: VirtualPath.temporary(try RelativePath(validating: "foo.o")).intern(), type: .object)]
)
let compileMain = Job(
moduleName: "main",
kind: .compile,
tool: try toolchain.resolvedTool(.swiftCompiler),
commandLine: [
"-frontend",
"-c",
.path(.relative(try .init(validating: "foo.swift"))),
"-primary-file",
.path(inputs["main"]!.file),
"-target", .flag(hostTriple.triple),
"-enable-objc-interop",
"-sdk",
.path(.absolute(try toolchain.sdk.get())),
"-module-name", "main",
"-o", .path(.temporary(try RelativePath(validating: "main.o"))),
],
inputs: Array(inputs.values),
primaryInputs: [inputs["main"]!],
outputs: [.init(file: VirtualPath.temporary(try RelativePath(validating: "main.o")).intern(), type: .object)]
)
let link = Job(
moduleName: "main",
kind: .link,
tool: try toolchain.resolvedTool(.dynamicLinker),
commandLine: [
.path(.temporary(try RelativePath(validating: "foo.o"))),
.path(.temporary(try RelativePath(validating: "main.o"))),
.path(.absolute(try toolchain.clangRT.get())),
"--sysroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", .flag("--target=\(hostTriple.triple)"),
"-L", .path(.absolute(try toolchain.resourcesDirectory.get())),
"-L", .path(.absolute(try toolchain.sdkStdlib(sdk: toolchain.sdk.get()))),
"-rpath", "/usr/lib/swift", "-o",
.path(.relative(try .init(validating: "main"))),
],
inputs: [
.init(file: VirtualPath.temporary(try RelativePath(validating: "foo.o")).intern(), type: .object),
.init(file: VirtualPath.temporary(try RelativePath(validating: "main.o")).intern(), type: .object),
],
primaryInputs: [],
outputs: [.init(file: VirtualPath.relative(try .init(validating: "main")).intern(), type: .image)]
)
let delegate = JobCollectingDelegate()
let executor = MultiJobExecutor(workload: .all([compileFoo, compileMain, link]),
resolver: resolver, executorDelegate: delegate, diagnosticsEngine: DiagnosticsEngine())
try executor.execute(env: toolchain.env, fileSystem: localFileSystem)
let output = try TSCBasic.Process.checkNonZeroExit(args: exec.pathString)
XCTAssertEqual(output, "5\n")
XCTAssertEqual(delegate.started.count, 3)
let fooObject = try resolver.resolve(.path(.temporary(try RelativePath(validating: "foo.o"))))
XCTAssertTrue(localFileSystem.exists(try AbsolutePath(validating: fooObject)), "expected foo.o to be present in the temporary directory")
try resolver.removeTemporaryDirectory()
XCTAssertFalse(localFileSystem.exists(try AbsolutePath(validating: fooObject)), "expected foo.o to be removed from the temporary directory")
}
#endif
}
/// Ensure the executor is capable of forwarding its standard input to the compile job that requires it.
func testInputForwarding() throws {
#if os(macOS)
let hostTriple = try Driver(args: ["swiftc", "test.swift"]).hostTriple
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let toolchain = DarwinToolchain(env: ProcessEnv.vars, executor: executor)
try withTemporaryDirectory { path in
let exec = path.appending(component: "main")
let compile = Job(
moduleName: "main",
kind: .compile,
tool: try toolchain.resolvedTool(.swiftCompiler),
commandLine: [
"-frontend",
"-c",
"-primary-file",
// This compile job must read the input from STDIN
"-",
"-target", .flag(hostTriple.triple),
"-enable-objc-interop",
"-sdk",
.path(.absolute(try toolchain.sdk.get())),
"-module-name", "main",
"-o", .path(.temporary(try RelativePath(validating: "main.o"))),
],
inputs: [TypedVirtualPath(file: .standardInput, type: .swift )],
primaryInputs: [TypedVirtualPath(file: .standardInput, type: .swift )],
outputs: [.init(file: VirtualPath.temporary(try RelativePath(validating: "main.o")).intern(),
type: .object)]
)
let link = Job(
moduleName: "main",
kind: .link,
tool: try toolchain.resolvedTool(.dynamicLinker),
commandLine: [
.path(.temporary(try RelativePath(validating: "main.o"))),
"--sysroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", .flag("--target=\(hostTriple.triple)"),
"-L", .path(.absolute(try toolchain.resourcesDirectory.get())),
"-L", .path(.absolute(try toolchain.sdkStdlib(sdk: toolchain.sdk.get()))),
"-o", .path(.absolute(exec)),
],
inputs: [
.init(file: VirtualPath.temporary(try RelativePath(validating: "main.o")).intern(), type: .object),
],
primaryInputs: [],
outputs: [.init(file: VirtualPath.relative(try .init(validating: "main")).intern(), type: .image)]
)
// Create a file with inpuit
let inputFile = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(inputFile, bytes: "print(\"Hello, World\")")
// We are going to override he executors standard input FileHandle to the above
// input file, to simulate it being piped over standard input to this compilation.
let testFile: FileHandle = FileHandle(forReadingAtPath: inputFile.description)!
let delegate = JobCollectingDelegate()
let resolver = try ArgsResolver(fileSystem: localFileSystem)
let executor = MultiJobExecutor(workload: .all([compile, link]),
resolver: resolver, executorDelegate: delegate,
diagnosticsEngine: DiagnosticsEngine(),
inputHandleOverride: testFile)
try executor.execute(env: toolchain.env, fileSystem: localFileSystem)
// Execute the resulting program
let output = try TSCBasic.Process.checkNonZeroExit(args: exec.pathString)
XCTAssertEqual(output, "Hello, World\n")
}
#endif
}
func testStubProcessProtocol() throws {
#if os(Windows)
throw XCTSkip("processId.getter returning `-1`")
#else
let job = Job(
moduleName: "main",
kind: .compile,
tool: ResolvedTool(path: try AbsolutePath(validating: "/usr/bin/swift"), supportsResponseFiles: false),
commandLine: [.flag("something")],
inputs: [],
primaryInputs: [],
outputs: [.init(file: VirtualPath.temporary(try RelativePath(validating: "main")).intern(), type: .object)]
)
let delegate = JobCollectingDelegate()
let executor = MultiJobExecutor(
workload: .all([job]), resolver: try ArgsResolver(fileSystem: localFileSystem),
executorDelegate: delegate,
diagnosticsEngine: DiagnosticsEngine(),
processType: JobCollectingDelegate.StubProcess.self
)
try executor.execute(env: ProcessEnv.vars, fileSystem: localFileSystem)
XCTAssertEqual(try delegate.finished[0].1.utf8Output(), "test")
#endif
}
func testSwiftDriverExecOverride() throws {
var env = ProcessEnv.vars
let envVarName = "SWIFT_DRIVER_SWIFT_FRONTEND_EXEC"
let dummyPath = "/some/garbage/path/fnord"
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: env)
// DarwinToolchain
env.removeValue(forKey: envVarName)
let normalSwiftPath = try DarwinToolchain(env: env, executor: executor).getToolPath(.swiftCompiler)
// Match Toolchain temporary shim of a fallback to looking for "swift" before failing.
XCTAssertTrue(normalSwiftPath.basenameWithoutExt == "swift-frontend" ||
normalSwiftPath.basenameWithoutExt == "swift")
env[envVarName] = dummyPath
let overriddenSwiftPath = try DarwinToolchain(env: env, executor: executor).getToolPath(.swiftCompiler)
XCTAssertEqual(overriddenSwiftPath, try AbsolutePath(validating: dummyPath))
// GenericUnixToolchain
env.removeValue(forKey: envVarName)
let unixSwiftPath = try GenericUnixToolchain(env: env, executor: executor).getToolPath(.swiftCompiler)
XCTAssertTrue(unixSwiftPath.basenameWithoutExt == "swift-frontend" ||
unixSwiftPath.basenameWithoutExt == "swift")
env[envVarName] = dummyPath
let unixOverriddenSwiftPath = try GenericUnixToolchain(env: env, executor: executor).getToolPath(.swiftCompiler)
XCTAssertEqual(unixOverriddenSwiftPath, try AbsolutePath(validating: dummyPath))
}
func testInputModifiedDuringSingleJobBuild() throws {
#if os(Windows)
throw XCTSkip("Requires -sdk")
#else
try withTemporaryDirectory { path in
let main = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(main, bytes: "let foo = 1")
var driver = try Driver(args: ["swift", main.pathString])
let jobs = try driver.planBuild()
XCTAssertTrue(jobs.count == 1 && jobs[0].requiresInPlaceExecution)
// Change the file
try localFileSystem.writeFileContents(main, bytes: "let foo = 1")
XCTAssertThrowsError(try driver.run(jobs: jobs)) {
XCTAssertEqual($0 as? Job.InputError,
.inputUnexpectedlyModified(TypedVirtualPath(file: VirtualPath.absolute(main).intern(), type: .swift)))
}
}
#endif
}
func testShellEscapingArgsInJobDescription() throws {
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: [:])
let job = Job(moduleName: "Module",
kind: .compile,
tool: ResolvedTool(
path: try AbsolutePath(validating: "/path/to/the tool"),
supportsResponseFiles: false),
commandLine: [.path(.absolute(try .init(validating: "/with space"))),
.path(.absolute(try .init(validating: "/withoutspace")))],
inputs: [], primaryInputs: [], outputs: [])
#if os(Windows)
XCTAssertEqual(try executor.description(of: job, forceResponseFiles: false),
#""\path\to\the tool" "\with space" \withoutspace"#)
#else
XCTAssertEqual(try executor.description(of: job, forceResponseFiles: false),
"'/path/to/the tool' '/with space' /withoutspace")
#endif
}
func testInputModifiedDuringMultiJobBuild() throws {
try withTemporaryDirectory { path in
let main = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(main, bytes: "let foo = 1")
let other = path.appending(component: "other.swift")
try localFileSystem.writeFileContents(other, bytes: "let bar = 2")
let output = path.appending(component: "a.out")
// Sleep for 1s to allow for quiescing mtimes on filesystems with
// insufficient timestamp precision.
Thread.sleep(forTimeInterval: 1)
try assertDriverDiagnostics(args: ["swiftc", main.pathString, other.pathString,
"-o", output.pathString]) {driver, verifier in
let jobs = try driver.planBuild()
XCTAssertTrue(jobs.count > 1)
// Change the file
try localFileSystem.writeFileContents(other, bytes: "let bar = 3")
verifier.expect(.error("input file '\(other.description)' was modified during the build"))
// There's a tool-specific linker error that usually happens here from
// whatever job runs last - probably the linker.
// It's no use testing for a particular error message, let's just make
// sure we emit the diagnostic we need.
verifier.permitUnexpected(.error)
XCTAssertThrowsError(try driver.run(jobs: jobs))
}
}
}
func testTemporaryFileWriting() throws {
try withTemporaryDirectory { path in
let resolver = try ArgsResolver(fileSystem: localFileSystem, temporaryDirectory: .absolute(path))
let tmpPath = VirtualPath.temporaryWithKnownContents(try .init(validating: "one.txt"), "hello, world!".data(using: .utf8)!)
let resolvedOnce = try resolver.resolve(.path(tmpPath))
let readContents = try localFileSystem.readFileContents(.init(validating: resolvedOnce))
XCTAssertEqual(readContents, "hello, world!")
let resolvedTwice = try resolver.resolve(.path(tmpPath))
XCTAssertEqual(resolvedOnce, resolvedTwice)
let readContents2 = try localFileSystem.readFileContents(.init(validating: resolvedTwice))
XCTAssertEqual(readContents2, readContents)
}
}
func testResolveSquashedArgs() throws {
try withTemporaryDirectory { path in
let resolver = try ArgsResolver(fileSystem: localFileSystem, temporaryDirectory: .absolute(path))
let tmpPath = VirtualPath.temporaryWithKnownContents(try .init(validating: "one.txt"), "hello, world!".data(using: .utf8)!)
let tmpPath2 = VirtualPath.temporaryWithKnownContents(try .init(validating: "two.txt"), "goodbye!".data(using: .utf8)!)
let resolvedCommandLine = try resolver.resolve(
.squashedArgumentList(option: "--opt=", args: [.path(tmpPath), .path(tmpPath2)]))
XCTAssertEqual(resolvedCommandLine, "--opt=\(path.appending(component: "one.txt").pathString) \(path.appending(component: "two.txt").pathString)")
#if os(Windows)
XCTAssertEqual(resolvedCommandLine.spm_shellEscaped(),
#""--opt=\#(path.appending(component: "one.txt").pathString) \#(path.appending(component: "two.txt").pathString)""#)
#else
XCTAssertEqual(resolvedCommandLine.spm_shellEscaped(), "'--opt=\(path.appending(component: "one.txt").pathString) \(path.appending(component: "two.txt").pathString)'")
#endif
}
}
private func getHostToolchainSdkArg(_ executor: SwiftDriverExecutor) throws -> [String] {
#if os(macOS)
let toolchain = DarwinToolchain(env: ProcessEnv.vars, executor: executor)
return try ["-sdk", toolchain.sdk.get().pathString]
#elseif os(Windows)
let toolchain = WindowsToolchain(env: ProcessEnv.vars, executor: executor)
if let path = try toolchain.defaultSDKPath(nil) {
return ["-sdk", path.nativePathString(escaped: false)]
}
return []
#else
return []
#endif
}
func testSaveTemps() throws {
do {
try withTemporaryDirectory { path in
let main = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(main, bytes: "print(\"hello, world!\")")
let diags = DiagnosticsEngine()
let executor = try SwiftDriverExecutor(diagnosticsEngine: diags,
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let outputPath = path.appending(component: "finalOutput")
var driver = try Driver(args: ["swiftc", main.pathString,
"-driver-filelist-threshold", "0",
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
env: ProcessEnv.vars,
diagnosticsOutput: .engine(diags),
fileSystem: localFileSystem,
executor: executor)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.removingAutolinkExtractJobs().map(\.kind), [.compile, .link])
XCTAssertEqual(jobs[0].outputs.count, 1)
let compileOutput = jobs[0].outputs[0].file
guard matchTemporary(compileOutput, "main.o") else {
XCTFail("unexpected output")
return
}
try driver.run(jobs: jobs)
XCTAssertTrue(localFileSystem.exists(outputPath))
// -save-temps wasn't passed, so ensure the temporary file was removed.
XCTAssertFalse(
localFileSystem.exists(try .init(validating: try executor.resolver.resolve(.path(driver.allSourcesFileList!))))
)
XCTAssertFalse(localFileSystem.exists(try .init(validating: try executor.resolver.resolve(.path(compileOutput)))))
}
}
do {
try withTemporaryDirectory { path in
let main = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(main, bytes: "print(\"hello, world!\")")
let diags = DiagnosticsEngine()
let executor = try SwiftDriverExecutor(diagnosticsEngine: diags,
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let outputPath = path.appending(component: "finalOutput")
var driver = try Driver(args: ["swiftc", main.pathString,
"-save-temps",
"-driver-filelist-threshold", "0",
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
env: ProcessEnv.vars,
diagnosticsOutput: .engine(diags),
fileSystem: localFileSystem,
executor: executor)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.removingAutolinkExtractJobs().map(\.kind), [.compile, .link])
XCTAssertEqual(jobs[0].outputs.count, 1)
let compileOutput = jobs[0].outputs[0].file
guard matchTemporary(compileOutput, "main.o") else {
XCTFail("unexpected output")
return
}
try driver.run(jobs: jobs)
XCTAssertTrue(localFileSystem.exists(outputPath))
// -save-temps was passed, so ensure the temporary file was not removed.
XCTAssertTrue(
localFileSystem.exists(try .init(validating: executor.resolver.resolve(.path(driver.allSourcesFileList!))))
)
XCTAssertTrue(localFileSystem.exists(try .init(validating: executor.resolver.resolve(.path(compileOutput)))))
}
}
do {
try withTemporaryDirectory { path in
let main = path.appending(component: "main.swift")
try localFileSystem.writeFileContents(main, bytes: "print(\"hello, world!\")")
let diags = DiagnosticsEngine()
let executor = try SwiftDriverExecutor(diagnosticsEngine: diags,
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let outputPath = path.appending(component: "finalOutput")
var driver = try Driver(args: ["swiftc", main.pathString,
"-driver-filelist-threshold", "0",
"-Xfrontend", "-debug-crash-immediately",
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
env: ProcessEnv.vars,
diagnosticsOutput: .engine(diags),
fileSystem: localFileSystem,
executor: executor)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.removingAutolinkExtractJobs().map(\.kind), [.compile, .link])
XCTAssertEqual(jobs[0].outputs.count, 1)
let compileOutput = jobs[0].outputs[0].file
guard matchTemporary(compileOutput, "main.o") else {
XCTFail("unexpected output")
return
}
try? driver.run(jobs: jobs)
// A job crashed, so ensure any temporary files written so far are preserved.
XCTAssertTrue(
localFileSystem.exists(try .init(validating: executor.resolver.resolve(.path(driver.allSourcesFileList!))))
)
}
}
}
}
|