Skip to content

Commit 95726ce

Browse files
committed
Add unit test for writing to file
1 parent 891ce87 commit 95726ce

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/models/app_size_metrics_payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def send_metrics(base_url:, api_token:)
5353
if uri.is_a?(URI::File)
5454
UI.message("Writing metrics payload to file #{uri.path} (instead of sending it to a server)")
5555
File.write(uri.path, to_h.to_json)
56-
return
56+
return 200 # To make it easy at call site to check for pseudo-status code 200 even in non-HTTP cases
5757
end
5858

5959
UI.message("Sending metrics to #{uri}...")

spec/ios_send_app_size_metrics_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,33 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
8181
app_version: '19.8.0.2'
8282
)
8383
end
84+
85+
it 'writes the payload to a file if provided a file:// base URL' do
86+
in_tmp_dir do |tmp_dir|
87+
# Arrange: Generate a fake `.ipa` file with the expected size
88+
ipa_path = File.join(tmp_dir, 'fake.ipa')
89+
File.write(ipa_path, SecureRandom.random_bytes(fake_ipa_size))
90+
output_path = File.join(tmp_dir, 'payload.json')
91+
92+
app_thinning_plist_path = File.join(test_data_dir, 'app-thinning.plist')
93+
expected_fixture = File.join(test_data_dir, 'app-size-metrics-payload.json')
94+
# Parse as Hash then reconvert to JSON, in order to get the 'minified' JSON version
95+
expected_json = JSON.parse(File.read(expected_fixture)).to_json
96+
97+
# Act
98+
code = run_described_fastlane_action(
99+
api_base_url: File.join('file://localhost/', output_path),
100+
app_name: 'wordpress',
101+
build_type: 'internal',
102+
app_version: '19.8.0.2',
103+
ipa_path: ipa_path,
104+
app_thinning_plist_path: app_thinning_plist_path
105+
)
106+
107+
# Assert
108+
expect(code).to eq(200)
109+
expect(File).to exist(output_path)
110+
expect(JSON.parse(File.read(output_path))).to eq(JSON.parse(expected_json))
111+
end
112+
end
84113
end

0 commit comments

Comments
 (0)