Skip to content

Commit

Permalink
Minor refactor of options
Browse files Browse the repository at this point in the history
  • Loading branch information
iainsmith committed Apr 24, 2020
1 parent 8dc1a97 commit f8a31ee
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
13 changes: 8 additions & 5 deletions Sources/SwiftDockerLib/CLIOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public struct CLIOptions: ParsableArguments {
@Flag(name: .shortAndLong, help: "Increase the level of output")
var verbose: Bool

@Flag(name: .customLong("skip-validation"), help: .hidden)
var skipValidation: Bool

var absolutePath: AbsolutePath {
try! AbsolutePath(validating: url.path)
}
Expand All @@ -44,9 +41,15 @@ public struct CLIOptions: ParsableArguments {

public init() {}

public func validate() throws {
if skipValidation { return }
public init(swift: String = "latest", image: String? = nil, path: String = ".", verbose: Bool) {
self.swift = swift
self.image = image
let expanded = NSString(string: path).expandingTildeInPath
self.url = URL(fileURLWithPath: expanded)
self.verbose = verbose
}

public func validate() throws {
if swift != "latest", image != nil {
throw ValidationError("--swift and --image are exclusive options")
}
Expand Down
8 changes: 0 additions & 8 deletions Sources/SwiftDockerLib/Process.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/SwiftDockerLibTests/BuildCommandUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import XCTest

class TestCommandUnitTests: XCTestCase {
func testSuccessfullBuildAndTest() throws {
let options = try CLIOptions.parse(["--swift", "5.2", "--path", "/hello/my-project", "-v", "--skip-validation"])
let options = CLIOptions(swift: "5.2", path: "/hello/my-project", verbose: true)
let output = MockOutput()
let shell = MockShell.self
shell.clear()
Expand Down
16 changes: 8 additions & 8 deletions Tests/SwiftDockerLibTests/CLIOptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@ import XCTest
extension CLIOptions {
static func parseWithoutValidation(_ args: [String]? = nil) throws -> CLIOptions {
let fullArgs = args ?? []
return try parse(fullArgs + ["--skip-validation"])
return try parse(fullArgs)
}
}

class CLIOptionTests: XCTestCase {
func testDefaultDockerTag() throws {
let options = try CLIOptions.parseWithoutValidation()
let options = CLIOptions(path: ".", verbose: true)
XCTAssertEqual(options.dockerBaseImage, DockerTag.officialSwiftVersion("latest"))
}

func testCustomSwiftVersion() throws {
let options = try CLIOptions.parseWithoutValidation(["--swift", "5.1"])
let options = CLIOptions(swift: "5.1", verbose: true)
XCTAssertEqual(options.dockerBaseImage, DockerTag.officialSwiftVersion("5.1"))
}

func testCustomImage() throws {
let options = try CLIOptions.parseWithoutValidation(["--image", "vapor/ubuntu:latest"])
let options = CLIOptions(image: "vapor/ubuntu:latest", verbose: true)
XCTAssertEqual(options.dockerBaseImage, DockerTag.image("vapor/ubuntu:latest"))
}

func testCustomAbsolutePath() throws {
let options = try CLIOptions.parseWithoutValidation(["--path", "/tmp/hello/world"])
let options = CLIOptions(path: "/tmp/hello/world", verbose: true)
XCTAssertEqual(options.absolutePath, AbsolutePath("/tmp/hello/world"))
}

func testCustomTildaPath() throws {
let options = try CLIOptions.parseWithoutValidation(["--path", "~/hello/world"])
let options = CLIOptions(path: "~/hello/world", verbose: true)
XCTAssertEqual(options.absolutePath, AbsolutePath("hello/world", relativeTo: localFileSystem.homeDirectory))
}

func testRelativePath() throws {
let options = try CLIOptions.parseWithoutValidation(["--path", "../../hello/world"])
let options = CLIOptions(path: "../../hello/world", verbose: true)
let currentDir = localFileSystem.currentWorkingDirectory!
XCTAssertEqual(options.absolutePath, AbsolutePath("hello/world", relativeTo: currentDir.parentDirectory.parentDirectory))
}

func testProjectName() throws {
let options = try CLIOptions.parseWithoutValidation(["--path", "../../hello/world"])
let options = CLIOptions(path: "../../hello/world", verbose: true)
XCTAssertEqual(options.projectName, "world")
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDockerLibTests/TestCommandUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import XCTest

class BuiildCommandUnitTests: XCTestCase {
func testSuccessfullBuild() throws {
let options = try CLIOptions.parse(["--swift", "5.2", "--path", "/hello/my-Project", "-v", "--skip-validation"])
let options = CLIOptions(swift: "5.2", path: "/hello/my-Project", verbose: true)
let fileSystem = InMemoryFileSystem()
try fileSystem.createDirectory(AbsolutePath("/tmp"))
let output = MockOutput()
Expand Down

0 comments on commit f8a31ee

Please sign in to comment.