From 7c149cf490e8e9ad587d751a54a9dbadd379f5b5 Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Wed, 4 Sep 2024 08:42:38 -0700 Subject: [PATCH] Fix filtering of swift-testing tests. * 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 --- tools/test_observer/SwiftTestingRunner.swift | 27 +++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tools/test_observer/SwiftTestingRunner.swift b/tools/test_observer/SwiftTestingRunner.swift index 04d69c306..15334b96f 100644 --- a/tools/test_observer/SwiftTestingRunner.swift +++ b/tools/test_observer/SwiftTestingRunner.swift @@ -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. @@ -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)) {