@@ -28,6 +28,7 @@ import SPMBuildCore
28
28
import struct SPMBuildCore. BuildParameters
29
29
import TSCTestSupport
30
30
import Workspace
31
+ import Testing
31
32
import func XCTest. XCTFail
32
33
import struct XCTest. XCTSkip
33
34
@@ -112,7 +113,8 @@ public func testWithTemporaryDirectory<Result>(
112
113
/// The temporary copy is deleted after the block returns. The fixture name may
113
114
/// contain `/` characters, which are treated as path separators, exactly as if
114
115
/// the name were a relative path.
115
- @discardableResult public func fixture< T> (
116
+ @available ( * , deprecated, message: " Migrate test to Swift Testing and use 'fixture' instead " )
117
+ @discardableResult public func fixtureXCTest< T> (
116
118
name: String ,
117
119
createGitRepo: Bool = true ,
118
120
file: StaticString = #file,
@@ -133,7 +135,44 @@ public func testWithTemporaryDirectory<Result>(
133
135
try ? localFileSystem. removeFileTree ( tmpDirPath)
134
136
}
135
137
136
- let fixtureDir = try verifyFixtureExists ( at: fixtureSubpath, file: file, line: line)
138
+ let fixtureDir = try verifyFixtureExistsXCTest ( at: fixtureSubpath, file: file, line: line)
139
+ let preparedFixture = try setup (
140
+ fixtureDir: fixtureDir,
141
+ in: tmpDirPath,
142
+ copyName: copyName,
143
+ createGitRepo: createGitRepo
144
+ )
145
+ return try body ( preparedFixture)
146
+ }
147
+ } catch SwiftPMError . executionFailure( let error, let output, let stderr) {
148
+ print ( " **** FAILURE EXECUTING SUBPROCESS **** " )
149
+ print ( " output: " , output)
150
+ print ( " stderr: " , stderr)
151
+ throw error
152
+ }
153
+ }
154
+
155
+ @discardableResult public func fixture< T> (
156
+ name: String ,
157
+ createGitRepo: Bool = true ,
158
+ sourceLocation: SourceLocation = #_sourceLocation,
159
+ body: ( AbsolutePath ) throws -> T
160
+ ) throws -> T {
161
+ do {
162
+ // Make a suitable test directory name from the fixture subpath.
163
+ let fixtureSubpath = try RelativePath ( validating: name)
164
+ let copyName = fixtureSubpath. components. joined ( separator: " _ " )
165
+
166
+ // Create a temporary directory for the duration of the block.
167
+ return try withTemporaryDirectory ( prefix: copyName) { tmpDirPath in
168
+
169
+ defer {
170
+ // Unblock and remove the tmp dir on deinit.
171
+ try ? localFileSystem. chmod ( . userWritable, path: tmpDirPath, options: [ . recursive] )
172
+ try ? localFileSystem. removeFileTree ( tmpDirPath)
173
+ }
174
+
175
+ let fixtureDir = try verifyFixtureExists ( at: fixtureSubpath, sourceLocation: sourceLocation)
137
176
let preparedFixture = try setup (
138
177
fixtureDir: fixtureDir,
139
178
in: tmpDirPath,
@@ -154,7 +193,8 @@ public enum TestError: Error {
154
193
case platformNotSupported
155
194
}
156
195
157
- @discardableResult public func fixture< T> (
196
+ @available ( * , deprecated, message: " Migrate test to Swift Testing and use 'fixture' instead " )
197
+ @discardableResult public func fixtureXCTest< T> (
158
198
name: String ,
159
199
createGitRepo: Bool = true ,
160
200
file: StaticString = #file,
@@ -175,7 +215,7 @@ public enum TestError: Error {
175
215
try ? localFileSystem. removeFileTree ( tmpDirPath)
176
216
}
177
217
178
- let fixtureDir = try verifyFixtureExists ( at: fixtureSubpath, file: file, line: line)
218
+ let fixtureDir = try verifyFixtureExistsXCTest ( at: fixtureSubpath, file: file, line: line)
179
219
let preparedFixture = try setup (
180
220
fixtureDir: fixtureDir,
181
221
in: tmpDirPath,
@@ -192,7 +232,44 @@ public enum TestError: Error {
192
232
}
193
233
}
194
234
195
- fileprivate func verifyFixtureExists( at fixtureSubpath: RelativePath , file: StaticString = #file, line: UInt = #line) throws -> AbsolutePath {
235
+ @discardableResult public func fixture< T> (
236
+ name: String ,
237
+ createGitRepo: Bool = true ,
238
+ sourceLocation: SourceLocation = #_sourceLocation,
239
+ body: ( AbsolutePath ) async throws -> T
240
+ ) async throws -> T {
241
+ do {
242
+ // Make a suitable test directory name from the fixture subpath.
243
+ let fixtureSubpath = try RelativePath ( validating: name)
244
+ let copyName = fixtureSubpath. components. joined ( separator: " _ " )
245
+
246
+ // Create a temporary directory for the duration of the block.
247
+ return try await withTemporaryDirectory ( prefix: copyName) { tmpDirPath in
248
+
249
+ defer {
250
+ // Unblock and remove the tmp dir on deinit.
251
+ try ? localFileSystem. chmod ( . userWritable, path: tmpDirPath, options: [ . recursive] )
252
+ try ? localFileSystem. removeFileTree ( tmpDirPath)
253
+ }
254
+
255
+ let fixtureDir = try verifyFixtureExists ( at: fixtureSubpath, sourceLocation: sourceLocation)
256
+ let preparedFixture = try setup (
257
+ fixtureDir: fixtureDir,
258
+ in: tmpDirPath,
259
+ copyName: copyName,
260
+ createGitRepo: createGitRepo
261
+ )
262
+ return try await body ( preparedFixture)
263
+ }
264
+ } catch SwiftPMError . executionFailure( let error, let output, let stderr) {
265
+ print ( " **** FAILURE EXECUTING SUBPROCESS **** " )
266
+ print ( " output: " , output)
267
+ print ( " stderr: " , stderr)
268
+ throw error
269
+ }
270
+ }
271
+
272
+ fileprivate func verifyFixtureExistsXCTest( at fixtureSubpath: RelativePath , file: StaticString = #file, line: UInt = #line) throws -> AbsolutePath {
196
273
let fixtureDir = AbsolutePath ( " ../../../Fixtures " , relativeTo: #file)
197
274
. appending ( fixtureSubpath)
198
275
@@ -205,6 +282,19 @@ fileprivate func verifyFixtureExists(at fixtureSubpath: RelativePath, file: Stat
205
282
return fixtureDir
206
283
}
207
284
285
+ fileprivate func verifyFixtureExists( at fixtureSubpath: RelativePath , sourceLocation: SourceLocation = #_sourceLocation) throws -> AbsolutePath {
286
+ let fixtureDir = AbsolutePath ( " ../../../Fixtures " , relativeTo: #file)
287
+ . appending ( fixtureSubpath)
288
+
289
+ // Check that the fixture is really there.
290
+ guard localFileSystem. isDirectory ( fixtureDir) else {
291
+ Issue . record ( " No such fixture: \( fixtureDir) " , sourceLocation: sourceLocation)
292
+ throw SwiftPMError . packagePathNotFound
293
+ }
294
+
295
+ return fixtureDir
296
+ }
297
+
208
298
fileprivate func setup(
209
299
fixtureDir: AbsolutePath ,
210
300
in tmpDirPath: AbsolutePath ,
0 commit comments