Class: Fastlane::Actions::TryAdbTestAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



98
99
100
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 98

def self.authors
  ["alteral"]
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 102

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :apk,
                         description: "Either relative or absolute path to application apk that needs to be tested",
                            optional: false,
                           is_string: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :test_apk,
                         description: "Either relative or absolute path to apk with tests",
                            optional: false,
                           is_string: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :test_runner,
                         description: "Fully qualified name of test runner class you're using",
                            optional: true,
                       default_value: '',
                           is_string: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :output_directory,
                         description: "Either relative or absolute path to directory for output: reports, files from devices and so on",
                            optional: true,
                             default_value: "fastlane/output",
                           is_string: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :instrumentation_arguments,
                         description: "Key-value pairs to pass to Instrumentation Runner",
                            optional: true,
                           is_string: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :device_pattern,
                         description: "Connected devices/emulators that will be used to run tests against (example: 'emulator.+')",
                       default_value: '',
                            optional: true,
                           is_string: true,
                                type: String,
                 conflicting_options: [:devices]),
    FastlaneCore::ConfigItem.new(key: :devices,
                         description: "Connected devices/emulators that will be used to run tests against (example: 'emulator-5554 emulator-5556')",
                       default_value: '',
                            optional: true,
                           is_string: true,
                                type: String,
                 conflicting_options: [:device_pattern]),
    FastlaneCore::ConfigItem.new(key: :verbose,
                         description: "Either true or false to enable/disable verbose output for Composer",
                       default_value: false,
                            optional: true,
                           is_string: false,
                                type: Boolean),
    FastlaneCore::ConfigItem.new(key: :shard,
                         description: "Enable/disable test sharding which statically shards tests between available devices/emulators",
                       default_value: true,
                            optional: true,
                           is_string: false,
                                type: Boolean),
    FastlaneCore::ConfigItem.new(key: :fail_if_no_tests,
                         description: "Either true or false to enable/disable error on empty test suite",
                       default_value: true,
                            optional: true,
                           is_string: false,
                                type: Boolean),
    FastlaneCore::ConfigItem.new(key: :extra_apks,
                         description: "Apks to be installed for utilities. What you would typically declare in gradle as androidTestUtil",
                       default_value: '',
                            optional: true,
                           is_string: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :install_timeout,
                         description: "APK installation timeout in seconds. Applicable to both test APK and APK under test",
                       default_value: 120,
                            optional: true,
                           is_string: false,
                                type: Integer),
    FastlaneCore::ConfigItem.new(key: :try_count,
                         description: "Number of times to try to get your tests green",
                            optional: true,
                       default_value: 1,
                           is_string: false,
                                type: Integer)
  ]
end

.clean_up(params) ⇒ Object



12
13
14
15
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 12

def self.clean_up(params)
  FileUtils.rm_rf(params[:output_directory])
  FileUtils.mkdir_p(params[:output_directory])
end

.composer(params:, instrumentation_arguments:, output_directory:, fail_build:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 24

def self.composer(params:,
                  instrumentation_arguments:,
                  output_directory:,
                  fail_build:)
  begin
    test_runner = params[:test_runner].empty? ? '' : "--test-runner #{params[:test_runner]}"
    extra_apks = params[:extra_apks].empty? ? '' : "--extra-apks #{params[:extra_apks]}"
    devices =
      if !params[:device_pattern].empty?
        "--device-pattern #{params[:device_pattern]}"
      elsif !params[:devices].empty?
        "--devices #{params[:devices]}"
      else
        ''
      end

    Actions.sh(
      "java -jar #{params[:output_directory]}/composer.jar" \
        " --apk #{params[:apk]}" \
        " --test-apk #{params[:test_apk]}" \
        " --output-directory #{output_directory}" \
        " --instrumentation-arguments #{instrumentation_arguments}" \
        " --verbose-output #{params[:verbose]}" \
        " --install-timeout #{params[:install_timeout]}" \
        " --fail-if-no-tests #{params[:fail_if_no_tests]}" \
        " --shard #{params[:shard]}" \
        " #{test_runner}" \
        " #{devices}" \
        " #{extra_apks}"
    )
  rescue
    raise FastlaneCore::UI.test_failure!('Tests have failed') if fail_build
  end
end

.download_composer(params) ⇒ Object



17
18
19
20
21
22
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 17

def self.download_composer(params)
  composer_version = TryAdbTest::COMPOSER_VERSION
  composer_url = "https://jcenter.bintray.com/com/gojuno/composer/composer/#{composer_version}/composer-#{composer_version}.jar"
  curl = Curl.get(composer_url)
  File.open("#{params[:output_directory]}/composer.jar", 'w+') { |f| f.write(curl.body_str) }
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 184

def self.is_supported?(platform)
  [:android].include?(platform)
end

.run(params) ⇒ Object



6
7
8
9
10
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 6

def self.run(params)
  clean_up(params)
  download_composer(params)
  run_tests(params)
end

.run_tests(params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 59

def self.run_tests(params)
  attempt = 1
  while attempt <= params[:try_count]
    FastlaneCore::UI.important("Getting started #{attempt} shot\n")
    output_directory = "#{params[:output_directory]}/#{attempt}_run"
    instrumentation_arguments =
      if @failed_tests.nil?
        params[:instrumentation_arguments]
      else
        "class #{@failed_tests.join(',')}"
      end
    fail_build = (attempt >= params[:try_count]) ? true : false
    composer(
      params: params,
      instrumentation_arguments: instrumentation_arguments,
      output_directory: output_directory,
      fail_build: fail_build
    )
    @failed_tests = scan_test_report(output_directory)
    break if @failed_tests.empty?

    attempt += 1
  end
end

.scan_test_report(output_directory) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fastlane/plugin/try_adb_test/actions/try_adb_test_action.rb', line 84

def self.scan_test_report(output_directory)
  junit = "#{output_directory}/junit4-reports/"
  output_files = Dir.glob(File.join(junit, '**', '*')).select { |f| File.file?(f) }
  failed_tests = []
  output_files.each do |report|
    Nokogiri::XML(File.open(report)).root.xpath('//failure/..').each do |failure|
      test_name = failure.xpath('@name')
      class_name = failure.xpath('@classname')
      failed_tests << "#{class_name}##{test_name}"
    end
  end
  failed_tests
end