Skip to content

Commit e4170a8

Browse files
authored
Add support for swift test list --verbose. (#589)
When `--verbose` is passed to `swift test list`, it is intended that we handle it by emitting all test IDs including source location info, not just ambiguous test IDs. This change will be useful to the Swift VSCode plugin in ensuring that tests are properly identified. This change also allows you to pass `list` directly to the test executable instead of `--list-tests`, which simplifies running tests in Wasm. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 79e3928 commit e4170a8

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func entryPoint(passing args: __CommandLineArguments_v0?, eventHandler: Event.Ha
7878
tests = await Array(Test.all)
7979

8080
if args.verbosity > .min {
81-
for testID in listTestsForEntryPoint(tests) {
81+
for testID in listTestsForEntryPoint(tests, verbosity: args.verbosity) {
8282
// Print the test ID to stdout (classical CLI behavior.)
8383
#if SWT_TARGET_OS_APPLE && !SWT_NO_FILE_IO
8484
try? FileHandle.stdout.write("\(testID)\n")
@@ -130,16 +130,26 @@ func entryPoint(passing args: __CommandLineArguments_v0?, eventHandler: Event.Ha
130130
///
131131
/// - Parameters:
132132
/// - tests: The tests to list.
133+
/// - verbosity: The verbosity level. A level higher than `0` forces the
134+
/// inclusion of source locations for all tests.
133135
///
134136
/// - Returns: An array of strings representing the IDs of `tests`.
135-
func listTestsForEntryPoint(_ tests: some Sequence<Test>) -> [String] {
137+
func listTestsForEntryPoint(_ tests: some Sequence<Test>, verbosity: Int) -> [String] {
136138
// Filter out hidden tests and test suites. Hidden tests should not generally
137139
// be presented to the user, and suites (XCTestCase classes) are not included
138140
// in the equivalent XCTest-based output.
139141
let tests = tests.lazy
140142
.filter { !$0.isSuite }
141143
.filter { !$0.isHidden }
142144

145+
// Early exit for verbose output (no need to check for ambiguity.)
146+
if verbosity > 0 {
147+
return tests.lazy
148+
.map(\.id)
149+
.map(String.init(describing:))
150+
.sorted(by: <)
151+
}
152+
143153
// Group tests by the name components of the tests' IDs. If the name
144154
// components of two tests' IDs are ambiguous, present their source locations
145155
// to disambiguate.
@@ -345,6 +355,10 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
345355

346356
if args.contains("--list-tests") {
347357
result.listTests = true
358+
} else if args.first == "list" {
359+
// Allow the "list" subcommand explicitly in place of "--list-tests". This
360+
// makes invocation from e.g. Wasmtime a bit more intuitive/idiomatic.
361+
result.listTests = true
348362
}
349363

350364
// Parallelization (on by default)

Tests/TestingTests/SwiftPMTests.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,25 @@ struct SwiftPMTests {
309309

310310
@Test("list subcommand")
311311
func list() async throws {
312-
let testIDs = await listTestsForEntryPoint(Test.all)
313-
let currentTestID = try #require(
314-
Test.current
315-
.flatMap(\.id.parent)
316-
.map(String.init(describing:))
317-
)
312+
do {
313+
let args = try parseCommandLineArguments(from: ["PATH", "--list-tests"])
314+
#expect(args.listTests == true)
315+
}
316+
do {
317+
let args = try parseCommandLineArguments(from: ["PATH", "list"])
318+
#expect(args.listTests == true)
319+
}
320+
let testIDs = await listTestsForEntryPoint(Test.all, verbosity: 0)
321+
let currentTestID = String(describing: try #require(Test.current?.id.parent))
322+
#expect(testIDs.contains(currentTestID))
323+
}
324+
325+
@Test("list --verbose subcommand")
326+
func listVerbose() async throws {
327+
let testIDs = await listTestsForEntryPoint(Test.all, verbosity: 1)
328+
let currentTestID = String(describing: try #require(Test.current?.id))
318329
#expect(testIDs.contains(currentTestID))
330+
#expect(testIDs.allSatisfy { $0.contains(".swift:") })
319331
}
320332

321333
@Test(

0 commit comments

Comments
 (0)