Skip to content

Commit e1ed897

Browse files
committed
Add a hostRuntimeEnvironment test helper function
This may fix the few tests which are failing due to missing DLLs on Windows in certain environments.
1 parent d0715c9 commit e1ed897

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

Sources/SWBTestSupport/RunDestinationTestSupport.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,26 @@ extension RunDestinationInfo {
316316
return .elf
317317
}
318318
}
319+
320+
/// An `Environment` object with `PATH` or `LD_LIBRARY_PATH` set appropriately pointing into the toolchain to be able to run a built Swift binary in tests.
321+
///
322+
/// - note: On macOS, the OS provided Swift runtime is used, so `DYLD_LIBRARY_PATH` is never set for Mach-O destinations.
323+
package func hostRuntimeEnvironment(_ core: Core, initialEnvironment: Environment = Environment()) -> Environment {
324+
var environment = initialEnvironment
325+
guard let toolchain = core.toolchainRegistry.defaultToolchain else {
326+
return environment
327+
}
328+
switch imageFormat(core) {
329+
case .elf:
330+
environment.prependPath(key: "LD_LIBRARY_PATH", value: toolchain.path.join("usr/lib/swift/\(platform)").str)
331+
case .pe:
332+
environment.prependPath(key: .path, value: core.developerPath.path.join("Runtimes").join(toolchain.version.description).join("usr/bin").str)
333+
case .macho:
334+
// Fall back to the OS provided Swift runtime
335+
break
336+
}
337+
return environment
338+
}
319339
}
320340

321341
extension _RunDestinationInfo {

Tests/SWBBuildSystemTests/BuildOperationTests.swift

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,7 @@ fileprivate struct BuildOperationTests: CoreBasedTests {
145145
try await tester.checkBuild(runDestination: destination, signableTargets: Set(provisioningInputs.keys), signableTargetInputs: provisioningInputs) { results in
146146
results.checkNoErrors()
147147

148-
let toolchain = try #require(core.toolchainRegistry.defaultToolchain)
149-
let environment: Environment
150-
if destination.imageFormat(core) == .elf {
151-
environment = ["LD_LIBRARY_PATH": toolchain.path.join("usr/lib/swift/\(destination.platform)").str]
152-
} else {
153-
environment = .init()
154-
}
155-
156-
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "tool")).str), arguments: [], environment: environment)
148+
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "tool")).str), arguments: [], environment: destination.hostRuntimeEnvironment(core))
157149
#expect(executionResult.exitStatus == .exit(0))
158150
if core.hostOperatingSystem == .windows {
159151
#expect(String(decoding: executionResult.stdout, as: UTF8.self) == "Hello world\r\n")
@@ -378,15 +370,7 @@ fileprivate struct BuildOperationTests: CoreBasedTests {
378370
}
379371
}
380372

381-
let toolchain = try #require(try await getCore().toolchainRegistry.defaultToolchain)
382-
let environment: Environment
383-
if destination.platform == "linux" {
384-
environment = ["LD_LIBRARY_PATH": toolchain.path.join("usr/lib/swift/linux").str]
385-
} else {
386-
environment = .init()
387-
}
388-
389-
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "tool")).str), arguments: [], environment: environment)
373+
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "tool")).str), arguments: [], environment: destination.hostRuntimeEnvironment(core))
390374
#expect(executionResult.exitStatus == .exit(0))
391375
if core.hostOperatingSystem == .windows {
392376
#expect(String(decoding: executionResult.stdout, as: UTF8.self) == "Hello world\r\n")
@@ -508,13 +492,7 @@ fileprivate struct BuildOperationTests: CoreBasedTests {
508492
try await tester.checkBuild(runDestination: destination, persistent: true) { results in
509493
results.checkNoErrors()
510494

511-
let toolchain = try #require(try await getCore().toolchainRegistry.defaultToolchain)
512-
let environment: Environment
513-
if destination.platform == "linux" {
514-
environment = ["LD_LIBRARY_PATH": "\(toolchain.path.join("usr/lib/swift/linux").str):\(projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)"))"]
515-
} else {
516-
environment = .init()
517-
}
495+
let environment = destination.hostRuntimeEnvironment(core)
518496

519497
do {
520498
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "UnitTestRunner")).str), arguments: [], environment: environment)

Tests/SWBBuildSystemTests/CustomTaskBuildOperationTests.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ fileprivate struct CustomTaskBuildOperationTests: CoreBasedTests {
3030
let destination: RunDestinationInfo = .host
3131
let core = try await getCore()
3232
let toolchain = try #require(core.toolchainRegistry.defaultToolchain)
33-
let environment: [String: String]
34-
if destination.imageFormat(core) == .elf {
35-
environment = ["LD_LIBRARY_PATH": toolchain.path.join("usr/lib/swift/\(destination.platform)").str]
36-
} else {
37-
environment = ProcessInfo.processInfo.environment.filter { $0.key.uppercased() == "PATH" } // important to allow swift to be looked up in PATH on Windows/Linux
38-
}
33+
let environment = destination.hostRuntimeEnvironment(core)
3934

4035
let testProject = TestProject(
4136
"aProject",
@@ -69,7 +64,7 @@ fileprivate struct CustomTaskBuildOperationTests: CoreBasedTests {
6964
customTasks: [
7065
TestCustomTask(
7166
commandLine: ["$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/tool\(destination == .windows ? ".exe" : "")"],
72-
environment: environment,
67+
environment: .init(environment),
7368
workingDirectory: tmpDir.str,
7469
executionDescription: "My Custom Task",
7570
inputs: ["$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/tool\(destination == .windows ? ".exe" : "")"],

0 commit comments

Comments
 (0)