From 9341833c0317b92ef261a9e0928e2a3fa487f1ae Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 1 Aug 2024 17:21:07 -0400 Subject: [PATCH] Don't invoke XCTContext when running in the Testing framework. --- Sources/SnapshotTesting/AssertSnapshot.swift | 5 ++++- .../Internal/RecordIssue.swift | 8 ++++++++ .../AssertSnapshotSwiftTests.swift | 19 +++++++++++++++++++ .../AssertSnapshotSwiftTests/dump.1.txt | 4 ++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift create mode 100644 Tests/SnapshotTestingTests/__Snapshots__/AssertSnapshotSwiftTests/dump.1.txt diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index b2d473d38..7aa1113d9 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -311,7 +311,10 @@ public func verifySnapshot( func recordSnapshot() throws { try snapshotting.diffing.toData(diffable).write(to: snapshotFileUrl) #if !os(Linux) && !os(Windows) - if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") { + if + !isSwiftTesting, + ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") + { XCTContext.runActivity(named: "Attached Recorded Snapshot") { activity in let attachment = XCTAttachment(contentsOfFile: snapshotFileUrl) activity.add(attachment) diff --git a/Sources/SnapshotTesting/Internal/RecordIssue.swift b/Sources/SnapshotTesting/Internal/RecordIssue.swift index 01c700df8..a6953291f 100644 --- a/Sources/SnapshotTesting/Internal/RecordIssue.swift +++ b/Sources/SnapshotTesting/Internal/RecordIssue.swift @@ -4,6 +4,14 @@ import XCTest import Testing #endif +var isSwiftTesting: Bool { +#if canImport(Testing) + return Test.current != nil + #else + return false + #endif +} + @_spi(Internals) public func recordIssue( _ message: @autoclosure () -> String, diff --git a/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift b/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift new file mode 100644 index 000000000..12528b843 --- /dev/null +++ b/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift @@ -0,0 +1,19 @@ +#if canImport(Testing) + import Testing + import Foundation + import InlineSnapshotTesting + @_spi(Experimental) import SnapshotTesting + + @Suite( + .snapshots( + record: .missing + ) + ) + struct AssertSnapshotTests { + @Test func dump() { + struct User { let id: Int, name: String, bio: String } + let user = User(id: 1, name: "Blobby", bio: "Blobbed around the world.") + assertSnapshot(of: user, as: .dump) + } + } +#endif diff --git a/Tests/SnapshotTestingTests/__Snapshots__/AssertSnapshotSwiftTests/dump.1.txt b/Tests/SnapshotTestingTests/__Snapshots__/AssertSnapshotSwiftTests/dump.1.txt new file mode 100644 index 000000000..0e3f0406c --- /dev/null +++ b/Tests/SnapshotTestingTests/__Snapshots__/AssertSnapshotSwiftTests/dump.1.txt @@ -0,0 +1,4 @@ +▿ User + - bio: "Blobbed around the world." + - id: 1 + - name: "Blobby"