Skip to content
Open
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
pkg
sim_launcher*gem
Gemfile.lock
12 changes: 12 additions & 0 deletions lib/sim_launcher/sdk_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ def latest_sdk_version
available_sdk_versions.sort.last
end

def available_device_types
@simulator.showdevicetypes.chomp.split("\n").map { |device_type_line|
[device_type_line, Float(device_type_line.split(',').last.strip)]
}
end

def latest_device_type
available_device_types.sort do |line1, line2|
line1.last <=> line2.last
end.last.first
end

end
end
50 changes: 40 additions & 10 deletions lib/sim_launcher/simulator.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
module SimLauncher

class Cml
def run(arg)
return `#{arg}`
end
end
class Simulator

def initialize( iphonesim_path_external = nil )
def initialize( iphonesim_path_external = nil, cml = nil )
@cml = cml || Cml.new
@iphonesim_path = iphonesim_path_external || iphonesim_path
end

def showsdks
run_synchronous_command( 'showsdks' )
end

def showdevicetypes
run_synchronous_command( 'showdevicetypes' )
end

def start_simulator(sdk_version=nil, device_family="iphone")
sdk_version ||= SdkDetector.new(self).latest_sdk_version
run_synchronous_command( :start, '--sdk', sdk_version, '--family', device_family, '--exit' )
if iphonesim_version < 3.0
sdk_version ||= SdkDetector.new(self).latest_sdk_version
run_synchronous_command( :start, '--sdk', sdk_version, '--family', device_family, '--exit' )
else
sdk_version ||= SdkDetector.new(self).latest_device_type
run_synchronous_command( :start, '--devicetypeid', sdk_version, '--exit')
end
end


Expand Down Expand Up @@ -50,9 +64,14 @@ def launch_ios_app(app_path, sdk_version, device_family, app_args = nil)
bangs = '!'*80
raise "\n#{bangs}\nENCOUNTERED A PROBLEM WITH THE SPECIFIED APP PATH:\n\n#{problem}\n#{bangs}"
end
sdk_version ||= SdkDetector.new(self).latest_sdk_version
args = ["--args"] + app_args.flatten if app_args
run_synchronous_command( :launch, app_path, '--sdk', sdk_version, '--family', device_family, '--exit', *args )
if iphonesim_version < 3.0
sdk_version ||= SdkDetector.new(self).latest_sdk_version
run_synchronous_command( :launch, app_path, '--sdk', sdk_version, '--family', device_family, '--exit', *args )
else
sdk_version ||= SdkDetector.new(self).latest_device_type
run_synchronous_command( :launch, app_path, '--devicetypeid', sdk_version, '--exit', *args )
end
end

def launch_ipad_app( app_path, sdk )
Expand All @@ -74,14 +93,16 @@ def launch_iphone_app_with_name( app_name, sdk )
end

def quit_simulator
`echo 'application "iPhone Simulator" quit' | osascript`
@cml.run("echo 'application \"iPhone Simulator\" quit' | osascript")
# new version of simulator called "Simulator"
@cml.run("echo 'application \"Simulator\" quit' | osascript")
end

def run_synchronous_command( *args )
args.compact!
cmd = cmd_line_with_args( args )
puts "executing #{cmd}" if $DEBUG
`#{cmd}`
@cml.run("#{cmd}")
end

def cmd_line_with_args( args )
Expand All @@ -90,7 +111,7 @@ def cmd_line_with_args( args )
end

def xcode_version
version_out = `xcodebuild -version`
version_out = @cml.run("xcodebuild -version")
begin
Float(version_out[/([0-9]\.[0-9])/, 1])
rescue => ex
Expand All @@ -99,13 +120,22 @@ def xcode_version
end

def iphonesim_path
installed = `which ios-sim`
installed = @cml.run("which ios-sim")
if installed =~ /(.*ios-sim)/
puts "Using installed ios-sim at #{$1}" if $DEBUG
return $1
end

raise "Didn't find the ios-sim binary in your $PATH.\n\nPlease install, e.g. using Homebrew:\n\tbrew install ios-sim\n\n"
end

def iphonesim_version
version_out = @cml.run("#{@iphonesim_path} --version")
begin
Float(version_out[/([0-9]\.[0-9])/, 1])
rescue => ex
raise "Cannot determine ios-sim version: #{ex}"
end
end
end
end
2 changes: 2 additions & 0 deletions sim_launcher.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ Gem::Specification.new do |s|
EOS

s.add_dependency "sinatra"

