Skip to content

Commit 5b60b0a

Browse files
authored
Don't hard-code numeric representations of ABI versions. (#968)
This PR replaces some hard-coded ABI version numbers (`-1`, `0`, and `1`) with symbolic references. It also removes a common code path between the Xcode 16-compatible C entry point and the formal "v0" one so that deleting the Xcode 16 legacy paths will be easier later. ### 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 eaae48c commit 5b60b0a

File tree

4 files changed

+34
-54
lines changed

4 files changed

+34
-54
lines changed

Sources/Testing/ABI/Encoded/ABI.EncodedTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension ABI {
9898
sourceLocation = test.sourceLocation
9999
id = ID(encoding: test.id)
100100

101-
if V.versionNumber >= 1 {
101+
if V.versionNumber >= ABI.v1.versionNumber {
102102
let tags = test.tags
103103
if !tags.isEmpty {
104104
_tags = tags.map(String.init(describing:))

Sources/Testing/ABI/EntryPoints/ABIEntryPoint.swift

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ extension ABI.v0 {
4747
/// callback.
4848
public static var entryPoint: EntryPoint {
4949
return { configurationJSON, recordHandler in
50-
try await _entryPoint(
51-
configurationJSON: configurationJSON,
52-
recordHandler: recordHandler
53-
) == EXIT_SUCCESS
50+
let args = try configurationJSON.map { configurationJSON in
51+
try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
52+
}
53+
let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, encodeAsJSONLines: false, forwardingTo: recordHandler)
54+
55+
switch await Testing.entryPoint(passing: args, eventHandler: eventHandler) {
56+
case EXIT_SUCCESS, EXIT_NO_TESTS_FOUND:
57+
return true
58+
default:
59+
return false
60+
}
5461
}
5562
}
5663
}
@@ -89,48 +96,21 @@ extension ABI.Xcode16 {
8996
@usableFromInline func copyABIEntryPoint_v0() -> UnsafeMutableRawPointer {
9097
let result = UnsafeMutablePointer<ABI.Xcode16.EntryPoint>.allocate(capacity: 1)
9198
result.initialize { configurationJSON, recordHandler in
92-
try await _entryPoint(
93-
configurationJSON: configurationJSON,
94-
eventStreamVersionIfNil: -1,
95-
recordHandler: recordHandler
96-
)
99+
var args = try configurationJSON.map { configurationJSON in
100+
try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
101+
}
102+
if args?.eventStreamVersion == nil {
103+
args?.eventStreamVersion = ABI.Xcode16.versionNumber
104+
}
105+
let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, encodeAsJSONLines: false, forwardingTo: recordHandler)
106+
107+
var exitCode = await Testing.entryPoint(passing: args, eventHandler: eventHandler)
108+
if exitCode == EXIT_NO_TESTS_FOUND {
109+
exitCode = EXIT_SUCCESS
110+
}
111+
return exitCode
97112
}
98113
return .init(result)
99114
}
100115
#endif
101-
102-
// MARK: -
103-
104-
/// A common implementation for ``ABI/v0/entryPoint-swift.type.property`` and
105-
/// ``copyABIEntryPoint_v0()`` that provides Xcode&nbsp;16 compatibility.
106-
///
107-
/// This function will be removed (with its logic incorporated into
108-
/// ``ABI/v0/entryPoint-swift.type.property``) in a future update.
109-
private func _entryPoint(
110-
configurationJSON: UnsafeRawBufferPointer?,
111-
eventStreamVersionIfNil: Int? = nil,
112-
recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void
113-
) async throws -> CInt {
114-
var args = try configurationJSON.map { configurationJSON in
115-
try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
116-
}
117-
118-
// If the caller needs a nil event stream version to default to a specific
119-
// JSON schema, apply it here as if they'd specified it in the configuration
120-
// JSON blob.
121-
if let eventStreamVersionIfNil, args?.eventStreamVersion == nil {
122-
args?.eventStreamVersion = eventStreamVersionIfNil
123-
}
124-
125-
let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, encodeAsJSONLines: false, forwardingTo: recordHandler)
126-
let exitCode = await entryPoint(passing: args, eventHandler: eventHandler)
127-
128-
// To maintain compatibility with Xcode 16, suppress custom exit codes. (This
129-
// is also needed by ABI.v0.entryPoint to correctly treat the no-tests as a
130-
// successful run.)
131-
if exitCode == EXIT_NO_TESTS_FOUND {
132-
return EXIT_SUCCESS
133-
}
134-
return exitCode
135-
}
136116
#endif

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,14 +632,14 @@ func eventHandlerForStreamingEvents(
632632
case nil:
633633
eventHandler(for: ABI.CurrentVersion.self)
634634
#if !SWT_NO_SNAPSHOT_TYPES
635-
case -1:
635+
case ABI.Xcode16.versionNumber:
636636
// Legacy support for Xcode 16. Support for this undocumented version will
637637
// be removed in a future update. Do not use it.
638638
eventHandler(for: ABI.Xcode16.self)
639639
#endif
640-
case 0:
640+
case ABI.v0.versionNumber:
641641
eventHandler(for: ABI.v0.self)
642-
case 1:
642+
case ABI.v1.versionNumber:
643643
eventHandler(for: ABI.v1.self)
644644
case let .some(unsupportedVersionNumber):
645645
throw _EntryPointError.invalidArgument("--event-stream-version", value: "\(unsupportedVersionNumber)")

Tests/TestingTests/SwiftPMTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ struct SwiftPMTests {
228228

229229
@Test("--event-stream-output-path argument (writes to a stream and can be read back)",
230230
arguments: [
231-
("--event-stream-output-path", "--event-stream-version", 0),
232-
("--experimental-event-stream-output", "--experimental-event-stream-version", 0),
233-
("--experimental-event-stream-output", "--experimental-event-stream-version", 1),
231+
("--event-stream-output-path", "--event-stream-version", ABI.v0.versionNumber),
232+
("--experimental-event-stream-output", "--experimental-event-stream-version", ABI.v0.versionNumber),
233+
("--experimental-event-stream-output", "--experimental-event-stream-version", ABI.v1.versionNumber),
234234
])
235235
func eventStreamOutput(outputArgumentName: String, versionArgumentName: String, version: Int) async throws {
236236
switch version {
237-
case 0:
237+
case ABI.v0.versionNumber:
238238
try await eventStreamOutput(outputArgumentName: outputArgumentName, versionArgumentName: versionArgumentName, version: ABI.v0.self)
239-
case 1:
239+
case ABI.v1.versionNumber:
240240
try await eventStreamOutput(outputArgumentName: outputArgumentName, versionArgumentName: versionArgumentName, version: ABI.v1.self)
241241
default:
242242
Issue.record("Unreachable event stream version \(version)")
@@ -282,7 +282,7 @@ struct SwiftPMTests {
282282
}
283283
#expect(testRecords.count == 1)
284284
for testRecord in testRecords {
285-
if version.versionNumber >= 1 {
285+
if version.versionNumber >= ABI.v1.versionNumber {
286286
#expect(testRecord._tags != nil)
287287
} else {
288288
#expect(testRecord._tags == nil)

0 commit comments

Comments
 (0)