Skip to content

[SwiftTestTool] Allow skipping SwiftPM tests using an env variable #2181

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 1 commit into from
Jun 22, 2019
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
8 changes: 8 additions & 0 deletions Documentation/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,11 @@ installed. This path can be overridden by setting the environment variable
absolute search paths separated by colon (":"). SwiftPM will choose the first
path which exists on disk. If none of the path are present on disk, it will fall
back to built-in computation.

## Skip SwiftPM tests

SwiftPM has a hidden env variable `_SWIFTPM_SKIP_TESTS_LIST` that can be used
to skip a list of tests. This value of the variable is either a file path that contains a
new-line separated list of tests to skip or a colon separated list of tests.

This is only a development feature and should be considered _unsupported_.
45 changes: 41 additions & 4 deletions Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,38 @@ public class TestToolOptions: ToolOptions {
/// Generate LinuxMain entries and exit.
var shouldGenerateLinuxMain = false

var testCaseSpecifier: TestCaseSpecifier = .none
var testCaseSpecifier: TestCaseSpecifier {
testCaseSpecifierOverride() ?? _testCaseSpecifier
}

var _testCaseSpecifier: TestCaseSpecifier = .none

/// Path where the xUnit xml file should be generated.
var xUnitOutput: AbsolutePath?

/// Returns the test case specifier if overridden in the env.
private func testCaseSpecifierOverride() -> TestCaseSpecifier? {
guard let override = ProcessEnv.vars["_SWIFTPM_SKIP_TESTS_LIST"] else {
return nil
}

do {
let skipTests: [String.SubSequence]
// Read from the file if it exists.
if let path = try? AbsolutePath(validating: override), localFileSystem.exists(path) {
let contents = try localFileSystem.readFileContents(path).cString
skipTests = contents.split(separator: "\n", omittingEmptySubsequences: true)
} else {
// Otherwise, read the env variable.
skipTests = override.split(separator: ":", omittingEmptySubsequences: true)
}

return .skip(skipTests.map(String.init))
} catch {
// FIXME: We should surface errors from here.
}
return nil
}
}

/// Tests filtering specifier
Expand All @@ -152,6 +180,7 @@ public enum TestCaseSpecifier {
case none
case specific(String)
case regex(String)
case skip([String])
}

public enum TestMode {
Expand Down Expand Up @@ -234,7 +263,7 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
)
ranSuccessfully = runner.test()

case .regex, .specific:
case .regex, .specific, .skip:
// If old specifier `-s` option was used, emit deprecation notice.
if case .specific = options.testCaseSpecifier {
diagnostics.emit(data: SpecifierDeprecatedDiagnostic())
Expand Down Expand Up @@ -419,7 +448,7 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {

binder.bind(
option: parser.add(option: "--specifier", shortName: "-s", kind: String.self),
to: { $0.testCaseSpecifier = .specific($1) })
to: { $0._testCaseSpecifier = .specific($1) })

binder.bind(
option: parser.add(option: "--xunit-output", kind: PathArgument.self),
Expand All @@ -429,7 +458,7 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
option: parser.add(option: "--filter", kind: String.self,
usage: "Run test cases matching regular expression, Format: <test-target>.<test-case> or " +
"<test-target>.<test-case>/<test>"),
to: { $0.testCaseSpecifier = .regex($1) })
to: { $0._testCaseSpecifier = .regex($1) })

binder.bind(
option: parser.add(option: "--enable-code-coverage", kind: Bool.self,
Expand Down Expand Up @@ -926,6 +955,14 @@ fileprivate extension Sequence where Iterator.Element == TestSuite {
})
case .specific(let name):
return allTests.filter{ $0.specifier == name }
case .skip(let skippedTests):
var result = allTests
for skippedTest in skippedTests {
result = result.filter{
$0.specifier.range(of: skippedTest, options: .regularExpression) == nil
}
}
return result
}
}
}
Expand Down