Skip to content

Commit 1a26c0e

Browse files
authored
Testing: withMockedHome: Forward errors from function under test (#424)
* withMockedHome: Don't swallow errors The code was silently swallowing errors coming from the function under test, so errors in the tests did not surface as test failures. This throws the error if the cleaning the bin directory doesn't fail first. * Update UseTests: Swallow unknown version errors Now that the mock home directory isn't swallowing function errors, these are resulting in test failures when they aren't actually intended. In these cases, we use `#expect` to verify the version, but swallow any errors returned by the swiftly invocation. In all cases, these were the result of trying to switch to a toolchain that didn't exist. This should emit an error message, but leave the selected toolchain unchanged.
1 parent a28817f commit 1a26c0e

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Tests/SwiftlyTests/SwiftlyTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ public enum SwiftlyTests {
336336
if cleanBinDir {
337337
try await fs.remove(atPath: Swiftly.currentPlatform.swiftlyBinDir(Self.ctx))
338338
}
339+
throw error
339340
}
340341
}
341342
}

Tests/SwiftlyTests/UseTests.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ import Testing
1111
/// Execute a `use` command with the provided argument. Then validate that the configuration is updated properly and
1212
/// the in-use swift executable prints the the provided expectedVersion.
1313
func useAndValidate(argument: String, expectedVersion: ToolchainVersion) async throws {
14-
try await SwiftlyTests.runCommand(Use.self, ["use", "-g", argument])
14+
do {
15+
try await SwiftlyTests.runCommand(Use.self, ["use", "-g", argument])
16+
} catch {
17+
// Swallow any errors. Some of these tests verify that switching to
18+
// a non-existent toolchain does not change the version
19+
// configuration.
20+
// Swiftly should emit an error message, but we don't care.
21+
}
1522

16-
#expect(try await Config.load().inUse == expectedVersion)
23+
let useToolchain = try await Config.load().inUse
24+
#expect(useToolchain == expectedVersion)
1725
}
1826

1927
/// Tests that the `use` command can switch between installed stable release toolchains.
@@ -201,15 +209,20 @@ import Testing
201209
/// Tests that the `use` command gracefully exits when executed before any toolchains have been installed.
202210
@Test(.mockedSwiftlyVersion(), .mockHomeToolchains(toolchains: []))
203211
func useNoInstalledToolchains() async throws {
204-
try await SwiftlyTests.runCommand(Use.self, ["use", "-g", "latest"])
212+
do {
213+
try await SwiftlyTests.runCommand(Use.self, ["use", "-g", "latest"])
205214

206-
var config = try await Config.load()
207-
#expect(config.inUse == nil)
215+
var config = try await Config.load()
216+
#expect(config.inUse == nil)
208217

209-
try await SwiftlyTests.runCommand(Use.self, ["use", "-g", "5.6.0"])
218+
try await SwiftlyTests.runCommand(Use.self, ["use", "-g", "5.6.0"])
210219

211-
config = try await Config.load()
212-
#expect(config.inUse == nil)
220+
config = try await Config.load()
221+
#expect(config.inUse == nil)
222+
} catch {
223+
// Swallow errors. This test is verifying that we don't change the
224+
// in-use toolchain if no toolchains are installed
225+
}
213226
}
214227

215228
/// Tests that the `use` command gracefully handles being executed with toolchain names that haven't been installed.

0 commit comments

Comments
 (0)