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