From 1670772f8aa0fa39a817d8fd105373707e4e5a6c Mon Sep 17 00:00:00 2001 From: Peter Zhao Date: Sat, 18 Jun 2016 23:34:23 -0400 Subject: [PATCH] Provide right options to ios-sim 3.x --- .gitignore | 1 + lib/sim_launcher/sdk_detector.rb | 12 ++++ lib/sim_launcher/simulator.rb | 50 +++++++++++--- sim_launcher.gemspec | 2 + spec/simulator_spec.rb | 108 +++++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 spec/simulator_spec.rb diff --git a/.gitignore b/.gitignore index 4602396..44131bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store pkg sim_launcher*gem +Gemfile.lock diff --git a/lib/sim_launcher/sdk_detector.rb b/lib/sim_launcher/sdk_detector.rb index 684c61e..572443f 100644 --- a/lib/sim_launcher/sdk_detector.rb +++ b/lib/sim_launcher/sdk_detector.rb @@ -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 diff --git a/lib/sim_launcher/simulator.rb b/lib/sim_launcher/simulator.rb index ccbe151..bfc1ad0 100644 --- a/lib/sim_launcher/simulator.rb +++ b/lib/sim_launcher/simulator.rb @@ -1,8 +1,13 @@ 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 @@ -10,9 +15,18 @@ 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 @@ -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 ) @@ -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 ) @@ -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 @@ -99,7 +120,7 @@ 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 @@ -107,5 +128,14 @@ def iphonesim_path 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 diff --git a/sim_launcher.gemspec b/sim_launcher.gemspec index 1eb21b9..0a14dbe 100644 --- a/sim_launcher.gemspec +++ b/sim_launcher.gemspec @@ -28,4 +28,6 @@ Gem::Specification.new do |s| EOS s.add_dependency "sinatra" + + s.add_development_dependency("rspec", [">=2.14.1"]) end diff --git a/spec/simulator_spec.rb b/spec/simulator_spec.rb new file mode 100644 index 0000000..fc3180b --- /dev/null +++ b/spec/simulator_spec.rb @@ -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) { +<&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 +