Skip to content
Snippets Groups Projects
Commit 0432bdf1 authored by Jacob Vosmaer's avatar Jacob Vosmaer
Browse files

Change Gitlab::Popen to use arrays for commands

parent 9f20580e
No related branches found
No related tags found
No related merge requests found
require 'fileutils' require 'fileutils'
require 'open3'
   
module Gitlab module Gitlab
module Popen module Popen
def popen(cmd, path) def popen(cmd, path)
unless cmd.is_a?(Array)
raise "System commands must be given as an array of strings"
end
vars = { "PWD" => path } vars = { "PWD" => path }
options = { chdir: path } options = { chdir: path }
   
Loading
@@ -12,10 +17,10 @@ module Gitlab
Loading
@@ -12,10 +17,10 @@ module Gitlab
   
@cmd_output = "" @cmd_output = ""
@cmd_status = 0 @cmd_status = 0
Open3.popen3(vars, cmd, options) do |stdin, stdout, stderr, wait_thr| Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
@cmd_status = wait_thr.value.exitstatus
@cmd_output << stdout.read @cmd_output << stdout.read
@cmd_output << stderr.read @cmd_output << stderr.read
@cmd_status = wait_thr.value.exitstatus
end end
   
return @cmd_output, @cmd_status return @cmd_output, @cmd_status
Loading
Loading
Loading
@@ -10,7 +10,7 @@ describe 'Gitlab::Popen', no_db: true do
Loading
@@ -10,7 +10,7 @@ describe 'Gitlab::Popen', no_db: true do
   
context 'zero status' do context 'zero status' do
before do before do
@output, @status = @klass.new.popen('ls', path) @output, @status = @klass.new.popen(%W(ls), path)
end end
   
it { @status.should be_zero } it { @status.should be_zero }
Loading
@@ -19,11 +19,18 @@ describe 'Gitlab::Popen', no_db: true do
Loading
@@ -19,11 +19,18 @@ describe 'Gitlab::Popen', no_db: true do
   
context 'non-zero status' do context 'non-zero status' do
before do before do
@output, @status = @klass.new.popen('cat NOTHING', path) @output, @status = @klass.new.popen(%W(cat NOTHING), path)
end end
   
it { @status.should == 1 } it { @status.should == 1 }
it { @output.should include('No such file or directory') } it { @output.should include('No such file or directory') }
end end
context 'unsafe string command' do
it 'raises an error when it gets called with a string argument' do
expect { @klass.new.popen('ls', path) }.to raise_error
end
end
end end
   
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment