Skip to content

Commit 3b88982

Browse files
committed
Add unit test for writing to file
1 parent 004e075 commit 3b88982

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

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
@@ -87,4 +87,33 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
8787
app_version: '19.8.0.2'
8888
)
8989
end
90+
91+
it 'writes the payload to a file if provided a file:// base URL' do
92+
in_tmp_dir do |tmp_dir|
93+
# Arrange: Generate a fake `.ipa` file with the expected size
94+
ipa_path = File.join(tmp_dir, 'fake.ipa')
95+
File.write(ipa_path, SecureRandom.random_bytes(fake_ipa_size))
96+
output_path = File.join(tmp_dir, 'payload.json')
97+
98+
app_thinning_plist_path = File.join(test_data_dir, 'app-thinning.plist')
99+
expected_fixture = File.join(test_data_dir, 'app-size-metrics-payload.json')
100+
# Parse as Hash then reconvert to JSON, in order to get the 'minified' JSON version
101+
expected_json = JSON.parse(File.read(expected_fixture)).to_json
102+
103+
# Act
104+
code = run_described_fastlane_action(
105+
api_base_url: File.join('file://localhost/', output_path),
106+
app_name: 'wordpress',
107+
build_type: 'internal',
108+
app_version: '19.8.0.2',
109+
ipa_path: ipa_path,
110+
app_thinning_plist_path: app_thinning_plist_path
111+
)
112+
113+
# Assert
114+
expect(code).to eq(200)
115+
expect(File).to exist(output_path)
116+
expect(JSON.parse(File.read(output_path))).to eq(JSON.parse(expected_json))
117+
end
118+
end
90119
end

0 commit comments

Comments
 (0)