Skip to content

Set an environment variable in swift test to indicate which testing library is in use. #7573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
}