Skip to content

Commit

Permalink
Record Attachments in Never mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jflan-dd committed Jan 14, 2025
1 parent 58773b2 commit 4aaa2d7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
66 changes: 52 additions & 14 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,42 +352,69 @@ public func verifySnapshot<Value, Format>(
return "Couldn't snapshot value"
}

func recordSnapshot() throws {
try snapshotting.diffing.toData(diffable).write(to: snapshotFileUrl)
func recordSnapshot(writeToDisk: Bool) throws {
let snapshotData = snapshotting.diffing.toData(diffable)

if writeToDisk {
try snapshotData.write(to: snapshotFileUrl)
}

#if !os(Linux) && !os(Windows)
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)
if writeToDisk {
// Snapshot was written to disk. Create attachment from file
let attachment = XCTAttachment(contentsOfFile: snapshotFileUrl)
activity.add(attachment)
} else {
// Snapshot was not written to disk. Create attachment from data and path extension
let typeIdentifier = snapshotting.pathExtension.flatMap(uniformTypeIdentifier(fromExtension:))

let attachment = XCTAttachment(
uniformTypeIdentifier: typeIdentifier,
name: snapshotFileUrl.lastPathComponent,
payload: snapshotData
)

activity.add(attachment)
}
}
}
#endif
}

guard
record != .all,
(record != .missing && record != .failed)
|| fileManager.fileExists(atPath: snapshotFileUrl.path)
else {
try recordSnapshot()
if record == .all {
try recordSnapshot(writeToDisk: true)

return SnapshotTestingConfiguration.current?.record == .all
? """
return """
Record mode is on. Automatically recorded snapshot: …
open "\(snapshotFileUrl.absoluteString)"
Turn record mode off and re-run "\(testName)" to assert against the newly-recorded snapshot
"""
: """
}

guard fileManager.fileExists(atPath: snapshotFileUrl.path) else {
if record == .never {
try recordSnapshot(writeToDisk: false)

return """
No reference was found on disk. New snapshot was not recorded because recording is disabled
"""
} else {
try recordSnapshot(writeToDisk: true)

return """
No reference was found on disk. Automatically recorded snapshot: …
open "\(snapshotFileUrl.absoluteString)"
Re-run "\(testName)" to assert against the newly-recorded snapshot.
"""
}
}

let data = try Data(contentsOf: snapshotFileUrl)
Expand Down Expand Up @@ -444,7 +471,7 @@ public func verifySnapshot<Value, Format>(
}

if record == .failed {
try recordSnapshot()
try recordSnapshot(writeToDisk: true)
failureMessage += " A new snapshot was automatically recorded."
}

Expand Down Expand Up @@ -473,6 +500,17 @@ func sanitizePathComponent(_ string: String) -> String {
.replacingOccurrences(of: "^-|-$", with: "", options: .regularExpression)
}

func uniformTypeIdentifier(fromExtension pathExtension: String) -> String? {
// This can be much cleaner in macOS 11+ using UTType
let unmanagedString = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension as CFString,
pathExtension as CFString,
nil
)

return unmanagedString?.takeRetainedValue() as String?
}

// We need to clean counter between tests executions in order to support test-iterations.
private class CleanCounterBetweenTestCases: NSObject, XCTestObservation {
private static var registered = false
Expand Down
2 changes: 1 addition & 1 deletion Tests/SnapshotTestingTests/RecordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RecordTests: XCTestCase {
}
} issueMatcher: {
$0.compactDescription == """
failed - The file “testRecordNever.1.json” couldn’t be opened because there is no such file.
failed - No reference was found on disk. New snapshot was not recorded because recording is disabled
"""
}

Expand Down

0 comments on commit 4aaa2d7

Please sign in to comment.