Skip to content

Commit

Permalink
Fix filtering of swift-testing tests.
Browse files Browse the repository at this point in the history
* Remove the module name from the test identifier used for filtering.
* If no tests matched the filter, request explicit skipping of all tests so that the runner doesn't interpret the empty filter set as "run all tests".

PiperOrigin-RevId: 670984261
  • Loading branch information
allevato authored and swiple-rules-gardener committed Sep 4, 2024
1 parent b032fd7 commit 7c149cf
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tools/test_observer/SwiftTestingRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public final class SwiftTestingRunner: Sendable {
private struct Test: Testable {
/// The identifier of the test.
let testIdentifier: String

init(testIdentifier: String) {
// The test identifier given by swift-testing starts with the name of the module, which we
// strip off because it isn't terribly useful for filtering -- it's burdensome for users to
// type and IDEs cannot easily determine it without querying the build system.
let components = testIdentifier.split(separator: ".", maxSplits: 1)
if components.count > 1 {
self.testIdentifier = String(components[1])
} else {
self.testIdentifier = testIdentifier
}
}
}

/// A test or suite discovered by the swift-testing framework.
Expand Down Expand Up @@ -134,10 +146,17 @@ public final class SwiftTestingRunner: Sendable {
private func runTests(selectedTests: [Test]?) async throws {
var runTestsConfiguration: [String: JSON] = [:]
if let selectedTests {
runTestsConfiguration["filter"] = .array(
selectedTests.map {
JSON.string(NSRegularExpression.escapedPattern(for: $0.testIdentifier))
})
// If we applied a test filter and no tests were selected, setting the `filter` configuration
// value to an empty array will be treated by the runner as if there were no filter (and thus
// run *all* tests). To handle this correctly, we ask the runner to explicitly skip all tests.
if selectedTests.isEmpty {
runTestsConfiguration["skip"] = [".*"]
} else {
runTestsConfiguration["filter"] = .array(
selectedTests.map {
JSON.string(NSRegularExpression.escapedPattern(for: $0.testIdentifier))
})
}
}
for try await recordJSON in try await entryPoint(configuration: .object(runTestsConfiguration))
{
Expand Down

1 comment on commit 7c149cf

@brentleyjones
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please sign in to comment.