Implement MockSystem
This commit is contained in:
parent
4b2306bb9a
commit
d1483d385e
|
@ -32,6 +32,12 @@ public struct System3Result {
|
|||
public let stdout: String
|
||||
public let stderror: String
|
||||
public let exitcode: Int
|
||||
public init(stdout: String, stderror: String, exitcode: Int) {
|
||||
self.stdout = stdout
|
||||
self.stderror = stderror
|
||||
self.exitcode = exitcode
|
||||
}
|
||||
|
||||
public func throwIfError() throws {
|
||||
if exitcode != 0 { throw SystemError(stderror: stderror, exitcode: exitcode) }
|
||||
}
|
||||
|
@ -40,6 +46,11 @@ public struct System3Result {
|
|||
public struct System2eResult {
|
||||
public let std: String
|
||||
public let exitcode: Int
|
||||
public init(std: String, exitcode: Int) {
|
||||
self.std = std
|
||||
self.exitcode = exitcode
|
||||
}
|
||||
|
||||
public func throwIfError() throws {
|
||||
if exitcode != 0 { throw SystemError(stderror: nil, exitcode: exitcode) }
|
||||
}
|
||||
|
@ -48,6 +59,11 @@ public struct System2eResult {
|
|||
public struct System2Result {
|
||||
public let stdout: String
|
||||
public let exitcode: Int
|
||||
public init(stdout: String, exitcode: Int) {
|
||||
self.stdout = stdout
|
||||
self.exitcode = exitcode
|
||||
}
|
||||
|
||||
public func throwIfError() throws {
|
||||
if exitcode != 0 { throw SystemError(stderror: nil, exitcode: exitcode) }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
import Foundation
|
||||
import TuistCore
|
||||
|
||||
public final class MockSystem: Systeming {
|
||||
private var stubs: [String: (stderror: String?, stdout: String?, exitstatus: Int?)] = [:]
|
||||
|
||||
public init() {}
|
||||
|
||||
public func stub(args: [String], stderror: String? = nil, stdout: String? = nil, exitstatus: Int? = nil) {
|
||||
stubs[args.joined(separator: " ")] = (stderror: stderror, stdout: stdout, exitstatus: exitstatus)
|
||||
}
|
||||
|
||||
public func capture2(_ args: [String], verbose _: Bool) -> System2Result {
|
||||
let command = args.joined(separator: " ")
|
||||
if let stub = stubs[command] {
|
||||
return System2Result(stdout: stub.stdout ?? "", exitcode: stub.exitstatus ?? -1)
|
||||
} else {
|
||||
return System2Result(stdout: "", exitcode: -1)
|
||||
}
|
||||
}
|
||||
|
||||
public func capture2(_ args: String..., verbose: Bool) -> System2Result {
|
||||
return capture2(args, verbose: verbose)
|
||||
}
|
||||
|
||||
public func capture2e(_ args: [String], verbose _: Bool) -> System2eResult {
|
||||
let command = args.joined(separator: " ")
|
||||
if let stub = stubs[command] {
|
||||
return System2eResult(std: stub.stdout ?? stub.stderror ?? "", exitcode: stub.exitstatus ?? -1)
|
||||
} else {
|
||||
return System2eResult(std: "", exitcode: -1)
|
||||
}
|
||||
}
|
||||
|
||||
public func capture2e(_ args: String..., verbose: Bool) -> System2eResult {
|
||||
return capture2e(args, verbose: verbose)
|
||||
}
|
||||
|
||||
public func capture3(_ args: [String], verbose _: Bool) -> System3Result {
|
||||
let command = args.joined(separator: " ")
|
||||
if let stub = stubs[command] {
|
||||
return System3Result(stdout: stub.stdout ?? "", stderror: stub.stderror ?? "", exitcode: stub.exitstatus ?? -1)
|
||||
} else {
|
||||
return System3Result(stdout: "", stderror: "", exitcode: -1)
|
||||
}
|
||||
}
|
||||
|
||||
public func capture3(_ args: String..., verbose: Bool) -> System3Result {
|
||||
return capture3(args, verbose: verbose)
|
||||
}
|
||||
|
||||
public func popen(_ args: String..., printing: Bool, verbose: Bool, onOutput: ((String) -> Void)?, onError: ((String) -> Void)?, onCompletion: ((Int) -> Void)?) {
|
||||
popen(args, printing: printing, verbose: verbose, onOutput: onOutput, onError: onError, onCompletion: onCompletion)
|
||||
}
|
||||
|
||||
public func popen(_ args: [String], printing _: Bool, verbose _: Bool, onOutput: ((String) -> Void)?, onError: ((String) -> Void)?, onCompletion: ((Int) -> Void)?) {
|
||||
let command = args.joined(separator: " ")
|
||||
if let stub = stubs[command] {
|
||||
if let stdout = stub.stdout {
|
||||
onOutput?(stdout)
|
||||
}
|
||||
if let stderror = stub.stderror {
|
||||
onError?(stderror)
|
||||
}
|
||||
onCompletion?(stub.exitstatus ?? -1)
|
||||
} else {
|
||||
onCompletion?(-1)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -39,7 +39,8 @@ final class Installer: Installing {
|
|||
|
||||
func install(version: String,
|
||||
temporaryDirectory: TemporaryDirectory,
|
||||
verbose: Bool = false) throws {
|
||||
verbose: Bool = false,
|
||||
printing: Bool = true) throws {
|
||||
try versionsController.install(version: version) { installationDirectory in
|
||||
// Paths
|
||||
let buildDirectory = temporaryDirectory.path.appending(RelativePath(".build/release/"))
|
||||
|
@ -50,11 +51,11 @@ final class Installer: Installing {
|
|||
}
|
||||
|
||||
// Cloning and building
|
||||
printer.print("Cloning repository")
|
||||
if printing { printer.print("Cloning repository") }
|
||||
try system.capture3("git", "clone", Constants.gitRepositorySSH, temporaryDirectory.path.asString, verbose: verbose).throwIfError()
|
||||
printer.print("Checking out \(version) reference")
|
||||
if printing { printer.print("Checking out \(version) reference") }
|
||||
try system.capture3("git", "-C", temporaryDirectory.path.asString, "checkout", version, verbose: verbose).throwIfError()
|
||||
printer.print("Building using Swift (it might take a while)")
|
||||
if printing { printer.print("Building using Swift (it might take a while)") }
|
||||
try system.capture3("swift", "build", "--package-path", temporaryDirectory.path.asString, "--configuration", "release", verbose: verbose).throwIfError()
|
||||
|
||||
// Copying built files
|
||||
|
@ -66,7 +67,7 @@ final class Installer: Installing {
|
|||
let tuistVersionPath = installationDirectory.appending(component: Constants.versionFileName)
|
||||
try "\(version)".write(to: tuistVersionPath.url, atomically: true, encoding: .utf8)
|
||||
|
||||
printer.print(success: "Version \(version) installed.")
|
||||
if printing { printer.print("Version \(version) installed.") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue