Skip to content

Commit edfb4b3

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 70af35a commit edfb4b3

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
@@ -296,6 +296,26 @@ extension RunDestinationInfo {
296296
return .elf
297297
}
298298
}
299+
300+
/// 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.
301+
///
302+
/// - note: On macOS, the OS provided Swift runtime is used, so `DYLD_LIBRARY_PATH` is never set for Mach-O destinations.
303+
package func hostRuntimeEnvironment(_ core: Core, initialEnvironment: Environment = Environment()) -> Environment {
304+
var environment = initialEnvironment
305+
guard let toolchain = core.toolchainRegistry.defaultToolchain else {
306+
return environment
307+
}
308+
switch imageFormat(core) {
309+
case .elf:
310+
environment.prependPath(key: "LD_LIBRARY_PATH", value: toolchain.path.join("usr/lib/swift/\(platform)").str)
311+
case .pe:
312+
environment.prependPath(key: .path, value: core.developerPath.path.join("Runtimes").join(toolchain.version.description).join("usr/bin").str)
313+
case .macho:
314+
// Fall back to the OS provided Swift runtime
315+
break
316+
}
317+
return environment
318+
}
299319
}
300320

301321
extension _RunDestinationInfo {

Tests/SWBBuildSystemTests/BuildOperationTests.swift

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

147-
let toolchain = try #require(core.toolchainRegistry.defaultToolchain)
148-
let environment: Environment
149-
if destination.imageFormat(core) == .elf {
150-
environment = ["LD_LIBRARY_PATH": toolchain.path.join("usr/lib/swift/\(destination.platform)").str]
151-
} else {
152-
environment = .init()
153-
}
154-
155-
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)
147+
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))
156148
#expect(executionResult.exitStatus == .exit(0))
157149
if core.hostOperatingSystem == .windows {
158150
#expect(String(decoding: executionResult.stdout, as: UTF8.self) == "Hello world\r\n")
@@ -377,15 +369,7 @@ fileprivate struct BuildOperationTests: CoreBasedTests {
377369
}
378370
}
379371

380-
let toolchain = try #require(try await getCore().toolchainRegistry.defaultToolchain)
381-
let environment: Environment
382-
if destination.platform == "linux" {
383-
environment = ["LD_LIBRARY_PATH": toolchain.path.join("usr/lib/swift/linux").str]
384-
} else {
385-
environment = .init()
386-
}
387-
388-
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)
372+
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))
389373
#expect(executionResult.exitStatus == .exit(0))
390374
if core.hostOperatingSystem == .windows {
391375
#expect(String(decoding: executionResult.stdout, as: UTF8.self) == "Hello world\r\n")
@@ -480,13 +464,7 @@ fileprivate struct BuildOperationTests: CoreBasedTests {
480464
try await tester.checkBuild(runDestination: destination, persistent: true) { results in
481465
results.checkNoErrors()
482466

483-
let toolchain = try #require(try await getCore().toolchainRegistry.defaultToolchain)
484-
let environment: Environment
485-
if destination.platform == "linux" {
486-
environment = ["LD_LIBRARY_PATH": toolchain.path.join("usr/lib/swift/linux").str]
487-
} else {
488-
environment = .init()
489-
}
467+
let environment = destination.hostRuntimeEnvironment(core)
490468

491469
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "test.xctest")).str), arguments: ["--testing-library", "swift-testing"], environment: environment)
492470
#expect(String(decoding: executionResult.stderr, as: UTF8.self).contains("Test run started"))

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)