Class: XamarinTestCloud::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/xamarin-test-cloud-appium/cli.rb

Constant Summary collapse

FILE_UPLOAD_ENDPOINT =
'upload2'
FORM_URL_ENCODED_ENDPOINT =
'upload'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 42

def initialize(*args)
  self.session_id = SecureRandom.hex
  begin
    r = JSON.parse(http_post("check_version", {args: ARGV}))
    if r["error_message"]
      puts r["error_message"]
      exit 1
    end
  rescue
  end
  super(*args)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def api_key
  @api_key
end

#appObject

Returns the value of attribute app.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def app
  @app
end

#appiumObject

Returns the value of attribute appium.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def appium
  @appium
end

#appnameObject

Returns the value of attribute appname.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def appname
  @appname
end

#asyncObject

Returns the value of attribute async.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def async
  @async
end

#async_jsonObject

Returns the value of attribute async_json.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def async_json
  @async_json
end

#configObject

Returns the value of attribute config.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def config
  @config
end

#device_selectionObject

Returns the value of attribute device_selection.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def device_selection
  @device_selection
end

#dry_runObject

Returns the value of attribute dry_run.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def dry_run
  @dry_run
end

#dsymObject

Returns the value of attribute dsym.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def dsym
  @dsym
end

#endpoint_pathObject

Returns the value of attribute endpoint_path.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def endpoint_path
  @endpoint_path
end

#localeObject

Returns the value of attribute locale.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def locale
  @locale
end

#prettyObject

Returns the value of attribute pretty.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def pretty
  @pretty
end

#priorityObject

Returns the value of attribute priority.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def priority
  @priority
end

#profileObject

Returns the value of attribute profile.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def profile
  @profile
end

#seriesObject

Returns the value of attribute series.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def series
  @series
end

#session_idObject

Returns the value of attribute session_id.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def session_id
  @session_id
end

#skip_config_checkObject

Returns the value of attribute skip_config_check.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def skip_config_check
  @skip_config_check
end

#test_parametersObject

Returns the value of attribute test_parameters.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def test_parameters
  @test_parameters
end

#userObject

Returns the value of attribute user.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def user
  @user
end

#workspaceObject

Returns the value of attribute workspace.



32
33
34
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 32

def workspace
  @workspace
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 38

def self.exit_on_failure?
  true
end

.source_rootObject



59
60
61
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 59

def self.source_root
  File.join(File.dirname(__FILE__), '..')
end

Instance Method Details

#submit(app, api_key) ⇒ Object



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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 155

def submit(app, api_key)

  self.pretty = options[:pretty]
  self.async_json = options['async-json']
  self.async = options[:async] || self.async_json

  # Async mode wraps all console output in a json object
  # So we need to intercept all writes to $stdout
  if self.async_json
    @async_log = StringIO.new
    @async_result = {
      test_run_id: nil,
      error_messages: [],
      log: []
    }
    $stdout = @async_log
  end

  app_path = File.expand_path(app)
  unless File.exist?(app_path)
    raise ValidationError, "App is not a file: #{app_path}"
  end

  if shared_runtime?(app_path)
    puts "Xamarin Test Cloud doesn't yet support shared runtime apps."
    puts "To test your app it needs to be compiled for release."
    puts "You can learn how to compile you app for release here:"
    puts "http://docs.xamarin.com/guides/android/deployment%2C_testing%2C_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release"
    raise ValidationError, "Aborting"
  end

  app_extension = File.extname(app_path)
  unless /ipa/i.match(app_extension) || /apk/i.match(app_extension)
    raise ValidationError, "App #{app_path} must be an .ipa or .apk file"
  end


  self.app = app_path

  self.user = options['user']

  self.dry_run = options['dry-run']

  self.api_key = api_key

  self.test_parameters = options['test-parameters'] || {}

  self.appname = options['app-name']

  self.device_selection = options['devices']

  device_selection_data = validate_device_selection

  self.locale = options['locale']

  self.series = options['series']

  self.priority = options['priority']

  self.skip_config_check = options['skip-config-check']

  self.dsym = options['dsym-file']

  self.appium = true

  if dsym
    dsym_extension = File.extname(self.dsym)
    unless /dsym/i.match(dsym_extension) && File.directory?(dsym)
      raise ValidationError, "dsym-file must be a directory and have dSYM extension: #{dsym}"
    end
  end

  workspace_path = options[:workspace] || File.expand_path('.')

  unless File.directory?(workspace_path)
    raise ValidationError, "Provided workspace: #{workspace_path} is not a directory."
  end

  workspace_basename = File.basename(workspace_path)
  if workspace_basename.downcase == 'features'
    self.workspace = File.expand_path(File.join(workspace_path, '..'))
    puts "Deriving workspace #{self.workspace} from features folder #{workspace_basename}"
  else
    self.workspace = File.expand_path(workspace_path)
  end

  self.workspace = File.join(self.workspace, File::Separator)


  unless appium || File.directory?(File.join(self.workspace, 'features'))
    log_header "Did not find features folder in workspace #{self.workspace}"
    puts "Either run the test-cloud command from the directory containing your features"
    puts "or use the --workspace option to refer to this directory"
    puts "See also test-cloud help submit"
    raise ValidationError, "Unable to find features folder in #{self.workspace}"
  end

  parse_and_set_config_and_profile
  unless self.skip_config_check
    default_config = File.join(self.workspace, 'config', 'cucumber.yml')
    if File.exist?(default_config) && self.config.nil?
      log_header 'Warning: Detected cucumber.yml config file, but no --config specified'
      puts "Please specify --config #{default_config}"
      puts 'and specify a profile via --profile'
      puts 'If you know what you are doing you can skip this check with'
      puts '--skip-config-check'
      raise ValidationError, "#{default_config} detected but no profile selected."
    end
  end

  if debug?
    puts "Host = #{self.host}"
    puts "User = #{self.user}"
    puts "App = #{self.app}"
    puts "App Name = #{self.app}"
    puts "TestParams = #{self.test_parameters}"
    puts "API Key = #{self.api_key}"
    puts "Device Selection = #{self.device_selection}"
    puts "Workspace = #{self.workspace}"
    puts "Config = #{self.config}"
    puts "Profile = #{self.profile}"
    puts "dSym = #{self.dsym}"
  end


  #Argument parsing done

  test_jon_data = submit_test_job(device_selection_data)
  if self.dry_run
    return
  end
  json = test_jon_data[:body]
  if debug?
    p json
  end

  log_header('Test enqueued')
  puts "User: #{json['user_email']}"
  puts "Team: #{json['team']}" if json['team']


  rejected_devices = json['rejected_devices']
  if rejected_devices && rejected_devices.size > 0
    puts 'Skipping devices (you can update your selections via https://testcloud.xamarin.com)'
    rejected_devices.each { |d| puts d }
  end
  puts ''

  puts 'Running on Devices:'
  json['devices'].each do |device|
    puts device
  end
  puts ''


  unless self.async
    wait_for_job(json['id'])
  else
    log 'Async mode: not awaiting test results'
    @async_result[:test_run_id] = json['id'] if self.async_json
  end

rescue XamarinTestCloud::ValidationError => e
  if self.async_json
    @async_result[:error_messages] << e.message
  else
    raise
  end

ensure
  $stdout = STDOUT
  if self.async_json
    process_async_log
    puts @async_result.to_json
  end
end

#versionObject



65
66
67
# File 'lib/xamarin-test-cloud-appium/cli.rb', line 65

def version
  puts XamarinTestCloud::VERSION
end