lib/snapshot/runner.rb in snapshot-0.4.13 vs lib/snapshot/runner.rb in snapshot-0.5.0

- old
+ new

@@ -1,39 +1,38 @@ require 'pty' +require 'shellwords' module Snapshot class Runner TRACE_DIR = '/tmp/snapshot_traces' def work(clean: true) SnapshotConfig.shared_instance.js_file # to verify the file can be found earlier Builder.new.build_app(clean: clean) - @app_path = Dir.glob("/tmp/snapshot/build/*.app").first + @app_path = determine_app_path counter = 0 errors = [] FileUtils.rm_rf SnapshotConfig.shared_instance.screenshots_path if SnapshotConfig.shared_instance.clear_previous_screenshots SnapshotConfig.shared_instance.devices.each do |device| - - SnapshotConfig.shared_instance.blocks[:setup_for_device_change].call(device, udid_for_simulator(device)) # Callback - SnapshotConfig.shared_instance.languages.each do |language| - SnapshotConfig.shared_instance.blocks[:setup_for_language_change].call(language, device) # Callback - reinstall_app(device, language) unless ENV["SNAPSHOT_SKIP_UNINSTALL"] + + prepare_simulator(device, language) + begin errors.concat(run_tests(device, language)) counter += copy_screenshots(language) rescue => ex Helper.log.error(ex) end - SnapshotConfig.shared_instance.blocks[:teardown_language].call(language, device) # Callback + + teardown_simulator(device, language) end - SnapshotConfig.shared_instance.blocks[:teardown_device].call(device) # Callback end `killall "iOS Simulator"` # close the simulator after the script is finished ReportsGenerator.new.generate @@ -53,26 +52,32 @@ def clean_old_traces FileUtils.rm_rf(TRACE_DIR) FileUtils.mkdir_p(TRACE_DIR) end + def prepare_simulator(device, language) + SnapshotConfig.shared_instance.blocks[:setup_for_device_change].call(device, udid_for_simulator(device), language) # Callback + SnapshotConfig.shared_instance.blocks[:setup_for_language_change].call(language, device) # deprecated + end + + def teardown_simulator(device, language) + SnapshotConfig.shared_instance.blocks[:teardown_language].call(language, device) # Callback + SnapshotConfig.shared_instance.blocks[:teardown_device].call(device, language) # deprecated + end + def udid_for_simulator(name) # fetches the UDID of the simulator type all = `instruments -s`.split("\n") all.each do |current| return current.match(/\[(.*)\]/)[1] if current.include?name end raise "Could not find simulator '#{name}' to install the app on." end def reinstall_app(device, language) - def app_identifier - @app_identifier ||= ENV["SNAPSHOT_APP_IDENTIFIER"] - @app_identifier ||= `/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /tmp/snapshot/build/*.app/Info.plist` - @app_identifier ||= `/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /tmp/snapshot/build/*.app/*.plist` - @app_identifier.strip - end + app_identifier = ENV["SNAPSHOT_APP_IDENTIFIER"] + app_identifier ||= @app_identifier def com(cmd) puts cmd.magenta result = `#{cmd}` puts result if result.to_s.length > 0 @@ -84,11 +89,11 @@ com("killall 'iOS Simulator'") sleep 3 com("xcrun simctl boot '#{udid}'") com("xcrun simctl uninstall booted '#{app_identifier}'") sleep 3 - com("xcrun simctl install booted '#{@app_path}'") + com("xcrun simctl install booted '#{@app_path.shellescape}'") com("xcrun simctl shutdown booted") end def run_tests(device, language) Helper.log.info "Running tests on #{device} in language #{language}".green @@ -144,10 +149,25 @@ end return errors end + def determine_app_path + # Determine the path to the actual app and not the WatchKit app + Dir.glob("/tmp/snapshot/build/*.app/*.plist").each do |path| + watchkit_enabled = `/usr/libexec/PlistBuddy -c 'Print WKWatchKitApp' '#{path}'`.strip + next if watchkit_enabled == 'true' # we don't care about WatchKit Apps + + app_identifier = `/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' '#{path}'`.strip + if app_identifier and app_identifier.length > 0 + # This seems to be the valid Info.plist + @app_identifier = app_identifier + return File.expand_path("..", path) # the app + end + end + end + def parse_test_line(line) if line =~ /.*Target failed to run.*/ return :retry elsif line.include?"Screenshot captured" return :screenshot @@ -186,10 +206,10 @@ [ "instruments", "-w '#{device}'", "-D '#{TRACE_DIR}/trace'", "-t 'Automation'", - "'#{@app_path}'", + "#{@app_path.shellescape}", "-e UIARESULTSPATH '#{TRACE_DIR}'", "-e UIASCRIPT '#{script_path}'", "-AppleLanguages '(#{language})'", "-AppleLocale '#{language}'" ].join(' ')