Skip to content

Commit ac4f8da

Browse files
authored
Validate format of event stream version CLI argument when passed (#1231)
This adds validation of the user-specified `--event-stream-version` (or `--experimental-event-stream-version`) command-line arguments when they are passed, to ensure they have a valid format. ### Motivation: Currently, these flags only accept integers like `0`, `1`, etc. But I am moving towards making the entry point and event stream versions align with the Swift version, rather than having an independent versioning scheme which users must look up or keep track of. Once this effort is finished, in order to use the event stream format included in (for example) Swift 6.3, a user would pass `--event-stream-version 6.3`. In swiftlang/swift-package-manager#8944, I recently landed a SwiftPM change which will permit any arbitrary string for these arguments, and this means we need to begin validating the format of supported versions within the testing library. In a subsequent PR, I plan to introduce support for non-integer versions, at which point having some existing validation logic will be even more valuable. ### 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 824f6a4 commit ac4f8da

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,16 @@ public struct __CommandLineArguments_v0: Sendable {
255255
/// whichever occurs first.
256256
public var eventStreamOutputPath: String?
257257

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

269270
/// The value(s) of the `--filter` argument.
@@ -376,16 +377,23 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
376377
}
377378
}
378379
if let eventOutputVersionIndex, !isLastArgument(at: eventOutputVersionIndex) {
379-
result.eventStreamVersion = Int(args[args.index(after: eventOutputVersionIndex)])
380+
let versionString = args[args.index(after: eventOutputVersionIndex)]
381+
382+
// If the caller specified a version that could not be parsed, treat it as
383+
// an invalid argument.
384+
guard let eventStreamVersion = Int(versionString) else {
385+
let argument = allowExperimental ? "--experimental-event-stream-version" : "--event-stream-version"
386+
throw _EntryPointError.invalidArgument(argument, value: versionString)
387+
}
380388

381389
// If the caller specified an experimental ABI version, they must
382390
// explicitly use --experimental-event-stream-version, otherwise it's
383391
// treated as unsupported.
384-
if let eventStreamVersion = result.eventStreamVersion,
385-
eventStreamVersion > ABI.CurrentVersion.versionNumber,
386-
!allowExperimental {
392+
if eventStreamVersion > ABI.CurrentVersion.versionNumber, !allowExperimental {
387393
throw _EntryPointError.experimentalABIVersion(eventStreamVersion)
388394
}
395+
396+
result.eventStreamVersion = eventStreamVersion
389397
}
390398
}
391399
#endif

Tests/TestingTests/SwiftPMTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ struct SwiftPMTests {
308308
_ = try configurationForEntryPoint(withArguments: ["PATH", "--event-stream-version", "\(experimentalVersion)"])
309309
}
310310
}
311+
312+
@Test("Invalid event stream version throws an invalid argument error")
313+
func invalidEventStreamVersionThrows() {
314+
#expect(throws: (any Error).self) {
315+
_ = try configurationForEntryPoint(withArguments: ["PATH", "--event-stream-version", "xyz-invalid"])
316+
}
317+
}
311318
#endif
312319
#endif
313320

0 commit comments

Comments
 (0)