Skip to content

Commit ff6daf7

Browse files
authored
refactor test fixtures throw underlying error (#4124)
motivation: test fixtures currently swallow the original error and hide it with XCTFail which does not allow using XCTSkip inside a fixture block changes: * update the fixture function to be throwing * adjust call sites (lots of them!) * cleanup use of XCTSkip for platform check
1 parent 25bf487 commit ff6daf7

39 files changed

+952
-845
lines changed

Sources/SPMTestSupport/misc.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public func fixture(
3838
file: StaticString = #file,
3939
line: UInt = #line,
4040
body: (AbsolutePath) throws -> Void
41-
) {
41+
) throws {
4242
do {
4343
// Make a suitable test directory name from the fixture subpath.
4444
let fixtureSubpath = RelativePath(name)
@@ -91,9 +91,7 @@ public func fixture(
9191
print("**** FAILURE EXECUTING SUBPROCESS ****")
9292
print("output:", output)
9393
print("stderr:", stderr)
94-
XCTFail("\(error)", file: file, line: line)
95-
} catch {
96-
XCTFail("\(error)", file: file, line: line)
94+
throw error
9795
}
9896
}
9997

Tests/BasicsTests/URLSessionHTTPClientTests.swift

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ final class URLSessionHTTPClientTest: XCTestCase {
209209

210210
// MARK: - download
211211

212-
// URLSession Download tests can only run on macOS
213-
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
214-
// and there is no way to set it in a mock
215-
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
216-
#if os(macOS)
212+
217213
func testDownloadSuccess() throws {
214+
#if !os(macOS)
215+
// URLSession Download tests can only run on macOS
216+
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
217+
// and there is no way to set it in a mock
218+
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
219+
try XCTSkipIf(true, "test is only supported on macOS")
220+
#endif
218221
let configuration = URLSessionConfiguration.default
219222
configuration.protocolClasses = [MockURLProtocol.self]
220223
let urlSession = URLSessionHTTPClient(configuration: configuration)
@@ -270,6 +273,13 @@ final class URLSessionHTTPClientTest: XCTestCase {
270273
}
271274

272275
func testDownloadAuthenticatedSuccess() throws {
276+
#if !os(macOS)
277+
// URLSession Download tests can only run on macOS
278+
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
279+
// and there is no way to set it in a mock
280+
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
281+
try XCTSkipIf(true, "test is only supported on macOS")
282+
#endif
273283
let netrcContent = "machine protected.downloader-tests.com login anonymous password qwerty"
274284
guard case .success(let netrc) = Netrc.from(netrcContent) else {
275285
return XCTFail("Cannot load netrc content")
@@ -334,6 +344,13 @@ final class URLSessionHTTPClientTest: XCTestCase {
334344
}
335345

336346
func testDownloadDefaultAuthenticationSuccess() throws {
347+
#if !os(macOS)
348+
// URLSession Download tests can only run on macOS
349+
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
350+
// and there is no way to set it in a mock
351+
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
352+
try XCTSkipIf(true, "test is only supported on macOS")
353+
#endif
337354
let netrcContent = "default login default password default"
338355
guard case .success(let netrc) = Netrc.from(netrcContent) else {
339356
return XCTFail("Cannot load netrc content")
@@ -398,6 +415,13 @@ final class URLSessionHTTPClientTest: XCTestCase {
398415
}
399416

400417
func testDownloadClientError() throws {
418+
#if !os(macOS)
419+
// URLSession Download tests can only run on macOS
420+
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
421+
// and there is no way to set it in a mock
422+
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
423+
try XCTSkipIf(true, "test is only supported on macOS")
424+
#endif
401425
let configuration = URLSessionConfiguration.default
402426
configuration.protocolClasses = [MockURLProtocol.self]
403427
let urlSession = URLSessionHTTPClient(configuration: configuration)
@@ -453,6 +477,13 @@ final class URLSessionHTTPClientTest: XCTestCase {
453477
}
454478

455479
func testDownloadServerError() throws {
480+
#if !os(macOS)
481+
// URLSession Download tests can only run on macOS
482+
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
483+
// and there is no way to set it in a mock
484+
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
485+
try XCTSkipIf(true, "test is only supported on macOS")
486+
#endif
456487
let configuration = URLSessionConfiguration.default
457488
configuration.protocolClasses = [MockURLProtocol.self]
458489
let urlSession = URLSessionHTTPClient(configuration: configuration)
@@ -493,6 +524,13 @@ final class URLSessionHTTPClientTest: XCTestCase {
493524
}
494525

495526
func testDownloadFileSystemError() throws {
527+
#if !os(macOS)
528+
// URLSession Download tests can only run on macOS
529+
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request.
530+
// and there is no way to set it in a mock
531+
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part
532+
try XCTSkipIf(true, "test is only supported on macOS")
533+
#endif
496534
let configuration = URLSessionConfiguration.default
497535
configuration.protocolClasses = [MockURLProtocol.self]
498536
let urlSession = URLSessionHTTPClient(configuration: configuration)
@@ -525,7 +563,6 @@ final class URLSessionHTTPClientTest: XCTestCase {
525563
wait(for: [completionExpectation], timeout: 1.0)
526564
}
527565
}
528-
#endif
529566
}
530567

531568
private class MockURLProtocol: URLProtocol {

Tests/BuildTests/IncrementalBuildTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ import TSCBasic
3535
///
3636
final class IncrementalBuildTests: XCTestCase {
3737

38-
func testIncrementalSingleModuleCLibraryInSources() {
39-
fixture(name: "CFamilyTargets/CLibrarySources") { prefix in
38+
func testIncrementalSingleModuleCLibraryInSources() throws {
39+
try fixture(name: "CFamilyTargets/CLibrarySources") { fixturePath in
4040
// Build it once and capture the log (this will be a full build).
41-
let (fullLog, _) = try executeSwiftBuild(prefix)
41+
let (fullLog, _) = try executeSwiftBuild(fixturePath)
4242

4343
// Check various things that we expect to see in the full build log.
4444
// FIXME: This is specific to the format of the log output, which
4545
// is quite unfortunate but not easily avoidable at the moment.
4646
XCTAssertMatch(fullLog, .contains("Compiling CLibrarySources Foo.c"))
4747

48-
let llbuildManifest = prefix.appending(components: ".build", "debug.yaml")
48+
let llbuildManifest = fixturePath.appending(components: ".build", "debug.yaml")
4949

5050
// Modify the source file in a way that changes its size so that the low-level
5151
// build system can detect the change (the timestamp change might be too small
5252
// for the granularity of the file system to represent as distinct values).
53-
let sourceFile = prefix.appending(components: "Sources", "Foo.c")
53+
let sourceFile = fixturePath.appending(components: "Sources", "Foo.c")
5454
let sourceStream = BufferedOutputByteStream()
5555
sourceStream <<< (try localFileSystem.readFileContents(sourceFile)) <<< "\n"
5656
try localFileSystem.writeFileContents(sourceFile, bytes: sourceStream.bytes)
@@ -59,15 +59,15 @@ final class IncrementalBuildTests: XCTestCase {
5959
let llbuildContents1: String = try localFileSystem.readFileContents(llbuildManifest)
6060

6161
// Now build again. This should be an incremental build.
62-
let (log2, _) = try executeSwiftBuild(prefix)
62+
let (log2, _) = try executeSwiftBuild(fixturePath)
6363
XCTAssertMatch(log2, .contains("Compiling CLibrarySources Foo.c"))
6464

6565
// Read the second llbuild manifest.
6666
let llbuildContents2: String = try localFileSystem.readFileContents(llbuildManifest)
6767

6868
// Now build again without changing anything. This should be a null
6969
// build.
70-
let (log3, _) = try executeSwiftBuild(prefix)
70+
let (log3, _) = try executeSwiftBuild(fixturePath)
7171
XCTAssertNoMatch(log3, .contains("Compiling CLibrarySources Foo.c"))
7272

7373
// Read the third llbuild manifest.
@@ -79,22 +79,22 @@ final class IncrementalBuildTests: XCTestCase {
7979
// Modify the header file in a way that changes its size so that the low-level
8080
// build system can detect the change (the timestamp change might be too small
8181
// for the granularity of the file system to represent as distinct values).
82-
let headerFile = prefix.appending(components: "Sources", "include", "Foo.h")
82+
let headerFile = fixturePath.appending(components: "Sources", "include", "Foo.h")
8383
let headerStream = BufferedOutputByteStream()
8484
headerStream <<< (try localFileSystem.readFileContents(headerFile)) <<< "\n"
8585
try localFileSystem.writeFileContents(headerFile, bytes: headerStream.bytes)
8686

8787
// Now build again. This should be an incremental build.
88-
let (log4, _) = try executeSwiftBuild(prefix)
88+
let (log4, _) = try executeSwiftBuild(fixturePath)
8989
XCTAssertMatch(log4, .contains("Compiling CLibrarySources Foo.c"))
9090
}
9191
}
9292

93-
func testBuildManifestCaching() {
94-
fixture(name: "ValidLayouts/SingleModule/Library") { prefix in
93+
func testBuildManifestCaching() throws {
94+
try fixture(name: "ValidLayouts/SingleModule/Library") { fixturePath in
9595
@discardableResult
9696
func build() throws -> String {
97-
return try executeSwiftBuild(prefix).stdout
97+
return try executeSwiftBuild(fixturePath).stdout
9898
}
9999

100100
// Perform a full build.
@@ -110,7 +110,7 @@ final class IncrementalBuildTests: XCTestCase {
110110
XCTAssertNoMatch(log3, .contains("Planning build"))
111111

112112
// Check that we do run planning when a new source file is added.
113-
let sourceFile = prefix.appending(components: "Sources", "Library", "new.swift")
113+
let sourceFile = fixturePath.appending(components: "Sources", "Library", "new.swift")
114114
try localFileSystem.writeFileContents(sourceFile, bytes: "")
115115
let log4 = try build()
116116
XCTAssertMatch(log4, .contains("Compiling Library"))
@@ -123,11 +123,11 @@ final class IncrementalBuildTests: XCTestCase {
123123
}
124124
}
125125

126-
func testDisableBuildManifestCaching() {
127-
fixture(name: "ValidLayouts/SingleModule/Library") { prefix in
126+
func testDisableBuildManifestCaching() throws {
127+
try fixture(name: "ValidLayouts/SingleModule/Library") { fixturePath in
128128
@discardableResult
129129
func build() throws -> String {
130-
return try executeSwiftBuild(prefix, extraArgs: ["--disable-build-manifest-caching"]).stdout
130+
return try executeSwiftBuild(fixturePath, extraArgs: ["--disable-build-manifest-caching"]).stdout
131131
}
132132

133133
// Perform a full build.

0 commit comments

Comments
 (0)