Skip to content

Commit 6b5147e

Browse files
committed
test clean up
1 parent 4463f37 commit 6b5147e

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

Tests/SnapshotTestingTests/RecordTests.swift

+17-30
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@ import SnapshotTesting
22
import XCTest
33

44
class RecordTests: XCTestCase {
5+
var snapshotURL: URL!
6+
57
override func setUp() {
68
super.setUp()
9+
10+
let testName = self.testRun!.test.name.split(separator: " ").last!.dropLast()
11+
let fileURL = URL(fileURLWithPath: #file, isDirectory: false)
12+
let fileName = fileURL.deletingPathExtension().lastPathComponent
13+
let testDirectory = fileURL
14+
.deletingLastPathComponent()
15+
.appendingPathComponent("__Snapshots__")
16+
.appendingPathComponent(fileName)
17+
snapshotURL = testDirectory
18+
.appendingPathComponent("\(testName).1.json")
19+
try? FileManager.default
20+
.removeItem(at: snapshotURL.deletingLastPathComponent())
721
try? FileManager.default
8-
.createDirectory(
9-
at: snapshotURL().deletingLastPathComponent(), withIntermediateDirectories: true)
22+
.createDirectory(at: testDirectory, withIntermediateDirectories: true)
1023
}
24+
1125
override func tearDown() {
1226
super.tearDown()
1327
try? FileManager.default
14-
.removeItem(at: snapshotURL().deletingLastPathComponent())
28+
.removeItem(at: snapshotURL.deletingLastPathComponent())
1529
}
1630

1731
#if canImport(Darwin)
1832
func testRecordNever() {
19-
let snapshotURL = snapshotURL()
20-
2133
XCTExpectFailure {
2234
withSnapshotTesting(record: .never) {
2335
assertSnapshot(of: 42, as: .json)
@@ -37,9 +49,6 @@ class RecordTests: XCTestCase {
3749

3850
#if canImport(Darwin)
3951
func testRecordMissing() {
40-
let snapshotURL = snapshotURL()
41-
try? FileManager.default.removeItem(at: snapshotURL)
42-
4352
XCTExpectFailure {
4453
withSnapshotTesting(record: .missing) {
4554
assertSnapshot(of: 42, as: .json)
@@ -60,8 +69,6 @@ class RecordTests: XCTestCase {
6069

6170
#if canImport(Darwin)
6271
func testRecordMissing_ExistingFile() throws {
63-
let snapshotURL = snapshotURL()
64-
try? FileManager.default.removeItem(at: snapshotURL)
6572
try Data("999".utf8).write(to: snapshotURL)
6673

6774
XCTExpectFailure {
@@ -84,9 +91,6 @@ class RecordTests: XCTestCase {
8491

8592
#if canImport(Darwin)
8693
func testRecordAll_Fresh() throws {
87-
let snapshotURL = snapshotURL()
88-
try? FileManager.default.removeItem(at: snapshotURL)
89-
9094
XCTExpectFailure {
9195
withSnapshotTesting(record: .all) {
9296
assertSnapshot(of: 42, as: .json)
@@ -107,8 +111,6 @@ class RecordTests: XCTestCase {
107111

108112
#if canImport(Darwin)
109113
func testRecordAll_Overwrite() throws {
110-
let snapshotURL = snapshotURL()
111-
try? FileManager.default.removeItem(at: snapshotURL)
112114
try Data("999".utf8).write(to: snapshotURL)
113115

114116
XCTExpectFailure {
@@ -131,8 +133,6 @@ class RecordTests: XCTestCase {
131133

132134
#if canImport(Darwin)
133135
func testRecordFailed_WhenFailure() throws {
134-
let snapshotURL = snapshotURL()
135-
try? FileManager.default.removeItem(at: snapshotURL)
136136
try Data("999".utf8).write(to: snapshotURL)
137137

138138
XCTExpectFailure {
@@ -154,8 +154,6 @@ class RecordTests: XCTestCase {
154154
#endif
155155

156156
func testRecordFailed_NoFailure() throws {
157-
let snapshotURL = snapshotURL()
158-
try? FileManager.default.removeItem(at: snapshotURL)
159157
try Data("42".utf8).write(to: snapshotURL)
160158
let modifiedDate =
161159
try FileManager.default
@@ -175,15 +173,4 @@ class RecordTests: XCTestCase {
175173
modifiedDate
176174
)
177175
}
178-
179-
func snapshotURL(_ function: StaticString = #function) -> URL {
180-
let fileURL = URL(fileURLWithPath: #file, isDirectory: false)
181-
let fileName = fileURL.deletingPathExtension().lastPathComponent
182-
return
183-
fileURL
184-
.deletingLastPathComponent()
185-
.appendingPathComponent("__Snapshots__")
186-
.appendingPathComponent(fileName)
187-
.appendingPathComponent("\(String(describing: function).dropLast(2)).1.json")
188-
}
189176
}

0 commit comments

Comments
 (0)