2013-02-26 15:53:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:36 -05:00
|
|
|
describe 'Gitlab::Popen', lib: true, no_db: true 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 }
|
|
|
|
it { expect(@output).to include('cache') }
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it { expect(@status).to be_zero }
|
|
|
|
it { expect(@output).to eq('hello') }
|
|
|
|
end
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|