Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ _None_
### New Features

- Propose to retry when the download of GlotPress translations failed for a locale (especially useful for occurrences of `429 - Too Many Requests` quota limits) [#402]
- Add a `test_targets` parameter to the `android_firebase_test` action to be able to filter the tests to be run. [#403]

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def self.run(params)
apk_path: params[:apk_path],
test_apk_path: params[:test_apk_path],
device: device,
test_targets: params[:test_targets],
type: params[:type]
)

Expand Down Expand Up @@ -106,6 +107,13 @@ def self.available_options
UI.user_error!("Invalid test APK: #{value}") unless File.exist?(value)
end
),
FastlaneCore::ConfigItem.new(
key: :test_targets,
description: 'A list of one or more test target filters to apply',
type: String,
optional: true,
default_value: nil
),
FastlaneCore::ConfigItem.new(
key: :model,
description: 'The device model to use to run the test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ def self.preflight(verify_gcloud_binary: true, verify_logged_in: true)
# @param [FirebaseDevice] device The virtual device to run tests on.
# @param [String] type The type of test to run.
#
def self.run_tests(project_id:, apk_path:, test_apk_path:, device:, type: 'instrumentation')
def self.run_tests(project_id:, apk_path:, test_apk_path:, device:, test_targets: nil, type: 'instrumentation')
raise "Unable to find apk: #{apk_path}" unless File.file?(apk_path)
raise "Unable to find apk: #{test_apk_path}" unless File.file?(test_apk_path)
raise "Invalid Type: #{type}" unless VALID_TEST_TYPES.include?(type)

command = Shellwords.join [
'gcloud', 'firebase', 'test', 'android', 'run',
'--project', project_id,
'--type', type,
'--app', apk_path,
'--test', test_apk_path,
'--device', device.to_s,
'--verbosity', 'info',
]
params = {
project: project_id,
type: type,
app: apk_path,
test: test_apk_path,
'test-targets': test_targets,
device: device.to_s,
verbosity: 'info'
}.compact.flat_map { |k, v| ["--#{k}", v] }
command = Shellwords.join(['gcloud', 'firebase', 'test', 'android', 'run', *params])
Comment on lines +36 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AliSoftware Can you explain the benefits of switching to this syntax from the previous syntax? I'm not questioning it, just curious 😃

Copy link
Contributor Author

@AliSoftware AliSoftware Aug 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax allowed me to inject the compact call on the Hash before transforming it into an array of key-value-pairs.

The effect of that .compact is to remove any entry from the Hash with a nil value, which was necessary to completely remove --test-targets <value> from the params if the user didn't provide any value for it. Otherwise we'd be passing --test-targets "" (or actually even worse, just --test-targets without a corresponding value next to it) to the gcloud command, instead of skipping that param entirely when it's not provided by the user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thank you for explaining!


log_file_path = Fastlane::Actions.lane_context[:FIREBASE_TEST_LOG_FILE_PATH]

Expand Down
10 changes: 9 additions & 1 deletion spec/firebase_test_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
run_tests
end

it 'includes and properly escapes the test targets if any are provided' do
allow(Fastlane::Action).to receive('sh').with(
"gcloud firebase test android run --project foo-bar-baz --type instrumentation --app #{default_file} --test #{default_file} --test-targets notPackage\\ org.wordpress.android.ui.screenshots --device device --verbosity info 2>&1 | tee #{runner_temp_file}"
)
run_tests(test_targets: 'notPackage org.wordpress.android.ui.screenshots')
end

it 'properly escapes the app path' do
temp_file_path = File.join(Dir.tmpdir(), 'path with spaces.txt')
expected_temp_file_path = File.join(Dir.tmpdir(), 'path\ with\ spaces.txt')
Expand Down Expand Up @@ -66,13 +73,14 @@
expect { run_tests(type: 'foo') }.to raise_exception('Invalid Type: foo')
end

def run_tests(project_id: 'foo-bar-baz', apk_path: default_file, test_apk_path: default_file, device: 'device', type: 'instrumentation')
def run_tests(project_id: 'foo-bar-baz', apk_path: default_file, test_apk_path: default_file, device: 'device', test_targets: nil, type: 'instrumentation')
Fastlane::Actions.lane_context[:FIREBASE_TEST_LOG_FILE_PATH] = runner_temp_file
described_class.run_tests(
project_id: project_id,
apk_path: apk_path,
test_apk_path: test_apk_path,
device: device,
test_targets: test_targets,
type: type
)
end
Expand Down