Skip to content

Commit

Permalink
Add Process::Status#exit_signal?
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Dec 17, 2024
1 parent 1df4b23 commit 27b081b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/std/process/status_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ describe Process::Status do
err1.hash.should eq(err2.hash)
end

it "#exit_signal?" do
Process::Status.new(exit_status(0)).exit_signal?.should be_nil
Process::Status.new(exit_status(1)).exit_signal?.should be_nil

status_for(:interrupted).exit_signal?.should eq({% if flag?(:unix) %}Signal::INT{% else %}nil{% end %})
end

{% if flag?(:unix) && !flag?(:wasi) %}
it "#exit_signal" do
Process::Status.new(Signal::HUP.value).exit_signal.should eq Signal::HUP
Expand All @@ -110,6 +117,16 @@ describe Process::Status do
Process::Status.new(unknown_signal.value).exit_signal.should eq unknown_signal
end

it "#exit_signal?" do
Process::Status.new(Signal::HUP.value).exit_signal?.should eq Signal::HUP
Process::Status.new(Signal::INT.value).exit_signal?.should eq Signal::INT
last_signal = Signal.values[-1]
Process::Status.new(last_signal.value).exit_signal?.should eq last_signal

unknown_signal = Signal.new(126)
Process::Status.new(unknown_signal.value).exit_signal?.should eq unknown_signal
end

it "#normal_exit? with signal code" do
Process::Status.new(0x00).normal_exit?.should be_true
Process::Status.new(0x01).normal_exit?.should be_false
Expand Down
14 changes: 14 additions & 0 deletions src/process/status.cr
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ class Process::Status
{% end %}
end

# Returns the exit `Signal` or `nil` if there is none.
#
# On Windows returns always `nil`.
#
# * `#exit_reason` is a portable alternative.
def exit_signal? : Signal?
{% if flag?(:unix) && !flag?(:wasm32) %}
code = signal_code
unless code.zero?
Signal.new(code)
end
{% end %}
end

# Returns the exit code of the process if it exited normally (`#normal_exit?`).
#
# Raises `RuntimeError` if the status describes an abnormal exit.
Expand Down

0 comments on commit 27b081b

Please sign in to comment.