2013-02-26 15:53:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe 'Gitlab::Popen' do
|
2015-06-22 08:39:07 -04:00
|
|
|
let(:path) { Rails.root.join('tmp').to_s }
|
2013-02-26 15:53:46 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
@klass = Class.new(Object)
|
|
|
|
@klass.send(:include, Gitlab::Popen)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'zero status' do
|
|
|
|
before do
|
2016-06-30 07:31:52 -04:00
|
|
|
@output, @status = @klass.new.popen(%w(ls), path)
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(@status).to be_zero }
|
2017-10-03 10:47:56 -04:00
|
|
|
it { expect(@output).to include('tests') }
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-zero status' do
|
|
|
|
before do
|
2016-06-30 07:31:52 -04:00
|
|
|
@output, @status = @klass.new.popen(%w(cat NOTHING), path)
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(@status).to eq(1) }
|
|
|
|
it { expect(@output).to include('No such file or directory') }
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|
2014-02-25 05:57:42 -05:00
|
|
|
|
|
|
|
context 'unsafe string command' do
|
|
|
|
it 'raises an error when it gets called with a string argument' do
|
2015-06-17 21:29:18 -04:00
|
|
|
expect { @klass.new.popen('ls', path) }.to raise_error(RuntimeError)
|
2014-02-25 05:57:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-03 14:28:29 -04:00
|
|
|
context 'with custom options' do
|
|
|
|
let(:vars) { { 'foobar' => 123, 'PWD' => path } }
|
|
|
|
let(:options) { { chdir: path } }
|
|
|
|
|
|
|
|
it 'calls popen3 with the provided environment variables' do
|
|
|
|
expect(Open3).to receive(:popen3).with(vars, 'ls', options)
|
|
|
|
|
|
|
|
@output, @status = @klass.new.popen(%w(ls), path, { 'foobar' => 123 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-25 05:58:22 -05:00
|
|
|
context 'without a directory argument' do
|
|
|
|
before do
|
2016-06-30 07:31:52 -04:00
|
|
|
@output, @status = @klass.new.popen(%w(ls))
|
2014-02-25 05:58:22 -05:00
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(@status).to be_zero }
|
|
|
|
it { expect(@output).to include('spec') }
|
2014-02-25 05:58:22 -05:00
|
|
|
end
|
2016-08-30 10:06:40 -04:00
|
|
|
|
|
|
|
context 'use stdin' do
|
|
|
|
before do
|
|
|
|
@output, @status = @klass.new.popen(%w[cat]) { |stdin| stdin.write 'hello' }
|
|
|
|
end
|
2017-07-03 14:28:29 -04:00
|
|
|
|
2016-08-30 10:06:40 -04:00
|
|
|
it { expect(@status).to be_zero }
|
|
|
|
it { expect(@output).to eq('hello') }
|
|
|
|
end
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|