Skip to content

Commit

Permalink
Merge pull request #488 from calabash/feature/dont-call-ps-in-the-con…
Browse files Browse the repository at this point in the history
…text-of-xcrun

Don't call ps in the context of xcrun
  • Loading branch information
jescriba authored Jul 20, 2016
2 parents 0960362 + e99d711 commit 4548d1f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
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

0 comments on commit 4548d1f

Please sign in to comment.