Skip to content
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
8 changes: 5 additions & 3 deletions lib/linux_admin/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def run(cmd, options = {})
params = options[:params] || options[:parameters]

begin
out = launch(build_cmd(cmd, params))
launch_params = {}
launch_params[:chdir] = options[:chdir] if options[:chdir]
out = launch(build_cmd(cmd, params), launch_params)

if options[:return_output] && exitstatus == 0
out
Expand Down Expand Up @@ -67,9 +69,9 @@ def build_cmd(cmd, params = nil)
# http://stackoverflow.com/questions/13829830/ruby-process-spawn-stdout-pipe-buffer-size-limit/13846146#13846146
THREAD_SYNC_KEY = "LinuxAdmin-exitstatus"

def launch(cmd)
def launch(cmd, spawn_options = {})
pipe_r, pipe_w = IO.pipe
pid = Kernel.spawn(cmd, :err => [:child, :out], :out => pipe_w)
pid = Kernel.spawn(cmd, {:err => [:child, :out], :out => pipe_w}.merge(spawn_options))
wait_for_process(pid, pipe_w)
wait_for_output(pipe_r)
end
Expand Down
11 changes: 8 additions & 3 deletions spec/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ class TestClass
context ".run" do
context "with params" do
it "sanitizes crazy params" do
subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg --pool 123 --pool 456")
subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg --pool 123 --pool 456", {})
subject.run("true", :params => modified_params, :return_exitstatus => true)
end

it "as empty hash" do
subject.should_receive(:launch).once.with("true")
subject.should_receive(:launch).once.with("true", {})
subject.run("true", :params => {}, :return_exitstatus => true)
end

it "as nil" do
subject.should_receive(:launch).once.with("true")
subject.should_receive(:launch).once.with("true", {})
subject.run("true", :params => nil, :return_exitstatus => true)
end

Expand Down Expand Up @@ -105,5 +105,10 @@ class TestClass
expect { subject.run("XXXXX", :return_output => true) }.to raise_error
end
end

it "supports spawn's chdir option" do
subject.should_receive(:launch).once.with("true", {:chdir => ".."})
subject.run("true", :chdir => "..", :return_exitstatus => true)
end
end
end