Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't call ps in the context of xcrun #488

Merged
merged 3 commits into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/run_loop/core_simulator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# A class to manage interactions with CoreSimulators.
class RunLoop::CoreSimulator

require "run_loop/shell"
include RunLoop::Shell

# These options control various aspects of an app's life cycle on the iOS
# Simulator.
#
Expand Down Expand Up @@ -568,7 +571,7 @@ def running_simulator_pid
process_name = "MacOS/#{sim_name}"

args = ["ps", "x", "-o", "pid,command"]
hash = xcrun.run_command_in_context(args)
hash = run_shell_command(args)

exit_status = hash[:exit_status]
if exit_status != 0
Expand Down
2 changes: 1 addition & 1 deletion lib/run_loop/process_terminator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def process_alive?
# @!visibility private
# The details of the process reported by `ps`.
def ps_details
`xcrun ps -p #{pid} -o pid,comm | grep #{pid}`.strip
`ps -p #{pid} -o pid,comm | grep #{pid}`.strip
end

# @!visibility private
Expand Down
6 changes: 3 additions & 3 deletions lib/run_loop/sim_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ def self.terminate_all_sims
# @todo Maybe should try to send -TERM first and -KILL if TERM fails.
# @todo Needs benchmarking.
processes.each do |process_name|
descripts = `xcrun ps x -o pid,command | grep "#{process_name}" | grep -v grep`.strip.split("\n")
descripts = `ps x -o pid,command | grep "#{process_name}" | grep -v grep`.strip.split("\n")
descripts.each do |process_desc|
pid = process_desc.split(' ').first
Open3.popen3("xcrun kill -9 #{pid} && xcrun wait #{pid}") do |_, stdout, stderr, _|
Open3.popen3("kill -9 #{pid} && xcrun wait #{pid}") do |_, stdout, stderr, _|
if ENV['DEBUG_UNIX_CALLS'] == '1'
out = stdout.read.strip
err = stderr.read.strip
Expand Down Expand Up @@ -601,7 +601,7 @@ def enable_software_keyboard(device)
# @return [String, nil] The pid as a String or nil if no process is found.
def sim_pid
process_name = "MacOS/#{sim_name}"
`xcrun ps x -o pid,command | grep "#{process_name}" | grep -v grep`.strip.split(' ').first
`ps x -o pid,command | grep "#{process_name}" | grep -v grep`.strip.split(' ').first
end

# @!visibility private
Expand Down
19 changes: 7 additions & 12 deletions spec/lib/core_simulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,16 @@
end

describe "#running_simulator_pid" do
let(:xcrun) { RunLoop::Xcrun.new }
let(:hash) do
{
:exit_status => 0,
:out => "something, anything"
}
end

before do
allow(core_sim).to receive(:xcrun).and_return xcrun
end

it "xcrun exit status is non-zero" do
hash[:exit_status] = 1
expect(xcrun).to receive(:run_command_in_context).and_return(hash)
expect(core_sim).to receive(:run_shell_command).and_return hash

expect do
core_sim.send(:running_simulator_pid)
Expand All @@ -346,7 +341,7 @@
describe "xcrun returns no :out" do
it "out is nil" do
hash[:out] = nil
expect(xcrun).to receive(:run_command_in_context).and_return(hash)
expect(core_sim).to receive(:run_shell_command).and_return hash

expect do
core_sim.send(:running_simulator_pid)
Expand All @@ -355,7 +350,7 @@

it "out is empty string" do
hash[:out] = ""
expect(xcrun).to receive(:run_command_in_context).and_return(hash)
expect(core_sim).to receive(:run_shell_command).and_return hash

expect do
core_sim.send(:running_simulator_pid)
Expand All @@ -364,15 +359,15 @@
end

it "no matching process is found" do
hash[:out] =
%Q{
hash[:out] =
%Q{
27247 login -pf moody
46238 tmate
31098 less run_loop.out
32976 vim lib/run_loop/xcrun.rb
7656 /bin/ps x -o pid,command
}
expect(xcrun).to receive(:run_command_in_context).and_return(hash)
expect(core_sim).to receive(:run_shell_command).and_return hash

expect(core_sim.send(:running_simulator_pid)).to be == nil
end
Expand All @@ -387,7 +382,7 @@
7656 /MacOS/SillySim
}
expect(core_sim).to receive(:sim_name).and_return("SillySim")
expect(xcrun).to receive(:run_command_in_context).and_return(hash)
expect(core_sim).to receive(:run_shell_command).and_return hash

expect(core_sim.send(:running_simulator_pid)).to be == 7656
end
Expand Down