s.add_development_dependency("rspec", [">=2.14.1"])
end
108 changes: 108 additions & 0 deletions spec/simulator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
require 'rspec'
require_relative '../lib/sim_launcher'

describe(SimLauncher::Simulator) do
let(:cml) { double }
let(:appPath) { "myAppPath" }
let(:invalid_appPath) { "invalidAppPath" }
let(:simulator) { SimLauncher::Simulator.new(nil, cml) }
let(:sdks) {
<<EOS
Simulator SDK Roots:
'iOS 9.2' (9.2)
/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 9.2.simruntime/Contents/Resources/RuntimeRoot
'iOS 9.1' (9.1)
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk
EOS
}
let(:device_types) {
<<EOS
com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.2
com.apple.CoreSimulator.SimDeviceType.iPhone-6s-Plus, 9.1
EOS
}

before :each do
allow(SimLauncher).to receive(:check_app_path).with(appPath).and_return(nil)
allow(SimLauncher).to receive(:check_app_path).with(invalid_appPath).and_return("wrong app path")
allow(cml).to receive(:run).with('which ios-sim').and_return('/usr/bin/ios-sim')
allow(cml).to receive(:run).with("/usr/bin/ios-sim \"showsdks\" 2>&1").and_return(sdks)
allow(cml).to receive(:run).with("/usr/bin/ios-sim \"showdevicetypes\" 2>&1").and_return(device_types)
end

context("when ios-sim version is under 3.x") do

before :each do
allow(cml).to receive(:run).with('/usr/bin/ios-sim --version').and_return('2.0.1')
end

it("should tell the version of ios-sim") do
expect(simulator.iphonesim_version).to eql(2.0)
end

it("should launch simulator") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"launch\" \"myAppPath\" \"--sdk\" \"9.3\" \"--family\" \"iPhone\" \"--exit\" \"--args\" \"appArgs\" 2>&1")
simulator.launch_ios_app(appPath, '9.3', 'iPhone', ['appArgs'])
end

it("should get error if app path is invalid") do
expect{simulator.launch_ios_app(invalid_appPath, '9.3', 'iPhone', ['appArgs'])}.to raise_error(RuntimeError)
end

it("should get the latest sdk version if no sdk version is provided") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"launch\" \"myAppPath\" \"--sdk\" \"9.2\" \"--family\" \"iPhone\" \"--exit\" 2>&1")
simulator.launch_ios_app(appPath, nil, 'iPhone')
end

it("should start simulator") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"start\" \"--sdk\" \"9.3\" \"--family\" \"iPhone\" \"--exit\" 2>&1")
simulator.start_simulator('9.3', 'iPhone')
end

it("should start simulator with the latest sdk version if no sdk version is provided") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"start\" \"--sdk\" \"9.2\" \"--family\" \"iPhone\" \"--exit\" 2>&1")
simulator.start_simulator(nil, 'iPhone')
end

end

context("when ios-sim version is 3.x") do

before :each do
allow(cml).to receive(:run).with('/usr/bin/ios-sim --version').and_return('3.0.1')
end

it("should tell the version of ios-sim") do
expect(simulator.iphonesim_version).to eql(3.0)
end

it("should launch simulator") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"launch\" \"myAppPath\" \"--devicetypeid\" \"com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.3\" \"--exit\" \"--args\" \"appArgs\" 2>&1")
simulator.launch_ios_app(appPath, 'com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.3', 'iPhone', ['appArgs'])
end

it("should get the latest device type if no sdk version is provided") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"launch\" \"myAppPath\" \"--devicetypeid\" \"com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.2\" \"--exit\" 2>&1")
simulator.launch_ios_app(appPath, nil, 'iPhone')
end

it("should start simulator") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"start\" \"--devicetypeid\" \"com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.3\" \"--exit\" 2>&1")
simulator.start_simulator("com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.3")
end

it("should start simulator with the latest sdk version if no sdk version is provided") do
expect(cml).to receive(:run).with("/usr/bin/ios-sim \"start\" \"--devicetypeid\" \"com.apple.CoreSimulator.SimDeviceType.iPhone-6s, 9.2\" \"--exit\" 2>&1")
simulator.start_simulator(nil, 'iPhone')
end
end

context("quit simulator") do
it("should handle old version and new version simulator application names") do
expect(cml).to receive(:run).with("echo 'application \"iPhone Simulator\" quit' | osascript")
expect(cml).to receive(:run).with("echo 'application \"Simulator\" quit' | osascript")
simulator.quit_simulator
end
end
end