Skip to content

Commit fa5edf7

Browse files
committed
Print the currently in-use toolchain if no argument is provided to use (swiftlang#75)
1 parent acaf22d commit fa5edf7

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

Sources/Swiftly/Use.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import SwiftlyCore
33

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

99
@Argument(help: ArgumentHelp(
1010
"The toolchain to use.",
1111
discussion: """
1212
13+
If no toolchain is provided, the currently in-use toolchain will be printed, if any:
14+
15+
$ swiftly use
16+
1317
The string "latest" can be provided to use the most recent stable version release:
1418
1519
$ swiftly use latest
@@ -32,18 +36,26 @@ internal struct Use: SwiftlyCommand {
3236
Likewise, the latest snapshot associated with a given development branch can be \
3337
used by omitting the date:
3438
35-
$ swiftly install 5.7-snapshot
36-
$ swiftly install main-snapshot
39+
$ swiftly use 5.7-snapshot
40+
$ swiftly use main-snapshot
3741
"""
3842
))
39-
var toolchain: String
43+
var toolchain: String?
4044

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

48+
guard let toolchain = self.toolchain else {
49+
if let inUse = config.inUse {
50+
SwiftlyCore.print("\(inUse) (in use)")
51+
}
52+
return
53+
}
54+
55+
let selector = try ToolchainSelector(parsing: toolchain)
56+
4557
guard let toolchain = config.listInstalledToolchains(selector: selector).max() else {
46-
SwiftlyCore.print("No installed toolchains match \"\(self.toolchain)\"")
58+
SwiftlyCore.print("No installed toolchains match \"\(toolchain)\"")
4759
return
4860
}
4961

Tests/SwiftlyTests/UseTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,24 @@ final class UseTests: SwiftlyTests {
306306
XCTAssertEqual(yConfig.inUse, toolchain)
307307
}
308308
}
309+
310+
/// Tests that running a use command without an argument prints the currently in-use toolchain.
311+
func testPrintInUse() async throws {
312+
let toolchains = [
313+
Self.newStable,
314+
Self.newMainSnapshot,
315+
Self.newReleaseSnapshot,
316+
]
317+
try await self.withMockedHome(homeName: Self.homeName, toolchains: Set(toolchains)) {
318+
for toolchain in toolchains {
319+
var use = try self.parseCommand(Use.self, ["use", toolchain.name])
320+
try await use.run()
321+
322+
var useEmpty = try self.parseCommand(Use.self, ["use"])
323+
let output = try await useEmpty.runWithMockedIO()
324+
325+
XCTAssert(output.contains(where: { $0.contains(String(describing: toolchain)) }))
326+
}
327+
}
328+
}
309329
}

0 commit comments

Comments
 (0)