Skip to content

Don't hard-code numeric representations of ABI versions. #968

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 1 commit into from
Feb 20, 2025
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
2 changes: 1 addition & 1 deletion Sources/Testing/ABI/Encoded/ABI.EncodedTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ extension ABI {
sourceLocation = test.sourceLocation
id = ID(encoding: test.id)

if V.versionNumber >= 1 {
if V.versionNumber >= ABI.v1.versionNumber {
let tags = test.tags
if !tags.isEmpty {
_tags = tags.map(String.init(describing:))
Expand Down
68 changes: 24 additions & 44 deletions Sources/Testing/ABI/EntryPoints/ABIEntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ extension ABI.v0 {
/// callback.
public static var entryPoint: EntryPoint {
return { configurationJSON, recordHandler in
try await _entryPoint(
configurationJSON: configurationJSON,
recordHandler: recordHandler
) == EXIT_SUCCESS
let args = try configurationJSON.map { configurationJSON in
try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
}
let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, encodeAsJSONLines: false, forwardingTo: recordHandler)

switch await Testing.entryPoint(passing: args, eventHandler: eventHandler) {
case EXIT_SUCCESS, EXIT_NO_TESTS_FOUND:
return true
default:
return false
}
}
}
}
Expand Down Expand Up @@ -89,48 +96,21 @@ extension ABI.Xcode16 {
@usableFromInline func copyABIEntryPoint_v0() -> UnsafeMutableRawPointer {
let result = UnsafeMutablePointer<ABI.Xcode16.EntryPoint>.allocate(capacity: 1)
result.initialize { configurationJSON, recordHandler in
try await _entryPoint(
configurationJSON: configurationJSON,
eventStreamVersionIfNil: -1,
recordHandler: recordHandler
)
var args = try configurationJSON.map { configurationJSON in
try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
}
if args?.eventStreamVersion == nil {
args?.eventStreamVersion = ABI.Xcode16.versionNumber
}
let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, encodeAsJSONLines: false, forwardingTo: recordHandler)

var exitCode = await Testing.entryPoint(passing: args, eventHandler: eventHandler)
if exitCode == EXIT_NO_TESTS_FOUND {
exitCode = EXIT_SUCCESS
}
return exitCode
}
return .init(result)
}
#endif

// MARK: -

/// A common implementation for ``ABI/v0/entryPoint-swift.type.property`` and
/// ``copyABIEntryPoint_v0()`` that provides Xcode&nbsp;16 compatibility.
///
/// This function will be removed (with its logic incorporated into
/// ``ABI/v0/entryPoint-swift.type.property``) in a future update.
private func _entryPoint(
configurationJSON: UnsafeRawBufferPointer?,
eventStreamVersionIfNil: Int? = nil,
recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void
) async throws -> CInt {
var args = try configurationJSON.map { configurationJSON in
try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
}

// If the caller needs a nil event stream version to default to a specific
// JSON schema, apply it here as if they'd specified it in the configuration
// JSON blob.
if let eventStreamVersionIfNil, args?.eventStreamVersion == nil {
args?.eventStreamVersion = eventStreamVersionIfNil
}

let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, encodeAsJSONLines: false, forwardingTo: recordHandler)
let exitCode = await entryPoint(passing: args, eventHandler: eventHandler)

// To maintain compatibility with Xcode 16, suppress custom exit codes. (This
// is also needed by ABI.v0.entryPoint to correctly treat the no-tests as a
// successful run.)
if exitCode == EXIT_NO_TESTS_FOUND {
return EXIT_SUCCESS
}
return exitCode
}
#endif
6 changes: 3 additions & 3 deletions Sources/Testing/ABI/EntryPoints/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,14 @@ func eventHandlerForStreamingEvents(
case nil:
eventHandler(for: ABI.CurrentVersion.self)
#if !SWT_NO_SNAPSHOT_TYPES
case -1:
case ABI.Xcode16.versionNumber:
// Legacy support for Xcode 16. Support for this undocumented version will
// be removed in a future update. Do not use it.
eventHandler(for: ABI.Xcode16.self)
#endif
case 0:
case ABI.v0.versionNumber:
eventHandler(for: ABI.v0.self)
case 1:
case ABI.v1.versionNumber:
eventHandler(for: ABI.v1.self)
case let .some(unsupportedVersionNumber):
throw _EntryPointError.invalidArgument("--event-stream-version", value: "\(unsupportedVersionNumber)")
Expand Down
12 changes: 6 additions & 6 deletions Tests/TestingTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ struct SwiftPMTests {

@Test("--event-stream-output-path argument (writes to a stream and can be read back)",
arguments: [
("--event-stream-output-path", "--event-stream-version", 0),
("--experimental-event-stream-output", "--experimental-event-stream-version", 0),
("--experimental-event-stream-output", "--experimental-event-stream-version", 1),
("--event-stream-output-path", "--event-stream-version", ABI.v0.versionNumber),
("--experimental-event-stream-output", "--experimental-event-stream-version", ABI.v0.versionNumber),
("--experimental-event-stream-output", "--experimental-event-stream-version", ABI.v1.versionNumber),
])
func eventStreamOutput(outputArgumentName: String, versionArgumentName: String, version: Int) async throws {
switch version {
case 0:
case ABI.v0.versionNumber:
try await eventStreamOutput(outputArgumentName: outputArgumentName, versionArgumentName: versionArgumentName, version: ABI.v0.self)
case 1:
case ABI.v1.versionNumber:
try await eventStreamOutput(outputArgumentName: outputArgumentName, versionArgumentName: versionArgumentName, version: ABI.v1.self)
default:
Issue.record("Unreachable event stream version \(version)")
Expand Down Expand Up @@ -282,7 +282,7 @@ struct SwiftPMTests {
}
#expect(testRecords.count == 1)
for testRecord in testRecords {
if version.versionNumber >= 1 {
if version.versionNumber >= ABI.v1.versionNumber {
#expect(testRecord._tags != nil)
} else {
#expect(testRecord._tags == nil)
Expand Down