Skip to content

Commit 81bbb32

Browse files
authored
Xcode 16 beta 5: Fix snapshots test trait (#885)
It is no longer possible to `@_spi(Experimental) import Testing`, so we can no longer ship a custom execution test trait. Instead of using such a trait to override a task local for the duration of a test, we can use a more general trait to store the information, and the assertion helpers can coalesce to the trait, instead.
1 parent e883fc9 commit 81bbb32

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import XCTest
22

3+
#if canImport(Testing)
4+
import Testing
5+
#endif
6+
37
/// Enhances failure messages with a command line diff tool expression that can be copied and pasted
48
/// into a terminal.
59
@available(
@@ -9,12 +13,33 @@ import XCTest
913
"Use 'withSnapshotTesting' to customize the diff tool. See the documentation for more information."
1014
)
1115
public var diffTool: SnapshotTestingConfiguration.DiffTool {
12-
get { _diffTool }
16+
get {
17+
_diffTool
18+
}
1319
set { _diffTool = newValue }
1420
}
1521

1622
@_spi(Internals)
17-
public var _diffTool: SnapshotTestingConfiguration.DiffTool = .default
23+
public var _diffTool: SnapshotTestingConfiguration.DiffTool {
24+
get {
25+
#if canImport(Testing)
26+
if let test = Test.current {
27+
for trait in test.traits.reversed() {
28+
if let diffTool = (trait as? _SnapshotsTestTrait)?.configuration.diffTool {
29+
return diffTool
30+
}
31+
}
32+
}
33+
#endif
34+
return __diffTool
35+
}
36+
set {
37+
__diffTool = newValue
38+
}
39+
}
40+
41+
@_spi(Internals)
42+
public var __diffTool: SnapshotTestingConfiguration.DiffTool = .default
1843

1944
/// Whether or not to record all new references.
2045
@available(
@@ -28,7 +53,26 @@ public var isRecording: Bool {
2853
}
2954

3055
@_spi(Internals)
31-
public var _record: SnapshotTestingConfiguration.Record = {
56+
public var _record: SnapshotTestingConfiguration.Record {
57+
get {
58+
#if canImport(Testing)
59+
if let test = Test.current {
60+
for trait in test.traits.reversed() {
61+
if let record = (trait as? _SnapshotsTestTrait)?.configuration.record {
62+
return record
63+
}
64+
}
65+
}
66+
#endif
67+
return __record
68+
}
69+
set {
70+
__record = newValue
71+
}
72+
}
73+
74+
@_spi(Internals)
75+
public var __record: SnapshotTestingConfiguration.Record = {
3276
if let value = ProcessInfo.processInfo.environment["SNAPSHOT_TESTING_RECORD"],
3377
let record = SnapshotTestingConfiguration.Record(rawValue: value)
3478
{

Sources/SnapshotTesting/SnapshotsTestTrait.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#if canImport(Testing)
2-
@_spi(Experimental) import Testing
2+
import Testing
33

44
@_spi(Experimental)
55
extension Trait where Self == _SnapshotsTestTrait {
@@ -32,21 +32,21 @@
3232

3333
/// A type representing the configuration of snapshot testing.
3434
@_spi(Experimental)
35-
public struct _SnapshotsTestTrait: CustomExecutionTrait, SuiteTrait, TestTrait {
35+
public struct _SnapshotsTestTrait: SuiteTrait, TestTrait {
3636
public let isRecursive = true
3737
let configuration: SnapshotTestingConfiguration
3838

39-
public func execute(
40-
_ function: @escaping () async throws -> Void,
41-
for test: Test,
42-
testCase: Test.Case?
43-
) async throws {
44-
try await withSnapshotTesting(
45-
record: configuration.record,
46-
diffTool: configuration.diffTool
47-
) {
48-
try await function()
49-
}
50-
}
39+
// public func execute(
40+
// _ function: @escaping () async throws -> Void,
41+
// for test: Test,
42+
// testCase: Test.Case?
43+
// ) async throws {
44+
// try await withSnapshotTesting(
45+
// record: configuration.record,
46+
// diffTool: configuration.diffTool
47+
// ) {
48+
// try await function()
49+
// }
50+
// }
5151
}
5252
#endif

Tests/SnapshotTestingTests/SnapshotsTraitTests.swift

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#if compiler(>=6) && canImport(Testing)
2-
@_spi(Experimental) import Testing
2+
import Testing
33
@_spi(Experimental) @_spi(Internals) import SnapshotTesting
44

55
struct SnapshotsTraitTests {
66
@Test(.snapshots(diffTool: "ksdiff"))
77
func testDiffTool() {
88
#expect(
9-
SnapshotTestingConfiguration.current?
10-
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
9+
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
1110
== "ksdiff old.png new.png"
1211
)
1312
}
@@ -17,8 +16,7 @@
1716
@Test(.snapshots(diffTool: "difftool"))
1817
func testDiffToolOverride() {
1918
#expect(
20-
SnapshotTestingConfiguration.current?
21-
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
19+
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
2220
== "difftool old.png new.png"
2321
)
2422
}
@@ -28,23 +26,21 @@
2826
@Test
2927
func config() {
3028
#expect(
31-
SnapshotTestingConfiguration.current?
32-
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
29+
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
3330
== "ksdiff old.png new.png"
3431
)
35-
#expect(SnapshotTestingConfiguration.current?.record == .all)
32+
#expect(_record == .all)
3633
}
3734

3835
@Suite(.snapshots(record: .failed, diffTool: "diff"))
3936
struct OverrideDiffToolAndRecord {
4037
@Test
4138
func config() {
4239
#expect(
43-
SnapshotTestingConfiguration.current?
44-
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
40+
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
4541
== "diff old.png new.png"
4642
)
47-
#expect(SnapshotTestingConfiguration.current?.record == .failed)
43+
#expect(_record == .failed)
4844
}
4945
}
5046
}

0 commit comments

Comments
 (0)