Skip to content

Commit

Permalink
Add Process::Status#system_exit_status (#15296)
Browse files Browse the repository at this point in the history
Provides a consistent, portable API to retrieve the platform-specific exit status
  • Loading branch information
straight-shoota authored Dec 23, 2024
1 parent c5455ce commit 7d15e96
Show file tree
Hide file tree
Showing 2 changed files with 17 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 @@ -42,6 +42,16 @@ describe Process::Status do
status_for(:interrupted).exit_code?.should be_nil
end

it "#system_exit_status" do
Process::Status.new(exit_status(0)).system_exit_status.should eq 0_u32
Process::Status.new(exit_status(1)).system_exit_status.should eq({{ flag?(:unix) ? 0x0100_u32 : 1_u32 }})
Process::Status.new(exit_status(127)).system_exit_status.should eq({{ flag?(:unix) ? 0x7f00_u32 : 127_u32 }})
Process::Status.new(exit_status(128)).system_exit_status.should eq({{ flag?(:unix) ? 0x8000_u32 : 128_u32 }})
Process::Status.new(exit_status(255)).system_exit_status.should eq({{ flag?(:unix) ? 0xFF00_u32 : 255_u32 }})

status_for(:interrupted).system_exit_status.should eq({% if flag?(:unix) %}Signal::INT.value{% else %}LibC::STATUS_CONTROL_C_EXIT{% end %})
end

it "#success?" do
Process::Status.new(exit_status(0)).success?.should be_true
Process::Status.new(exit_status(1)).success?.should be_false
Expand Down
7 changes: 7 additions & 0 deletions src/process/status.cr
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ class Process::Status
@exit_status.to_i32!
end

# Returns the exit status as indicated by the operating system.
#
# It can encode exit codes and termination signals and is platform-specific.
def system_exit_status : UInt32
@exit_status.to_u32!
end

{% if flag?(:win32) %}
# :nodoc:
def initialize(@exit_status : UInt32)
Expand Down

0 comments on commit 7d15e96

Please sign in to comment.