Skip to content

Commit 5915e3b

Browse files
Add Process::Status#abnormal_exit? (#15266)
Convenience methods for the inverse of `ExitReason#normal`? and `Status#normal_exit?`
1 parent ee69981 commit 5915e3b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

spec/std/process/status_spec.cr

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ describe Process::Status do
6262
status_for(:interrupted).normal_exit?.should be_false
6363
end
6464

65+
it "#abnormal_exit?" do
66+
Process::Status.new(exit_status(0)).abnormal_exit?.should be_false
67+
Process::Status.new(exit_status(1)).abnormal_exit?.should be_false
68+
Process::Status.new(exit_status(127)).abnormal_exit?.should be_false
69+
Process::Status.new(exit_status(128)).abnormal_exit?.should be_false
70+
Process::Status.new(exit_status(255)).abnormal_exit?.should be_false
71+
72+
status_for(:interrupted).abnormal_exit?.should be_true
73+
end
74+
6575
it "#signal_exit?" do
6676
Process::Status.new(exit_status(0)).signal_exit?.should be_false
6777
Process::Status.new(exit_status(1)).signal_exit?.should be_false

src/process/status.cr

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ enum Process::ExitReason
9191
# * On Unix-like systems, this corresponds to `Signal::TERM`.
9292
# * On Windows, this corresponds to the `CTRL_LOGOFF_EVENT` and `CTRL_SHUTDOWN_EVENT` messages.
9393
SessionEnded
94+
95+
# Returns `true` if the process exited abnormally.
96+
#
97+
# This includes all values except `Normal`.
98+
def abnormal?
99+
!normal?
100+
end
94101
end
95102

96103
# The status of a terminated process. Returned by `Process#wait`.
@@ -186,10 +193,21 @@ class Process::Status
186193
# Equivalent to `ExitReason::Normal`
187194
#
188195
# * `#exit_reason` provides more insights into other exit reasons.
196+
# * `#abnormal_exit?` returns the inverse.
189197
def normal_exit? : Bool
190198
exit_reason.normal?
191199
end
192200

201+
# Returns `true` if the process terminated abnormally.
202+
#
203+
# Equivalent to `ExitReason#abnormal?`
204+
#
205+
# * `#exit_reason` provides more insights into the specific exit reason.
206+
# * `#normal_exit?` returns the inverse.
207+
def abnormal_exit? : Bool
208+
exit_reason.abnormal?
209+
end
210+
193211
# If `signal_exit?` is `true`, returns the *Signal* the process
194212
# received and didn't handle. Will raise if `signal_exit?` is `false`.
195213
#

0 commit comments

Comments
 (0)