Skip to content

Commit

Permalink
Merge pull request #3 from oreillymedia/ticket/local-changes
Browse files Browse the repository at this point in the history
Re-apply local changes after updating main
  • Loading branch information
lj-dickey authored Nov 29, 2023
2 parents 59b663f + d816142 commit ce751cc
Show file tree
Hide file tree
Showing 2 changed files with 29 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
27 changes: 21 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,25 @@ 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
// But, if we're recording, don't bother checking the bundle, since we aren't comparing it to anything, and
// want the new file to be generated in the source directory, not the bundle.
var snapshotFileUrlCandidate: URL?
if !recording {
let thisBundle = Bundle(for: CleanCounterBetweenTestCases.self)
let resourcePath = thisBundle.path(forResource: "\(testName).\(identifier)", ofType: snapshotting.pathExtension)
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 ce751cc

Please sign in to comment.