Skip to content

Print the currently in-use toolchain if no argument is provided to use #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions Sources/Swiftly/Use.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import SwiftlyCore

internal struct Use: SwiftlyCommand {
public static var configuration = CommandConfiguration(
abstract: "Set the active toolchain."
abstract: "Set the active toolchain. If no toolchain is provided, print the currently in-use toolchain, if any."
)

@Argument(help: ArgumentHelp(
"The toolchain to use.",
discussion: """

If no toolchain is provided, the currently in-use toolchain will be printed, if any:

$ swiftly use

The string "latest" can be provided to use the most recent stable version release:

$ swiftly use latest
Expand All @@ -32,18 +36,26 @@ internal struct Use: SwiftlyCommand {
Likewise, the latest snapshot associated with a given development branch can be \
used by omitting the date:

$ swiftly install 5.7-snapshot
$ swiftly install main-snapshot
$ swiftly use 5.7-snapshot
$ swiftly use main-snapshot
"""
))
var toolchain: String
var toolchain: String?

internal mutating func run() async throws {
let selector = try ToolchainSelector(parsing: self.toolchain)
var config = try Config.load()

guard let toolchain = self.toolchain else {
if let inUse = config.inUse {
SwiftlyCore.print("\(inUse) (in use)")
}
return
}

let selector = try ToolchainSelector(parsing: toolchain)

guard let toolchain = config.listInstalledToolchains(selector: selector).max() else {
SwiftlyCore.print("No installed toolchains match \"\(self.toolchain)\"")
SwiftlyCore.print("No installed toolchains match \"\(toolchain)\"")
return
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/SwiftlyTests/UseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,24 @@ final class UseTests: SwiftlyTests {
XCTAssertEqual(yConfig.inUse, toolchain)
}
}

/// Tests that running a use command without an argument prints the currently in-use toolchain.
func testPrintInUse() async throws {
let toolchains = [
Self.newStable,
Self.newMainSnapshot,
Self.newReleaseSnapshot,
]
try await self.withMockedHome(homeName: Self.homeName, toolchains: Set(toolchains)) {
for toolchain in toolchains {
var use = try self.parseCommand(Use.self, ["use", toolchain.name])
try await use.run()

var useEmpty = try self.parseCommand(Use.self, ["use"])
let output = try await useEmpty.runWithMockedIO()

XCTAssert(output.contains(where: { $0.contains(String(describing: toolchain)) }))
}
}
}
}