diff --git a/spec/std/process/status_spec.cr b/spec/std/process/status_spec.cr index bdfb2ee38d26..96802be31489 100644 --- a/spec/std/process/status_spec.cr +++ b/spec/std/process/status_spec.cr @@ -27,7 +27,13 @@ describe Process::Status do Process::Status.new(exit_status(128)).exit_code.should eq 128 Process::Status.new(exit_status(255)).exit_code.should eq 255 - status_for(:interrupted).exit_code.should eq({% if flag?(:unix) %}0{% else %}LibC::STATUS_CONTROL_C_EXIT.to_i32!{% end %}) + if {{ flag?(:unix) }} + expect_raises(RuntimeError, "Abnormal exit has no exit code") do + status_for(:interrupted).exit_code + end + else + status_for(:interrupted).exit_code.should eq({% if flag?(:unix) %}0{% else %}LibC::STATUS_CONTROL_C_EXIT.to_i32!{% end %}) + end end it "#success?" do diff --git a/src/process/status.cr b/src/process/status.cr index de29351ff12f..38e1336bfbcc 100644 --- a/src/process/status.cr +++ b/src/process/status.cr @@ -205,9 +205,13 @@ class Process::Status {% end %} end - # If `normal_exit?` is `true`, returns the exit code of the process. + # Returns the exit code of the process if it exited normally. + # + # Raises if the status describes an abnormal exit. def exit_code : Int32 {% if flag?(:unix) %} + raise RuntimeError.new("Abnormal exit has no exit code") unless normal_exit? + # define __WEXITSTATUS(status) (((status) & 0xff00) >> 8) (@exit_status & 0xff00) >> 8 {% else %}