3
3
describe Fastlane ::Actions ::AndroidSendAppSizeMetricsAction do
4
4
let ( :test_data_dir ) { File . join ( File . dirname ( __FILE__ ) , 'test-data' , 'app_size_metrics' ) }
5
5
6
- def test_app_size_action ( fake_aab_size :, fake_apks :, expected_payload :, **other_action_args )
6
+ def test_app_size_action ( fake_aab_size :, fake_apks :, fake_universal_apk_sizes : , expected_payload :, **other_action_args )
7
7
in_tmp_dir do |tmp_dir |
8
8
# Arrange
9
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 )
10
+ aab_path = nil
11
+ unless fake_aab_size . nil?
12
+ aab_path = File . join ( tmp_dir , 'fake.aab' )
13
+ File . write ( aab_path , '-fake-aab-file-' )
14
+ allow ( File ) . to receive ( :size ) . with ( aab_path ) . and_return ( fake_aab_size )
15
+ end
16
+ universal_apk_path = nil
17
+ unless fake_universal_apk_sizes . empty?
18
+ universal_apk_path = File . join ( tmp_dir , 'fake.apk' )
19
+ File . write ( universal_apk_path , '-fake-universal-apk-file-' )
20
+ allow ( File ) . to receive ( :size ) . with ( universal_apk_path ) . and_return ( fake_universal_apk_sizes [ 0 ] )
21
+ end
13
22
14
23
if other_action_args [ :include_split_sizes ] != false
15
24
# Arrange: fake that apkanalyzer exists
16
25
apkanalyzer_bin = File . join ( '__ANDROID_SDK_ROOT__FOR_TESTS__' , 'cmdline-tools' , 'latest' , 'bin' , 'apkanalyzer' )
17
26
allow ( described_class ) . to receive ( :find_apkanalyzer_binary ) . and_return ( apkanalyzer_bin )
18
27
allow ( File ) . to receive ( :executable? ) . with ( apkanalyzer_bin ) . and_return ( true )
19
28
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 )
29
+ unless fake_apks . empty?
30
+ # Arrange: fake that `bundletool` exists and mock its call to create fake apks with corresponding apkanalyzer calls mocks
31
+ allow ( Fastlane ::Action ) . to receive ( :sh ) . with ( 'command' , '-v' , 'bundletool' , anything )
32
+ allow ( Fastlane ::Action ) . to receive ( :sh ) . with ( 'bundletool' , 'build-apks' , '--bundle' , aab_path , '--output-format' , 'DIRECTORY' , '--output' , anything ) do |*args |
33
+ bundletool_tmpdir = args . last
34
+ FileUtils . mkdir ( File . join ( bundletool_tmpdir , 'splits' ) )
35
+ fake_apks . each do |apk_name , sizes |
36
+ apk_path = File . join ( bundletool_tmpdir , 'splits' , apk_name . to_s )
37
+ File . write ( apk_path , "Fake APK file (#{ sizes } )" )
38
+ allow ( Fastlane ::Action ) . to receive ( :sh ) . with ( apkanalyzer_bin , 'apk' , 'file-size' , apk_path , anything ) . and_return ( sizes [ 0 ] . to_s )
39
+ allow ( Fastlane ::Action ) . to receive ( :sh ) . with ( apkanalyzer_bin , 'apk' , 'download-size' , apk_path , anything ) . and_return ( sizes [ 1 ] . to_s )
40
+ end
30
41
end
31
42
end
43
+ unless fake_universal_apk_sizes . empty?
44
+ allow ( Fastlane ::Action ) . to receive ( :sh ) . with ( apkanalyzer_bin , 'apk' , 'file-size' , universal_apk_path , anything ) . and_return ( fake_universal_apk_sizes [ 1 ] . to_s )
45
+ allow ( Fastlane ::Action ) . to receive ( :sh ) . with ( apkanalyzer_bin , 'apk' , 'download-size' , universal_apk_path , anything ) . and_return ( fake_universal_apk_sizes [ 2 ] . to_s )
46
+ end
32
47
end
33
48
34
49
# Act
35
50
code = run_described_fastlane_action (
36
51
api_url : File . join ( 'file://localhost/' , output_file ) ,
37
52
aab_path : aab_path ,
53
+ universal_apk_path : universal_apk_path ,
38
54
**other_action_args
39
55
)
40
56
@@ -51,6 +67,17 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
51
67
end
52
68
53
69
context 'when `include_split_sizes` is turned off' do
70
+ let ( :common_action_args ) do
71
+ {
72
+ app_name : 'my-app' ,
73
+ app_version_name : '10.2-rc-3' ,
74
+ product_flavor : 'Vanilla' ,
75
+ build_type : 'Release' ,
76
+ source : 'unit-test' ,
77
+ include_split_sizes : false
78
+ }
79
+ end
80
+
54
81
context 'when only providing an `aab_path`' do
55
82
it 'generates the expected payload compressed by default' do
56
83
expected = {
@@ -70,13 +97,9 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
70
97
test_app_size_action (
71
98
fake_aab_size : 123_456 ,
72
99
fake_apks : { } ,
100
+ fake_universal_apk_sizes : [ ] ,
73
101
expected_payload : expected ,
74
- app_name : 'my-app' ,
75
- app_version_name : '10.2-rc-3' ,
76
- product_flavor : 'Vanilla' ,
77
- build_type : 'Release' ,
78
- source : 'unit-test' ,
79
- include_split_sizes : false
102
+ **common_action_args
80
103
)
81
104
end
82
105
@@ -92,37 +115,90 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
92
115
] ,
93
116
metrics : [
94
117
{ name : 'AAB File Size' , value : 123_456 } ,
118
+ { name : 'Universal APK File Size' , value : 56_789 } ,
95
119
]
96
120
}
97
121
98
122
test_app_size_action (
99
123
fake_aab_size : 123_456 ,
100
124
fake_apks : { } ,
125
+ fake_universal_apk_sizes : [ 56_789 ] ,
101
126
expected_payload : expected ,
102
- app_name : 'my-app' ,
103
- app_version_name : '10.2-rc-3' ,
104
- product_flavor : 'Vanilla' ,
105
- build_type : 'Release' ,
106
- source : 'unit-test' ,
107
- include_split_sizes : false ,
127
+ **common_action_args ,
108
128
use_gzip_content_encoding : false
109
129
)
110
130
end
111
131
end
112
132
113
133
context 'when only providing an `universal_apk_path`' do
114
- it 'generates the expected payload containing the apk file size'
134
+ it 'generates the expected payload containing the apk file size' do
135
+ expected = {
136
+ meta : [
137
+ { name : 'Platform' , value : 'Android' } ,
138
+ { name : 'App Name' , value : 'my-app' } ,
139
+ { name : 'App Version' , value : '10.2-rc-3' } ,
140
+ { name : 'Product Flavor' , value : 'Vanilla' } ,
141
+ { name : 'Build Type' , value : 'Release' } ,
142
+ { name : 'Source' , value : 'unit-test' } ,
143
+ ] ,
144
+ metrics : [
145
+ { name : 'Universal APK File Size' , value : 567_654_321 } ,
146
+ ]
147
+ }
148
+
149
+ test_app_size_action (
150
+ fake_aab_size : nil ,
151
+ fake_apks : { } ,
152
+ fake_universal_apk_sizes : [ 567_654_321 ] ,
153
+ expected_payload : expected ,
154
+ **common_action_args
155
+ )
156
+ end
115
157
end
116
158
117
159
context 'when providing both an `aab_path` and an `universal_apk_path`' do
118
- it 'generates the expected payload containing the aab and universal apk file size'
160
+ it 'generates the expected payload containing the aab and universal apk file size' do
161
+ expected = {
162
+ meta : [
163
+ { name : 'Platform' , value : 'Android' } ,
164
+ { name : 'App Name' , value : 'my-app' } ,
165
+ { name : 'App Version' , value : '10.2-rc-3' } ,
166
+ { name : 'Product Flavor' , value : 'Vanilla' } ,
167
+ { name : 'Build Type' , value : 'Release' } ,
168
+ { name : 'Source' , value : 'unit-test' } ,
169
+ ] ,
170
+ metrics : [
171
+ { name : 'AAB File Size' , value : 123_456 } ,
172
+ { name : 'Universal APK File Size' , value : 567_654_321 } ,
173
+ ]
174
+ }
175
+
176
+ test_app_size_action (
177
+ fake_aab_size : 123_456 ,
178
+ fake_apks : { } ,
179
+ fake_universal_apk_sizes : [ 567_654_321 ] ,
180
+ expected_payload : expected ,
181
+ **common_action_args
182
+ )
183
+ end
119
184
end
120
185
end
121
186
122
187
context 'when keeping the default value of `include_split_sizes` turned on' do
188
+ let ( :common_action_args ) do
189
+ {
190
+ app_name : 'wordpress' ,
191
+ app_version_name : '19.8-rc-3' ,
192
+ app_version_code : 1214 ,
193
+ product_flavor : 'Vanilla' ,
194
+ build_type : 'Release' ,
195
+ source : 'unit-test'
196
+ }
197
+ end
198
+
123
199
context 'when only providing an `aab_path`' do
124
200
it 'generates the expected payload containing the aab file size and optimized split sizes' do
125
- expected_fixture = File . join ( test_data_dir , 'android-metrics-payload.json' )
201
+ expected_fixture = File . join ( test_data_dir , 'android-metrics-payload-aab .json' )
126
202
expected = JSON . parse ( File . read ( expected_fixture ) )
127
203
128
204
test_app_size_action (
@@ -135,23 +211,48 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
135
211
'base-armeabi_v7a.apk' : [ 150_070 , 50_070 ] ,
136
212
'base-armeabi_v7a_2.apk' : [ 150_072 , 50_072 ]
137
213
} ,
214
+ fake_universal_apk_sizes : [ ] ,
138
215
expected_payload : expected ,
139
- app_name : 'wordpress' ,
140
- app_version_name : '19.8-rc-3' ,
141
- app_version_code : 1214 ,
142
- product_flavor : 'Vanilla' ,
143
- build_type : 'Release' ,
144
- source : 'unit-test'
216
+ **common_action_args
145
217
)
146
218
end
147
219
end
148
220
149
221
context 'when only providing an `universal_apk_path`' do
150
- it 'generates the expected payload containing the apk file size and optimized file and download sizes'
222
+ it 'generates the expected payload containing the apk file size and optimized file and download sizes' do
223
+ expected_fixture = File . join ( test_data_dir , 'android-metrics-payload-apk.json' )
224
+ expected = JSON . parse ( File . read ( expected_fixture ) )
225
+
226
+ test_app_size_action (
227
+ fake_aab_size : nil ,
228
+ fake_apks : { } ,
229
+ fake_universal_apk_sizes : [ 567_654_321 , 555_000 , 533_000 ] ,
230
+ expected_payload : expected ,
231
+ **common_action_args
232
+ )
233
+ end
151
234
end
152
235
153
236
context 'when providing both an `aab_path` and an `universal_apk_path`' do
154
- it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits'
237
+ it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits' do
238
+ expected_fixture = File . join ( test_data_dir , 'android-metrics-payload-aab+apk.json' )
239
+ expected = JSON . parse ( File . read ( expected_fixture ) )
240
+
241
+ test_app_size_action (
242
+ fake_aab_size : 987_654_321 ,
243
+ fake_apks : {
244
+ 'base-arm64_v8a.apk' : [ 164_080 , 64_080 ] ,
245
+ 'base-arm64_v8a_2.apk' : [ 164_082 , 64_082 ] ,
246
+ 'base-armeabi.apk' : [ 150_000 , 50_000 ] ,
247
+ 'base-armeabi_2.apk' : [ 150_002 , 50_002 ] ,
248
+ 'base-armeabi_v7a.apk' : [ 150_070 , 50_070 ] ,
249
+ 'base-armeabi_v7a_2.apk' : [ 150_072 , 50_072 ]
250
+ } ,
251
+ fake_universal_apk_sizes : [ 567_654_321 , 555_000 , 533_000 ] ,
252
+ expected_payload : expected ,
253
+ **common_action_args
254
+ )
255
+ end
155
256
end
156
257
end
157
258
end
0 commit comments