Skip to content

Commit 9b10bb0

Browse files
committed
Improve error message when using xcode select without xcode install
1 parent aa795f8 commit 9b10bb0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Sources/XcodesKit/XcodeSelect.swift

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public func selectXcode(shouldPrint: Bool, pathOrVersion: String, directory: Pat
2424

2525
let versionToSelect = pathOrVersion.isEmpty ? Version.fromXcodeVersionFile() : Version(xcodeVersion: pathOrVersion)
2626
let installedXcodes = Current.files.installedXcodes(directory)
27+
28+
if installedXcodes.isEmpty {
29+
Current.logging.log("No Xcode version installed. Please run 'xcodes install' and try again.".red)
30+
Current.shell.exit(1)
31+
return Promise(error: XcodeSelectError.noInstalledXcodes)
32+
}
33+
2734
if let version = versionToSelect,
2835
let installedXcode = installedXcodes.first(withVersion: version) {
2936
let selectedInstalledXcodeVersion = installedXcodes.first { output.out.hasPrefix($0.path.string) }.map { $0.version }
@@ -153,13 +160,16 @@ public func selectXcodeAtPath(_ pathString: String) -> Promise<ProcessOutput> {
153160
public enum XcodeSelectError: LocalizedError {
154161
case invalidPath(String)
155162
case invalidIndex(min: Int, max: Int, given: String?)
163+
case noInstalledXcodes
156164

157165
public var errorDescription: String? {
158166
switch self {
159167
case .invalidPath(let pathString):
160168
return "Not a valid Xcode path: \(pathString)"
161169
case .invalidIndex(let min, let max, let given):
162170
return "Not a valid number. Expecting a whole number between \(min)-\(max), but given \(given ?? "nothing")."
171+
case .noInstalledXcodes:
172+
return "No Xcode version installed. Please run 'xcodes install' and try again."
163173
}
164174
}
165175
}

Tests/XcodesKitTests/XcodesKitTests.swift

+16
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,22 @@ final class XcodesKitTests: XCTestCase {
954954
""")
955955
}
956956

957+
func test_SelectXcode_WithNoInstalledVersions_LogsError() {
958+
var log = ""
959+
Current.files.installedXcodes = { _ in [] } // Simulate no installed Xcodes
960+
XcodesKit.Current.logging.log = { log = $0 }
961+
962+
let expectation = XCTestExpectation(description: "Select Xcode")
963+
selectXcode(shouldPrint: false, pathOrVersion: "", directory: Path.root.join("Applications"))
964+
.ensure {
965+
XCTAssertEqual(log, "No Xcode version installed. Please run 'xcodes install' and try again.")
966+
expectation.fulfill()
967+
}
968+
.cauterize()
969+
970+
wait(for: [expectation], timeout: 0.5)
971+
}
972+
957973
func test_SelectPath() {
958974
var log = ""
959975
XcodesKit.Current.logging.log = { log.append($0 + "\n") }

0 commit comments

Comments
 (0)