Module: RunLoop::Core
- Defined in:
- lib/run_loop/core.rb
Constant Summary collapse
- START_DELIMITER =
"OUTPUT_JSON:\n"
- END_DELIMITER =
"\nEND_OUTPUT"
- SCRIPTS_PATH =
File.(File.join(File.dirname(__FILE__), '..', '..', 'scripts'))
- SCRIPTS =
{ :dismiss => 'run_dismiss_location.js', :run_loop_fast_uia => 'run_loop_fast_uia.js', :run_loop_host => 'run_loop_host.js', :run_loop_basic => 'run_loop_basic.js' }
- READ_SCRIPT_PATH =
File.join(SCRIPTS_PATH, 'read-cmd.sh')
Class Method Summary collapse
-
.above_or_eql_version?(target_version, xcode_version) ⇒ Boolean
deprecated
Deprecated.
since 1.0.0
- .automation_template(xctools, candidate = ) ⇒ Object
- .create_uia_pipe(repl_path) ⇒ Object
-
.default_simulator(xcode_tools = RunLoop::XCTools.new) ⇒ Object
Returns the a default simulator to target.
- .default_tracetemplate(xctools = RunLoop::XCTools.new) ⇒ Object
- .detect_connected_device ⇒ Object
-
.dylib_path_from_options(options) ⇒ String?
Extracts the value of :inject_dylib from options Hash.
-
.ensure_instruments_not_running! ⇒ Object
deprecated
Deprecated.
1.0.5
- .ensure_lldb_not_running ⇒ Object
- .find_cf_bundle_executable(bundle_dir_or_bundle_id) ⇒ Object
- .instruments_command(options, xctools = RunLoop::XCTools.new) ⇒ Object
- .instruments_command_prefix(udid, results_dir_trace) ⇒ Object
-
.instruments_pids ⇒ Object
deprecated
Deprecated.
1.0.5
- .instruments_running? ⇒ Boolean
- .jruby? ⇒ Boolean
- .log(message) ⇒ Object
- .log_header(message) ⇒ Object
- .log_run_loop_options(options, xctools) ⇒ Object
-
.pids_for_run_loop(run_loop, &block) ⇒ Object
deprecated
Deprecated.
1.0.5
- .read_response(run_loop, expected_index, empty_file_timeout = 10) ⇒ Object
- .run_with_options(options) ⇒ Object
- .script_for_key(key) ⇒ Object
- .scripts_path ⇒ Object
- .udid_and_bundle_for_launcher(device_target, options, xctools = RunLoop::XCTools.new) ⇒ Object
- .write_request(run_loop, cmd) ⇒ Object
-
.xcode_version(xctools = RunLoop::XCTools.new) ⇒ Object
deprecated
Deprecated.
1.0.0 replaced with Xctools#version
Class Method Details
.above_or_eql_version?(target_version, xcode_version) ⇒ Boolean
Deprecated.
since 1.0.0
still used extensively in calabash-ios launcher
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/run_loop/core.rb', line 52 def self.above_or_eql_version?(target_version, xcode_version) if target_version.is_a?(RunLoop::Version) target = target_version else target = RunLoop::Version.new(target_version) end if xcode_version.is_a?(RunLoop::Version) xcode = xcode_version else xcode = RunLoop::Version.new(xcode_version) end target >= xcode end |
.automation_template(xctools, candidate = ) ⇒ Object
568 569 570 571 572 573 |
# File 'lib/run_loop/core.rb', line 568 def self.automation_template(xctools, candidate = ENV['TRACE_TEMPLATE']) unless candidate && File.exist?(candidate) candidate = default_tracetemplate xctools end candidate end |
.create_uia_pipe(repl_path) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/run_loop/core.rb', line 420 def self.create_uia_pipe(repl_path) begin Timeout::timeout(5, TimeoutError) do loop do begin FileUtils.rm_f(repl_path) return repl_path if system(%Q[mkfifo "#{repl_path}"]) rescue Errno::EINTR => e #retry sleep(0.1) end end end rescue TimeoutError => _ raise TimeoutError, 'Unable to create pipe (mkfifo failed)' end end |
.default_simulator(xcode_tools = RunLoop::XCTools.new) ⇒ Object
Returns the a default simulator to target. This default needs to be one that installed by default in the current Xcode version.
For historical reasons, the most recent non-64b SDK should be used.
360 361 362 363 364 365 366 |
# File 'lib/run_loop/core.rb', line 360 def self.default_simulator(xcode_tools=RunLoop::XCTools.new) if xcode_tools.xcode_version_gte_6? 'iPhone 5 (8.0 Simulator)' else 'iPhone Retina (4-inch) - Simulator - iOS 7.1' end end |
.default_tracetemplate(xctools = RunLoop::XCTools.new) ⇒ Object
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/run_loop/core.rb', line 575 def self.default_tracetemplate(xctools=RunLoop::XCTools.new) templates = xctools.instruments :templates if xctools.xcode_version_gte_61? templates.delete_if do |path| not path =~ /\/Automation.tracetemplate/ end.delete_if do |path| not path =~ /Xcode/ end.first.tr("\"", '').strip elsif xctools.xcode_version_gte_6? templates.delete_if do |name| not name == 'Automation' end.first else templates.delete_if do |path| not path =~ /\/Automation.tracetemplate/ end.delete_if do |path| not path =~ /Xcode/ end.first.tr("\"", '').strip end end |
.detect_connected_device ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/run_loop/core.rb', line 74 def self.detect_connected_device begin Timeout::timeout(1, TimeoutError) do return `#{File.join(scripts_path, 'udidetect')}`.chomp end rescue TimeoutError => _ `killall udidetect &> /dev/null` end nil end |
.dylib_path_from_options(options) ⇒ String?
Extracts the value of :inject_dylib from options Hash.
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/run_loop/core.rb', line 331 def self.() inject_dylib = .fetch(:inject_dylib, nil) return nil if inject_dylib.nil? unless inject_dylib.is_a? String raise ArgumentError, "Expected :inject_dylib to be a path to a dylib, but found '#{inject_dylib}'" end dylib_path = File.(inject_dylib) unless File.exist?(dylib_path) raise "Cannot load dylib. The file '#{dylib_path}' does not exist." end dylib_path end |
.ensure_instruments_not_running! ⇒ Object
Deprecated.
1.0.5
611 612 613 |
# File 'lib/run_loop/core.rb', line 611 def self.ensure_instruments_not_running! RunLoop::Instruments.new.kill_instruments end |
.ensure_lldb_not_running ⇒ Object
TODO:
This is a WIP
TODO:
Needs rspec test
626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/run_loop/core.rb', line 626 def self.ensure_lldb_not_running descripts = `xcrun ps x -o pid,command | grep "lldb" | grep -v grep`.strip.split("\n") descripts.each do |process_desc| pid = process_desc.split(' ').first Open3.popen3("xcrun kill -9 #{pid} && xcrun wait #{pid}") do |_, stdout, stderr, _| out = stdout.read.strip err = stderr.read.strip next if out.to_s.empty? and err.to_s.empty? # there lots of 'ownership' problems trying to kill the lldb process #puts "kill process '#{pid}' => stdout: '#{out}' | stderr: '#{err}'" end end end |
.find_cf_bundle_executable(bundle_dir_or_bundle_id) ⇒ Object
344 345 346 347 348 349 350 351 |
# File 'lib/run_loop/core.rb', line 344 def self.find_cf_bundle_executable(bundle_dir_or_bundle_id) unless File.directory?(bundle_dir_or_bundle_id) raise "Injecting dylibs currently only works with simulator and app bundles" end info_plist = Dir[File.join(bundle_dir_or_bundle_id, 'Info.plist')].first raise "Unable to find Info.plist in #{bundle_dir_or_bundle_id}" if info_plist.nil? `/usr/libexec/PlistBuddy -c "Print :CFBundleExecutable" "#{info_plist}"`.strip end |
.instruments_command(options, xctools = RunLoop::XCTools.new) ⇒ Object
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/run_loop/core.rb', line 544 def self.instruments_command(, xctools=RunLoop::XCTools.new) udid = [:udid] results_dir_trace = [:results_dir_trace] bundle_dir_or_bundle_id = [:bundle_dir_or_bundle_id] results_dir = [:results_dir] script = [:script] log_file = [:log_file] args= [:args] || [] instruments_prefix = instruments_command_prefix(udid, results_dir_trace) cmd = [ instruments_prefix, '-t', "\"#{automation_template(xctools)}\"", "\"#{bundle_dir_or_bundle_id}\"", '-e', 'UIARESULTSPATH', results_dir, '-e', 'UIASCRIPT', script, args.join(' ') ] if log_file cmd << "&> #{log_file}" end cmd end |
.instruments_command_prefix(udid, results_dir_trace) ⇒ Object
534 535 536 537 538 539 540 541 |
# File 'lib/run_loop/core.rb', line 534 def self.instruments_command_prefix(udid, results_dir_trace) instruments_path = 'xcrun instruments' if udid instruments_path = "#{instruments_path} -w \"#{udid}\"" end instruments_path << " -D \"#{results_dir_trace}\"" if results_dir_trace instruments_path end |
.instruments_pids ⇒ Object
Deprecated.
1.0.5
620 621 622 |
# File 'lib/run_loop/core.rb', line 620 def self.instruments_pids RunLoop::Instruments.new.instruments_pids end |
.instruments_running? ⇒ Boolean
615 616 617 |
# File 'lib/run_loop/core.rb', line 615 def self.instruments_running? RunLoop::Instruments.new.instruments_running? end |
.jruby? ⇒ Boolean
438 439 440 |
# File 'lib/run_loop/core.rb', line 438 def self.jruby? RUBY_PLATFORM == 'java' end |
.log(message) ⇒ Object
596 597 598 599 600 601 |
# File 'lib/run_loop/core.rb', line 596 def self.log() if ENV['DEBUG']=='1' puts "#{Time.now } #{}" $stdout.flush end end |
.log_header(message) ⇒ Object
603 604 605 606 607 608 |
# File 'lib/run_loop/core.rb', line 603 def self.log_header() if ENV['DEBUG']=='1' puts "\n\e[#{35}m### #{} ###\e[0m" $stdout.flush end end |
.log_run_loop_options(options, xctools) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/run_loop/core.rb', line 33 def self.(, xctools) return unless ENV['DEBUG'] == '1' # Ignore :sim_control b/c it is a ruby object; printing is not useful. ignored_keys = [:sim_control] = {} .each_pair do |key, value| next if ignored_keys.include?(key) [key] = value end # Objects that override '==' cannot be printed by awesome_print # https://github.com/michaeldv/awesome_print/issues/154 # RunLoop::Version overrides '==' [:xcode] = xctools.xcode_version.to_s [:xcode_path] = xctools.xcode_developer_dir ap(, {:sort_keys => true}) end |
.pids_for_run_loop(run_loop, &block) ⇒ Object
Deprecated.
1.0.5
530 531 532 |
# File 'lib/run_loop/core.rb', line 530 def self.pids_for_run_loop(run_loop, &block) RunLoop::Instruments.new.instruments_pids(&block) end |
.read_response(run_loop, expected_index, empty_file_timeout = 10) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/run_loop/core.rb', line 451 def self.read_response(run_loop, expected_index, empty_file_timeout=10) log_file = run_loop[:log_file] initial_offset = run_loop[:initial_offset] || 0 offset = initial_offset result = nil loop do unless File.exist?(log_file) && File.size?(log_file) sleep(0.2) next end size = File.size(log_file) output = File.read(log_file, size-offset, offset) if /AXError: Could not auto-register for pid status change/.match(output) if /kAXErrorServerNotFound/.match(output) $stderr.puts "\n\n****** Accessibility is not enabled on device/simulator, please enable it *** \n\n" $stderr.flush end raise TimeoutError.new('AXError: Could not auto-register for pid status change') end if /Automation Instrument ran into an exception/.match(output) raise TimeoutError.new('Exception while running script') end index_if_found = output.index(START_DELIMITER) if ENV['DEBUG_READ']=='1' puts output.gsub('*', '') puts "Size #{size}" puts "offset #{offset}" puts "index_of #{START_DELIMITER}: #{index_if_found}" end if index_if_found offset = offset + index_if_found rest = output[index_if_found+START_DELIMITER.size..output.length] index_of_json = rest.index("}#{END_DELIMITER}") if index_of_json.nil? #Wait for rest of json sleep(0.1) next end json = rest[0..index_of_json] if ENV['DEBUG_READ']=='1' puts "Index #{index_if_found}, Size: #{size} Offset #{offset}" puts ("parse #{json}") end offset = offset + json.size parsed_result = JSON.parse(json) if ENV['DEBUG_READ']=='1' p parsed_result end json_index_if_present = parsed_result['index'] if json_index_if_present && json_index_if_present == expected_index result = parsed_result break end else sleep(0.1) end end run_loop[:initial_offset] = offset result end |
.run_with_options(options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/run_loop/core.rb', line 85 def self.() before = Time.now sim_control ||= [:sim_control] || RunLoop::SimControl.new xctools ||= [:xctools] || sim_control.xctools RunLoop::Instruments.new.kill_instruments(xctools) if self.simulator_target?(, sim_control) # @todo only enable accessibility on the targeted simulator sim_control.enable_accessibility_on_sims({:verbose => true}) end device_target = [:udid] || [:device_target] || detect_connected_device || 'simulator' if device_target && device_target.to_s.downcase == 'device' device_target = detect_connected_device end log_file = [:log_path] timeout = [:timeout] || 30 results_dir = [:results_dir] || Dir.mktmpdir('run_loop') results_dir_trace = File.join(results_dir, 'trace') FileUtils.mkdir_p(results_dir_trace) dependencies = [:dependencies] || [] dependencies << File.join(scripts_path, 'calabash_script_uia.js') dependencies.each do |dep| FileUtils.cp(dep, results_dir) end script = File.join(results_dir, '_run_loop.js') code = File.read([:script]) code = code.gsub(/\$PATH/, results_dir) code = code.gsub(/\$READ_SCRIPT_PATH/, READ_SCRIPT_PATH) code = code.gsub(/\$MODE/, 'FLUSH') unless [:no_flush] repl_path = File.join(results_dir, 'repl-cmd.pipe') FileUtils.rm_f(repl_path) uia_strategy = [:uia_strategy] if uia_strategy == :host create_uia_pipe(repl_path) end cal_script = File.join(SCRIPTS_PATH, 'calabash_script_uia.js') File.open(script, 'w') do |file| if include_calabash_script?() file.puts IO.read(cal_script) end file.puts code end # Compute udid and bundle_dir / bundle_id from options and target depending on Xcode version udid, bundle_dir_or_bundle_id = udid_and_bundle_for_launcher(device_target, , xctools) args = .fetch(:args, []) inject_dylib = self. # WIP This is brute-force call against all lldb processes. self.ensure_lldb_not_running if inject_dylib log_file ||= File.join(results_dir, 'run_loop.out') after = Time.now if ENV['DEBUG']=='1' puts "Preparation took #{after-before} seconds" end = { :udid => udid, :results_dir_trace => results_dir_trace, :bundle_dir_or_bundle_id => bundle_dir_or_bundle_id, :results_dir => results_dir, :script => script, :log_file => log_file, :args => args } = .merge() self.(, xctools) cmd = instruments_command(, xctools) log_header("Starting on #{device_target} App: #{bundle_dir_or_bundle_id}") cmd_str = cmd.join(' ') log(cmd_str) if ENV['DEBUG'] == '1' if !jruby? && RUBY_VERSION && RUBY_VERSION.start_with?('1.8') pid = fork do exec(cmd_str) end else pid = spawn(cmd_str) end Process.detach(pid) File.open(File.join(results_dir, 'run_loop.pid'), 'w') do |f| f.write pid end run_loop = {:pid => pid, :index => 1, :uia_strategy => uia_strategy, :udid => udid, :app => bundle_dir_or_bundle_id, :repl_path => repl_path, :log_file => log_file, :results_dir => results_dir} uia_timeout = [:uia_timeout] || (ENV['UIA_TIMEOUT'] && ENV['UIA_TIMEOUT'].to_f) || 10 raw_lldb_output = nil before = Time.now begin if [:validate_channel] [:validate_channel].call(run_loop, 0, uia_timeout) else File.open(repl_path, 'w') { |file| file.puts "0:UIALogger.logMessage('Listening for run loop commands');" } Timeout::timeout(timeout, TimeoutError) do read_response(run_loop, 0, uia_timeout) end end # inject_dylib will be nil or a path to a dylib if inject_dylib lldb_template_file = File.join(scripts_path, 'calabash.lldb.erb') lldb_template = ::ERB.new(File.read(lldb_template_file)) lldb_template.filename = lldb_template_file # Special! # These are required by the ERB in calabash.lldb.erb # noinspection RubyUnusedLocalVariable cf_bundle_executable = find_cf_bundle_executable(bundle_dir_or_bundle_id) # noinspection RubyUnusedLocalVariable dylib_path_for_target = inject_dylib lldb_cmd = lldb_template.result(binding) tmpdir = Dir.mktmpdir('lldb_cmd') lldb_script = File.join(tmpdir, 'lldb') File.open(lldb_script, 'w') { |f| f.puts(lldb_cmd) } if ENV['DEBUG'] == '1' puts "lldb script #{lldb_script}" puts "=== lldb script ===" counter = 0 File.open(lldb_script, 'r').readlines.each { |line| puts "#{counter} #{line}" counter = counter + 1 } puts "=== lldb script ===" end # Forcing a timeout. Do not retry here. If lldb is hanging, # RunLoop::Core.run* needs to be called again. Put another way, # instruments and lldb must be terminated. Retriable.retriable({:tries => 1, :timeout => 12, :interval => 1}) do raw_lldb_output = `xcrun lldb -s #{lldb_script}` if ENV['DEBUG'] == '1' puts raw_lldb_output end end end rescue TimeoutError => e if ENV['DEBUG'] == '1' puts "Failed to launch." puts "#{e}: #{e && e.}" if raw_lldb_output puts "LLDB OUTPUT: #{raw_lldb_output}" end end raise TimeoutError, "Time out waiting for UIAutomation run-loop to Start. \n Logfile #{log_file} \n\n #{File.read(log_file)}\n" end after = Time.now() if ENV['DEBUG']=='1' puts "Launching took #{after-before} seconds" end run_loop end |
.script_for_key(key) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/run_loop/core.rb', line 67 def self.script_for_key(key) if SCRIPTS[key].nil? return nil end File.join(scripts_path, SCRIPTS[key]) end |
.scripts_path ⇒ Object
29 30 31 |
# File 'lib/run_loop/core.rb', line 29 def self.scripts_path SCRIPTS_PATH end |
.udid_and_bundle_for_launcher(device_target, options, xctools = RunLoop::XCTools.new) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/run_loop/core.rb', line 368 def self.udid_and_bundle_for_launcher(device_target, , xctools=RunLoop::XCTools.new) bundle_dir_or_bundle_id = [:app] || ENV['BUNDLE_ID']|| ENV['APP_BUNDLE_PATH'] || ENV['APP'] unless bundle_dir_or_bundle_id raise 'key :app or environment variable APP_BUNDLE_PATH, BUNDLE_ID or APP must be specified as path to app bundle (simulator) or bundle id (device)' end udid = nil if xctools.xcode_version_gte_51? if device_target.nil? || device_target.empty? || device_target == 'simulator' device_target = self.default_simulator(xctools) end udid = device_target unless /simulator/i.match(device_target) bundle_dir_or_bundle_id = [:bundle_id] if [:bundle_id] end else if device_target == 'simulator' unless File.exist?(bundle_dir_or_bundle_id) raise "Unable to find app in directory #{bundle_dir_or_bundle_id} when trying to launch simulator" end device = [:device] || :iphone device = device && device.to_sym plistbuddy='/usr/libexec/PlistBuddy' plistfile="#{bundle_dir_or_bundle_id}/Info.plist" if device == :iphone uidevicefamily=1 else uidevicefamily=2 end system("#{plistbuddy} -c 'Delete :UIDeviceFamily' '#{plistfile}'") system("#{plistbuddy} -c 'Add :UIDeviceFamily array' '#{plistfile}'") system("#{plistbuddy} -c 'Add :UIDeviceFamily:0 integer #{uidevicefamily}' '#{plistfile}'") else udid = device_target bundle_dir_or_bundle_id = [:bundle_id] if [:bundle_id] end end return udid, bundle_dir_or_bundle_id end |
.write_request(run_loop, cmd) ⇒ Object
442 443 444 445 446 447 448 449 |
# File 'lib/run_loop/core.rb', line 442 def self.write_request(run_loop, cmd) repl_path = run_loop[:repl_path] index = run_loop[:index] File.open(repl_path, 'w') { |f| f.puts("#{index}:#{cmd}") } run_loop[:index] = index + 1 index end |
.xcode_version(xctools = RunLoop::XCTools.new) ⇒ Object
Deprecated.
1.0.0 replaced with Xctools#version
416 417 418 |
# File 'lib/run_loop/core.rb', line 416 def self.xcode_version(xctools=RunLoop::XCTools.new) xctools.xcode_version.to_s end |