Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Use simctl #11

Merged
merged 3 commits into from
Mar 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/israkel.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'israkel/tasks'
require 'israkel/device'
require 'israkel/simctl'
10 changes: 8 additions & 2 deletions lib/israkel/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ def self.with_sdk_and_type(sdk_version, type)
end

def self.stop
self.shutdown_devices
system 'killall', '-m', '-TERM', 'iOS Simulator'
end

def self.shutdown_devices
SIMCTL.booted_devices_uuids.each do |uuid|
SIMCTL.shutdown(uuid)
end
end

def self.all
devices = []
dirs = Dir.entries(Device.sim_root_path).reject { |entry| File.directory? entry }
Expand Down Expand Up @@ -95,8 +102,7 @@ def start
end

def reset
FileUtils.rm_rf File.join(path)
FileUtils.mkdir File.join(path)
SIMCTL.erase @UUID
end

def self.sim_root_path
Expand Down
21 changes: 21 additions & 0 deletions lib/israkel/simctl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class SIMCTL

def self.list
`xcrun simctl list`
end

def self.booted_devices_uuids
devices = self.list.split("\n")
devices.keep_if { |entry| entry =~ /(\(Booted\))/ }
devices.map { |device| device.match(/\(([^\)]+)\)/)[1] }
end

def self.shutdown(device_uuid)
system "xcrun simctl shutdown #{device_uuid}"
end

def self.erase(device_uuid)
system "xcrun simctl erase #{device_uuid}"
end

end
9 changes: 6 additions & 3 deletions spec/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
describe "#all" do
before do
allow(Device).to receive(:sim_root_path) { File.join('spec', 'fixtures', 'sim_root_path') }
allow(SIMCTL).to receive(:list) { File.readlines(File.join('spec', 'fixtures', 'simctl_list_output.txt')).join("\n") }
end

it "returns correct number of devices" do
Expand All @@ -92,6 +93,9 @@

it "#stop" do
expect(Device).to receive(:system).with('killall', '-m', '-TERM', 'iOS Simulator')
SIMCTL.booted_devices_uuids.each do |uuid|
allow(SIMCTL).to receive(:system).with("xcrun simctl shutdown #{uuid}")
end
Device.stop
end

Expand Down Expand Up @@ -165,6 +169,7 @@
@subject = Device.from_hash(@hash)
@path = File.join('spec', 'fixtures', 'sim_root_path')
allow(Device).to receive(:sim_root_path) { @path }
allow(SIMCTL).to receive(:list) { File.readlines(File.join('spec', 'fixtures', 'simctl_list_output.txt')).join("\n") }
end

it "#start" do
Expand All @@ -173,9 +178,7 @@
end

