Skip to content

[6.0] Set an environment variable in swift test to indicate which testing library is in use. #7577

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
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 @@ -422,7 +422,8 @@ public struct SwiftTestCommand: AsyncSwiftCommand {
let testEnv = try TestingSupport.constructTestEnvironment(
toolchain: toolchain,
buildParameters: buildParameters,
sanitizers: globalOptions.build.sanitizers
sanitizers: globalOptions.build.sanitizers,
library: library
)

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

let additionalArguments = ["--list-tests"] + CommandLine.arguments.dropFirst()
Expand Down Expand Up @@ -1007,7 +1009,8 @@ final class ParallelTestRunner {
let testEnv = try TestingSupport.constructTestEnvironment(
toolchain: self.toolchain,
buildParameters: self.buildParameters,
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,
buildParameters: 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 @@ -116,7 +116,8 @@ enum TestingSupport {
experimentalTestOutput: experimentalTestOutput,
library: .xctest
),
sanitizers: sanitizers
sanitizers: sanitizers,
library: .xctest
)

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

Expand All @@ -156,6 +159,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)
Copy link
Contributor

@jakepetroules jakepetroules May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is convenient for shared libraries which want to have support code that might be run by either an XCTest or SwiftTesting test case.

However, I suggest we name this more generally (for example, APPLE_TEST_LIBRARY) because it's not SwifPM-specific, and other build tools / IDEs might want to have a uniform way to identify the Apple-provided testing library in use.

I also expect we wouldn't this environment variable if SwiftPM gained direct support for additional testing libraries from other platforms/environments, since those would likely be run via their own test harnesses which might define their own environment variables.

#7584


// 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))
}
}
}