Add Simctl that wraps xcrun simctl calls

This commit is contained in:
Simon Kågedal Reimer 2019-05-04 20:19:15 +02:00
parent 951280361d
commit a6052d167d
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,28 @@
import Foundation
import Basic
import SPMUtility
struct Simctl {
enum Error: Swift.Error {
case listDevices(errorOutput: String)
}
static func listDevices() throws -> Data {
let process = Process(
args: "xcrun", "simctl", "list", "--json", "devices"
)
try process.launch()
let result = try process.waitUntilExit()
guard result.exitStatus == .terminated(code: 0) else {
let errorOutput = try (result.utf8Output() + result.utf8stderrOutput()).spm_chuzzle() ?? ""
throw Error.listDevices(errorOutput: errorOutput)
}
switch result.output {
case .success(let bytes):
return Data(bytes)
case .failure(let error):
throw error
}
}
}

View File

@ -0,0 +1,16 @@
import Foundation
import XCTest
@testable import XcodeSimulatorKit
class SimctlTests: XCTestCase {
func testListDevices() throws {
let jsonData = try Simctl.listDevices()
let obj = try JSONSerialization.jsonObject(with: jsonData, options: [])
XCTAssertTrue(obj is [String: Any])
guard let dict = obj as? [String: Any] else {
return
}
XCTAssertEqual(dict.keys.count, 1)
XCTAssertEqual(dict.keys.first, "devices")
}
}