Skip to content

Commit ce751cc

Browse files
authored
Merge pull request #3 from oreillymedia/ticket/local-changes
Re-apply local changes after updating main
2 parents 59b663f + d816142 commit ce751cc

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
Delightful Swift snapshot testing.
99

10+
<!--
11+
![An example of a snapshot failure in Xcode.](.github/snapshot-test-1.png)
12+
-->
13+
14+
## This is a fork
15+
16+
It differs from the original in just one key way. When looking for a snapshot resource (usually an image), it tries to load it from the test bundle first. Everything else _should_ be the same.
17+
1018
## Usage
1119

1220
Once [installed](#installation), _no additional configuration is required_. You can import the

Sources/SnapshotTesting/AssertSnapshot.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public var isRecording = false
1616
/// Due to a name clash in Xcode 12, this has been renamed to `isRecording`.
1717
@available(*, deprecated, renamed: "isRecording")
1818
public var record: Bool {
19-
get { isRecording }
20-
set { isRecording = newValue }
19+
get { isRecording }
20+
set { isRecording = newValue }
2121
}
2222

2323
/// Asserts that a given value matches a reference on disk.
@@ -220,10 +220,25 @@ public func verifySnapshot<Value, Format>(
220220
}
221221

222222
let testName = sanitizePathComponent(testName)
223-
let snapshotFileUrl =
224-
snapshotDirectoryUrl
225-
.appendingPathComponent("\(testName).\(identifier)")
226-
.appendingPathExtension(snapshotting.pathExtension ?? "")
223+
224+
// Check the bundle for the resource first, then the file system
225+
// But, if we're recording, don't bother checking the bundle, since we aren't comparing it to anything, and
226+
// want the new file to be generated in the source directory, not the bundle.
227+
var snapshotFileUrlCandidate: URL?
228+
if !recording {
229+
let thisBundle = Bundle(for: CleanCounterBetweenTestCases.self)
230+
let resourcePath = thisBundle.path(forResource: "\(testName).\(identifier)", ofType: snapshotting.pathExtension)
231+
snapshotFileUrlCandidate = resourcePath.map({ URL(fileURLWithPath: $0) })
232+
}
233+
if snapshotFileUrlCandidate == nil {
234+
snapshotFileUrlCandidate = snapshotDirectoryUrl
235+
.appendingPathComponent("\(testName).\(identifier)")
236+
.appendingPathExtension(snapshotting.pathExtension ?? "")
237+
}
238+
guard let snapshotFileUrl = snapshotFileUrlCandidate else {
239+
return nil
240+
}
241+
227242
let fileManager = FileManager.default
228243
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)
229244

0 commit comments

Comments
 (0)