From 695293dc00ce5f2b35cb8b1d0a067ff16796167a Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Sat, 18 Nov 2023 11:49:11 -0500 Subject: [PATCH] =?UTF-8?q?`swift=20test`=20should=20warn=20when=20`--filt?= =?UTF-8?q?er`=20excludes=20all=20tests=20(=5F=C3=A0=20la=5F=20XCTest.)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using XCTest and `swift test --filter`, if all tests are filtered out a diagnostic of the form: > warning: No matching test cases were run Is emitted by SwiftPM. We should have a similar warning we emit from swift-testing in this scenario. --- Sources/Testing/Running/EntryPoint.swift | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Sources/Testing/Running/EntryPoint.swift b/Sources/Testing/Running/EntryPoint.swift index 54007d89d..8f773fc94 100644 --- a/Sources/Testing/Running/EntryPoint.swift +++ b/Sources/Testing/Running/EntryPoint.swift @@ -208,11 +208,13 @@ func configurationForSwiftPMEntryPoint(withArguments args: [String]) throws -> C /// - Parameters: /// - configuration: The configuration to use for running. func runTests(configuration: Configuration) async { - let eventRecorder = Event.ConsoleOutputRecorder(options: .forStandardError) { string in + let writeOptions: [Event.ConsoleOutputRecorder.Option] = .forStandardError + @Sendable func write(_ string: String) { let stderr = swt_stderr() fputs(string, stderr) fflush(stderr) } + let eventRecorder = Event.ConsoleOutputRecorder(options: writeOptions, writingUsing: write) var configuration = configuration let oldEventHandler = configuration.eventHandler @@ -222,7 +224,24 @@ func runTests(configuration: Configuration) async { } let runner = await Runner(configuration: configuration) - await runner.run() + + // If there are no steps to run, the plan is empty and we should report that + // instead of trying to run nothing. An empty plan can be produced either if + // there are no tests in the current process or if `configuration.testFilter` + // excludes all tests. + let isPlanEmpty = runner.plan.steps.lazy + .map(\.action) + .filter { action in + if case .run = action { + return true + } + return false + }.isEmpty + if isPlanEmpty { + write(warning("No matching tests were run.", options: writeOptions)) + } else { + await runner.run() + } } // MARK: - Command-line interface options