Skip to content

Commit

Permalink
Lowercase all input to be more flexible when calling test/build (#6268)
Browse files Browse the repository at this point in the history
  • Loading branch information
waltflanagan committed May 21, 2024
1 parent a647f6f commit de6dcba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
21 changes: 21 additions & 0 deletions Sources/TuistGraph/Models/Platform.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import Foundation
import TuistSupport

struct UnsupportedPlatformError: FatalError, CustomStringConvertible, Equatable {
let type: TuistSupport.ErrorType = .abort

let input: String
var description: String {
"Specified platform \(input) does not map to any of these supported platforms: \(Platform.allCases.map(\.caseValue).joined(separator: ", ")) "
}
}

public enum Platform: String, CaseIterable, Codable, Comparable {
case iOS = "ios"
Expand All @@ -17,6 +27,17 @@ public enum Platform: String, CaseIterable, Codable, Comparable {
}
}

init?(commandLineValue: String) {
self.init(rawValue: commandLineValue.lowercased())
}

public static func from(commandLineValue: String) throws -> Platform {
guard let platform = Platform(rawValue: commandLineValue.lowercased()) else {
throw UnsupportedPlatformError(input: commandLineValue)
}
return platform
}

public static func < (lhs: Platform, rhs: Platform) -> Bool {
lhs.rawValue < rhs.rawValue
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/TuistKit/Services/BuildService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public final class BuildService {

let buildPlatform: TuistGraph.Platform

if let platform, let inputPlatform = TuistGraph.Platform(rawValue: platform) {
buildPlatform = inputPlatform
if let platform {
buildPlatform = try TuistGraph.Platform.from(commandLineValue: platform)
} else {
buildPlatform = try graphTarget.target.servicePlatform
}
Expand Down Expand Up @@ -141,8 +141,8 @@ public final class BuildService {

let buildPlatform: TuistGraph.Platform

if let platform, let inputPlatform = TuistGraph.Platform(rawValue: platform) {
buildPlatform = inputPlatform
if let platform {
buildPlatform = try TuistGraph.Platform.from(commandLineValue: platform)
} else {
buildPlatform = try graphTarget.target.servicePlatform
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/TuistKit/Services/TestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ public final class TestService { // swiftlint:disable:this type_body_length

let buildPlatform: TuistGraph.Platform

if let platform, let inputPlatform = TuistGraph.Platform(rawValue: platform) {
buildPlatform = inputPlatform
if let platform {
buildPlatform = try TuistGraph.Platform.from(commandLineValue: platform)
} else {
buildPlatform = try buildableTarget.target.servicePlatform
}
Expand Down
32 changes: 32 additions & 0 deletions Tests/TuistGraphTests/Models/PlatformTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ final class PlatformTests: XCTestCase {
XCTAssertCodable(subject)
}

func test_caseInsensitiveCommandInput() {
XCTAssertEqual(Platform.macOS, Platform(commandLineValue: "macos"))
XCTAssertEqual(Platform.macOS, Platform(commandLineValue: "macOS"))
XCTAssertEqual(Platform.macOS, Platform(commandLineValue: "MACOS"))
XCTAssertEqual(Platform.iOS, Platform(commandLineValue: "ios"))
XCTAssertEqual(Platform.iOS, Platform(commandLineValue: "iOS"))
XCTAssertEqual(Platform.iOS, Platform(commandLineValue: "IOS"))
XCTAssertEqual(Platform.tvOS, Platform(commandLineValue: "tvos"))
XCTAssertEqual(Platform.tvOS, Platform(commandLineValue: "tvOS"))
XCTAssertEqual(Platform.watchOS, Platform(commandLineValue: "watchos"))
XCTAssertEqual(Platform.watchOS, Platform(commandLineValue: "watchOS"))
XCTAssertEqual(Platform.visionOS, Platform(commandLineValue: "visionos"))
XCTAssertEqual(Platform.visionOS, Platform(commandLineValue: "visionOS"))
}

func test_caseInvalidPlatform_throws() {
do {
let _ = try Platform.from(commandLineValue: "not_a_platform")
XCTFail("Expected erro to be thrown")
} catch let error as UnsupportedPlatformError {
XCTAssertEqual(error, UnsupportedPlatformError(input: "not_a_platform"))
} catch {
XCTFail("Unexpected error thrown")
}
}

func test_caseValidPlatform_doesNotThrow() throws {
XCTAssertEqual(Platform.iOS, try Platform.from(commandLineValue: "iOS"))
XCTAssertEqual(Platform.macOS, try Platform.from(commandLineValue: "macOS"))
XCTAssertEqual(Platform.macOS, try Platform.from(commandLineValue: "macos"))
}

func test_xcodeSdkRoot_returns_the_right_value() {
XCTAssertEqual(Platform.macOS.xcodeSdkRoot, "macosx")
XCTAssertEqual(Platform.iOS.xcodeSdkRoot, "iphoneos")
Expand Down

0 comments on commit de6dcba

Please sign in to comment.