Skip to content

Commit 2aba76f

Browse files
authored
Specify custom snapshot directory (#170)
* wip * wip * wip * bring back line
1 parent 165019b commit 2aba76f

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,40 @@ public func assertSnapshots<Value, Format>(
114114

115115
/// Verifies that a given value matches a reference on disk.
116116
///
117+
/// Third party snapshot assert helpers can be built on top of this function. Simply invoke `verifySnapshot` with your own arguments, and then invoke `XCTFail` with the string returned if it is non-`nil`. For example, if you want the snapshot directory to be determined by an environment variable, you can create your own assert helper like so:
118+
///
119+
/// public func myAssertSnapshot<Value, Format>(
120+
/// matching value: @autoclosure () throws -> Value,
121+
/// as snapshotting: Snapshotting<Value, Format>,
122+
/// named name: String? = nil,
123+
/// record recording: Bool = false,
124+
/// timeout: TimeInterval = 5,
125+
/// file: StaticString = #file,
126+
/// testName: String = #function,
127+
/// line: UInt = #line
128+
/// ) {
129+
///
130+
/// let snapshotDirectory = ProcessInfo.processInfo.environment["SNAPSHOT_REFERENCE_DIR"]! + "/" + #file
131+
/// let failure = verifySnapshot(
132+
/// matching: value,
133+
/// as: snapshotting,
134+
/// named: name,
135+
/// record: recording,
136+
/// snapshotDirectory: snapshotDirectory,
137+
/// timeout: timeout,
138+
/// file: file,
139+
/// testName: testName
140+
/// )
141+
/// guard let message = failure else { return }
142+
/// XCTFail(message, file: file, line: line)
143+
/// }
144+
///
117145
/// - Parameters:
118146
/// - value: A value to compare against a reference.
119147
/// - snapshotting: A strategy for serializing, deserializing, and comparing values.
120148
/// - name: An optional description of the snapshot.
121149
/// - recording: Whether or not to record a new reference.
150+
/// - snapshotDirectory: Optional directory to save snapshots. By default snapshots will be saved in a directory with the same name as the test file, and that directory will sit inside a directory `__Snapshots__` that sits next to your test file.
122151
/// - timeout: The amount of time a snapshot must be generated in.
123152
/// - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.
124153
/// - testName: The name of the test in which failure occurred. Defaults to the function name of the test case in which this function was called.
@@ -129,6 +158,7 @@ public func verifySnapshot<Value, Format>(
129158
as snapshotting: Snapshotting<Value, Format>,
130159
named name: String? = nil,
131160
record recording: Bool = false,
161+
snapshotDirectory: String? = nil,
132162
timeout: TimeInterval = 5,
133163
file: StaticString = #file,
134164
testName: String = #function,
@@ -141,10 +171,12 @@ public func verifySnapshot<Value, Format>(
141171
do {
142172
let fileUrl = URL(fileURLWithPath: "\(file)")
143173
let fileName = fileUrl.deletingPathExtension().lastPathComponent
144-
let directoryUrl = fileUrl.deletingLastPathComponent()
145-
let snapshotDirectoryUrl: URL = directoryUrl
146-
.appendingPathComponent("__Snapshots__")
147-
.appendingPathComponent(fileName)
174+
175+
let snapshotDirectoryUrl = snapshotDirectory.map(URL.init(fileURLWithPath:))
176+
?? fileUrl
177+
.deletingLastPathComponent()
178+
.appendingPathComponent("__Snapshots__")
179+
.appendingPathComponent(fileName)
148180

149181
let identifier: String
150182
if let name = name {

Sources/SnapshotTesting/SnapshotTestCase.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ open class SnapshotTestCase: XCTestCase {
135135
as snapshotting: Snapshotting<Value, Format>,
136136
named name: String? = nil,
137137
record recording: Bool = false,
138+
snapshotDirectory: String? = nil,
138139
timeout: TimeInterval = 5,
139140
file: StaticString = #file,
140141
testName: String = #function,
@@ -147,10 +148,12 @@ open class SnapshotTestCase: XCTestCase {
147148
do {
148149
let fileUrl = URL(fileURLWithPath: "\(file)")
149150
let fileName = fileUrl.deletingPathExtension().lastPathComponent
150-
let directoryUrl = fileUrl.deletingLastPathComponent()
151-
let snapshotDirectoryUrl: URL = directoryUrl
152-
.appendingPathComponent("__Snapshots__")
153-
.appendingPathComponent(fileName)
151+
152+
let snapshotDirectoryUrl = snapshotDirectory.map(URL.init(fileURLWithPath:))
153+
?? fileUrl
154+
.deletingLastPathComponent()
155+
.appendingPathComponent("__Snapshots__")
156+
.appendingPathComponent(fileName)
154157

155158
let identifier: String
156159
if let name = name {

0 commit comments

Comments
 (0)