1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
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'`
#
# The ruby_exe helper also accepts an options hash with three
# keys: :options, :args and :env. For example:
# The ruby_exe helper also accepts an options hash with four
# keys: :options, :args :env and :exception.
#
# For example:
#
# ruby_exe('file.rb', :options => "-w",
# :args => "arg1 arg2",
@ -28,6 +30,9 @@ require 'mspec/helpers/tmp'
#
# 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
# 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)
# will be appended to RUBY_EXE so that the interpreter
# is always called with those flags.
#
# Failure of a Ruby command leads to raising exception by default.
def ruby_exe_options(option)
case option
@ -128,9 +135,19 @@ def ruby_exe(code = :not_given, opts = {})
code = tmpfile
end
expected_exit_status = opts.fetch(:exit_status, 0)
begin
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
ensure
saved_env.each { |key, value| ENV[key] = value }

View file

@ -146,6 +146,18 @@ RSpec.describe Object, "#ruby_exe" do
@script = RubyExeSpecs.new
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
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)
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
it "is deprecated" do
expect {
@ -197,4 +233,24 @@ RSpec.describe Object, "#ruby_exe" do
end.to raise_error(Exception)
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