Skip to content

Commit 423ffae

Browse files
authored
[6.0] Set an environment variable in swift test to indicate which testing library is in use. (#7577)
**Explanation**: Allows swift-testing and XCTest to detect at runtime which testing library SwiftPM is trying to run so that they can adjust their behaviour (e.g. `XCTestScaffold`) appropriately. **Scope**: Test build products at runtime. **Risk**: Low. No obvious risk here to adding a new environment variable nobody is looking for. **Testing**: New unit test (tested at desk, but must be temporarily disabled in CI until CI is running a version of SwiftPM that includes the change.) **Original PR**: #7573
1 parent 2d35c65 commit 423ffae

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// swift-tools-version: 5.10
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "CheckTestLibraryEnvironmentVariable",
7+
targets: [
8+
.testTarget(name: "CheckTestLibraryEnvironmentVariableTests"),
9+
]
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import XCTest
2+
3+
final class CheckTestLibraryEnvironmentVariableTests: XCTestCase {
4+
func testEnviromentVariable() throws {
5+
let envvar = ProcessInfo.processInfo.environment["SWIFT_PM_TEST_LIBRARY"]
6+
XCTAssertEqual(envvar, "XCTest")
7+
}
8+
}

Sources/Commands/SwiftTestCommand.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
422422
let testEnv = try TestingSupport.constructTestEnvironment(
423423
toolchain: toolchain,
424424
buildParameters: buildParameters,
425-
sanitizers: globalOptions.build.sanitizers
425+
sanitizers: globalOptions.build.sanitizers,
426+
library: library
426427
)
427428

428429
let runner = TestRunner(
@@ -697,7 +698,8 @@ extension SwiftTestCommand {
697698
let testEnv = try TestingSupport.constructTestEnvironment(
698699
toolchain: toolchain,
699700
buildParameters: buildParameters,
700-
sanitizers: globalOptions.build.sanitizers
701+
sanitizers: globalOptions.build.sanitizers,
702+
library: .swiftTesting
701703
)
702704

703705
let additionalArguments = ["--list-tests"] + CommandLine.arguments.dropFirst()
@@ -1007,7 +1009,8 @@ final class ParallelTestRunner {
10071009
let testEnv = try TestingSupport.constructTestEnvironment(
10081010
toolchain: self.toolchain,
10091011
buildParameters: self.buildParameters,
1010-
sanitizers: self.buildOptions.sanitizers
1012+
sanitizers: self.buildOptions.sanitizers,
1013+
library: .xctest // swift-testing does not use ParallelTestRunner
10111014
)
10121015

10131016
// Enqueue all the tests.

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ final class PluginDelegate: PluginInvocationDelegate {
236236
let testEnvironment = try TestingSupport.constructTestEnvironment(
237237
toolchain: toolchain,
238238
buildParameters: toolsBuildParameters,
239-
sanitizers: swiftCommandState.options.build.sanitizers
239+
sanitizers: swiftCommandState.options.build.sanitizers,
240+
library: .xctest // FIXME: support both libraries
240241
)
241242

242243
// Iterate over the tests and run those that match the filter.

Sources/Commands/Utilities/TestingSupport.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ enum TestingSupport {
116116
experimentalTestOutput: experimentalTestOutput,
117117
library: .xctest
118118
),
119-
sanitizers: sanitizers
119+
sanitizers: sanitizers,
120+
library: .xctest
120121
)
121122

122123
try TSCBasic.Process.checkNonZeroExit(arguments: args, environment: env)
@@ -131,7 +132,8 @@ enum TestingSupport {
131132
shouldSkipBuilding: shouldSkipBuilding,
132133
library: .xctest
133134
),
134-
sanitizers: sanitizers
135+
sanitizers: sanitizers,
136+
library: .xctest
135137
)
136138
args = [path.description, "--dump-tests-json"]
137139
let data = try Process.checkNonZeroExit(arguments: args, environment: env)
@@ -144,7 +146,8 @@ enum TestingSupport {
144146
static func constructTestEnvironment(
145147
toolchain: UserToolchain,
146148
buildParameters: BuildParameters,
147-
sanitizers: [Sanitizer]
149+
sanitizers: [Sanitizer],
150+
library: BuildParameters.Testing.Library
148151
) throws -> EnvironmentVariables {
149152
var env = EnvironmentVariables.process()
150153

@@ -156,6 +159,10 @@ enum TestingSupport {
156159
env["NO_COLOR"] = "1"
157160
}
158161

162+
// Set an environment variable to indicate which library's test product
163+
// is being executed.
164+
env["SWIFT_PM_TEST_LIBRARY"] = String(describing: library)
165+
159166
// Add the code coverage related variables.
160167
if buildParameters.testingParameters.enableCodeCoverage {
161168
// Defines the path at which the profraw files will be written on test execution.

Tests/CommandsTests/TestCommandTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,10 @@ final class TestCommandTests: CommandsTestCase {
303303
}
304304
}
305305
#endif
306+
307+
func testLibraryEnvironmentVariable() throws {
308+
try fixture(name: "Miscellaneous/CheckTestLibraryEnvironmentVariable") { fixturePath in
309+
XCTAssertNoThrow(try SwiftPM.Test.execute(packagePath: fixturePath))
310+
}
311+
}
306312
}

0 commit comments

Comments
 (0)