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..a3db8a7c4346 100644 --- a/src/process/status.cr +++ b/src/process/status.cr @@ -205,9 +205,19 @@ 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 (`#normal_exit?`). + # + # Raises `RuntimeError` if the status describes an abnormal exit. + # + # ``` + # Process.run("true").exit_code # => 1 + # Process.run("exit 123", shell: true).exit_code # => 123 + # Process.new("sleep", ["10"]).tap(&.terminate).wait.exit_code # RuntimeError: Abnormal exit has no exit code + # ``` 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 %}