File tree 3 files changed +41
-13
lines changed
3 files changed +41
-13
lines changed Original file line number Diff line number Diff line change @@ -99,6 +99,13 @@ describe Process::Status do
99
99
err1.hash.should eq(err2.hash)
100
100
end
101
101
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
+
102
109
{% if flag?(:unix ) && ! flag?(:wasi ) % }
103
110
it " #exit_signal" do
104
111
Process ::Status .new(Signal ::HUP .value).exit_signal.should eq Signal ::HUP
@@ -110,6 +117,16 @@ describe Process::Status do
110
117
Process ::Status .new(unknown_signal.value).exit_signal.should eq unknown_signal
111
118
end
112
119
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
+
113
130
it " #normal_exit? with signal code" do
114
131
Process ::Status .new(0x00 ).normal_exit?.should be_true
115
132
Process ::Status .new(0x01 ).normal_exit?.should be_false
Original file line number Diff line number Diff line change @@ -316,8 +316,7 @@ class Crystal::Command
316
316
private def exit_message (status )
317
317
case status.exit_reason
318
318
when .aborted?, .session_ended?, .terminal_disconnected?
319
- if status.signal_exit?
320
- signal = status.exit_signal
319
+ if signal = status.exit_signal?
321
320
if signal.kill?
322
321
" Program was killed"
323
322
else
Original file line number Diff line number Diff line change @@ -223,6 +223,20 @@ class Process::Status
223
223
{% end % }
224
224
end
225
225
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
+
226
240
# Returns the exit code of the process if it exited normally (`#normal_exit?`).
227
241
#
228
242
# Raises `RuntimeError` if the status describes an abnormal exit.
@@ -283,10 +297,10 @@ class Process::Status
283
297
@exit_status .to_s(io)
284
298
end
285
299
{% else % }
286
- if normal_exit ?
287
- exit_code .inspect(io)
300
+ if signal = exit_signal ?
301
+ signal .inspect(io)
288
302
else
289
- exit_signal .inspect(io)
303
+ exit_code .inspect(io)
290
304
end
291
305
{% end % }
292
306
io << " ]"
@@ -325,15 +339,14 @@ class Process::Status
325
339
@exit_status .to_s(io)
326
340
end
327
341
{% else % }
328
- if normal_exit?
329
- io << exit_code
330
- else
331
- signal = exit_signal
342
+ if signal = exit_signal?
332
343
if name = signal.member_name
333
344
io << name
334
345
else
335
346
signal.inspect(io)
336
347
end
348
+ else
349
+ io << exit_code
337
350
end
338
351
{% end % }
339
352
end
@@ -347,11 +360,10 @@ class Process::Status
347
360
{% if flag?(:win32 ) % }
348
361
name_for_win32_exit_status || @exit_status .to_s
349
362
{% else % }
350
- if normal_exit?
351
- exit_code.to_s
352
- else
353
- signal = exit_signal
363
+ if signal = exit_signal?
354
364
signal.member_name || signal.inspect
365
+ else
366
+ exit_code.to_s
355
367
end
356
368
{% end % }
357
369
end
You can’t perform that action at this time.
0 commit comments