@@ -2,22 +2,34 @@ import SnapshotTesting
2
2
import XCTest
3
3
4
4
class RecordTests : XCTestCase {
5
+ var snapshotURL : URL !
6
+
5
7
override func setUp( ) {
6
8
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 ( ) )
7
21
try ? FileManager . default
8
- . createDirectory (
9
- at: snapshotURL ( ) . deletingLastPathComponent ( ) , withIntermediateDirectories: true )
22
+ . createDirectory ( at: testDirectory, withIntermediateDirectories: true )
10
23
}
24
+
11
25
override func tearDown( ) {
12
26
super. tearDown ( )
13
27
try ? FileManager . default
14
- . removeItem ( at: snapshotURL ( ) . deletingLastPathComponent ( ) )
28
+ . removeItem ( at: snapshotURL. deletingLastPathComponent ( ) )
15
29
}
16
30
17
31
#if canImport(Darwin)
18
32
func testRecordNever( ) {
19
- let snapshotURL = snapshotURL ( )
20
-
21
33
XCTExpectFailure {
22
34
withSnapshotTesting ( record: . never) {
23
35
assertSnapshot ( of: 42 , as: . json)
@@ -37,9 +49,6 @@ class RecordTests: XCTestCase {
37
49
38
50
#if canImport(Darwin)
39
51
func testRecordMissing( ) {
40
- let snapshotURL = snapshotURL ( )
41
- try ? FileManager . default. removeItem ( at: snapshotURL)
42
-
43
52
XCTExpectFailure {
44
53
withSnapshotTesting ( record: . missing) {
45
54
assertSnapshot ( of: 42 , as: . json)
@@ -60,8 +69,6 @@ class RecordTests: XCTestCase {
60
69
61
70
#if canImport(Darwin)
62
71
func testRecordMissing_ExistingFile( ) throws {
63
- let snapshotURL = snapshotURL ( )
64
- try ? FileManager . default. removeItem ( at: snapshotURL)
65
72
try Data ( " 999 " . utf8) . write ( to: snapshotURL)
66
73
67
74
XCTExpectFailure {
@@ -84,9 +91,6 @@ class RecordTests: XCTestCase {
84
91
85
92
#if canImport(Darwin)
86
93
func testRecordAll_Fresh( ) throws {
87
- let snapshotURL = snapshotURL ( )
88
- try ? FileManager . default. removeItem ( at: snapshotURL)
89
-
90
94
XCTExpectFailure {
91
95
withSnapshotTesting ( record: . all) {
92
96
assertSnapshot ( of: 42 , as: . json)
@@ -107,8 +111,6 @@ class RecordTests: XCTestCase {
107
111
108
112
#if canImport(Darwin)
109
113
func testRecordAll_Overwrite( ) throws {
110
- let snapshotURL = snapshotURL ( )
111
- try ? FileManager . default. removeItem ( at: snapshotURL)
112
114
try Data ( " 999 " . utf8) . write ( to: snapshotURL)
113
115
114
116
XCTExpectFailure {
@@ -131,8 +133,6 @@ class RecordTests: XCTestCase {
131
133
132
134
#if canImport(Darwin)
133
135
func testRecordFailed_WhenFailure( ) throws {
134
- let snapshotURL = snapshotURL ( )
135
- try ? FileManager . default. removeItem ( at: snapshotURL)
136
136
try Data ( " 999 " . utf8) . write ( to: snapshotURL)
137
137
138
138
XCTExpectFailure {
@@ -154,8 +154,6 @@ class RecordTests: XCTestCase {
154
154
#endif
155
155
156
156
func testRecordFailed_NoFailure( ) throws {
157
- let snapshotURL = snapshotURL ( )
158
- try ? FileManager . default. removeItem ( at: snapshotURL)
159
157
try Data ( " 42 " . utf8) . write ( to: snapshotURL)
160
158
let modifiedDate =
161
159
try FileManager . default
@@ -175,15 +173,4 @@ class RecordTests: XCTestCase {
175
173
modifiedDate
176
174
)
177
175
}
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
- }
189
176
}
0 commit comments