Skip to content

Commit ed7dce3

Browse files
Redefine Process::Status#normal_exit? on Windows (#15255)
Aligns the definition of `Process::Status#normal_exit?` with `Process::ExitReason::Normal` as discusssed in #15231.
1 parent 512a8b7 commit ed7dce3

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

spec/std/process/status_spec.cr

+4-8
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ describe Process::Status do
2727
Process::Status.new(exit_status(128)).exit_code.should eq 128
2828
Process::Status.new(exit_status(255)).exit_code.should eq 255
2929

30-
if {{ flag?(:unix) }}
31-
expect_raises(RuntimeError, "Abnormal exit has no exit code") do
32-
status_for(:interrupted).exit_code
33-
end
34-
else
35-
status_for(:interrupted).exit_code.should eq({% if flag?(:unix) %}0{% else %}LibC::STATUS_CONTROL_C_EXIT.to_i32!{% end %})
30+
expect_raises(RuntimeError, "Abnormal exit has no exit code") do
31+
status_for(:interrupted).exit_code
3632
end
3733
end
3834

@@ -43,7 +39,7 @@ describe Process::Status do
4339
Process::Status.new(exit_status(128)).exit_code?.should eq 128
4440
Process::Status.new(exit_status(255)).exit_code?.should eq 255
4541

46-
status_for(:interrupted).exit_code?.should eq({% if flag?(:unix) %}nil{% else %}LibC::STATUS_CONTROL_C_EXIT.to_i32!{% end %})
42+
status_for(:interrupted).exit_code?.should be_nil
4743
end
4844

4945
it "#success?" do
@@ -63,7 +59,7 @@ describe Process::Status do
6359
Process::Status.new(exit_status(128)).normal_exit?.should be_true
6460
Process::Status.new(exit_status(255)).normal_exit?.should be_true
6561

66-
status_for(:interrupted).normal_exit?.should eq {{ flag?(:win32) }}
62+
status_for(:interrupted).normal_exit?.should be_false
6763
end
6864

6965
it "#signal_exit?" do

src/process/status.cr

+9-9
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ class Process::Status
135135
@exit_status & 0xC0000000_u32 == 0 ? ExitReason::Normal : ExitReason::Unknown
136136
end
137137
{% elsif flag?(:unix) && !flag?(:wasm32) %}
138-
if normal_exit?
138+
# define __WIFEXITED(status) (__WTERMSIG(status) == 0)
139+
if signal_code == 0
139140
ExitReason::Normal
140141
elsif signal_exit?
141142
case Signal.from_value?(signal_code)
@@ -181,13 +182,12 @@ class Process::Status
181182
end
182183

183184
# Returns `true` if the process terminated normally.
185+
#
186+
# Equivalent to `ExitReason::Normal`
187+
#
188+
# * `#exit_reason` provides more insights into other exit reasons.
184189
def normal_exit? : Bool
185-
{% if flag?(:unix) %}
186-
# define __WIFEXITED(status) (__WTERMSIG(status) == 0)
187-
signal_code == 0
188-
{% else %}
189-
true
190-
{% end %}
190+
exit_reason.normal?
191191
end
192192

193193
# If `signal_exit?` is `true`, returns the *Signal* the process
@@ -228,9 +228,9 @@ class Process::Status
228228
# Process.new("sleep", ["10"]).tap(&.terminate).wait.exit_code? # => nil
229229
# ```
230230
def exit_code? : Int32?
231-
{% if flag?(:unix) %}
232-
return unless normal_exit?
231+
return unless normal_exit?
233232

233+
{% if flag?(:unix) %}
234234
# define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
235235
(@exit_status & 0xff00) >> 8
236236
{% else %}

0 commit comments

Comments
 (0)