Skip to content

Commit 864cf19

Browse files
committed
fix tests
1 parent 886fe38 commit 864cf19

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

Sources/Basics/Observability.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,21 @@ public struct ObservabilityMetadata: Equatable, CustomDebugStringConvertible {
422422
}
423423
}
424424

425+
//@available(*, deprecated, message: "temporary for transition DiagnosticsEngine -> DiagnosticsEmitter")
426+
public func droppingLegacyKeys() -> ObservabilityMetadata? {
427+
var metadata = ObservabilityMetadata()
428+
self.forEach { (key, value) in
429+
if key.keyType == LegacyLocationKey.self {
430+
return
431+
}
432+
if key.keyType == LegacyDataKey.self {
433+
return
434+
}
435+
metadata._storage[key] = value
436+
}
437+
return metadata.isEmpty ? .none : metadata
438+
}
439+
425440
/// A type-erased `ObservabilityMetadataKey` used when iterating through the `ObservabilityMetadata` using its `forEach` method.
426441
public struct AnyKey {
427442
/// The key's type represented erased to an `Any.Type`.
@@ -461,7 +476,9 @@ extension ObservabilityScope {
461476
extension Diagnostic {
462477
init?(_ diagnostic: TSCDiagnostic) {
463478
var metadata = ObservabilityMetadata()
464-
metadata.legacyDiagnosticLocation = .init(diagnostic.location)
479+
if !(diagnostic.location is UnknownLocation) {
480+
metadata.legacyDiagnosticLocation = .init(diagnostic.location)
481+
}
465482
metadata.legacyDiagnosticData = .init(diagnostic.data)
466483

467484
switch diagnostic.behavior {
@@ -537,7 +554,7 @@ extension ObservabilityMetadata {
537554
}
538555
}
539556

540-
fileprivate enum LegacyLocationKey: Key {
557+
private enum LegacyLocationKey: Key {
541558
typealias Value = DiagnosticLocationWrapper
542559
}
543560

@@ -565,7 +582,7 @@ extension ObservabilityMetadata {
565582
}
566583
}
567584

568-
enum LegacyDataKey: Key {
585+
private enum LegacyDataKey: Key {
569586
typealias Value = DiagnosticDataWrapper
570587
}
571588

Sources/SPMTestSupport/Observability.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public class DiagnosticsTestResult {
126126

127127
XCTAssertMatch(diagnostic.message, message, file: file, line: line)
128128
XCTAssertEqual(diagnostic.severity, severity, file: file, line: line)
129-
XCTAssertEqual(diagnostic.metadata, metadata, file: file, line: line)
129+
// FIXME: (diagnostics) compare complete metadata when legacy bridge is removed
130+
//XCTAssertEqual(diagnostic.metadata, metadata, file: file, line: line)
131+
XCTAssertEqual(diagnostic.metadata?.droppingLegacyKeys(), metadata?.droppingLegacyKeys(), file: file, line: line)
130132
}
131133

132134
public func checkUnordered(
@@ -145,9 +147,13 @@ public class DiagnosticsTestResult {
145147
return XCTFail("No diagnostics match \(diagnosticPattern)", file: file, line: line)
146148
} else if matching.count == 1, let diagnostic = matching.first, let index = self.uncheckedDiagnostics.firstIndex(where: { $0 == diagnostic }) {
147149
XCTAssertEqual(diagnostic.severity, severity, file: file, line: line)
148-
XCTAssertEqual(diagnostic.metadata, metadata, file: file, line: line)
150+
// FIXME: (diagnostics) compare complete metadata when legacy bridge is removed
151+
//XCTAssertEqual(diagnostic.metadata, metadata, file: file, line: line)
152+
XCTAssertEqual(diagnostic.metadata?.droppingLegacyKeys(), metadata?.droppingLegacyKeys(), file: file, line: line)
149153
self.uncheckedDiagnostics.remove(at: index)
150-
} else if let index = self.uncheckedDiagnostics.firstIndex(where: { diagnostic in diagnostic.severity == severity && diagnostic.metadata == metadata}) {
154+
// FIXME: (diagnostics) compare complete metadata when legacy bridge is removed
155+
//} else if let index = self.uncheckedDiagnostics.firstIndex(where: { diagnostic in diagnostic.severity == severity && diagnostic.metadata == metadata}) {
156+
} else if let index = self.uncheckedDiagnostics.firstIndex(where: { diagnostic in diagnostic.severity == severity && diagnostic.metadata?.droppingLegacyKeys() == metadata?.droppingLegacyKeys()}) {
151157
self.uncheckedDiagnostics.remove(at: index)
152158
}
153159
}

Tests/BasicsTests/ObservabilitySystemTests.swift

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,50 +159,49 @@ final class ObservabilitySystemTest: XCTestCase {
159159
result.check(diagnostic: "warning", severity: .warning, metadata: diagnosticMergedMetadata)
160160
}
161161
}
162-
162+
163163
@available(*, deprecated, message: "temporary for transition DiagnosticsEngine -> DiagnosticsEmitter")
164164
func testBridging() throws {
165-
166165
do {
167166
let collector = Collector()
168167
let observabilitySystem = ObservabilitySystem(collector)
169168
let diagnosticsEngine = observabilitySystem.topScope.makeDiagnosticsEngine()
170-
169+
171170
let data = TestData()
172171
let location = TestLocation()
173-
172+
174173
diagnosticsEngine.emit(.error(data), location: location)
175-
testDiagnostics(collector.diagnostics) { result in
176-
var expectedMetadata = ObservabilityMetadata()
177-
expectedMetadata.legacyDiagnosticLocation = .init(location)
178-
expectedMetadata.legacyDiagnosticData = .init(data)
179-
result.check(diagnostic: "\(data)", severity: .error, metadata: expectedMetadata)
180-
}
174+
175+
var expectedMetadata = ObservabilityMetadata()
176+
expectedMetadata.legacyDiagnosticLocation = .init(location)
177+
expectedMetadata.legacyDiagnosticData = .init(data)
178+
179+
XCTAssertEqual(collector.diagnostics.count, 1)
180+
XCTAssertEqual(collector.diagnostics.first?.metadata, expectedMetadata)
181181
}
182-
182+
183183
do {
184184
let diagnosticsEngine1 = DiagnosticsEngine()
185185
let observabilitySystem = ObservabilitySystem(diagnosticEngine: diagnosticsEngine1)
186186
let diagnosticsEngine2 = observabilitySystem.topScope.makeDiagnosticsEngine()
187-
187+
188188
let data = TestData()
189189
let location = TestLocation()
190-
190+
191191
diagnosticsEngine2.emit(.error(data), location: location)
192-
193-
192+
194193
XCTAssertEqual(diagnosticsEngine1.diagnostics.count, 1)
195194
XCTAssertEqual(diagnosticsEngine1.diagnostics.first!.message.data as? TestData, data)
196195
XCTAssertEqual(diagnosticsEngine1.diagnostics.first!.location as? TestLocation, location)
197196
}
198-
197+
199198
struct TestData: DiagnosticData, Equatable {
200199
var description: String = UUID().uuidString
201200
}
202-
201+
203202
struct TestLocation: DiagnosticLocation, Equatable {
204203
var description: String = UUID().uuidString
205-
204+
206205
}
207206
}
208207

0 commit comments

Comments
 (0)