Skip to content

Commit

Permalink
Add Process::Status#abnormal_exit? (#15266)
Browse files Browse the repository at this point in the history
Convenience methods for the inverse of `ExitReason#normal`?  and `Status#normal_exit?`
  • Loading branch information
straight-shoota authored Dec 13, 2024
1 parent ee69981 commit 5915e3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/std/process/status_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ describe Process::Status do
status_for(:interrupted).normal_exit?.should be_false
end

it "#abnormal_exit?" do
Process::Status.new(exit_status(0)).abnormal_exit?.should be_false
Process::Status.new(exit_status(1)).abnormal_exit?.should be_false
Process::Status.new(exit_status(127)).abnormal_exit?.should be_false
Process::Status.new(exit_status(128)).abnormal_exit?.should be_false
Process::Status.new(exit_status(255)).abnormal_exit?.should be_false

status_for(:interrupted).abnormal_exit?.should be_true
end

it "#signal_exit?" do
Process::Status.new(exit_status(0)).signal_exit?.should be_false
Process::Status.new(exit_status(1)).signal_exit?.should be_false
Expand Down
18 changes: 18 additions & 0 deletions src/process/status.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ enum Process::ExitReason
# * On Unix-like systems, this corresponds to `Signal::TERM`.
# * On Windows, this corresponds to the `CTRL_LOGOFF_EVENT` and `CTRL_SHUTDOWN_EVENT` messages.
SessionEnded

# Returns `true` if the process exited abnormally.
#
# This includes all values except `Normal`.
def abnormal?
!normal?
end
end

# The status of a terminated process. Returned by `Process#wait`.
Expand Down Expand Up @@ -186,10 +193,21 @@ class Process::Status
# Equivalent to `ExitReason::Normal`
#
# * `#exit_reason` provides more insights into other exit reasons.
# * `#abnormal_exit?` returns the inverse.
def normal_exit? : Bool
exit_reason.normal?
end

# Returns `true` if the process terminated abnormally.
#
# Equivalent to `ExitReason#abnormal?`
#
# * `#exit_reason` provides more insights into the specific exit reason.
# * `#normal_exit?` returns the inverse.
def abnormal_exit? : Bool
exit_reason.abnormal?
end

# If `signal_exit?` is `true`, returns the *Signal* the process
# received and didn't handle. Will raise if `signal_exit?` is `false`.
#
Expand Down

0 comments on commit 5915e3b

Please sign in to comment.