diff options
author | Benoit Daloze <eregontp@gmail.com> | 2020-07-27 21:41:08 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2020-07-27 21:41:08 +0200 |
commit | 126fd5f15cff0d3bf314d90d8c21a3ae25ae8e68 (patch) | |
tree | 33350f7170436c32ed4c8e79f0be2c334c7bc8a9 /spec/ruby/core/kernel | |
parent | 7429841ab6494b849106e6d3b119f147adfee3b7 (diff) |
Update to ruby/spec@07164da
Diffstat (limited to 'spec/ruby/core/kernel')
-rw-r--r-- | spec/ruby/core/kernel/__dir___spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/kernel/at_exit_spec.rb | 31 | ||||
-rw-r--r-- | spec/ruby/core/kernel/fixtures/__dir__.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/kernel/fixtures/at_exit.rb | 3 | ||||
-rw-r--r-- | spec/ruby/core/kernel/fixtures/warn_require.rb | 1 | ||||
-rw-r--r-- | spec/ruby/core/kernel/fixtures/warn_require_caller.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/kernel/raise_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/kernel/warn_spec.rb | 13 |
8 files changed, 77 insertions, 3 deletions
diff --git a/spec/ruby/core/kernel/__dir___spec.rb b/spec/ruby/core/kernel/__dir___spec.rb index 64b439161f..0686b31e97 100644 --- a/spec/ruby/core/kernel/__dir___spec.rb +++ b/spec/ruby/core/kernel/__dir___spec.rb @@ -5,6 +5,13 @@ describe "Kernel#__dir__" do __dir__.should == File.realpath(File.dirname(__FILE__)) end + it "returns the expanded path of the directory when used in the main script" do + fixtures_dir = File.dirname(fixture(__FILE__, '__dir__.rb')) + Dir.chdir(fixtures_dir) do + ruby_exe("__dir__.rb").should == "__dir__.rb\n#{fixtures_dir}\n" + end + end + context "when used in eval with a given filename" do it "returns File.dirname(filename)" do eval("__dir__", nil, "foo.rb").should == "." diff --git a/spec/ruby/core/kernel/at_exit_spec.rb b/spec/ruby/core/kernel/at_exit_spec.rb index 21149f965b..7bdb5391fe 100644 --- a/spec/ruby/core/kernel/at_exit_spec.rb +++ b/spec/ruby/core/kernel/at_exit_spec.rb @@ -23,7 +23,7 @@ describe "Kernel.at_exit" do it "gives access to the last raised exception" do code = <<-EOC at_exit do - puts "The exception matches: \#{$! == $exception}" + puts "The exception matches: \#{$! == $exception} (message=\#{$!.message})" end begin @@ -33,10 +33,35 @@ describe "Kernel.at_exit" do end EOC - result = ruby_exe(code, args: "2>&1", escape: true) - result.should =~ /The exception matches: true/ + result = ruby_exe(code, args: "2>&1") + result.lines.should.include?("The exception matches: true (message=foo)\n") end + it "both exceptions in at_exit and in the main script are printed" do + result = ruby_exe('at_exit { raise "at_exit_error" }; raise "main_script_error"', args: "2>&1") + result.should.include?('at_exit_error (RuntimeError)') + result.should.include?('main_script_error (RuntimeError)') + end + + it "decides the exit status if both at_exit and the main script raise SystemExit" do + ruby_exe('at_exit { exit 43 }; exit 42', args: "2>&1") + $?.exitstatus.should == 43 + end + + it "runs all at_exit even if some raise exceptions" do + code = 'at_exit { STDERR.puts "last" }; at_exit { exit 43 }; at_exit { STDERR.puts "first" }; exit 42' + result = ruby_exe(code, args: "2>&1") + result.should == "first\nlast\n" + $?.exitstatus.should == 43 + end + + it "runs at_exit handlers even if the main script fails to parse" do + script = fixture(__FILE__, "at_exit.rb") + result = ruby_exe('{', options: "-r#{script}", args: "2>&1") + $?.should_not.success? + result.should.include?("at_exit ran\n") + result.should.include?("syntax error") + end end describe "Kernel#at_exit" do diff --git a/spec/ruby/core/kernel/fixtures/__dir__.rb b/spec/ruby/core/kernel/fixtures/__dir__.rb new file mode 100644 index 0000000000..bf9a15e3c8 --- /dev/null +++ b/spec/ruby/core/kernel/fixtures/__dir__.rb @@ -0,0 +1,2 @@ +puts __FILE__ +puts __dir__ diff --git a/spec/ruby/core/kernel/fixtures/at_exit.rb b/spec/ruby/core/kernel/fixtures/at_exit.rb new file mode 100644 index 0000000000..9c11a7ad6c --- /dev/null +++ b/spec/ruby/core/kernel/fixtures/at_exit.rb @@ -0,0 +1,3 @@ +at_exit do + STDERR.puts "at_exit ran" +end diff --git a/spec/ruby/core/kernel/fixtures/warn_require.rb b/spec/ruby/core/kernel/fixtures/warn_require.rb new file mode 100644 index 0000000000..c4b0733233 --- /dev/null +++ b/spec/ruby/core/kernel/fixtures/warn_require.rb @@ -0,0 +1 @@ +warn 'warn-require-warning', uplevel: 1 diff --git a/spec/ruby/core/kernel/fixtures/warn_require_caller.rb b/spec/ruby/core/kernel/fixtures/warn_require_caller.rb new file mode 100644 index 0000000000..35a0f969f9 --- /dev/null +++ b/spec/ruby/core/kernel/fixtures/warn_require_caller.rb @@ -0,0 +1,2 @@ +# Use a different line than just 1 +require "#{__dir__}/warn_require" diff --git a/spec/ruby/core/kernel/raise_spec.rb b/spec/ruby/core/kernel/raise_spec.rb index bf26560246..591daa03cf 100644 --- a/spec/ruby/core/kernel/raise_spec.rb +++ b/spec/ruby/core/kernel/raise_spec.rb @@ -6,6 +6,27 @@ describe "Kernel#raise" do it "is a private method" do Kernel.should have_private_instance_method(:raise) end + + it "re-raises the previously rescued exception if no exception is specified" do + ScratchPad.record nil + + -> do + begin + raise Exception, "outer" + ScratchPad.record :no_abort + rescue Exception + begin + raise StandardError, "inner" + rescue StandardError + end + + raise + ScratchPad.record :no_reraise + end + end.should raise_error(Exception, "outer") + + ScratchPad.recorded.should be_nil + end end describe "Kernel#raise" do diff --git a/spec/ruby/core/kernel/warn_spec.rb b/spec/ruby/core/kernel/warn_spec.rb index 84cfa78672..79e78dd4a3 100644 --- a/spec/ruby/core/kernel/warn_spec.rb +++ b/spec/ruby/core/kernel/warn_spec.rb @@ -101,6 +101,19 @@ describe "Kernel#warn" do -> { w.f4("foo", 3) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f3_call_lineno}: warning: foo|) end + # Test both explicitly without and with RubyGems as RubyGems overrides Kernel#warn + it "shows the caller of #require and not #require itself without RubyGems" do + file = fixture(__FILE__ , "warn_require_caller.rb") + ruby_exe(file, options: "--disable-gems", args: "2>&1").should == "#{file}:2: warning: warn-require-warning\n" + end + + ruby_version_is "2.6" do + it "shows the caller of #require and not #require itself with RubyGems loaded" do + file = fixture(__FILE__ , "warn_require_caller.rb") + ruby_exe(file, options: "-rrubygems", args: "2>&1").should == "#{file}:2: warning: warn-require-warning\n" + end + end + it "converts first arg using to_s" do w = KernelSpecs::WarnInNestedCall.new |