File: sheller_spec.rb

package info (click to toggle)
ruby-shellany 0.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 218; sh: 4; makefile: 3
file content (141 lines) | stat: -rw-r--r-- 3,609 bytes parent folder | download | duplicates (3)
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
140
141
require "shellany/sheller"

RSpec.describe Shellany::Sheller, :sheller_specs do
  before do
    allow(Kernel).to receive(:system) do |args|
      fail "Stub called with: #{args.inspect}"
    end
  end

  subject { described_class }

  context "without a command" do
    [:new, :run, :stdout, :stderr].each do |meth|
      describe ".#{meth}" do
        specify do
          expect { subject.send(meth) }.
            to raise_error ArgumentError, "no command given"
        end
      end
    end
  end

  context "with shell (string) cmd returning success" do
    let(:cmd) { "ls -l" }
    let(:output) { "foo.rb\n" }
    let(:errors) { "" }
    let(:result) do
      [instance_double(Process::Status, success?: true), output, errors]
    end

    context "when constructed with a cmd" do
      subject { described_class.new(cmd) }

      describe "#run" do
        it "runs the command given to constructor" do
          expect(described_class).to receive(:_system_with_capture).
            with(cmd).and_return(result)
          subject.run
        end
      end
    end
  end

  context "with array cmd returning success" do
    let(:cmd) { %w(ls -l) }
    let(:output) { "foo.rb\n" }
    let(:errors) { "" }
    let(:result) do
      [instance_double(Process::Status, success?: true), output, errors]
    end

    describe "when used as class" do
      describe ".run" do
        it "runs the given command" do
          expect(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
          subject.run(*cmd)
        end
      end

      describe ".new" do
        it "does not run anything" do
          expect(described_class).to_not receive(:_system_with_capture)
          subject
        end
      end

      describe ".stdout" do
        before do
          allow(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
        end

        it "runs command and returns output" do
          expect(subject.stdout(*cmd)).to eq "foo.rb\n"
        end
      end

      describe ".stderr" do
        before do
          allow(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
        end

        it "runs command and returns errors" do
          expect(subject.stderr(*cmd)).to eq ""
        end
      end
    end

    context "when constructed with a cmd" do
      subject { described_class.new(*cmd) }

      it "does not run anything" do
        expect(described_class).to_not receive(:_system_with_capture)
        subject
      end

      describe "#run" do
        it "runs the command given to constructor" do
          expect(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
          subject.run
        end
      end

      describe "#stdout" do
        before do
          allow(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
        end

        it "runs command and returns output" do
          expect(subject.stdout).to eq "foo.rb\n"
        end
      end

      describe "#stderr" do
        before do
          allow(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
        end

        it "runs command and returns output" do
          expect(subject.stderr).to eq ""
        end
      end

      describe "#ok?" do
        before do
          allow(described_class).to receive(:_system_with_capture).
            with(*cmd) { result }
        end

        it "runs command and returns output" do
          expect(subject).to be_ok
        end
      end
    end
  end
end