Skip to content

Commit

Permalink
addressing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
wuaar1003 committed Dec 11, 2023
1 parent d9ac907 commit d12b8bc
Show file tree
Hide file tree
Showing 8 changed files with 3,213 additions and 131 deletions.
26 changes: 11 additions & 15 deletions fastlane_core/lib/fastlane_core/device_manager.rb
Expand Up @@ -15,6 +15,17 @@ def all(requested_os_type = "")
def simulators(requested_os_type = "")
UI.verbose("Fetching available simulator devices")

output, status = Open3.capture2('xcrun simctl list runtimes -j')
begin
raise status unless status.success?
json = JSON.parse(output)
@runtime_build_os_versions = json['runtimes'].map { |h| [h['buildversion'], h['version']] }.to_h
rescue StandardError => e
UI.error(e)
UI.error('xcrun simctl CLI broken; cun `xcrun simctl list runtimes` and make sure it works')
UI.user_error!('xcrun simctl not working')
end

@devices = []
os_type = 'unknown'
os_version = 'unknown'
Expand All @@ -23,21 +34,6 @@ def simulators(requested_os_type = "")
output = stdout.read
end

runtime_info = ''
Open3.popen3('xcrun simctl list runtimes') do |stdin, stdout, stderr, wait_thr|
# This regex outputs the version info in the format "<platform> <version><exact version>"
runtime_info = stdout.read
end
unless runtime_info.include?("== Runtimes ==")
UI.error("xcrun simctl CLI broken, run `xcrun simctl list devices` and make sure it works")
UI.user_error!("xcrun simctl not working.")
end

@runtime_build_os_versions = runtime_info.lines.drop(1).each_with_object({}) do |line, res|
matches = line.match(/.*\((?<version>\S+)\s-\s(?<build>\S+)\)[\S\s]*/)
res[matches[:build]] = matches[:version]
end

unless output.include?("== Devices ==")
UI.error("xcrun simctl CLI broken, run `xcrun simctl list devices` and make sure it works")
UI.user_error!("xcrun simctl not working.")
Expand Down
88 changes: 50 additions & 38 deletions fastlane_core/spec/device_manager_spec.rb
Expand Up @@ -12,26 +12,36 @@
FastlaneCore::Simulator.clear_cache
end

it "raises an error if xcrun CLI prints garbage simulator" do
response = "response"
s = StringIO.new
s.puts(response)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, s, nil, nil)
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, s, nil, nil)
it 'raises an error if broken xcrun simctl list devices' do
status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

response = double('xcrun simctl list devices', read: 'garbage')
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)

expect do
devices = FastlaneCore::Simulator.all
end.to raise_error("xcrun simctl not working.")
end

it 'raises an error if broken xcrun simctl list runtimes' do
status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['garbage', status])

expect do
devices = FastlaneCore::Simulator.all
end.to raise_error(FastlaneCore::Interface::FastlaneError)
end


describe "properly parses the simctl output and generates Device objects for iOS simulator" do
it "Xcode 7" do
response = "response"
expect(response).to receive(:read).and_return(@simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::Simulator.all
expect(devices.count).to eq(6)
Expand Down Expand Up @@ -79,9 +89,9 @@
simctl_output = File.read('./fastlane_core/spec/fixtures/DeviceManagerSimctlOutputXcode8')
expect(response).to receive(:read).and_return(simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::Simulator.all
expect(devices.count).to eq(12)
Expand Down Expand Up @@ -111,9 +121,9 @@
simctl_output = File.read('./fastlane_core/spec/fixtures/DeviceManagerSimctlOutputXcode9')
expect(response).to receive(:read).and_return(simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::Simulator.all
expect(devices.count).to eq(15)
Expand Down Expand Up @@ -143,9 +153,9 @@
simctl_output = File.read('./fastlane_core/spec/fixtures/DeviceManagerSimctlOutputXcode11')
expect(response).to receive(:read).and_return(simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::Simulator.all
expect(devices.count).to eq(29)
Expand Down Expand Up @@ -175,9 +185,9 @@
response = "response"
expect(response).to receive(:read).and_return(@simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::SimulatorTV.all
expect(devices.count).to eq(1)
Expand All @@ -194,9 +204,9 @@
response = "response"
expect(response).to receive(:read).and_return(@simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::SimulatorWatch.all
expect(devices.count).to eq(2)
Expand All @@ -219,9 +229,9 @@
response = "response"
expect(response).to receive(:read).and_return(@simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::DeviceManager.simulators
expect(devices.count).to eq(9)
Expand Down Expand Up @@ -275,9 +285,9 @@
simctl_output = File.read('./fastlane_core/spec/fixtures/DeviceManagerSimctlOutputXcode10BootedUnavailable')
expect(response).to receive(:read).and_return(simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::DeviceManager.simulators
expect(devices.count).to eq(3)
Expand Down Expand Up @@ -384,9 +394,9 @@

expect(response).to receive(:read).and_return(@simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::DeviceManager.all('iOS')
expect(devices.count).to eq(8)
Expand Down Expand Up @@ -439,9 +449,9 @@

expect(response).to receive(:read).and_return(@simctl_output)
expect(Open3).to receive(:popen3).with("xcrun simctl list devices").and_yield(nil, response, nil, nil)
thing = {}
expect(thing).to receive(:read).and_return("== Runtimes ==\n")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)

status = double('status', "success?": true)
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return(['{"runtimes": [] }', status])

devices = FastlaneCore::DeviceManager.all('tvOS')
expect(devices.count).to eq(2)
Expand All @@ -464,13 +474,15 @@
response = double('xcrun simctl list devices', read: '== Devices ==')
allow(Open3).to receive(:popen3).with('xcrun simctl list devices').and_yield(nil, response, nil, nil)

thing = double('xcrun simctl list runtimes', read: "== Runtimes ==\niOS 17.0 (17.0 - 21A328) - com.apple.CoreSimulator.SimRuntime.iOS-17-0\niOS 17.0 (17.0.1 - 21A342) - com.apple.CoreSimulator.SimRuntime.iOS-17-0")
allow(Open3).to receive(:popen3).with("xcrun simctl list runtimes").and_yield(nil, thing, nil, nil)
status = double('status', "success?": true)
runtime_output = File.read('./fastlane_core/spec/fixtures/XcrunSimctlListRuntimesOutput')
expect(Open3).to receive(:capture2).with("xcrun simctl list runtimes -j").and_return([runtime_output, status])

devices = FastlaneCore::DeviceManager.simulators

expect(FastlaneCore::DeviceManager.runtime_build_os_versions['21A328']).to eq('17.0')
expect(FastlaneCore::DeviceManager.runtime_build_os_versions['21A342']).to eq('17.0.1')
expect(FastlaneCore::DeviceManager.runtime_build_os_versions['21R355']).to eq('10.0')
end

describe FastlaneCore::DeviceManager::Device do
Expand Down

0 comments on commit d12b8bc

Please sign in to comment.