Skip to content

Validate format of event stream version CLI argument when passed #1231

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
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
22 changes: 15 additions & 7 deletions Sources/Testing/ABI/EntryPoints/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,16 @@ public struct __CommandLineArguments_v0: Sendable {
/// whichever occurs first.
public var eventStreamOutputPath: String?

/// The version of the event stream schema to use when writing events to
/// ``eventStreamOutput``.
/// The value of the `--event-stream-version` or `--experimental-event-stream-version`
/// argument, representing the version of the event stream schema to use when
/// writing events to ``eventStreamOutput``.
///
/// The corresponding stable schema is used to encode events to the event
/// stream. ``ABI/Record`` is used if the value of this property is `0` or
/// higher.
///
/// If the value of this property is `nil`, the testing library assumes that
/// the newest available schema should be used.
/// the current supported (non-experimental) version should be used.
public var eventStreamVersion: Int?

/// The value(s) of the `--filter` argument.
Expand Down Expand Up @@ -376,16 +377,23 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
}
}
if let eventOutputVersionIndex, !isLastArgument(at: eventOutputVersionIndex) {
result.eventStreamVersion = Int(args[args.index(after: eventOutputVersionIndex)])
let versionString = args[args.index(after: eventOutputVersionIndex)]

// If the caller specified a version that could not be parsed, treat it as
// an invalid argument.
guard let eventStreamVersion = Int(versionString) else {
let argument = allowExperimental ? "--experimental-event-stream-version" : "--event-stream-version"
throw _EntryPointError.invalidArgument(argument, value: versionString)
}

// If the caller specified an experimental ABI version, they must
// explicitly use --experimental-event-stream-version, otherwise it's
// treated as unsupported.
if let eventStreamVersion = result.eventStreamVersion,
eventStreamVersion > ABI.CurrentVersion.versionNumber,
!allowExperimental {
if eventStreamVersion > ABI.CurrentVersion.versionNumber, !allowExperimental {
throw _EntryPointError.experimentalABIVersion(eventStreamVersion)
}

result.eventStreamVersion = eventStreamVersion
}
}
#endif
Expand Down
7 changes: 7 additions & 0 deletions Tests/TestingTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ struct SwiftPMTests {
_ = try configurationForEntryPoint(withArguments: ["PATH", "--event-stream-version", "\(experimentalVersion)"])
}
}

@Test("Invalid event stream version throws an invalid argument error")
func invalidEventStreamVersionThrows() {
#expect(throws: (any Error).self) {
_ = try configurationForEntryPoint(withArguments: ["PATH", "--event-stream-version", "xyz-invalid"])
}
}
#endif
#endif

Expand Down