summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/popen/runner_spec.rb
blob: 2e2cb4ca28fba653cfd6dd30e1d15f12852fcf3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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_success_and_clean?' do
    it 'returns true when exit status is 0 and stderr is empty' do
      run_command

      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_success_and_clean
    end

    it 'returns false when exit stderr has something' do
      run_command(stderr: 'stderr')

      expect(subject).not_to be_all_success_and_clean
    end
  end

  describe '#all_success?' do
    it 'returns true when exit status is 0' do
      run_command

      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_success
    end

    it 'returns true' do
      run_command(stderr: 'stderr')

      expect(subject).to be_all_success
    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