Module: RunLoop

Defined in:
lib/run_loop.rb,
lib/run_loop/app.rb,
lib/run_loop/ipa.rb,
lib/run_loop/core.rb,
lib/run_loop/fifo.rb,
lib/run_loop/l10n.rb,
lib/run_loop/lipo.rb,
lib/run_loop/lldb.rb,
lib/run_loop/otool.rb,
lib/run_loop/regex.rb,
lib/run_loop/xcode.rb,
lib/run_loop/xcrun.rb,
lib/run_loop/device.rb,
lib/run_loop/locale.rb,
lib/run_loop/simctl.rb,
lib/run_loop/cli/cli.rb,
lib/run_loop/logging.rb,
lib/run_loop/strings.rb,
lib/run_loop/version.rb,
lib/run_loop/codesign.rb,
lib/run_loop/language.rb,
lib/run_loop/template.rb,
lib/run_loop/xcuitest.rb,
lib/run_loop/directory.rb,
lib/run_loop/cli/errors.rb,
lib/run_loop/cli/locale.rb,
lib/run_loop/cli/simctl.rb,
lib/run_loop/host_cache.rb,
lib/run_loop/http/error.rb,
lib/run_loop/cache/cache.rb,
lib/run_loop/environment.rb,
lib/run_loop/http/server.rb,
lib/run_loop/instruments.rb,
lib/run_loop/plist_buddy.rb,
lib/run_loop/sim_control.rb,
lib/run_loop/cli/codesign.rb,
lib/run_loop/http/request.rb,
lib/run_loop/dylib_injector.rb,
lib/run_loop/process_waiter.rb,
lib/run_loop/cli/instruments.rb,
lib/run_loop/detect_aut/xcode.rb,
lib/run_loop/detect_aut/detect.rb,
lib/run_loop/detect_aut/errors.rb,
lib/run_loop/process_terminator.rb,
lib/run_loop/http/retriable_client.rb,
lib/run_loop/detect_aut/xamarin_studio.rb

Defined Under Namespace

Modules: CLI, Color, Core, DotDir, Fifo, HTTP Classes: App, CoreSimulator, Device, Directory, Environment, IncompatibleArchitecture, Instruments, Ipa, L10N, LLDB, Lipo, PlistBuddy, ProcessTerminator, ProcessWaiter, SimControl, TimeoutError, Version, WriteFailedError, Xcode, Xcrun

Constant Summary collapse

VERSION =
"2.1.1"

Class Method Summary collapse

Class Method Details

.default_script_for_uia_strategy(uia_strategy) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/run_loop.rb', line 215

def self.default_script_for_uia_strategy(uia_strategy)
  case uia_strategy
    when :preferences
      Core.script_for_key(:run_loop_fast_uia)
    when :host
      Core.script_for_key(:run_loop_host)
    when :shared_element
      Core.script_for_key(:run_loop_shared_element)
    else
      Core.script_for_key(:run_loop_basic)
  end
end

.deprecated(version, msg) ⇒ void

This method returns an undefined value.

Prints a deprecated message that includes the line number.

Parameters:

  • version (String)

    Indicates when the feature was deprecated.

  • msg (String)

    Deprecation message (possibly suggesting alternatives)



51
52
53
54
55
56
57
58
59
# File 'lib/run_loop.rb', line 51

def self.deprecated(version, msg)

  stack = Kernel.caller(0, 6)[1..-1].join("\n")

  msg = "deprecated '#{version}' - #{msg}\n#{stack}"

  $stderr.puts "\033[34mWARN: #{msg}\033[0m"
  $stderr.flush
end

.log_debug(msg) ⇒ Object

magenta



56
57
58
59
60
# File 'lib/run_loop/logging.rb', line 56

def self.log_debug(msg)
  if RunLoop::Environment.debug?
    puts Color.magenta("DEBUG: #{msg}") if msg
  end
end

.log_error(msg) ⇒ Object

red



69
70
71
# File 'lib/run_loop/logging.rb', line 69

def self.log_error(msg)
  puts Color.red("ERROR: #{msg}") if msg
end

.log_info(*args) ⇒ Object



244
245
246
# File 'lib/run_loop.rb', line 244

def self.log_info(*args)
  RunLoop::Logging.log_info(*args)
end

.log_info2(msg) ⇒ Object

.log_info is already taken by the XTC logger. (>_O) green



64
65
66
# File 'lib/run_loop/logging.rb', line 64

def self.log_info2(msg)
  puts Color.green("INFO: #{msg}") if msg
end

.log_unix_cmd(msg) ⇒ Object

cyan



44
45
46
47
48
# File 'lib/run_loop/logging.rb', line 44

def self.log_unix_cmd(msg)
  if RunLoop::Environment.debug?
    puts Color.cyan("EXEC: #{msg}") if msg
  end
end

.log_warn(msg) ⇒ Object

blue



51
52
53
# File 'lib/run_loop/logging.rb', line 51

def self.log_warn(msg)
  puts Color.blue("WARN: #{msg}") if msg
end

