From 6174769023a148afe3d5a9b37e0988d78cc6b080 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Wed, 11 Oct 2023 01:22:20 -0400 Subject: [PATCH 1/4] add --use flag to install --- Sources/Swiftly/Install.swift | 12 ++++++++---- Sources/Swiftly/Update.swift | 2 +- Tests/SwiftlyTests/InstallTests.swift | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Sources/Swiftly/Install.swift b/Sources/Swiftly/Install.swift index 80701754..636c20e5 100644 --- a/Sources/Swiftly/Install.swift +++ b/Sources/Swiftly/Install.swift @@ -43,6 +43,9 @@ struct Install: SwiftlyCommand { )) var version: String + @Flag(name: .shortAndLong, help: "Whether to mark the newly installed toolchain as in-use.") + var use: Bool = false + @Option(help: ArgumentHelp( "A GitHub authentiation token to use for any GitHub API requests.", discussion: """ @@ -57,10 +60,10 @@ struct Install: SwiftlyCommand { let selector = try ToolchainSelector(parsing: self.version) HTTP.githubToken = self.token let toolchainVersion = try await self.resolve(selector: selector) - try await Self.execute(version: toolchainVersion) + try await Self.execute(version: toolchainVersion, use: self.use) } - internal static func execute(version: ToolchainVersion) async throws { + internal static func execute(version: ToolchainVersion, use: Bool) async throws { var config = try Config.load() guard !config.installedToolchains.contains(version) else { @@ -158,8 +161,9 @@ struct Install: SwiftlyCommand { config.installedToolchains.insert(version) try config.save() - // If this is the first installed toolchain, mark it as in-use. - if config.inUse == nil { + // If this is the first installed toolchain, mark it as in-use regardless of whether the + // --use argument was provided. + if use || config.inUse == nil { try await Use.execute(version, config: &config) } diff --git a/Sources/Swiftly/Update.swift b/Sources/Swiftly/Update.swift index 575d2d10..db0f42e8 100644 --- a/Sources/Swiftly/Update.swift +++ b/Sources/Swiftly/Update.swift @@ -56,7 +56,7 @@ struct Update: SwiftlyCommand { } print("updating \(oldToolchain) -> \(newToolchain)") - try await Install.execute(version: newToolchain) + try await Install.execute(version: newToolchain, use: false) try Swiftly.currentPlatform.uninstall(oldToolchain) print("successfully updated \(oldToolchain) -> \(newToolchain)") } diff --git a/Tests/SwiftlyTests/InstallTests.swift b/Tests/SwiftlyTests/InstallTests.swift index 90495e35..058fa5b5 100644 --- a/Tests/SwiftlyTests/InstallTests.swift +++ b/Tests/SwiftlyTests/InstallTests.swift @@ -255,4 +255,18 @@ final class InstallTests: SwiftlyTests { try await self.validateInUse(expected: ToolchainVersion(major: 5, minor: 7, patch: 0)) } } + + /// Verify that the installed toolchain will be marked as in-use if the --use flag is specified. + func testInstallUseFlag() async throws { + try await self.withTestHome { + try self.installMockedToolchain(toolchain: Self.oldStable) + var use = try self.parseCommand(Use.self, ["use", Self.oldStable.name]) + try await use.run() + try await validateInUse(expected: Self.oldStable) + + var installOther = try self.parseCommand(Install.self, ["install", "--use", Self.newStable.name]) + try await installOther.run() + try await self.validateInUse(expected: Self.newStable) + } + } } From 87650092d72db5468f0fae95011d72bdd0f064e7 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Wed, 11 Oct 2023 01:25:00 -0400 Subject: [PATCH 2/4] improve help text --- Sources/Swiftly/Install.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Swiftly/Install.swift b/Sources/Swiftly/Install.swift index 636c20e5..26c22261 100644 --- a/Sources/Swiftly/Install.swift +++ b/Sources/Swiftly/Install.swift @@ -43,7 +43,7 @@ struct Install: SwiftlyCommand { )) var version: String - @Flag(name: .shortAndLong, help: "Whether to mark the newly installed toolchain as in-use.") + @Flag(name: .shortAndLong, help: "Mark the newly installed toolchain as in-use.") var use: Bool = false @Option(help: ArgumentHelp( From deca0a7a6dc7d6d8492081d9300ea23b3bd4e5f3 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Fri, 3 Nov 2023 00:18:47 -0400 Subject: [PATCH 3/4] adopt use flag in update --- Sources/Swiftly/Install.swift | 4 ++-- Sources/Swiftly/Update.swift | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/Swiftly/Install.swift b/Sources/Swiftly/Install.swift index 6279da9c..6ec60cd3 100644 --- a/Sources/Swiftly/Install.swift +++ b/Sources/Swiftly/Install.swift @@ -67,7 +67,7 @@ struct Install: SwiftlyCommand { self.httpClient.githubToken = self.token let toolchainVersion = try await self.resolve(selector: selector) var config = try Config.load() - try await Self.execute(version: toolchainVersion, config, self.httpClient, use: self.use) + try await Self.execute(version: toolchainVersion, &config, self.httpClient, useInstalledToolchain: self.use) } internal static func execute( @@ -174,7 +174,7 @@ struct Install: SwiftlyCommand { // If this is the first installed toolchain, mark it as in-use regardless of whether the // --use argument was provided. - if use || config.inUse == nil { + if useInstalledToolchain || config.inUse == nil { try await Use.execute(version, &config) } diff --git a/Sources/Swiftly/Update.swift b/Sources/Swiftly/Update.swift index c58270db..96d6c6d5 100644 --- a/Sources/Swiftly/Update.swift +++ b/Sources/Swiftly/Update.swift @@ -100,11 +100,12 @@ struct Update: SwiftlyCommand { } } - try await Install.execute(version: newToolchain, &config, self.httpClient, useInstalledToolchain: false) - - if config.inUse == parameters.oldToolchain { - try await Use.execute(newToolchain, &config) - } + try await Install.execute( + version: newToolchain, + &config, + self.httpClient, + useInstalledToolchain: config.inUse == parameters.oldToolchain + ) try await Uninstall.execute(parameters.oldToolchain, &config) SwiftlyCore.print("Successfully updated \(parameters.oldToolchain) ⟶ \(newToolchain)") From 14760c21171cc21eb24eae6952809a0db3914f89 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Fri, 3 Nov 2023 00:24:38 -0400 Subject: [PATCH 4/4] mock install --use test --- Sources/Swiftly/Install.swift | 2 +- Tests/SwiftlyTests/InstallTests.swift | 6 ++---- Tests/SwiftlyTests/SwiftlyTests.swift | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Sources/Swiftly/Install.swift b/Sources/Swiftly/Install.swift index 6ec60cd3..bd5e1f68 100644 --- a/Sources/Swiftly/Install.swift +++ b/Sources/Swiftly/Install.swift @@ -59,7 +59,7 @@ struct Install: SwiftlyCommand { public var httpClient = SwiftlyHTTPClient() private enum CodingKeys: String, CodingKey { - case version, token + case version, token, use } mutating func run() async throws { diff --git a/Tests/SwiftlyTests/InstallTests.swift b/Tests/SwiftlyTests/InstallTests.swift index 058fa5b5..04fc5ee6 100644 --- a/Tests/SwiftlyTests/InstallTests.swift +++ b/Tests/SwiftlyTests/InstallTests.swift @@ -259,13 +259,11 @@ final class InstallTests: SwiftlyTests { /// Verify that the installed toolchain will be marked as in-use if the --use flag is specified. func testInstallUseFlag() async throws { try await self.withTestHome { - try self.installMockedToolchain(toolchain: Self.oldStable) + try await self.installMockedToolchain(toolchain: Self.oldStable) var use = try self.parseCommand(Use.self, ["use", Self.oldStable.name]) try await use.run() try await validateInUse(expected: Self.oldStable) - - var installOther = try self.parseCommand(Install.self, ["install", "--use", Self.newStable.name]) - try await installOther.run() + try await self.installMockedToolchain(selector: Self.newStable.name, args: ["--use"]) try await self.validateInUse(expected: Self.newStable) } } diff --git a/Tests/SwiftlyTests/SwiftlyTests.swift b/Tests/SwiftlyTests/SwiftlyTests.swift index 266f147b..605a5631 100644 --- a/Tests/SwiftlyTests/SwiftlyTests.swift +++ b/Tests/SwiftlyTests/SwiftlyTests.swift @@ -172,8 +172,8 @@ class SwiftlyTests: XCTestCase { /// in its bin directory. /// /// When executed, the mocked executables will simply print the toolchain version and return. - func installMockedToolchain(selector: String, executables: [String]? = nil) async throws { - var install = try self.parseCommand(Install.self, ["install", "\(selector)"]) + func installMockedToolchain(selector: String, args: [String] = [], executables: [String]? = nil) async throws { + var install = try self.parseCommand(Install.self, ["install", "\(selector)"] + args) install.httpClient = SwiftlyHTTPClient(toolchainDownloader: MockToolchainDownloader(executables: executables)) try await install.run() }