it "#reset" do
sim_path = "spec/fixtures/sim_root_path/EFA1B4B1-5741-4396-AF52-F8AD29229CFC/data"
expect(FileUtils).to receive(:rm_rf).with(sim_path)
expect(FileUtils).to receive(:mkdir).with(sim_path)
expect(SIMCTL).to receive(:system).with("xcrun simctl erase EFA1B4B1-5741-4396-AF52-F8AD29229CFC")
@subject.reset
end
end
Expand Down
52 changes: 52 additions & 0 deletions spec/fixtures/simctl_list_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
== Device Types ==
iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
iPhone 5 (com.apple.CoreSimulator.SimDeviceType.iPhone-5)
iPhone 5s (com.apple.CoreSimulator.SimDeviceType.iPhone-5s)
iPhone 6 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus)
iPhone 6 (com.apple.CoreSimulator.SimDeviceType.iPhone-6)
iPad 2 (com.apple.CoreSimulator.SimDeviceType.iPad-2)
iPad Retina (com.apple.CoreSimulator.SimDeviceType.iPad-Retina)
iPad Air (com.apple.CoreSimulator.SimDeviceType.iPad-Air)
Resizable iPhone (com.apple.CoreSimulator.SimDeviceType.Resizable-iPhone)
Resizable iPad (com.apple.CoreSimulator.SimDeviceType.Resizable-iPad)
== Runtimes ==
iOS 8.2 (8.2 - 12D508) (com.apple.CoreSimulator.SimRuntime.iOS-8-2)
== Devices ==
-- iOS 8.2 --
iPhone 4s (C1D8B76B-EB4B-4BB6-8CFE-D6A60C749FBA) (Shutdown)
iPhone 5 (2C387DB6-ABE9-4AFC-A79A-58184B882293) (Shutdown)
iPhone 5s (1B22E427-F9BE-45D3-BD38-97C80AA8EC27) (Booted)
iPhone 6 Plus (E6EC89BD-C92C-4907-A47E-60C2F9CF3F08) (Shutdown)
iPhone 6 (4BA2717C-9F59-4432-8AD4-1579CBA3478B) (Shutdown)
iPad 2 (B87B2ED8-E184-44E0-AC02-4B162AB5FDD4) (Booted)
iPad Retina (6E4145A9-E25F-4961-9BA3-E80EC2E06C9E) (Shutdown)
iPad Air (C2934F50-8110-4731-9D6A-B2FF10A60499) (Shutdown)
Resizable iPhone (3C026DD0-D0BB-446E-B3ED-30A044FBE68C) (Shutdown)
Resizable iPad (287881CE-C127-4241-9966-6A81F072897E) (Shutdown)
-- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-7-1 --
iPhone 4s (637EFC12-A9FA-4F60-A37E-F7B0026C641C) (Shutdown) (unavailable, runtime profile not found)
iPhone 5 (BD0A4228-16F7-4C18-8C26-A03D7B00F20D) (Shutdown) (unavailable, runtime profile not found)
iPhone 5s (A85B772F-0162-429C-A492-92CE23AC3A90) (Shutdown) (unavailable, runtime profile not found)
iPad Air (CF878740-0E71-420F-BB21-2C73AC6FC390) (Shutdown) (unavailable, runtime profile not found)
-- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-8-0 --
iPhone 4s (3BE7E1E4-83E0-4A1C-B50C-4EE7474ECBCD) (Shutdown) (unavailable, runtime profile not found)
iPhone 5 (3360D3E5-4999-4D03-9EDA-FE82DC1BE370) (Shutdown) (unavailable, runtime profile not found)
iPhone 5s (4F28855B-ABA6-4ECA-A644-5112ACF19BF8) (Shutdown) (unavailable, runtime profile not found)
iPhone 6 Plus (79756099-1F4E-4A04-973E-F933DC6D0B25) (Shutdown) (unavailable, runtime profile not found)
iPad Air (1FD635DC-8585-4A76-84B7-41D4B1559A20) (Shutdown) (unavailable, runtime profile not found)
-- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-8-1 --
iPhone 5s (48F5823F-33CD-495F-A8E0-FD725E54EFE0) (Shutdown) (unavailable, runtime profile not found)
iPhone 6 Plus (8B875A0A-7E45-4156-A2A1-126A9B151911) (Shutdown) (unavailable, runtime profile not found)
iPhone 6 (35434942-C5ED-449D-9C17-9152AF784945) (Shutdown) (unavailable, runtime profile not found)
iPad Air (89605448-18BD-4C95-9937-B47FB1CC447C) (Shutdown) (unavailable, runtime profile not found)
-- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-8-3 --
iPhone 4s (49EE32E1-824A-46FA-B074-FD3CB3F2C8C9) (Shutdown) (unavailable, runtime profile not found)
iPhone 5 (2EEEBAD0-0D77-47DE-B94C-78C275051E49) (Shutdown) (unavailable, runtime profile not found)
iPhone 5s (B7BA4BF7-225F-481E-8296-9A71BFDCB609) (Shutdown) (unavailable, runtime profile not found)
iPhone 6 Plus (611F33B5-8203-465C-90BD-0EC19A2B27C7) (Shutdown) (unavailable, runtime profile not found)
iPhone 6 (CC2BD3D9-1B0E-4CBE-BEB0-BF6B8A2231C7) (Shutdown) (unavailable, runtime profile not found)
iPad 2 (2AFA96BF-ED24-4180-9A79-236639CC8F01) (Shutdown) (unavailable, runtime profile not found)
iPad Retina (C0987016-C741-4267-A2DA-FA27ADD16E53) (Shutdown) (unavailable, runtime profile not found)
iPad Air (B20F5FD4-9FC3-4696-AC0F-A147A5996823) (Shutdown) (unavailable, runtime profile not found)
Resizable iPhone (526CCAAC-47D6-4941-A071-279C18B34A63) (Shutdown) (unavailable, runtime profile not found)
Resizable iPad (0BD3C90A-67A7-437A-BF5A-DD56C5E58642) (Shutdown) (unavailable, runtime profile not found)
37 changes: 37 additions & 0 deletions spec/simctl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

module Kernel
def `(cmd)
"call #{cmd}"
end
end

describe SIMCTL do
describe "#booted_devices_uuids" do
before do
allow(SIMCTL).to receive(:list) { File.readlines(File.join('spec', 'fixtures', 'simctl_list_output.txt')).join("\n") }
end

it "returns all uuids for booted devices" do
expect(SIMCTL.booted_devices_uuids).to eql([
"1B22E427-F9BE-45D3-BD38-97C80AA8EC27",
"B87B2ED8-E184-44E0-AC02-4B162AB5FDD4"
])
end

it "shuts down the process via simctl" do
SIMCTL.booted_devices_uuids.each do |uuid|
expect(SIMCTL).to receive(:system).with("xcrun simctl shutdown #{uuid}")
SIMCTL.shutdown(uuid)
end
end

it "erase the device via simctl" do
SIMCTL.booted_devices_uuids.each do |uuid|
expect(SIMCTL).to receive(:system).with("xcrun simctl erase #{uuid}")
SIMCTL.erase(uuid)
end
end
end

end