.run(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
# File 'lib/run_loop.rb', line 67

def self.run(options={})

  if options[:xcuitest]
    RunLoop::XCUITest.run(options)
  else

    if RunLoop::Instruments.new.instruments_app_running?
      raise %q(The Instruments.app is open.

If the Instruments.app is open, the instruments command line tool cannot take
control of your application.

Please quit the Instruments.app and try again.)

    end

    uia_strategy = options[:uia_strategy]
    if options[:script]
      script = validate_script(options[:script])
    else
      if uia_strategy
        script = default_script_for_uia_strategy(uia_strategy)
      else
        if options[:calabash_lite]
          uia_strategy = :host
          script = Core.script_for_key(:run_loop_host)
        else
          uia_strategy = :preferences
          script = default_script_for_uia_strategy(uia_strategy)
        end
      end
    end
    # At this point, 'script' has been chosen, but uia_strategy might not
    unless uia_strategy
      desired_script = options[:script]
      if desired_script.is_a?(String) #custom path to script
        uia_strategy = :host
      elsif desired_script == :run_loop_host
        uia_strategy = :host
      elsif desired_script == :run_loop_fast_uia
        uia_strategy = :preferences
      elsif desired_script == :run_loop_shared_element
        uia_strategy = :shared_element
      else
        raise "Inconsistent state: desired script #{desired_script} has not uia_strategy"
      end
    end

    # At this point script and uia_strategy selected
    cloned_options = options.clone
    cloned_options[:script] = script
    cloned_options[:uia_strategy] = uia_strategy

    # Xcode and SimControl will not be properly cloned and we don't want
    # them to be; we want to use the exact objects that were passed.
    if options[:xcode]
      cloned_options[:xcode] = options[:xcode]
    end

    if options[:sim_control]
      cloned_options[:sim_control] = options[:sim_control]
    end

    if options[:simctl]
      cloned_options[:simctl] = options[:simctl]
    end

    Core.run_with_options(cloned_options)
  end
end

.send_command(run_loop, cmd, options = {timeout: 60}, num_retries = 0, last_error = nil) ⇒ Object



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
# File 'lib/run_loop.rb', line 138

def self.send_command(run_loop, cmd, options={timeout: 60}, num_retries=0, last_error=nil)
  if num_retries > 3
    if last_error
      raise last_error
    else
      raise "Max retries exceeded #{num_retries} > 3. No error recorded."
    end
  end

  if options.is_a?(Numeric)
    options = {timeout: options}
  end

  if not cmd.is_a?(String)
    raise "Illegal command #{cmd} (must be a string)"
  end

  if not options.is_a?(Hash)
    raise "Illegal options #{options} (must be a Hash (or number for compatibility))"
  end

  timeout = options[:timeout] || 60
  logger = options[:logger]
  interrupt_retry_timeout = options[:interrupt_retry_timeout] || 25

  expected_index = run_loop[:index]
  result = nil
  begin
    expected_index = Core.write_request(run_loop, cmd, logger)
  rescue RunLoop::WriteFailedError, Errno::EINTR => write_error
    # Attempt recover from interrupt by attempting to read result (assuming write went OK)
    # or retry if attempted read result fails
    run_loop[:index] = expected_index # restore expected index in case it changed
    log_info(logger, "Core.write_request failed: #{write_error}. Attempting recovery...")
    log_info(logger, "Attempting read in case the request was received... Please wait (#{interrupt_retry_timeout})...")
    begin
      Timeout::timeout(interrupt_retry_timeout, TimeoutError) do
        result = Core.read_response(run_loop, expected_index)
      end
      # Update run_loop expected index since we succeeded in reading the index
      run_loop[:index] = expected_index + 1
      log_info(logger, "Did read response for interrupted request of index #{expected_index}... Proceeding.")
      return result
    rescue TimeoutError => _
      log_info(logger, "Read did not result in a response for index #{expected_index}... Retrying send_command...")
      return send_command(run_loop, cmd, options, num_retries+1, write_error)
    end
  end


  begin
    Timeout::timeout(timeout, TimeoutError) do
      result = Core.read_response(run_loop, expected_index)
    end
  rescue TimeoutError => _
    raise TimeoutError, "Time out waiting for UIAutomation run-loop for command #{cmd}. Waiting for index:#{expected_index}"
  end

  result
end

.stop(run_loop, out = Dir.pwd) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/run_loop.rb', line 199

def self.stop(run_loop, out=Dir.pwd)
  return if run_loop.nil?
  results_dir = run_loop[:results_dir]
  dest = out

  RunLoop::Instruments.new.kill_instruments

  FileUtils.mkdir_p(dest)
  if results_dir
    pngs = Dir.glob(File.join(results_dir, 'Run 1', '*.png'))
  else
    pngs = []
  end
  FileUtils.cp(pngs, dest) if pngs and pngs.length > 0
end

.validate_script(script) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/run_loop.rb', line 228

def self.validate_script(script)
  if script.is_a?(String)
    unless File.exist?(script)
      raise "Unable to find file: #{script}"
    end
  elsif script.is_a?(Symbol)
    script = Core.script_for_key(script)
    unless script
      raise "Unknown script for symbol: #{script}. Options: #{Core::SCRIPTS.keys.join(', ')}"
    end
  else
    raise "Script must be a symbol or path: #{script}"
  end
  script
end