From 54ca8d0d6c4744be53c7044b9bbfa05acc358090 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Tue, 23 Jan 2018 20:12:51 +0800 Subject: Fail static-analysis if there's output to stderr TODO: fix offenders --- spec/lib/gitlab/popen/runner_spec.rb | 139 +++++++++++++++++++++++++++++++++++ spec/lib/gitlab/popen_spec.rb | 16 +++- 2 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 spec/lib/gitlab/popen/runner_spec.rb (limited to 'spec/lib') diff --git a/spec/lib/gitlab/popen/runner_spec.rb b/spec/lib/gitlab/popen/runner_spec.rb new file mode 100644 index 00000000000..e0450b96e0f --- /dev/null +++ b/spec/lib/gitlab/popen/runner_spec.rb @@ -0,0 +1,139 @@ +require 'spec_helper' + +describe Gitlab::Popen::Runner do + subject { described_class.new } + + describe '#run' do + it 'runs the command and returns the result' do + run_command + + expect(Gitlab::Popen).to have_received(:popen_with_detail) + end + end + + describe '#all_good?' do + it 'returns true when exit status is 0 and stderr is empty' do + run_command + + expect(subject).to be_all_good + end + + it 'returns false when exit status is not 0' do + run_command(exitstatus: 1) + + expect(subject).not_to be_all_good + end + + it 'returns false when exit stderr has something' do + run_command(stderr: 'stderr') + + expect(subject).not_to be_all_good + end + end + + describe '#all_status_zero?' do + it 'returns true when exit status is 0' do + run_command + + expect(subject).to be_all_status_zero + end + + it 'returns false when exit status is not 0' do + run_command(exitstatus: 1) + + expect(subject).not_to be_all_status_zero + end + + it 'returns true' do + run_command(stderr: 'stderr') + + expect(subject).to be_all_status_zero + end + end + + describe '#all_stderr_empty?' do + it 'returns true when stderr is empty' do + run_command + + expect(subject).to be_all_stderr_empty + end + + it 'returns true when exit status is not 0' do + run_command(exitstatus: 1) + + expect(subject).to be_all_stderr_empty + end + + it 'returns false when exit stderr has something' do + run_command(stderr: 'stderr') + + expect(subject).not_to be_all_stderr_empty + end + end + + describe '#failed_results' do + it 'returns [] when everything is passed' do + run_command + + expect(subject.failed_results).to be_empty + end + + it 'returns the result when exit status is not 0' do + result = run_command(exitstatus: 1) + + expect(subject.failed_results).to contain_exactly(result) + end + + it 'returns [] when exit stderr has something' do + run_command(stderr: 'stderr') + + expect(subject.failed_results).to be_empty + end + end + + describe '#warned_results' do + it 'returns [] when everything is passed' do + run_command + + expect(subject.warned_results).to be_empty + end + + it 'returns [] when exit status is not 0' do + run_command(exitstatus: 1) + + expect(subject.warned_results).to be_empty + end + + it 'returns the result when exit stderr has something' do + result = run_command(stderr: 'stderr') + + expect(subject.warned_results).to contain_exactly(result) + end + end + + def run_command( + command: 'command', + stdout: 'stdout', + stderr: '', + exitstatus: 0, + status: double(exitstatus: exitstatus, success?: exitstatus.zero?), + duration: 0.1) + + result = + Gitlab::Popen::Result.new(command, stdout, stderr, status, duration) + + allow(Gitlab::Popen) + .to receive(:popen_with_detail) + .and_return(result) + + subject.run([command]) do |cmd, &run| + expect(cmd).to eq(command) + + cmd_result = run.call + + expect(cmd_result).to eq(result) + end + + subject.results.first + end +end diff --git a/spec/lib/gitlab/popen_spec.rb b/spec/lib/gitlab/popen_spec.rb index b145ca36f26..3401c86e540 100644 --- a/spec/lib/gitlab/popen_spec.rb +++ b/spec/lib/gitlab/popen_spec.rb @@ -1,11 +1,23 @@ require 'spec_helper' -describe 'Gitlab::Popen' do +describe Gitlab::Popen do let(:path) { Rails.root.join('tmp').to_s } before do @klass = Class.new(Object) - @klass.send(:include, Gitlab::Popen) + @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") } + it { expect(subject.status).to eq(3) } + it { expect(subject.duration).to be_kind_of(Numeric) } end context 'zero status' do -- cgit v1.2.1 From a2618310aea7d58e52d2d29ec4871e27717eb0f0 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Wed, 24 Jan 2018 21:05:01 +0800 Subject: Use Process::Status rather than an integer However keep backward compatibility --- spec/lib/gitlab/popen/runner_spec.rb | 16 ++++++++-------- spec/lib/gitlab/popen_spec.rb | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'spec/lib') diff --git a/spec/lib/gitlab/popen/runner_spec.rb b/spec/lib/gitlab/popen/runner_spec.rb index e0450b96e0f..2e2cb4ca28f 100644 --- a/spec/lib/gitlab/popen/runner_spec.rb +++ b/spec/lib/gitlab/popen/runner_spec.rb @@ -11,43 +11,43 @@ describe Gitlab::Popen::Runner do end end - describe '#all_good?' do + describe '#all_success_and_clean?' do it 'returns true when exit status is 0 and stderr is empty' do run_command - expect(subject).to be_all_good + expect(subject).to be_all_success_and_clean end it 'returns false when exit status is not 0' do run_command(exitstatus: 1) - expect(subject).not_to be_all_good + expect(subject).not_to be_all_success_and_clean end it 'returns false when exit stderr has something' do run_command(stderr: 'stderr') - expect(subject).not_to be_all_good + expect(subject).not_to be_all_success_and_clean end end - describe '#all_status_zero?' do + describe '#all_success?' do it 'returns true when exit status is 0' do run_command - expect(subject).to be_all_status_zero + expect(subject).to be_all_success end it 'returns false when exit status is not 0' do run_command(exitstatus: 1) - expect(subject).not_to be_all_status_zero + expect(subject).not_to be_all_success end it 'returns true' do run_command(stderr: 'stderr') - expect(subject).to be_all_status_zero + expect(subject).to be_all_success end end diff --git a/spec/lib/gitlab/popen_spec.rb b/spec/lib/gitlab/popen_spec.rb index 3401c86e540..1dbead16d5b 100644 --- a/spec/lib/gitlab/popen_spec.rb +++ b/spec/lib/gitlab/popen_spec.rb @@ -16,7 +16,7 @@ describe Gitlab::Popen do it { expect(subject.cmd).to eq(cmd) } it { expect(subject.stdout).to eq("1\n") } it { expect(subject.stderr).to eq("2\n") } - it { expect(subject.status).to eq(3) } + it { expect(subject.status.exitstatus).to eq(3) } it { expect(subject.duration).to be_kind_of(Numeric) } end -- cgit v1.2.1