diff options
author | Benoit Daloze <eregontp@gmail.com> | 2021-06-02 14:34:01 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2021-06-02 14:34:01 +0200 |
commit | a4fbc7e2884ba694278adea3b32ddb8c2ac10efe (patch) | |
tree | 3062a3c0bf1e644ff6d9f4ff9ecec6299f12f025 /spec | |
parent | 2048dfc5d37eecb6f1ae18e9d1770a71b46a40b9 (diff) |
Update to ruby/mspec@0091e8a
Diffstat (limited to 'spec')
-rw-r--r-- | spec/mspec/.rspec | 1 | ||||
-rw-r--r-- | spec/mspec/lib/mspec/helpers/ruby_exe.rb | 23 | ||||
-rw-r--r-- | spec/mspec/spec/helpers/ruby_exe_spec.rb | 56 |
3 files changed, 77 insertions, 3 deletions
diff --git a/spec/mspec/.rspec b/spec/mspec/.rspec new file mode 100644 index 0000000000..4e1e0d2f72 --- /dev/null +++ b/spec/mspec/.rspec @@ -0,0 +1 @@ +--color diff --git a/spec/mspec/lib/mspec/helpers/ruby_exe.rb b/spec/mspec/lib/mspec/helpers/ruby_exe.rb index 075a8aaef5..c62c19f3aa 100644 --- a/spec/mspec/lib/mspec/helpers/ruby_exe.rb +++ b/spec/mspec/lib/mspec/helpers/ruby_exe.rb @@ -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 } diff --git a/spec/mspec/spec/helpers/ruby_exe_spec.rb b/spec/mspec/spec/helpers/ruby_exe_spec.rb index 32b3818853..8f14f63a32 100644 --- a/spec/mspec/spec/helpers/ruby_exe_spec.rb +++ b/spec/mspec/spec/helpers/ruby_exe_spec.rb @@ -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 |