2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-26 15:53:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.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)
|
2018-01-23 07:12:51 -05:00
|
|
|
@klass.send(:include, described_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.popen_with_detail' do
|
|
|
|
subject { @klass.new.popen_with_detail(cmd) }
|
|
|
|
|
|
|
|
let(:cmd) { %W[#{Gem.ruby} -e $stdout.puts(1);$stderr.puts(2);exit(3)] }
|
|
|
|
|
|
|
|
it { expect(subject.cmd).to eq(cmd) }
|
|
|
|
it { expect(subject.stdout).to eq("1\n") }
|
|
|
|
it { expect(subject.stderr).to eq("2\n") }
|
2018-01-24 08:05:01 -05:00
|
|
|
it { expect(subject.status.exitstatus).to eq(3) }
|
2018-01-23 07:12:51 -05:00
|
|
|
it { expect(subject.duration).to be_kind_of(Numeric) }
|
2013-02-26 15:53:46 -05:00
|
|
|
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
|
|
|
|
2022-02-01 13:17:05 -05:00
|
|
|
context 'non-zero status with a kill' do
|
|
|
|
let(:cmd) { [Gem.ruby, "-e", "thr = Thread.new { sleep 5 }; Process.kill(9, Process.pid); thr.join"] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
@output, @status = @klass.new.popen(cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(@status).to eq(9) }
|
|
|
|
it { expect(@output).to be_empty }
|
|
|
|
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
|
|
|
|
|
2018-07-12 14:06:36 -04:00
|
|
|
context 'with a process that writes a lot of data to stderr' do
|
|
|
|
let(:test_string) { 'The quick brown fox jumped over the lazy dog' }
|
|
|
|
# The pipe buffer is typically 64K. This string is about 440K.
|
|
|
|
let(:spew_command) { ['bash', '-c', "for i in {1..10000}; do echo '#{test_string}' 1>&2; done"] }
|
|
|
|
|
|
|
|
it 'returns zero' do
|
|
|
|
output, status = @klass.new.popen(spew_command, path)
|
|
|
|
|
|
|
|
expect(output).to include(test_string)
|
|
|
|
expect(status).to eq(0)
|
|
|
|
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
|
2019-09-16 08:06:26 -04:00
|
|
|
|
|
|
|
context 'when binary is absent' do
|
|
|
|
it 'raises error' do
|
|
|
|
expect do
|
|
|
|
@klass.new.popen(%w[foobar])
|
2022-05-06 08:09:05 -04:00
|
|
|
end.to raise_error(Errno::ENOENT)
|
2019-09-16 08:06:26 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-26 15:53:46 -05:00
|
|
|
end
|