|
| 1 | +require_relative './spec_helper' |
| 2 | + |
| 3 | +describe Fastlane::Actions::AndroidSendAppSizeMetricsAction do |
| 4 | + let(:test_data_dir) { File.join(File.dirname(__FILE__), 'test-data', 'app_size_metrics') } |
| 5 | + |
| 6 | + def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_action_args) |
| 7 | + in_tmp_dir do |tmp_dir| |
| 8 | + # Arrange |
| 9 | + output_file = File.join(tmp_dir, 'output-payload') |
| 10 | + aab_path = File.join(tmp_dir, 'fake.aab') |
| 11 | + File.write(aab_path, '-fake-aab-file-') |
| 12 | + allow(File).to receive(:size).with(aab_path).and_return(fake_aab_size) |
| 13 | + |
| 14 | + if other_action_args[:include_split_sizes] != false |
| 15 | + # Arrange: fake that apkanalyzer exists |
| 16 | + ENV['ANDROID_SDK_ROOT'] = '__ANDROID_SDK_ROOT__FOR_TESTS__' |
| 17 | + apkanalyzer_bin = File.join('__ANDROID_SDK_ROOT__FOR_TESTS__', 'cmdline-tools', 'latest', 'bin', 'apkanalyzer') |
| 18 | + allow(File).to receive(:executable?).with(apkanalyzer_bin).and_return(true) |
| 19 | + |
| 20 | + # Arrange: fake that bundletool exists and mock its call to create fake apks with corresponding apkanalyzer calls mocks |
| 21 | + allow(Fastlane::Action).to receive(:sh).with('command', '-v', 'bundletool', anything) |
| 22 | + allow(Fastlane::Action).to receive(:sh).with('bundletool', 'build-apks', '--bundle', aab_path, '--output-format', 'DIRECTORY', '--output', anything) do |*args| |
| 23 | + bundletool_tmpdir = args.last |
| 24 | + FileUtils.mkdir(File.join(bundletool_tmpdir, 'splits')) |
| 25 | + fake_apks.each do |apk_name, sizes| |
| 26 | + apk_path = File.join(bundletool_tmpdir, 'splits', apk_name.to_s) |
| 27 | + File.write(apk_path, "Fake APK file (#{sizes})") |
| 28 | + allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'file-size', apk_path, anything).and_return(sizes[0].to_s) |
| 29 | + allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'download-size', apk_path, anything).and_return(sizes[1].to_s) |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + # Act |
| 35 | + code = run_described_fastlane_action( |
| 36 | + api_base_url: File.join('file://localhost/', output_file), |
| 37 | + aab_path: aab_path, |
| 38 | + **other_action_args |
| 39 | + ) |
| 40 | + |
| 41 | + # Asserts |
| 42 | + expect(code).to eq(201) |
| 43 | + expect(File).to exist(output_file) |
| 44 | + generated_payload = File.read(output_file) |
| 45 | + unless other_action_args[:use_gzip_content_encoding] === false |
| 46 | + expect do |
| 47 | + generated_payload = Zlib.gunzip(generated_payload) |
| 48 | + end.not_to raise_error, 'Payload was not valid GZipped data' |
| 49 | + end |
| 50 | + # Compare the payloads as pretty-formatted JSON, to make the diff in test failures more readable if one happen |
| 51 | + expect(JSON.pretty_generate(JSON.parse(generated_payload))).to eq(JSON.pretty_generate(expected_payload)), 'Decompressed JSON payload was not as expected' |
| 52 | + # Compare the payloads as raw uncompressed data as a final check |
| 53 | + expect(generated_payload).to eq(expected_payload.to_json) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + it 'sends the right payload from an aab file, without split size data' do |
| 58 | + expected = { |
| 59 | + meta: [ |
| 60 | + { name: 'Platform', value: 'Android' }, |
| 61 | + { name: 'App Name', value: 'my-app' }, |
| 62 | + { name: 'App Version', value: '10.2-rc-3' }, |
| 63 | + { name: 'Product Flavor', value: 'Vanilla' }, |
| 64 | + { name: 'Build Type', value: 'Release' }, |
| 65 | + { name: 'Source', value: 'unit-test' }, |
| 66 | + ], |
| 67 | + metrics: [ |
| 68 | + { name: 'AAB File Size', value: 123_456 }, |
| 69 | + ] |
| 70 | + } |
| 71 | + |
| 72 | + test_app_size_action( |
| 73 | + fake_aab_size: 123_456, |
| 74 | + fake_apks: {}, |
| 75 | + expected_payload: expected, |
| 76 | + app_name: 'my-app', |
| 77 | + app_version_name: '10.2-rc-3', |
| 78 | + build_type: 'Release', |
| 79 | + product_flavor: 'Vanilla', |
| 80 | + source: 'unit-test', |
| 81 | + include_split_sizes: false |
| 82 | + ) |
| 83 | + end |
| 84 | + |
| 85 | + it 'sends the right payload from an aab file, including split size data' do |
| 86 | + expected_fixture = File.join(test_data_dir, 'android-metrics-payload.json') |
| 87 | + expected = JSON.parse(File.read(expected_fixture)) |
| 88 | + |
| 89 | + test_app_size_action( |
| 90 | + fake_aab_size: 987_654_321, |
| 91 | + fake_apks: { |
| 92 | + 'base-arm64_v8a.apk': [164_080, 64_080], |
| 93 | + 'base-arm64_v8a_2.apk': [164_082, 64_082], |
| 94 | + 'base-armeabi.apk': [150_000, 50_000], |
| 95 | + 'base-armeabi_2.apk': [150_002, 50_002], |
| 96 | + 'base-armeabi_v7a.apk': [150_070, 50_070], |
| 97 | + 'base-armeabi_v7a_2.apk': [150_072, 50_072], |
| 98 | + }, |
| 99 | + expected_payload: expected, |
| 100 | + app_name: 'wordpress', |
| 101 | + app_version_name: '19.8-rc-3', |
| 102 | + app_version_code: 1214, |
| 103 | + build_type: 'fake-beta' |
| 104 | + ) |
| 105 | + end |
| 106 | +end |
0 commit comments