ruby-changes:66406
From: Benoit <ko1@a...>
Date: Wed, 2 Jun 2021 21:42:27 +0900 (JST)
Subject: [ruby-changes:66406] a4fbc7e288 (master): Update to ruby/mspec@0091e8a
https://git.ruby-lang.org/ruby.git/commit/?id=a4fbc7e288 From a4fbc7e2884ba694278adea3b32ddb8c2ac10efe Mon Sep 17 00:00:00 2001 From: Benoit Daloze <eregontp@g...> Date: Wed, 2 Jun 2021 14:34:01 +0200 Subject: Update to ruby/mspec@0091e8a --- spec/mspec/.rspec | 1 + spec/mspec/lib/mspec/helpers/ruby_exe.rb | 23 +++++++++++-- spec/mspec/spec/helpers/ruby_exe_spec.rb | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 spec/mspec/.rspec diff --git a/spec/mspec/.rspec b/spec/mspec/.rspec new file mode 100644 index 0000000..4e1e0d2 --- /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 075a8aa..c62c19f 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' https://github.com/ruby/ruby/blob/trunk/spec/mspec/lib/mspec/helpers/ruby_exe.rb#L15 # # `#{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' https://github.com/ruby/ruby/blob/trunk/spec/mspec/lib/mspec/helpers/ruby_exe.rb#L30 # # 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' https://github.com/ruby/ruby/blob/trunk/spec/mspec/lib/mspec/helpers/ruby_exe.rb#L57 # (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 = {}) https://github.com/ruby/ruby/blob/trunk/spec/mspec/lib/mspec/helpers/ruby_exe.rb#L135 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 32b3818..8f14f63 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 https://github.com/ruby/ruby/blob/trunk/spec/mspec/spec/helpers/ruby_exe_spec.rb#L146 @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 https://github.com/ruby/ruby/blob/trunk/spec/mspec/spec/helpers/ruby_exe_spec.rb#L172 @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 https://github.com/ruby/ruby/blob/trunk/spec/mspec/spec/helpers/ruby_exe_spec.rb#L233 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 -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/