Class: Fastlane::Saucectl::AndroidSuites

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/fastlane/plugin/saucectl/helper/android_suites.rb

Overview

This class will create test suites based on user specified configuration properties

Constant Summary collapse

UI =
FastlaneCore::UI

Constants included from FileUtils

FileUtils::CLASS_NAME_REGEX, FileUtils::FILE_TYPE_REGEX

Instance Method Summary collapse

Methods included from FileUtils

#find, #read_file, #search_retrieve_test_classes, #syscall

Constructor Details

#initialize(config) ⇒ AndroidSuites



17
18
19
20
21
22
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 17

def initialize(config)
  UI.user_error!("#{config[:kind]} is not a supported test framework for android. Use espresso") unless config[:kind].eql?('espresso')
  @devices = config[:devices].nil? ? config[:emulators] : config[:devices]
  @is_real_device = config[:emulators].nil?
  @config = config
end

Instance Method Details

#android_test_optionsObject



172
173
174
175
176
177
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 172

def android_test_options
  {
    'clearPackageData' => @config[:clear_data],
    'useTestOrchestrator' => @config[:use_test_orchestrator]
  }
end

#create_test_distribution_suiteObject



88
89
90
91
92
93
94
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 88

def create_test_distribution_suite
  if @config[:test_distribution].eql?('shard')
    shard_suites
  else
    test_distribution_suite
  end
end

#custom_test_class_suiteObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 75

def custom_test_class_suite
  test_suites = []
  @devices.each do |device|
    device_options = @is_real_device ? real_device_options(device) : virtual_device_options(device)
    test_classes = @config[:test_class].reject(&:empty?).join(',')
    test_suites << {
      'name' => suite_name(device[:name]).downcase,
      'testOptions' => test_option_type(test_classes.split(','))
    }.merge(device_options)
  end
  test_suites
end

#device_options(device) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 149

def device_options(device)
  {
    'carrierConnectivity' => device[:carrier_connectivity],
    'deviceType' => device[:device_type].upcase!,
    'private' => device[:private]
  }
end

#generateObject



24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 24

def generate
  if @config[:test_distribution] || @config[:size] || @config[:annotation]
    create_test_distribution_suite
  elsif @config[:test_class].kind_of?(Array)
    custom_test_class_suite
  else
    test_runner_suite
  end
end

#rdc_options(device) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 133

def rdc_options(device)
  device_type_key = device.key?(:id) ? 'id' : 'name'
  name = device.key?(:id) ? device[:id] : device[:name]

  base_device_hash = {
    device_type_key => name,
    'orientation' => device[:orientation]
  }.merge('options' => device_options(device))

  unless device[:platform_version].nil?
    base_device_hash = base_device_hash.merge({ 'platformVersion' => device[:platform_version] })
  end

  base_device_hash
end

#real_device_options(device) ⇒ Object



129
130
131
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 129

def real_device_options(device)
  { 'devices' => [rdc_options(device)] }
end

#shard_suitesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 46

def shard_suites
  UI.user_error!("❌ Cannot split #{@config[:test_distribution]}'s across devices with a single device/emulator. \nPlease specify a minimum of two devices/emulators!") if @devices.size.eql?(1)

  test_suites = []
  arr = test_distribution_array
  shards = arr.each_slice((arr.size / @devices.size.to_f).round).to_a
  shards.each_with_index do |suite, i|
    device_options = @is_real_device ? real_device_options(@devices[i]) : virtual_device_options(@devices[i])
    test_suites << {
      'name' => suite_name("shard #{i + 1}").downcase,
      'testOptions' => test_option_type(suite)
    }.merge(device_options)
  end
  test_suites
end

#suite_name(test_type) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 38

def suite_name(test_type)
  if ENV['JOB_NAME'].nil? && ENV['BUILD_NUMBER'].nil?
    "#{@config[:kind]}-#{test_type}"
  else
    "#{ENV['JOB_NAME']}-#{ENV['BUILD_NUMBER']}-#{test_type}"
  end
end

#test_distribution_arrayObject



34
35
36
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 34

def test_distribution_array
  @config[:test_class] || Fastlane::Saucectl::Espresso.new(@config).test_distribution
end

#test_distribution_suiteObject



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
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 96

def test_distribution_suite
  UI.user_error!("❌ to distribute tests you must specify the path to your espresso tests. For example `path_to_tests:someModule/src/androidTest`") if @config[:path_to_tests].nil?

  test_suites = []
  if @config[:size] || @config[:annotation]
    @devices.each do |device|
      device_options = @is_real_device ? real_device_options(device) : virtual_device_options(device)
      test_suites << {
        'name' => suite_name(device[:name]).downcase,
        'testOptions' => test_option_type
      }.merge(device_options)
    end
  else
    @devices.each do |device|
      device_options = @is_real_device ? real_device_options(device) : virtual_device_options(device)
      test_distribution_array.each do |test_type|
        test_suites << {
          'name' => suite_name(test_type).downcase,
          'testOptions' => test_option_type(test_type)
        }.merge(device_options)
      end
    end
  end
  test_suites
end

#test_option_type(test_type = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 157

def test_option_type(test_type = nil)
  if @config[:size] || @config[:annotation]
    key = @config[:size] ? 'size' : 'annotation'
    value = @config[:size] || @config[:annotation]
    { key => value }.merge(android_test_options)
  else
    if test_type.nil?
      android_test_options
    else
      test_option_type = @config[:test_distribution].eql?('package') ? 'package' : 'class'
      { test_option_type => test_type }.merge(android_test_options)
    end
  end
end

#test_runner_suiteObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 62

def test_runner_suite
  test_suites = []
  @devices.each do |device|
    device_options = @is_real_device ? real_device_options(device) : virtual_device_options(device)
    device_name = device.key?(:id) ? device[:id] : device[:name]
    test_suites << {
      'name' => suite_name(device_name),
      'testOptions' => test_option_type
    }.merge(device_options)
  end
  test_suites
end

#virtual_device_options(device) ⇒ Object



122
123
124
125
126
127
# File 'lib/fastlane/plugin/saucectl/helper/android_suites.rb', line 122

def virtual_device_options(device)
  platform_versions = device[:platform_versions].reject(&:empty?).join(',')
  { 'emulators' => [{ 'name' => device[:name],
                      'orientation' => device[:orientation],
                      'platformVersions' => platform_versions.split(',') }] }
end