Skip to content

Commit 52c429e

Browse files
committed
Add new ruby_exe's exit_status option
1 parent aaf71af commit 52c429e

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

lib/mspec/helpers/ruby_exe.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ def ruby_exe(code = :not_given, opts = {})
135135
code = tmpfile
136136
end
137137

138-
exception = opts.fetch(:exception, true)
138+
expected_exit_status = opts.fetch(:exit_status, 0)
139139

140140
begin
141141
platform_is_not :opal do
142142
command = ruby_cmd(code, opts)
143143
output = `#{command}`
144144

145145
last_status = Process.last_status
146-
if !last_status.success? && exception
147-
raise "ruby_exe(#{command}) failed: #{last_status.inspect}"
146+
if last_status.exitstatus != expected_exit_status
147+
raise "Expected exit status is #{expected_exit_status} but actual is #{last_status.exitstatus}. Command ruby_exe(#{command})"
148148
end
149149

150150
output

spec/helpers/ruby_exe_spec.rb

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,23 @@ class RubyExeSpecs
221221
end
222222
end
223223

224-
describe "with :exception option" do
225-
context "when Ruby command fails" do
226-
before do
227-
status_failed = double(Process::Status, success?: false)
228-
allow(Process).to receive(:last_status).and_return(status_failed)
229-
end
230-
231-
it "raises exception when exception: true" do
232-
expect {
233-
@script.ruby_exe("path", exception: true)
234-
}.to raise_error(%r{ruby_exe\(.+\) failed:})
235-
end
236-
237-
it "does not raise exception when exception: false" do
238-
output = "output"
239-
allow(@script).to receive(:`).and_return(output)
240-
241-
expect(@script.ruby_exe("path", exception: false)).to eq output
242-
end
224+
describe "with :exit_status option" do
225+
before do
226+
status_failed = double(Process::Status, success?: false, exitstatus: 4)
227+
allow(Process).to receive(:last_status).and_return(status_failed)
228+
end
229+
230+
it "raises exception when command ends with not expected status" do
231+
expect {
232+
@script.ruby_exe("path", exit_status: 1)
233+
}.to raise_error(%r{Expected exit status is 1 but actual is 4. Command ruby_exe\(.+\)})
234+
end
235+
236+
it "does not raise exception when command ends with expected status" do
237+
output = "output"
238+
allow(@script).to receive(:`).and_return(output)
239+
240+
expect(@script.ruby_exe("path", exit_status: 4)).to eq output
243241
end
244242
end
245243
end

0 commit comments

Comments
 (0)