2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-19 13:31:48 +02:00
|
|
|
describe ErrorDuringExecution do
|
|
|
|
subject(:error) { described_class.new(command, status: status, output: output) }
|
2018-09-20 09:07:56 +01:00
|
|
|
|
2018-07-19 13:31:48 +02:00
|
|
|
let(:command) { ["false"] }
|
2021-02-23 21:30:36 +00:00
|
|
|
let(:status) { instance_double(Process::Status, exitstatus: exitstatus, termsig: nil) }
|
2018-07-19 13:31:48 +02:00
|
|
|
let(:exitstatus) { 1 }
|
|
|
|
let(:output) { nil }
|
|
|
|
|
|
|
|
describe "#initialize" do
|
|
|
|
it "fails when only given a command" do
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2018-07-19 13:31:48 +02:00
|
|
|
described_class.new(command)
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(ArgumentError)
|
2018-07-19 13:31:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when only given a status" do
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2018-07-19 13:31:48 +02:00
|
|
|
described_class.new(status: status)
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(ArgumentError)
|
2018-07-19 13:31:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not raise an error when given both a command and a status" do
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2018-07-19 13:31:48 +02:00
|
|
|
described_class.new(command, status: status)
|
2023-03-08 23:14:46 +00:00
|
|
|
end.not_to raise_error
|
2018-07-19 13:31:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#to_s" do
|
|
|
|
context "when only given a command and a status" do
|
|
|
|
its(:to_s) { is_expected.to eq "Failure while executing; `false` exited with 1." }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when additionally given the output" do
|
2023-03-08 23:14:46 +00:00
|
|
|
let(:output) do
|
2018-07-19 13:31:48 +02:00
|
|
|
[
|
|
|
|
[:stdout, "This still worked.\n"],
|
|
|
|
[:stderr, "Here something went wrong.\n"],
|
|
|
|
]
|
2023-03-08 23:14:46 +00:00
|
|
|
end
|
2018-07-19 13:31:48 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow($stdout).to receive(:tty?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2023-03-08 23:14:46 +00:00
|
|
|
its(:to_s) do
|
2021-01-31 13:14:23 -05:00
|
|
|
expect(error.to_s).to eq <<~EOS
|
2018-07-19 13:31:48 +02:00
|
|
|
Failure while executing; `false` exited with 1. Here's the output:
|
|
|
|
This still worked.
|
|
|
|
#{Formatter.error("Here something went wrong.\n")}
|
|
|
|
EOS
|
2023-03-08 23:14:46 +00:00
|
|
|
end
|
2018-07-19 13:31:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when command arguments contain special characters" do
|
|
|
|
let(:command) { ["env", "PATH=/bin", "cat", "with spaces"] }
|
|
|
|
|
2023-03-08 23:14:46 +00:00
|
|
|
its(:to_s) do
|
2021-01-31 13:14:23 -05:00
|
|
|
expect(error.to_s)
|
2018-09-20 13:40:38 +01:00
|
|
|
.to eq 'Failure while executing; `env PATH=/bin cat with\ spaces` exited with 1.'
|
2023-03-08 23:14:46 +00:00
|
|
|
end
|
2018-07-19 13:31:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|