This commit is contained in:
Benoit Daloze 2021-06-02 14:34:01 +02:00
parent 2048dfc5d3
commit a4fbc7e288
3 changed files with 77 additions and 3 deletions

1
spec/mspec/.rspec Normal file
View File

@ -0,0 +1 @@
--color

View File

@ -15,8 +15,10 @@ require 'mspec/helpers/tmp'
# #
# `#{RUBY_EXE} 'path/to/some/file.rb'` # `#{RUBY_EXE} 'path/to/some/file.rb'`
# #
# The ruby_exe helper also accepts an options hash with three # The ruby_exe helper also accepts an options hash with four
# keys: :options, :args and :env. For example: # keys: :options, :args :env and :exception.
#
# For example:
# #
# ruby_exe('file.rb', :options => "-w", # ruby_exe('file.rb', :options => "-w",
# :args => "arg1 arg2", # :args => "arg1 arg2",
@ -28,6 +30,9 @@ require 'mspec/helpers/tmp'
# #
# with access to ENV["FOO"] with value "bar". # with access to ENV["FOO"] with value "bar".
# #
# When `exception: false` and Ruby command fails then exception will not be
# raised.
#
# If +nil+ is passed for the first argument, the command line # If +nil+ is passed for the first argument, the command line
# will be built only from the options hash. # will be built only from the options hash.
# #
@ -52,6 +57,8 @@ require 'mspec/helpers/tmp'
# (with -T on the command line or in the config with set :flags) # (with -T on the command line or in the config with set :flags)
# will be appended to RUBY_EXE so that the interpreter # will be appended to RUBY_EXE so that the interpreter
# is always called with those flags. # is always called with those flags.
#
# Failure of a Ruby command leads to raising exception by default.
def ruby_exe_options(option) def ruby_exe_options(option)
case option case option
@ -128,9 +135,19 @@ def ruby_exe(code = :not_given, opts = {})
code = tmpfile code = tmpfile
end end
expected_exit_status = opts.fetch(:exit_status, 0)
begin begin
platform_is_not :opal do platform_is_not :opal do
`#{ruby_cmd(code, opts)}` command = ruby_cmd(code, opts)
output = `#{command}`
last_status = Process.last_status
if last_status.exitstatus != expected_exit_status
raise "Expected exit status is #{expected_exit_status.inspect} but actual is #{last_status.exitstatus.inspect} for command ruby_exe(#{command.inspect})"
end
output
end end
ensure ensure
saved_env.each { |key, value| ENV[key] = value } saved_env.each { |key, value| ENV[key] = value }

View File

@ -146,6 +146,18 @@ RSpec.describe Object, "#ruby_exe" do
@script = RubyExeSpecs.new @script = RubyExeSpecs.new
allow(@script).to receive(:`) allow(@script).to receive(:`)
status_successful = double(Process::Status, exitstatus: 0)
allow(Process).to receive(:last_status).and_return(status_successful)
end
it "returns command STDOUT when given command" do
code = "code"
options = {}
output = "output"
allow(@script).to receive(:`).and_return(output)
expect(@script.ruby_exe(code, options)).to eq output
end end
it "returns an Array containing the interpreter executable and flags when given no arguments" do it "returns an Array containing the interpreter executable and flags when given no arguments" do
@ -160,6 +172,30 @@ RSpec.describe Object, "#ruby_exe" do
@script.ruby_exe(code, options) @script.ruby_exe(code, options)
end end
it "raises exception when command exit status is not successful" do
code = "code"
options = {}
status_failed = double(Process::Status, exitstatus: 4)
allow(Process).to receive(:last_status).and_return(status_failed)
expect {
@script.ruby_exe(code, options)
}.to raise_error(%r{Expected exit status is 0 but actual is 4 for command ruby_exe\(.+\)})
end
it "shows in the exception message if exitstatus is nil (e.g., signal)" do
code = "code"
options = {}
status_failed = double(Process::Status, exitstatus: nil)
allow(Process).to receive(:last_status).and_return(status_failed)
expect {
@script.ruby_exe(code, options)
}.to raise_error(%r{Expected exit status is 0 but actual is nil for command ruby_exe\(.+\)})
end
describe "with :dir option" do describe "with :dir option" do
it "is deprecated" do it "is deprecated" do
expect { expect {
@ -197,4 +233,24 @@ RSpec.describe Object, "#ruby_exe" do
end.to raise_error(Exception) end.to raise_error(Exception)
end end
end end
describe "with :exit_status option" do
before do
status_failed = double(Process::Status, exitstatus: 4)
allow(Process).to receive(:last_status).and_return(status_failed)
end
it "raises exception when command ends with not expected status" do
expect {
@script.ruby_exe("path", exit_status: 1)
}.to raise_error(%r{Expected exit status is 1 but actual is 4 for command ruby_exe\(.+\)})
end
it "does not raise exception when command ends with expected status" do
output = "output"
allow(@script).to receive(:`).and_return(output)
expect(@script.ruby_exe("path", exit_status: 4)).to eq output
end
end
end end