Skip to content

Add an experimental CLI argument for passing a configuration value as JSON. #402

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 2 commits into from
May 7, 2024
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
57 changes: 40 additions & 17 deletions Sources/Testing/EntryPoints/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,46 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
// Do not consider the executable path AKA argv[0].
let args = args.dropFirst()

func isLastArgument(at index: [String].Index) -> Bool {
args.index(after: index) >= args.endIndex
}

#if !SWT_NO_FILE_IO
#if canImport(Foundation)
// Configuration for the test run passed in as a JSON file (experimental)
//
// This argument should always be the first one we parse.
//
// NOTE: While the output event stream is opened later, it is necessary to
// open the configuration file early (here) in order to correctly construct
// the resulting __CommandLineArguments_v0 instance.
if let configurationIndex = args.firstIndex(of: "--experimental-configuration-path"), !isLastArgument(at: configurationIndex) {
let path = args[args.index(after: configurationIndex)]
let file = try FileHandle(forReadingAtPath: path)
let configurationJSON = try file.readToEnd()
result = try configurationJSON.withUnsafeBufferPointer { configurationJSON in
try JSON.decode(__CommandLineArguments_v0.self, from: .init(configurationJSON))
}

// NOTE: We don't return early or block other arguments here: a caller is
// allowed to pass a configuration AND --verbose and they'll both be
// respected (it should be the least "surprising" outcome of passing both.)
}

// Event stream output (experimental)
if let eventOutputIndex = args.firstIndex(of: "--experimental-event-stream-output"), !isLastArgument(at: eventOutputIndex) {
result.experimentalEventStreamOutput = args[args.index(after: eventOutputIndex)]
}
#endif

// XML output
if let xunitOutputIndex = args.firstIndex(of: "--xunit-output"), !isLastArgument(at: xunitOutputIndex) {
result.xunitOutput = args[args.index(after: xunitOutputIndex)]
}
#endif

if args.contains("--list-tests") {
result.listTests = true
return result // do not bother parsing the other arguments
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This early return prevented saying --list-tests --verbose for example. Ditch it while I'm here.

}

// Parallelization (on by default)
Expand All @@ -206,20 +243,6 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
result.verbose = true
}

#if !SWT_NO_FILE_IO
// XML output
if let xunitOutputIndex = args.firstIndex(of: "--xunit-output"), xunitOutputIndex < args.endIndex {
result.xunitOutput = args[args.index(after: xunitOutputIndex)]
}

#if canImport(Foundation)
// Event stream output (experimental)
if let eventOutputIndex = args.firstIndex(of: "--experimental-event-stream-output"), eventOutputIndex < args.endIndex {
result.experimentalEventStreamOutput = args[args.index(after: eventOutputIndex)]
}
#endif
#endif

// Filtering
func filterValues(forArgumentsWithLabel label: String) -> [String] {
args.indices.lazy
Expand All @@ -230,10 +253,10 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
result.skip = filterValues(forArgumentsWithLabel: "--skip")

// Set up the iteration policy for the test run.
if let repetitionsIndex = args.firstIndex(of: "--repetitions"), repetitionsIndex < args.endIndex {
if let repetitionsIndex = args.firstIndex(of: "--repetitions"), !isLastArgument(at: repetitionsIndex) {
result.repetitions = Int(args[args.index(after: repetitionsIndex)])
}
if let repeatUntilIndex = args.firstIndex(of: "--repeat-until"), repeatUntilIndex < args.endIndex {
if let repeatUntilIndex = args.firstIndex(of: "--repeat-until"), !isLastArgument(at: repeatUntilIndex) {
result.repeatUntil = args[args.index(after: repeatUntilIndex)]
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/TestingTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ struct SwiftPMTests {
}
}

@Test("--xunit-output argument (missing path)")
func xunitOutputWithMissingPath() throws {
// Test that a missing path doesn't read off the end of the argument array.
let args = try parseCommandLineArguments(from: ["PATH", "--xunit-output"])
#expect(args.xunitOutput == nil)
}

@Test("--xunit-output argument (writes to file)")
func xunitOutputIsWrittenToFile() throws {
// Test that a file is opened when requested. Testing of the actual output
Expand Down