Skip to content

Commit

Permalink
adds the ability to call simctl erase (#17)
Browse files Browse the repository at this point in the history
* adds the ability to call simctl erase

* update README with new capability

* fixes linting issues
  • Loading branch information
bkbeachlabs authored Feb 3, 2022
1 parent 3b65420 commit 69ebb1c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following commands will be available in code in your (test) targets:
- Rename device
- Trigger iCloud Sync
- Open URLs including registered URL schemes
- Erase the contents and settings of the simulator

## ❔ Why would you (not) use this

Expand Down
14 changes: 14 additions & 0 deletions Sources/Simctl/SimctlClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public class SimctlClient {
dataTask(.terminateApp(env, appBundleIdentifier), completion)
}

/// Reset the contents and settings of the simulator
/// - Parameters:
/// - completion: Result callback of the call. Use this to wait for an expectation to fulfill in a test case.
public func erase(_ completion: @escaping DataTaskCallback) {
dataTask(.erase(env), completion)
}

/// Set the device UI appearance to given appearance
/// - Parameters:
/// - appearance: The appearance - currently light or dark.
Expand Down Expand Up @@ -250,6 +257,7 @@ extension SimctlClient {
case setPrivacy(SimctlClientEnvironment, PrivacyAction, PrivacyService)
case renameDevice(SimctlClientEnvironment, String)
case terminateApp(SimctlClientEnvironment, String)
case erase(SimctlClientEnvironment)
case setDeviceAppearance(SimctlClientEnvironment, DeviceAppearance)
case triggerICloudSync(SimctlClientEnvironment)
case uninstallApp(SimctlClientEnvironment, String)
Expand All @@ -267,6 +275,7 @@ extension SimctlClient {
case .setPrivacy,
.renameDevice,
.terminateApp,
.erase,
.setDeviceAppearance,
.triggerICloudSync,
.uninstallApp,
Expand All @@ -289,6 +298,9 @@ extension SimctlClient {
case .terminateApp:
return .terminateApp

case .erase:
return .erase

case .setDeviceAppearance:
return .deviceAppearance

Expand Down Expand Up @@ -322,6 +334,7 @@ extension SimctlClient {

switch self {
case let .sendPushNotification(env, _),
let .erase(env),
let .triggerICloudSync(env),
let .setStatusBarOverrides(env, _),
let .clearStatusBarOverrides(env),
Expand Down Expand Up @@ -367,6 +380,7 @@ extension SimctlClient {
case .setPrivacy,
.renameDevice,
.terminateApp,
.erase,
.setDeviceAppearance,
.triggerICloudSync,
.uninstallApp,
Expand Down
4 changes: 4 additions & 0 deletions Sources/SimctlCLI/Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ extension ShellOutCommand {
.init(string: simctl("terminate \(device.uuidString) \(appBundleIdentifier)"))
}

static func simctlErase(device: UUID) -> ShellOutCommand {
.init(string: simctl("erase \(device.uuidString)"))
}

/// Trigger iCloud sync on a device.
///
/// Usage: simctl icloud_sync <device>
Expand Down
18 changes: 18 additions & 0 deletions Sources/SimctlCLI/SimctlServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ internal final class SimctlServer {
}
}

func onErase(_ closure: @escaping (UUID) -> Result<String, Swift.Error>) {
server.GET[ServerPath.erase.rawValue] = { request in
guard let deviceId = request.headerValue(for: .deviceUdid, UUID.init) else {
return .badRequest(.text("Device Udid missing or corrupt."))
}

let result = closure(deviceId)

switch result {
case let .success(output):
return .ok(.text(output))

case let .failure(error):
return .badRequest(.text(error.localizedDescription))
}
}
}

func onSetDeviceAppearance(_ closure: @escaping (UUID, String?, DeviceAppearance) -> Result<String, Swift.Error>) {
server.GET[ServerPath.deviceAppearance.rawValue] = { request in
guard let deviceId = request.headerValue(for: .deviceUdid, UUID.init) else {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SimctlCLI/StartServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ struct StartServer: ParsableCommand {
runCommand( .simctlTerminateApp(device: deviceId, appBundleIdentifier: appBundleId), verbose: v)
}

server.onErase { deviceId -> Result<String, Swift.Error> in
runCommand( .simctlErase(device: deviceId), verbose: v)
}

server.onSetDeviceAppearance { deviceId, _, appearance -> Result<String, Swift.Error> in
runCommand(.simctlSetUI(appearance: appearance, on: deviceId), verbose: v)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SimctlShared/SimctlShared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public enum ServerPath: String {
case privacy = "/simctl/setPrivacy"
case renameDevice = "/simctl/renameDevice"
case terminateApp = "/simctl/terminateApp"
case erase = "/simctl/erase"
case deviceAppearance = "/simctl/setDeviceAppearance"
case iCloudSync = "/simctl/iCloudSync"
case uninstallApp = "/simctl/uninstallApp"
Expand Down

0 comments on commit 69ebb1c

Please sign in to comment.