Skip to content

Commit

Permalink
Fix Process::Status#to_s for unknown signal value
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Dec 14, 2024
1 parent 18ee5dd commit 7f0d4dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
12 changes: 3 additions & 9 deletions spec/std/process/status_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ describe Process::Status do
Process::Status.new(last_signal.value).exit_signal.should eq last_signal

unknown_signal = Signal.new(126)
expect_raises(Exception, "Unknown enum Signal value: 126") do
Process::Status.new(unknown_signal.value).exit_signal
end
Process::Status.new(unknown_signal.value).exit_signal.should eq unknown_signal
end

it "#normal_exit? with signal code" do
Expand Down Expand Up @@ -255,9 +253,7 @@ describe Process::Status do
last_signal = Signal.values[-1]
assert_prints Process::Status.new(last_signal.value).to_s, last_signal.to_s

expect_raises(Exception, "Unknown enum Signal value: 126") do
Process::Status.new(Signal.new(126).value).to_s
end
assert_prints Process::Status.new(Signal.new(126).value).to_s, "Signal[126]"
end
{% end %}
end
Expand Down Expand Up @@ -286,9 +282,7 @@ describe Process::Status do
assert_prints Process::Status.new(last_signal.value).inspect, "Process::Status[#{last_signal.inspect}]"

unknown_signal = Signal.new(126)
expect_raises(Exception, "Unknown enum Signal value: 126") do
Process::Status.new(unknown_signal.value).inspect
end
assert_prints Process::Status.new(unknown_signal.value).inspect, "Process::Status[Signal[126]]"
end
{% end %}
end
Expand Down
3 changes: 2 additions & 1 deletion src/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ struct Enum
end
end

private def member_name
# :nodoc:
def member_name : String?
# Can't use `case` here because case with duplicate values do
# not compile, but enums can have duplicates (such as `enum Foo; FOO = 1; BAR = 1; end`).
{% for member in @type.constants %}
Expand Down
12 changes: 9 additions & 3 deletions src/process/status.cr
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Process::Status
# which also works on Windows.
def exit_signal : Signal
{% if flag?(:unix) && !flag?(:wasm32) %}
Signal.from_value(signal_code)
Signal.new(signal_code)
{% else %}
raise NotImplementedError.new("Process::Status#exit_signal")
{% end %}
Expand Down Expand Up @@ -298,7 +298,12 @@ class Process::Status
if normal_exit?
io << exit_code
else
io << exit_signal
signal = exit_signal
if name = signal.member_name
io << name
else
signal.inspect(io)
end
end
{% end %}
end
Expand All @@ -314,7 +319,8 @@ class Process::Status
if normal_exit?
exit_code.to_s
else
exit_signal.to_s
signal = exit_signal
signal.member_name || signal.inspect
end
{% end %}
end
Expand Down

0 comments on commit 7f0d4dd

Please sign in to comment.