Skip to content

Commit 69ebb1c

Browse files
authored
adds the ability to call simctl erase (#17)
* adds the ability to call simctl erase * update README with new capability * fixes linting issues
1 parent 3b65420 commit 69ebb1c

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The following commands will be available in code in your (test) targets:
4141
- Rename device
4242
- Trigger iCloud Sync
4343
- Open URLs including registered URL schemes
44+
- Erase the contents and settings of the simulator
4445

4546
## ❔ Why would you (not) use this
4647

Sources/Simctl/SimctlClient.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public class SimctlClient {
8080
dataTask(.terminateApp(env, appBundleIdentifier), completion)
8181
}
8282

83+
/// Reset the contents and settings of the simulator
84+
/// - Parameters:
85+
/// - completion: Result callback of the call. Use this to wait for an expectation to fulfill in a test case.
86+
public func erase(_ completion: @escaping DataTaskCallback) {
87+
dataTask(.erase(env), completion)
88+
}
89+
8390
/// Set the device UI appearance to given appearance
8491
/// - Parameters:
8592
/// - appearance: The appearance - currently light or dark.
@@ -250,6 +257,7 @@ extension SimctlClient {
250257
case setPrivacy(SimctlClientEnvironment, PrivacyAction, PrivacyService)
251258
case renameDevice(SimctlClientEnvironment, String)
252259
case terminateApp(SimctlClientEnvironment, String)
260+
case erase(SimctlClientEnvironment)
253261
case setDeviceAppearance(SimctlClientEnvironment, DeviceAppearance)
254262
case triggerICloudSync(SimctlClientEnvironment)
255263
case uninstallApp(SimctlClientEnvironment, String)
@@ -267,6 +275,7 @@ extension SimctlClient {
267275
case .setPrivacy,
268276
.renameDevice,
269277
.terminateApp,
278+
.erase,
270279
.setDeviceAppearance,
271280
.triggerICloudSync,
272281
.uninstallApp,
@@ -289,6 +298,9 @@ extension SimctlClient {
289298
case .terminateApp:
290299
return .terminateApp
291300

301+
case .erase:
302+
return .erase
303+
292304
case .setDeviceAppearance:
293305
return .deviceAppearance
294306

@@ -322,6 +334,7 @@ extension SimctlClient {
322334

323335
switch self {
324336
case let .sendPushNotification(env, _),
337+
let .erase(env),
325338
let .triggerICloudSync(env),
326339
let .setStatusBarOverrides(env, _),
327340
let .clearStatusBarOverrides(env),
@@ -367,6 +380,7 @@ extension SimctlClient {
367380
case .setPrivacy,
368381
.renameDevice,
369382
.terminateApp,
383+
.erase,
370384
.setDeviceAppearance,
371385
.triggerICloudSync,
372386
.uninstallApp,

Sources/SimctlCLI/Commands.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ extension ShellOutCommand {
9696
.init(string: simctl("terminate \(device.uuidString) \(appBundleIdentifier)"))
9797
}
9898

99+
static func simctlErase(device: UUID) -> ShellOutCommand {
100+
.init(string: simctl("erase \(device.uuidString)"))
101+
}
102+
99103
/// Trigger iCloud sync on a device.
100104
///
101105
/// Usage: simctl icloud_sync <device>

Sources/SimctlCLI/SimctlServer.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ internal final class SimctlServer {
155155
}
156156
}
157157

158+
func onErase(_ closure: @escaping (UUID) -> Result<String, Swift.Error>) {
159+
server.GET[ServerPath.erase.rawValue] = { request in
160+
guard let deviceId = request.headerValue(for: .deviceUdid, UUID.init) else {
161+
return .badRequest(.text("Device Udid missing or corrupt."))
162+
}
163+
164+
let result = closure(deviceId)
165+
166+
switch result {
167+
case let .success(output):
168+
return .ok(.text(output))
169+
170+
case let .failure(error):
171+
return .badRequest(.text(error.localizedDescription))
172+
}
173+
}
174+
}
175+
158176
func onSetDeviceAppearance(_ closure: @escaping (UUID, String?, DeviceAppearance) -> Result<String, Swift.Error>) {
159177
server.GET[ServerPath.deviceAppearance.rawValue] = { request in
160178
guard let deviceId = request.headerValue(for: .deviceUdid, UUID.init) else {

Sources/SimctlCLI/StartServer.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ struct StartServer: ParsableCommand {
4040
runCommand( .simctlTerminateApp(device: deviceId, appBundleIdentifier: appBundleId), verbose: v)
4141
}
4242

43+
server.onErase { deviceId -> Result<String, Swift.Error> in
44+
runCommand( .simctlErase(device: deviceId), verbose: v)
45+
}
46+
4347
server.onSetDeviceAppearance { deviceId, _, appearance -> Result<String, Swift.Error> in
4448
runCommand(.simctlSetUI(appearance: appearance, on: deviceId), verbose: v)
4549
}

Sources/SimctlShared/SimctlShared.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public enum ServerPath: String {
7979
case privacy = "/simctl/setPrivacy"
8080
case renameDevice = "/simctl/renameDevice"
8181
case terminateApp = "/simctl/terminateApp"
82+
case erase = "/simctl/erase"
8283
case deviceAppearance = "/simctl/setDeviceAppearance"
8384
case iCloudSync = "/simctl/iCloudSync"
8485
case uninstallApp = "/simctl/uninstallApp"

0 commit comments

Comments
 (0)