Skip to content

Improve error message when using xcodes select without Xcode installed #351

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 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)"
case .invalidIndex(let min, let max, let given):
return "Not a valid number. Expecting a whole number between \(min)-\(max), but given \(given ?? "nothing")."
case .noInstalledXcodes:
return "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