Skip to content

Commit

Permalink
Check resource bundle for files
Browse files Browse the repository at this point in the history
  • Loading branch information
mblackmon authored and lj-dickey committed Nov 29, 2023
1 parent 59b663f commit 41af302
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

Delightful Swift snapshot testing.

<!--
![An example of a snapshot failure in Xcode.](.github/snapshot-test-1.png)
-->

## This is a fork

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.

## Usage

Once [installed](#installation), _no additional configuration is required_. You can import the
Expand Down
22 changes: 16 additions & 6 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public var isRecording = false
/// Due to a name clash in Xcode 12, this has been renamed to `isRecording`.
@available(*, deprecated, renamed: "isRecording")
public var record: Bool {
get { isRecording }
set { isRecording = newValue }
get { isRecording }
set { isRecording = newValue }
}

/// Asserts that a given value matches a reference on disk.
Expand Down Expand Up @@ -220,10 +220,20 @@ public func verifySnapshot<Value, Format>(
}

let testName = sanitizePathComponent(testName)
let snapshotFileUrl =
snapshotDirectoryUrl
.appendingPathComponent("\(testName).\(identifier)")
.appendingPathExtension(snapshotting.pathExtension ?? "")

// Check the bundle for the resource first, then the file system
let thisBundle = Bundle(for: CleanCounterBetweenTestCases.self)
let resourcePath = thisBundle.path(forResource: "\(testName).\(identifier)", ofType: snapshotting.pathExtension)
var snapshotFileUrlCandidate = resourcePath.map({ URL(fileURLWithPath: $0) })
if snapshotFileUrlCandidate == nil {
snapshotFileUrlCandidate = snapshotDirectoryUrl
.appendingPathComponent("\(testName).\(identifier)")
.appendingPathExtension(snapshotting.pathExtension ?? "")
}
guard let snapshotFileUrl = snapshotFileUrlCandidate else {
return nil
}

let fileManager = FileManager.default
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)

Expand Down

0 comments on commit 41af302

Please sign in to comment.