Skip to content

Commit

Permalink
Improve error message when using xcode select without xcode install
Browse files Browse the repository at this point in the history
  • Loading branch information
ngimelliUW committed Mar 4, 2024
1 parent aa795f8 commit 922007b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Sources/XcodesKit/XcodeSelect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public func selectXcode(shouldPrint: Bool, pathOrVersion: String, directory: Pat

let versionToSelect = pathOrVersion.isEmpty ? Version.fromXcodeVersionFile() : Version(xcodeVersion: pathOrVersion)
let installedXcodes = Current.files.installedXcodes(directory)

if installedXcodes.isEmpty {
Current.logging.log("No Xcode version installed. Please run 'xcodes install' and try again".red)
Current.shell.exit(1)
return Promise(error: XcodeSelectError.noInstalledXcodes)
}

if let version = versionToSelect,
let installedXcode = installedXcodes.first(withVersion: version) {
let selectedInstalledXcodeVersion = installedXcodes.first { output.out.hasPrefix($0.path.string) }.map { $0.version }
Expand Down Expand Up @@ -153,13 +160,16 @@ public func selectXcodeAtPath(_ pathString: String) -> Promise<ProcessOutput> {
public enum XcodeSelectError: LocalizedError {
case invalidPath(String)
case invalidIndex(min: Int, max: Int, given: String?)
case noInstalledXcodes

public var errorDescription: String? {
switch self {
case .invalidPath(let pathString):
return "Not a valid Xcode path: \(pathString)"
"Not a valid Xcode path: \(pathString)"
case .invalidIndex(let min, let max, let given):
return "Not a valid number. Expecting a whole number between \(min)-\(max), but given \(given ?? "nothing")."
"Not a valid number. Expecting a whole number between \(min)-\(max), but given \(given ?? "nothing")."
case .noInstalledXcodes:
"No Xcode version installed. Please run 'xcodes install' and try again."
}
}
}
16 changes: 16 additions & 0 deletions Tests/XcodesKitTests/XcodesKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,22 @@ final class XcodesKitTests: XCTestCase {
""")
}

func test_SelectXcode_WithNoInstalledVersions_LogsError() {
var log = ""
Current.files.installedXcodes = { _ in [] } // Simulate no installed Xcodes
XcodesKit.Current.logging.log = { log = $0 }

let expectation = XCTestExpectation(description: "Select Xcode")
selectXcode(shouldPrint: false, pathOrVersion: "", directory: Path.root.join("Applications"))
.ensure {
XCTAssertEqual(log, "No Xcode version installed. Please run 'xcodes install' and try again")
expectation.fulfill()
}
.cauterize()

wait(for: [expectation], timeout: 0.5)
}

func test_SelectPath() {
var log = ""
XcodesKit.Current.logging.log = { log.append($0 + "\n") }
Expand Down

0 comments on commit 922007b

Please sign in to comment.