Skip to content

Commit 887871b

Browse files
authored
Refactor AdvancedConsoleOutputRecorder to use ABI.EncodedEvent architecture (#1281)
### Key changes implemented: * Event → ABI.EncodedEvent conversion: Using ABI.EncodedEvent<ABI.HighestVersion>(encoding: event, in: eventContext, messages: messages) for forward compatibility * Extensible context storage system: Added Locked<Context> struct containing test storage dictionary, designed for easy addition of result data and other event information in future PRs * Test discovery handling: Populates context storage during testDiscovered events for later lookup by test ID string ABI event processing: Added _processABIEvent() method with placeholder switch statement ready for failure summary implementation * Still delegates to fallback recorder to maintain existing functionality while demonstrating ABI conversion flow * Uses Event.HumanReadableOutputRecorder to generate messages that are passed to ABI encoding ### 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 bf030fd commit 887871b

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ func entryPoint(passing args: __CommandLineArguments_v0?, eventHandler: Event.Ha
5757
// Check for experimental console output flag
5858
if Environment.flag(named: "SWT_ENABLE_EXPERIMENTAL_CONSOLE_OUTPUT") == true {
5959
// Use experimental AdvancedConsoleOutputRecorder
60-
var advancedOptions = Event.AdvancedConsoleOutputRecorder.Options()
60+
var advancedOptions = Event.AdvancedConsoleOutputRecorder<ABI.HighestVersion>.Options()
6161
advancedOptions.base = .for(.stderr)
6262

63-
let eventRecorder = Event.AdvancedConsoleOutputRecorder(options: advancedOptions) { string in
63+
let eventRecorder = Event.AdvancedConsoleOutputRecorder<ABI.HighestVersion>(options: advancedOptions) { string in
6464
try? FileHandle.stderr.write(string)
6565
}
6666

Sources/Testing/Events/Recorder/Event.AdvancedConsoleOutputRecorder.swift

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension Event {
1414
///
1515
/// This recorder is currently experimental and must be enabled via the
1616
/// `SWT_ENABLE_EXPERIMENTAL_CONSOLE_OUTPUT` environment variable.
17-
struct AdvancedConsoleOutputRecorder: Sendable {
17+
struct AdvancedConsoleOutputRecorder<V: ABI.Version>: Sendable {
1818
/// Configuration options for the advanced console output recorder.
1919
struct Options: Sendable {
2020
/// Base console output recorder options to inherit from.
@@ -25,6 +25,15 @@ extension Event {
2525
}
2626
}
2727

28+
/// Context for storing data across events during test execution.
29+
private struct _Context: Sendable {
30+
/// Storage for test information, keyed by test ID string value.
31+
/// This is needed because ABI.EncodedEvent doesn't contain full test context.
32+
var testStorage: [String: ABI.EncodedTest<V>] = [:]
33+
34+
// Future storage for result data and other event information can be added here
35+
}
36+
2837
/// The options for this recorder.
2938
let options: Options
3039

@@ -34,6 +43,12 @@ extension Event {
3443
/// The fallback console recorder for standard output.
3544
private let _fallbackRecorder: Event.ConsoleOutputRecorder
3645

46+
/// Context storage for test information and results.
47+
private let _context: Locked<_Context>
48+
49+
/// Human-readable output recorder for generating messages.
50+
private let _humanReadableRecorder: Event.HumanReadableOutputRecorder
51+
3752
/// Initialize the advanced console output recorder.
3853
///
3954
/// - Parameters:
@@ -43,21 +58,77 @@ extension Event {
4358
self.options = options
4459
self.write = write
4560
self._fallbackRecorder = Event.ConsoleOutputRecorder(options: options.base, writingUsing: write)
61+
self._context = Locked(rawValue: _Context())
62+
self._humanReadableRecorder = Event.HumanReadableOutputRecorder()
4663
}
4764
}
4865
}
4966

5067
extension Event.AdvancedConsoleOutputRecorder {
5168
/// Record an event by processing it and generating appropriate output.
5269
///
53-
/// Currently this is a skeleton implementation that delegates to
54-
/// ``Event/ConsoleOutputRecorder``.
70+
/// This implementation converts the Event to ABI.EncodedEvent for internal processing,
71+
/// following the ABI-based architecture for future separation into a harness process.
5572
///
5673
/// - Parameters:
5774
/// - event: The event to record.
5875
/// - eventContext: The context associated with the event.
5976
func record(_ event: borrowing Event, in eventContext: borrowing Event.Context) {
60-
// Skeleton implementation: delegate to ConsoleOutputRecorder
77+
// Handle test discovery to populate our test storage
78+
if case .testDiscovered = event.kind, let test = eventContext.test {
79+
let encodedTest = ABI.EncodedTest<V>(encoding: test)
80+
_context.withLock { context in
81+
context.testStorage[encodedTest.id.stringValue] = encodedTest
82+
}
83+
}
84+
85+
// Generate human-readable messages for the event
86+
let messages = _humanReadableRecorder.record(event, in: eventContext)
87+
88+
// Convert Event to ABI.EncodedEvent
89+
if let encodedEvent = ABI.EncodedEvent<V>(encoding: event, in: eventContext, messages: messages) {
90+
// Process the ABI event
91+
_processABIEvent(encodedEvent)
92+
}
93+
94+
// For now, still delegate to the fallback recorder to maintain existing functionality
6195
_fallbackRecorder.record(event, in: eventContext)
6296
}
97+
98+
/// Process an ABI.EncodedEvent for advanced console output.
99+
///
100+
/// This is where the enhanced console logic will be implemented in future PRs.
101+
/// Currently this is a placeholder that demonstrates the ABI conversion.
102+
///
103+
/// - Parameters:
104+
/// - encodedEvent: The ABI-encoded event to process.
105+
private func _processABIEvent(_ encodedEvent: ABI.EncodedEvent<V>) {
106+
// TODO: Implement enhanced console output logic here
107+
// This will be expanded in subsequent PRs for:
108+
// - Failure summary display
109+
// - Progress bar functionality
110+
// - Hierarchical test result display
111+
112+
// For now, we just demonstrate that we can access the ABI event data
113+
switch encodedEvent.kind {
114+
case .runStarted:
115+
// Could implement run start logic here
116+
break
117+
case .testStarted:
118+
// Could implement test start logic here
119+
break
120+
case .issueRecorded:
121+
// Could implement issue recording logic here
122+
break
123+
case .testEnded:
124+
// Could implement test end logic here
125+
break
126+
case .runEnded:
127+
// Could implement run end logic here
128+
break
129+
default:
130+
// Handle other event types
131+
break
132+
}
133+
}
63134
}

0 commit comments

Comments
 (0)