|
| 1 | +require 'securerandom' |
| 2 | + |
| 3 | +module Fastlane |
| 4 | + module Actions |
| 5 | + module SharedValues |
| 6 | + FIREBASE_TEST_RESULT = :FIREBASE_TEST_LOG_FILE |
| 7 | + FIREBASE_TEST_LOG_FILE_PATH = :FIREBASE_TEST_LOG_FILE_PATH |
| 8 | + end |
| 9 | + |
| 10 | + class AndroidFirebaseTestAction < Action |
| 11 | + def self.run(params) |
| 12 | + validate_options(params) |
| 13 | + |
| 14 | + UI.user_error!('You must be logged in to Firebase prior to calling this action. Use the `FirebaseLogin` Action to log in if needed') unless Fastlane::FirebaseAccount.authenticated? |
| 15 | + |
| 16 | + # Log in to Firebase (and validate credentials) |
| 17 | + run_uuid = params[:test_run_id] || SecureRandom.uuid |
| 18 | + test_dir = params[:results_output_dir] || File.join(Dir.tmpdir(), run_uuid) |
| 19 | + |
| 20 | + # Set up the log file and output directory |
| 21 | + FileUtils.mkdir_p(test_dir) |
| 22 | + Fastlane::Actions.lane_context[:FIREBASE_TEST_LOG_FILE_PATH] = File.join(test_dir, 'output.log') |
| 23 | + |
| 24 | + device = Fastlane::FirebaseDevice.new( |
| 25 | + model: params[:model], |
| 26 | + version: params[:version], |
| 27 | + locale: params[:locale], |
| 28 | + orientation: params[:orientation] |
| 29 | + ) |
| 30 | + |
| 31 | + result = FirebaseTestRunner.run_tests( |
| 32 | + project_id: params[:project_id], |
| 33 | + apk_path: params[:apk_path], |
| 34 | + test_apk_path: params[:test_apk_path], |
| 35 | + device: device, |
| 36 | + type: params[:type] |
| 37 | + ) |
| 38 | + |
| 39 | + # Download all of the outputs from the job to the local machine |
| 40 | + FirebaseTestRunner.download_result_files( |
| 41 | + result: result, |
| 42 | + destination: test_dir, |
| 43 | + project_id: params[:project_id], |
| 44 | + key_file_path: params[:key_file] |
| 45 | + ) |
| 46 | + |
| 47 | + FastlaneCore::UI.test_failure! "Firebase Tests failed – more information can be found at #{result.more_details_url}" unless result.success? |
| 48 | + |
| 49 | + UI.success 'Firebase Tests Complete' |
| 50 | + end |
| 51 | + |
| 52 | + # Fastlane doesn't eagerly validate options for us, so we'll do it first to have control over |
| 53 | + # when they're evalutated. |
| 54 | + def self.validate_options(params) |
| 55 | + available_options |
| 56 | + .reject { |opt| opt.optional || !opt.default_value.nil? } |
| 57 | + .map(&:key) |
| 58 | + .each { |k| params[k] } |
| 59 | + end |
| 60 | + |
| 61 | + ##################################################### |
| 62 | + # @!group Documentation |
| 63 | + ##################################################### |
| 64 | + |
| 65 | + def self.description |
| 66 | + 'Runs the specified tests in Firebase Test Lab' |
| 67 | + end |
| 68 | + |
| 69 | + def self.details |
| 70 | + description |
| 71 | + end |
| 72 | + |
| 73 | + def self.available_options |
| 74 | + [ |
| 75 | + FastlaneCore::ConfigItem.new( |
| 76 | + key: :project_id, |
| 77 | + # `env_name` comes from the Google Cloud default: https://cloud.google.com/functions/docs/configuring/env-var |
| 78 | + env_name: 'GCP_PROJECT', |
| 79 | + description: 'The Project ID to test in', |
| 80 | + type: String |
| 81 | + ), |
| 82 | + FastlaneCore::ConfigItem.new( |
| 83 | + key: :key_file, |
| 84 | + description: 'The key file used to authorize with Google Cloud', |
| 85 | + type: String, |
| 86 | + verify_block: proc do |value| |
| 87 | + UI.user_error!('The `:key_file` parameter is required') if value.empty? |
| 88 | + UI.user_error!("No Google Cloud Key file found at: #{value}") unless File.exist?(value) |
| 89 | + end |
| 90 | + ), |
| 91 | + FastlaneCore::ConfigItem.new( |
| 92 | + key: :apk_path, |
| 93 | + description: 'Path to the application APK on the local machine', |
| 94 | + type: String, |
| 95 | + verify_block: proc do |value| |
| 96 | + UI.user_error!('The `:apk_path` parameter is required') if value.empty? |
| 97 | + UI.user_error!("Invalid application APK: #{value}") unless File.exist?(value) |
| 98 | + end |
| 99 | + ), |
| 100 | + FastlaneCore::ConfigItem.new( |
| 101 | + key: :test_apk_path, |
| 102 | + description: 'Path to the test bundle APK on the local machine', |
| 103 | + type: String, |
| 104 | + verify_block: proc do |value| |
| 105 | + UI.user_error!('The `:test_apk_path` parameter is required') if value.empty? |
| 106 | + UI.user_error!("Invalid test APK: #{value}") unless File.exist?(value) |
| 107 | + end |
| 108 | + ), |
| 109 | + FastlaneCore::ConfigItem.new( |
| 110 | + key: :model, |
| 111 | + description: 'The device model to use to run the test', |
| 112 | + type: String, |
| 113 | + verify_block: proc do |value| |
| 114 | + UI.user_error!('The `:model` parameter is required') if value.empty? |
| 115 | + FirebaseTestRunner.verify_has_gcloud_binary! |
| 116 | + model_names = Fastlane::FirebaseDevice.valid_model_names |
| 117 | + UI.user_error!("Invalid Model Name: #{value}. Valid Model Names: #{model_names}") unless model_names.include?(value) |
| 118 | + end |
| 119 | + ), |
| 120 | + FastlaneCore::ConfigItem.new( |
| 121 | + key: :version, |
| 122 | + description: 'The Android version (API Level) to use to run the test', |
| 123 | + type: Integer, |
| 124 | + verify_block: proc do |value| |
| 125 | + FirebaseTestRunner.verify_has_gcloud_binary! |
| 126 | + version_numbers = Fastlane::FirebaseDevice.valid_version_numbers |
| 127 | + UI.user_error!("Invalid Version Number: #{value}. Valid Version Numbers: #{version_numbers}") unless version_numbers.include?(value) |
| 128 | + end |
| 129 | + ), |
| 130 | + FastlaneCore::ConfigItem.new( |
| 131 | + key: :locale, |
| 132 | + description: 'The locale code to use when running the test', |
| 133 | + type: String, |
| 134 | + default_value: 'en', |
| 135 | + verify_block: proc do |value| |
| 136 | + FirebaseTestRunner.verify_has_gcloud_binary! |
| 137 | + locale_codes = Fastlane::FirebaseDevice.valid_locales |
| 138 | + UI.user_error!("Invalid Locale: #{value}. Valid Locales: #{locale_codes}") unless locale_codes.include?(value) |
| 139 | + end |
| 140 | + ), |
| 141 | + FastlaneCore::ConfigItem.new( |
| 142 | + key: :orientation, |
| 143 | + description: 'Which orientation to run the device in', |
| 144 | + type: String, |
| 145 | + default_value: 'portrait', |
| 146 | + verify_block: proc do |value| |
| 147 | + orientations = Fastlane::FirebaseDevice.valid_orientations |
| 148 | + UI.user_error!("Invalid Orientation: #{value}. Valid Orientations: #{orientations}") unless orientations.include?(value) |
| 149 | + end |
| 150 | + ), |
| 151 | + FastlaneCore::ConfigItem.new( |
| 152 | + key: :type, |
| 153 | + description: 'The type of test to run (e.g. `instrumentation` or `robo`)', |
| 154 | + type: String, |
| 155 | + default_value: 'instrumentation', |
| 156 | + verify_block: proc do |value| |
| 157 | + types = Fastlane::FirebaseTestRunner::VALID_TEST_TYPES |
| 158 | + UI.user_error!("Invalid Test Type: #{value}. Valid Types: #{types}") unless types.include?(value) |
| 159 | + end |
| 160 | + ), |
| 161 | + FastlaneCore::ConfigItem.new( |
| 162 | + key: :test_run_id, |
| 163 | + description: 'A unique ID used to identify this test run', |
| 164 | + default_value_dynamic: true, |
| 165 | + optional: true, |
| 166 | + type: String |
| 167 | + ), |
| 168 | + FastlaneCore::ConfigItem.new( |
| 169 | + key: :results_output_dir, |
| 170 | + description: 'The path to the folder where we will store the results of this test run', |
| 171 | + default_value_dynamic: true, |
| 172 | + optional: true, |
| 173 | + type: String |
| 174 | + ), |
| 175 | + ] |
| 176 | + end |
| 177 | + |
| 178 | + def self.authors |
| 179 | + ['Automattic'] |
| 180 | + end |
| 181 | + |
| 182 | + def self.is_supported?(platform) |
| 183 | + platform == :android |
| 184 | + end |
| 185 | + end |
| 186 | + end |
| 187 | +end |
0 commit comments