File: subprocess_spec.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (65 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download | duplicates (5)
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
require 'spec_helper'
require 'r10k/util/subprocess'

describe R10K::Util::Subprocess do

  describe "selecting the runner implementation" do
    it "uses the windows implementation on Windows platforms" do
      expect(R10K::Util::Platform).to receive(:windows?).and_return true
      expect(described_class.runner).to eq R10K::Util::Subprocess::Runner::Windows
    end

    it "uses the posix implementation when not on windows" do
      expect(R10K::Util::Platform).to receive(:windows?).and_return true
      expect(described_class.runner).to eq R10K::Util::Subprocess::Runner::Windows
    end
  end

  describe "running commands" do
    subject { described_class.new(['/bin/echo', 'hello', 'world']) }

    let(:runner) do
      double('R10K::Util::Subprocess::Runner').tap do |i|
        allow(i).to receive(:run).and_return(result)
      end
    end

    let(:result) { double('R10K::Util::Subprocess::Result').as_null_object }

    before do
      allow(described_class).to receive(:runner).and_return(double(:new => runner))
    end

    it "copies the cwd to the runner if a cwd is given" do
      expect(runner).to receive(:cwd=).with('/tmp')
      subject.cwd = '/tmp'
      subject.execute
    end

    it "returns the result from the execution" do
      expect(subject.execute).to eq result
    end

    describe "when the command returned with a non-zero exit status" do
      before do
        allow(runner).to receive(:crashed?).and_return true
      end

      it "raises an exception if raise_on_fail is true" do
        subject.raise_on_fail = true

        allow(result).to receive(:exit_code).and_return(255)
        allow(result).to receive(:stderr).and_return('Command not found')

        expect {
          subject.execute
        }.to raise_error(R10K::Util::Subprocess::SubprocessError, /Command exited with non-zero exit code/)
      end

      it "doesn't raise an exception if raise_on_fail is false" do
        subject.raise_on_fail = false
        expect { subject.execute }.to_not raise_error
      end
    end
  end
end