Skip to content

Commit

Permalink
Xcode 16 beta 5: Fix snapshots test trait (#885)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephencelis authored Aug 7, 2024
1 parent e883fc9 commit 81bbb32
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
50 changes: 47 additions & 3 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import XCTest

#if canImport(Testing)
import Testing
#endif

/// Enhances failure messages with a command line diff tool expression that can be copied and pasted
/// into a terminal.
@available(
Expand All @@ -9,12 +13,33 @@ import XCTest
"Use 'withSnapshotTesting' to customize the diff tool. See the documentation for more information."
)
public var diffTool: SnapshotTestingConfiguration.DiffTool {
get { _diffTool }
get {
_diffTool
}
set { _diffTool = newValue }
}

@_spi(Internals)
public var _diffTool: SnapshotTestingConfiguration.DiffTool = .default
public var _diffTool: SnapshotTestingConfiguration.DiffTool {
get {
#if canImport(Testing)
if let test = Test.current {
for trait in test.traits.reversed() {
if let diffTool = (trait as? _SnapshotsTestTrait)?.configuration.diffTool {
return diffTool
}
}
}
#endif
return __diffTool
}
set {
__diffTool = newValue
}
}

@_spi(Internals)
public var __diffTool: SnapshotTestingConfiguration.DiffTool = .default

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

@_spi(Internals)
public var _record: SnapshotTestingConfiguration.Record = {
public var _record: SnapshotTestingConfiguration.Record {
get {
#if canImport(Testing)
if let test = Test.current {
for trait in test.traits.reversed() {
if let record = (trait as? _SnapshotsTestTrait)?.configuration.record {
return record
}
}
}
#endif
return __record
}
set {
__record = newValue
}
}

@_spi(Internals)
public var __record: SnapshotTestingConfiguration.Record = {
if let value = ProcessInfo.processInfo.environment["SNAPSHOT_TESTING_RECORD"],
let record = SnapshotTestingConfiguration.Record(rawValue: value)
{
Expand Down
28 changes: 14 additions & 14 deletions Sources/SnapshotTesting/SnapshotsTestTrait.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if canImport(Testing)
@_spi(Experimental) import Testing
import Testing

@_spi(Experimental)
extension Trait where Self == _SnapshotsTestTrait {
Expand Down Expand Up @@ -32,21 +32,21 @@

/// A type representing the configuration of snapshot testing.
@_spi(Experimental)
public struct _SnapshotsTestTrait: CustomExecutionTrait, SuiteTrait, TestTrait {
public struct _SnapshotsTestTrait: SuiteTrait, TestTrait {
public let isRecursive = true
let configuration: SnapshotTestingConfiguration

public func execute(
_ function: @escaping () async throws -> Void,
for test: Test,
testCase: Test.Case?
) async throws {
try await withSnapshotTesting(
record: configuration.record,
diffTool: configuration.diffTool
) {
try await function()
}
}
// public func execute(
// _ function: @escaping () async throws -> Void,
// for test: Test,
// testCase: Test.Case?
// ) async throws {
// try await withSnapshotTesting(
// record: configuration.record,
// diffTool: configuration.diffTool
// ) {
// try await function()
// }
// }
}
#endif
18 changes: 7 additions & 11 deletions Tests/SnapshotTestingTests/SnapshotsTraitTests.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#if compiler(>=6) && canImport(Testing)
@_spi(Experimental) import Testing
import Testing
@_spi(Experimental) @_spi(Internals) import SnapshotTesting

struct SnapshotsTraitTests {
@Test(.snapshots(diffTool: "ksdiff"))
func testDiffTool() {
#expect(
SnapshotTestingConfiguration.current?
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
== "ksdiff old.png new.png"
)
}
Expand All @@ -17,8 +16,7 @@
@Test(.snapshots(diffTool: "difftool"))
func testDiffToolOverride() {
#expect(
SnapshotTestingConfiguration.current?
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
== "difftool old.png new.png"
)
}
Expand All @@ -28,23 +26,21 @@
@Test
func config() {
#expect(
SnapshotTestingConfiguration.current?
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
== "ksdiff old.png new.png"
)
#expect(SnapshotTestingConfiguration.current?.record == .all)
#expect(_record == .all)
}

@Suite(.snapshots(record: .failed, diffTool: "diff"))
struct OverrideDiffToolAndRecord {
@Test
func config() {
#expect(
SnapshotTestingConfiguration.current?
.diffTool?(currentFilePath: "old.png", failedFilePath: "new.png")
_diffTool(currentFilePath: "old.png", failedFilePath: "new.png")
== "diff old.png new.png"
)
#expect(SnapshotTestingConfiguration.current?.record == .failed)
#expect(_record == .failed)
}
}
}
Expand Down

0 comments on commit 81bbb32

Please sign in to comment.