Skip to content

Commit 20c3653

Browse files
committed
Ensure --use will select the toolchain
If a toolchain was already installed when running `swiftly install --use`, said toolchain was not being set to be used as a different code path was followed. Update the code path to set the toolchain when `--use` is selected even if the toolchain is already installed.
1 parent 4a030e1 commit 20c3653

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Sources/Swiftly/Install.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,16 @@ struct Install: SwiftlyCommand {
254254
let installInfo = InstallInfo(
255255
version: version, alreadyInstalled: true
256256
)
257+
// If this is the first installed toolchain, mark it as in-use regardless of whether the
258+
// --use argument was provided.
259+
let pathChanged = if useInstalledToolchain {
260+
try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
261+
} else {
262+
false
263+
}
264+
257265
try await ctx.output(installInfo)
258-
return (nil, false)
266+
return (nil, pathChanged)
259267
}
260268

261269
// Ensure the system is set up correctly before downloading it. Problems that prevent installation

Tests/SwiftlyTests/InstallTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,25 @@ import Testing
264264
try await SwiftlyTests.validateInUse(expected: .newStable)
265265
}
266266

267+
/// Verify that the pre-installed toolchain will be marked as in-use if the --use flag is specified.
268+
@Test(.mockedSwiftlyVersion(), .testHomeMockedToolchain()) func installUseFlagOnAlreadyInstalledToolchain() async throws {
269+
// GIVEN we install an old toolchain
270+
try await SwiftlyTests.installMockedToolchain(toolchain: .oldStable)
271+
// AND we use the toolchain
272+
try await SwiftlyTests.runCommand(Use.self, ["use", ToolchainVersion.oldStable.name])
273+
// THEN the old toolchain should be in use/selected
274+
try await SwiftlyTests.validateInUse(expected: .oldStable)
275+
// GIVEN a new toolchain is installled without `--use`
276+
try await SwiftlyTests.installMockedToolchain(selector: ToolchainVersion.newStable.name)
277+
// THEN the old toolchain is in use/selected
278+
try await SwiftlyTests.validateInUse(expected: .oldStable)
279+
280+
// AND GIVEN we installing the new toolchain again with `-use`
281+
try await SwiftlyTests.installMockedToolchain(selector: ToolchainVersion.newStable.name, args: ["--use"])
282+
// THEN the new toolchain should be in use/selected
283+
try await SwiftlyTests.validateInUse(expected: .newStable)
284+
}
285+
267286
/// Verify that xcode can't be installed like regular toolchains
268287
@Test(.testHomeMockedToolchain()) func installXcode() async throws {
269288
try await #expect(throws: SwiftlyError.self) {

0 commit comments

Comments
 (0)