Skip to content

Commit 5545bca

Browse files
Add Process::Status#exit_signal? (#15284)
1 parent 7326cef commit 5545bca

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

spec/std/process/status_spec.cr

+17
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ describe Process::Status do
9999
err1.hash.should eq(err2.hash)
100100
end
101101

102+
it "#exit_signal?" do
103+
Process::Status.new(exit_status(0)).exit_signal?.should be_nil
104+
Process::Status.new(exit_status(1)).exit_signal?.should be_nil
105+
106+
status_for(:interrupted).exit_signal?.should eq({% if flag?(:unix) %}Signal::INT{% else %}nil{% end %})
107+
end
108+
102109
{% if flag?(:unix) && !flag?(:wasi) %}
103110
it "#exit_signal" do
104111
Process::Status.new(Signal::HUP.value).exit_signal.should eq Signal::HUP
@@ -110,6 +117,16 @@ describe Process::Status do
110117
Process::Status.new(unknown_signal.value).exit_signal.should eq unknown_signal
111118
end
112119

120+
it "#exit_signal?" do
121+
Process::Status.new(Signal::HUP.value).exit_signal?.should eq Signal::HUP
122+
Process::Status.new(Signal::INT.value).exit_signal?.should eq Signal::INT
123+
last_signal = Signal.values[-1]
124+
Process::Status.new(last_signal.value).exit_signal?.should eq last_signal
125+
126+
unknown_signal = Signal.new(126)
127+
Process::Status.new(unknown_signal.value).exit_signal?.should eq unknown_signal
128+
end
129+
113130
it "#normal_exit? with signal code" do
114131
Process::Status.new(0x00).normal_exit?.should be_true
115132
Process::Status.new(0x01).normal_exit?.should be_false

src/compiler/crystal/command.cr

+1-2
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ class Crystal::Command
316316
private def exit_message(status)
317317
case status.exit_reason
318318
when .aborted?, .session_ended?, .terminal_disconnected?
319-
if status.signal_exit?
320-
signal = status.exit_signal
319+
if signal = status.exit_signal?
321320
if signal.kill?
322321
"Program was killed"
323322
else

src/process/status.cr

+23-11
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ class Process::Status
223223
{% end %}
224224
end
225225

226+
# Returns the exit `Signal` or `nil` if there is none.
227+
#
228+
# On Windows returns always `nil`.
229+
#
230+
# * `#exit_reason` is a portable alternative.
231+
def exit_signal? : Signal?
232+
{% if flag?(:unix) && !flag?(:wasm32) %}
233+
code = signal_code
234+
unless code.zero?
235+
Signal.new(code)
236+
end
237+
{% end %}
238+
end
239+
226240
# Returns the exit code of the process if it exited normally (`#normal_exit?`).
227241
#
228242
# Raises `RuntimeError` if the status describes an abnormal exit.
@@ -283,10 +297,10 @@ class Process::Status
283297
@exit_status.to_s(io)
284298
end
285299
{% else %}
286-
if normal_exit?
287-
exit_code.inspect(io)
300+
if signal = exit_signal?
301+
signal.inspect(io)
288302
else
289-
exit_signal.inspect(io)
303+
exit_code.inspect(io)
290304
end
291305
{% end %}
292306
io << "]"
@@ -325,15 +339,14 @@ class Process::Status
325339
@exit_status.to_s(io)
326340
end
327341
{% else %}
328-
if normal_exit?
329-
io << exit_code
330-
else
331-
signal = exit_signal
342+
if signal = exit_signal?
332343
if name = signal.member_name
333344
io << name
334345
else
335346
signal.inspect(io)
336347
end
348+
else
349+
io << exit_code
337350
end
338351
{% end %}
339352
end
@@ -347,11 +360,10 @@ class Process::Status
347360
{% if flag?(:win32) %}
348361
name_for_win32_exit_status || @exit_status.to_s
349362
{% else %}
350-
if normal_exit?
351-
exit_code.to_s
352-
else
353-
signal = exit_signal
363+
if signal = exit_signal?
354364
signal.member_name || signal.inspect
365+
else
366+
exit_code.to_s
355367
end
356368
{% end %}
357369
end

0 commit comments

Comments
 (0)