2013-02-26 20:53:46 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 10:55:36 +00:00
|
|
|
describe 'Gitlab::Popen', lib: true, no_db: true do
|
2015-06-22 12:39:07 +00:00
|
|
|
let(:path) { Rails.root.join('tmp').to_s }
|
2013-02-26 20:53:46 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
@klass = Class.new(Object)
|
|
|
|
@klass.send(:include, Gitlab::Popen)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'zero status' do
|
|
|
|
before do
|
2016-06-30 11:31:52 +00:00
|
|
|
@output, @status = @klass.new.popen(%w(ls), path)
|
2013-02-26 20:53:46 +00:00
|
|
|
end
|
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { expect(@status).to be_zero }
|
|
|
|
it { expect(@output).to include('cache') }
|
2013-02-26 20:53:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-zero status' do
|
|
|
|
before do
|
2016-06-30 11:31:52 +00:00
|
|
|
@output, @status = @klass.new.popen(%w(cat NOTHING), path)
|
2013-02-26 20:53:46 +00:00
|
|
|
end
|
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { expect(@status).to eq(1) }
|
|
|
|
it { expect(@output).to include('No such file or directory') }
|
2013-02-26 20:53:46 +00:00
|
|
|
end
|
2014-02-25 10:57:42 +00:00
|
|
|
|
|
|
|
context 'unsafe string command' do
|
|
|
|
it 'raises an error when it gets called with a string argument' do
|
2015-06-18 01:29:18 +00:00
|
|
|
expect { @klass.new.popen('ls', path) }.to raise_error(RuntimeError)
|
2014-02-25 10:57:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-03 18:28:29 +00: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 10:58:22 +00:00
|
|
|
context 'without a directory argument' do
|
|
|
|
before do
|
2016-06-30 11:31:52 +00:00
|
|
|
@output, @status = @klass.new.popen(%w(ls))
|
2014-02-25 10:58:22 +00:00
|
|
|
end
|
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { expect(@status).to be_zero }
|
|
|
|
it { expect(@output).to include('spec') }
|
2014-02-25 10:58:22 +00:00
|
|
|
end
|
2016-08-30 14:06:40 +00:00
|
|
|
|
|
|
|
context 'use stdin' do
|
|
|
|
before do
|
|
|
|
@output, @status = @klass.new.popen(%w[cat]) { |stdin| stdin.write 'hello' }
|
|
|
|
end
|
2017-07-03 18:28:29 +00:00
|
|
|
|
2016-08-30 14:06:40 +00:00
|
|
|
it { expect(@status).to be_zero }
|
|
|
|
it { expect(@output).to eq('hello') }
|
|
|
|
end
|
2013-02-26 20:53:46 +00:00
|
|
|
end
|