Skip to content

Commit 51bb163

Browse files
afurmnekrichart-tykh
authored
Fix nil crash when deleting unused runtimes (#63)
Co-authored-by: Vitalii Budnik <nekrich@users.noreply.github.com> Co-authored-by: Artem Tykhonov <artem@macpaw.com>
1 parent 1f26353 commit 51bb163

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/fastlane/plugin/create_simulator_devices/helpers/create_simulator_devices/runtime_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def delete_unused_runtimes(needed_runtimes)
8383
UI.message('Deleting unused runtimes...')
8484
available_simctl_runtimes = shell_helper.simctl_runtimes
8585

86-
needed_simctl_runtimes = needed_runtimes.map do |needed_runtime|
86+
needed_simctl_runtimes = needed_runtimes.filter_map do |needed_runtime|
8787
# Check if available runtimes contain the needed runtime.
8888
simctl_runtime_matching_needed_runtime?(needed_runtime)
8989
end

spec/create_simulator_devices/runtime_helper_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@
6464
end
6565
end
6666

67+
describe '#delete_unused_runtimes' do
68+
it 'skips missing needed runtimes when deleting unused runtimes' do
69+
# GIVEN: A mix of needed runtimes where one runtime is still missing
70+
needed_runtime = instance_double(RequiredRuntime)
71+
missing_runtime = instance_double(RequiredRuntime)
72+
73+
matched_simctl_runtime = instance_double(Runtime, identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-18-0')
74+
unused_simctl_runtime = instance_double(Runtime, identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-17-0')
75+
installed_unused_runtime = instance_double(RuntimeWithState, runtime_identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-17-0', identifier: 'runtime-17')
76+
77+
allow(shell_helper).to receive(:simctl_runtimes).with(no_args).and_return([matched_simctl_runtime, unused_simctl_runtime])
78+
allow(shell_helper).to receive(:installed_runtimes_with_state).with(no_args).and_return([installed_unused_runtime])
79+
allow(shell_helper).to receive(:installed_runtimes_with_state).with(retry_count: 3, retry_delay: 60)
80+
allow(shell_helper).to receive(:simctl_delete_runtime)
81+
allow(shell_helper).to receive(:stop_core_simulator_services)
82+
allow(shell_helper).to receive(:simctl_runtimes).with(force: true)
83+
allow(shell_helper).to receive(:simctl_devices_for_runtimes).with(force: true)
84+
85+
allow(sut).to receive(:simctl_runtime_matching_needed_runtime?).with(needed_runtime).and_return(matched_simctl_runtime)
86+
allow(sut).to receive(:simctl_runtime_matching_needed_runtime?).with(missing_runtime).and_return(nil)
87+
88+
# WHEN: Deleting unused runtimes
89+
expect { sut.delete_unused_runtimes([needed_runtime, missing_runtime]) }.not_to raise_error
90+
91+
# THEN: It should still delete real unused runtimes and not crash on missing matches
92+
expect(shell_helper).to have_received(:simctl_delete_runtime).with(identifier: 'runtime-17')
93+
end
94+
end
95+
6796
describe '#missing_runtimes' do
6897
it 'returns runtimes that are not available' do
6998
# GIVEN: Needed runtimes with one missing

0 commit comments

Comments
 (0)