Skip to content

Commit

Permalink
Set an environment variable in swift test to indicate which testing…
Browse files Browse the repository at this point in the history
… library is in use. (#7573)

This PR sets a new environment variable `"SWIFT_PM_TEST_LIBRARY"` to the
name of the active testing library (`"XCTest"` or `"swift-testing"`.)
Code in these libraries can then use the presence of this environment
variable to adjust their behaviour.

Resolves rdar://128272138.
  • Loading branch information
grynspan authored May 17, 2024
1 parent e5123e4 commit c965d5a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
name: "CheckTestLibraryEnvironmentVariable",
targets: [
.testTarget(name: "CheckTestLibraryEnvironmentVariableTests"),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest

final class CheckTestLibraryEnvironmentVariableTests: XCTestCase {
func testEnviromentVariable() throws {
let envvar = ProcessInfo.processInfo.environment["SWIFT_PM_TEST_LIBRARY"]
XCTAssertEqual(envvar, "XCTest")
}
}
9 changes: 6 additions & 3 deletions Sources/Commands/SwiftTestCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
let testEnv = try TestingSupport.constructTestEnvironment(
toolchain: toolchain,
destinationBuildParameters: productsBuildParameters,
sanitizers: globalOptions.build.sanitizers
sanitizers: globalOptions.build.sanitizers,
library: library
)

let runner = TestRunner(
Expand Down Expand Up @@ -702,7 +703,8 @@ extension SwiftTestCommand {
let testEnv = try TestingSupport.constructTestEnvironment(
toolchain: toolchain,
destinationBuildParameters: productsBuildParameters,
sanitizers: globalOptions.build.sanitizers
sanitizers: globalOptions.build.sanitizers,
library: .swiftTesting
)

let additionalArguments = ["--list-tests"] + CommandLine.arguments.dropFirst()
Expand Down Expand Up @@ -1014,7 +1016,8 @@ final class ParallelTestRunner {
let testEnv = try TestingSupport.constructTestEnvironment(
toolchain: self.toolchain,
destinationBuildParameters: self.productsBuildParameters,
sanitizers: self.buildOptions.sanitizers
sanitizers: self.buildOptions.sanitizers,
library: .xctest // swift-testing does not use ParallelTestRunner
)

// Enqueue all the tests.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Commands/Utilities/PluginDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ final class PluginDelegate: PluginInvocationDelegate {
let testEnvironment = try TestingSupport.constructTestEnvironment(
toolchain: toolchain,
destinationBuildParameters: toolsBuildParameters,
sanitizers: swiftCommandState.options.build.sanitizers
sanitizers: swiftCommandState.options.build.sanitizers,
library: .xctest // FIXME: support both libraries
)

// Iterate over the tests and run those that match the filter.
Expand Down
13 changes: 10 additions & 3 deletions Sources/Commands/Utilities/TestingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ enum TestingSupport {
experimentalTestOutput: experimentalTestOutput,
library: .xctest
).productsBuildParameters,
sanitizers: sanitizers
sanitizers: sanitizers,
library: .xctest
)

try TSCBasic.Process.checkNonZeroExit(arguments: args, environment: env)
Expand All @@ -136,7 +137,8 @@ enum TestingSupport {
shouldSkipBuilding: shouldSkipBuilding,
library: .xctest
).productsBuildParameters,
sanitizers: sanitizers
sanitizers: sanitizers,
library: .xctest
)
args = [path.description, "--dump-tests-json"]
let data = try Process.checkNonZeroExit(arguments: args, environment: env)
Expand All @@ -149,7 +151,8 @@ enum TestingSupport {
static func constructTestEnvironment(
toolchain: UserToolchain,
destinationBuildParameters buildParameters: BuildParameters,
sanitizers: [Sanitizer]
sanitizers: [Sanitizer],
library: BuildParameters.Testing.Library
) throws -> EnvironmentVariables {
var env = EnvironmentVariables.process()

Expand All @@ -161,6 +164,10 @@ enum TestingSupport {
env["NO_COLOR"] = "1"
}

// Set an environment variable to indicate which library's test product
// is being executed.
env["SWIFT_PM_TEST_LIBRARY"] = String(describing: library)

// Add the code coverage related variables.
if buildParameters.testingParameters.enableCodeCoverage {
// Defines the path at which the profraw files will be written on test execution.
Expand Down
6 changes: 6 additions & 0 deletions Tests/CommandsTests/TestCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,10 @@ final class TestCommandTests: CommandsTestCase {
}
}
#endif

func testLibraryEnvironmentVariable() throws {
try fixture(name: "Miscellaneous/CheckTestLibraryEnvironmentVariable") { fixturePath in
XCTAssertNoThrow(try SwiftPM.Test.execute(packagePath: fixturePath))
}
}
}

0 comments on commit c965d5a

Please sign in to comment.