diff options
Diffstat (limited to 'spec/ruby/core/signal/trap_spec.rb')
-rw-r--r-- | spec/ruby/core/signal/trap_spec.rb | 91 |
1 files changed, 35 insertions, 56 deletions
diff --git a/spec/ruby/core/signal/trap_spec.rb b/spec/ruby/core/signal/trap_spec.rb index a3298c71e1..91ce1f1348 100644 --- a/spec/ruby/core/signal/trap_spec.rb +++ b/spec/ruby/core/signal/trap_spec.rb @@ -4,8 +4,7 @@ platform_is_not :windows do describe "Signal.trap" do before :each do ScratchPad.clear - - @proc = lambda { ScratchPad.record :proc_trap } + @proc = -> {} @saved_trap = Signal.trap(:HUP, @proc) end @@ -48,49 +47,34 @@ platform_is_not :windows do ScratchPad.recorded.should be_true end + it "registers an handler doing nothing with :IGNORE" do + Signal.trap :HUP, :IGNORE + Process.kill(:HUP, Process.pid).should == 1 + end + it "ignores the signal when passed nil" do Signal.trap :HUP, nil Signal.trap(:HUP, @saved_trap).should be_nil end - it "accepts 'DEFAULT' as a symbol in place of a proc" do + it "accepts :DEFAULT in place of a proc" do Signal.trap :HUP, :DEFAULT - Signal.trap(:HUP, :DEFAULT).should == "DEFAULT" + Signal.trap(:HUP, @saved_trap).should == "DEFAULT" end - it "accepts 'SIG_DFL' as a symbol in place of a proc" do + it "accepts :SIG_DFL in place of a proc" do Signal.trap :HUP, :SIG_DFL - Signal.trap(:HUP, :SIG_DFL).should == "DEFAULT" + Signal.trap(:HUP, @saved_trap).should == "DEFAULT" end - it "accepts 'SIG_IGN' as a symbol in place of a proc" do + it "accepts :SIG_IGN in place of a proc" do Signal.trap :HUP, :SIG_IGN - Signal.trap(:HUP, :SIG_IGN).should == "IGNORE" + Signal.trap(:HUP, @saved_trap).should == "IGNORE" end - it "accepts 'IGNORE' as a symbol in place of a proc" do + it "accepts :IGNORE in place of a proc" do Signal.trap :HUP, :IGNORE - Signal.trap(:HUP, :IGNORE).should == "IGNORE" - end - - it "accepts long names as Strings" do - Signal.trap "SIGHUP", @proc - Signal.trap("SIGHUP", @saved_trap).should equal(@proc) - end - - it "accepts short names as Strings" do - Signal.trap "HUP", @proc - Signal.trap("HUP", @saved_trap).should equal(@proc) - end - - it "accepts long names as Symbols" do - Signal.trap :SIGHUP, @proc - Signal.trap(:SIGHUP, @saved_trap).should equal(@proc) - end - - it "accepts short names as Symbols" do - Signal.trap :HUP, @proc - Signal.trap(:HUP, @saved_trap).should equal(@proc) + Signal.trap(:HUP, @saved_trap).should == "IGNORE" end it "accepts 'SIG_DFL' in place of a proc" do @@ -105,34 +89,38 @@ platform_is_not :windows do it "accepts 'SIG_IGN' in place of a proc" do Signal.trap :HUP, "SIG_IGN" - Signal.trap(:HUP, "SIG_IGN").should == "IGNORE" + Signal.trap(:HUP, @saved_trap).should == "IGNORE" end it "accepts 'IGNORE' in place of a proc" do Signal.trap :HUP, "IGNORE" - Signal.trap(:HUP, "IGNORE").should == "IGNORE" + Signal.trap(:HUP, @saved_trap).should == "IGNORE" end - end - describe "Signal.trap" do - cannot_be_trapped = %w[KILL STOP] # See man 2 signal - reserved_signals = %w[VTALRM] + it "accepts long names as Strings" do + Signal.trap "SIGHUP", @proc + Signal.trap("SIGHUP", @saved_trap).should equal(@proc) + end - if PlatformGuard.implementation?(:ruby) - reserved_signals += %w[SEGV ILL FPE BUS] + it "accepts short names as Strings" do + Signal.trap "HUP", @proc + Signal.trap("HUP", @saved_trap).should equal(@proc) end - if PlatformGuard.implementation?(:truffleruby) - if !TruffleRuby.native? - reserved_signals += %w[SEGV ILL FPE USR1 QUIT] - end + it "accepts long names as Symbols" do + Signal.trap :SIGHUP, @proc + Signal.trap(:SIGHUP, @saved_trap).should equal(@proc) end - if PlatformGuard.implementation?(:jruby) - reserved_signals += %w[SEGV ILL FPE BUS USR1 QUIT] + it "accepts short names as Symbols" do + Signal.trap :HUP, @proc + Signal.trap(:HUP, @saved_trap).should equal(@proc) end + end - cannot_be_trapped.each do |signal| + describe "Signal.trap" do + # See man 2 signal + %w[KILL STOP].each do |signal| it "raises ArgumentError or Errno::EINVAL for SIG#{signal}" do -> { trap(signal, -> {}) @@ -143,17 +131,8 @@ platform_is_not :windows do end end - reserved_signals.each do |signal| - it "raises ArgumentError for reserved signal: SIG#{signal}" do - -> { - trap(signal, -> {}) - }.should raise_error(ArgumentError, /can't trap reserved signal|Signal already used by VM or OS/) - end - end - - it "allows to register a handler for all known signals, except reserved signals" do - excluded = cannot_be_trapped + reserved_signals - out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: [*excluded, "2>&1"]) + it "allows to register a handler for all known signals, except reserved signals for which it raises ArgumentError" do + out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: "2>&1") out.should == "OK\n" $?.exitstatus.should == 0 end |