Skip to content

Commit

Permalink
Print the currently in-use toolchain if no argument is provided to use (
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfreed committed Oct 14, 2023
1 parent acaf22d commit fa5edf7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
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)) }))
}
}
}
}

0 comments on commit fa5edf7

Please sign in to comment.