From 18ee5dd19981f08aa6d6c1dec8b8dc0797fbc83d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 12 Dec 2024 11:22:27 +0100 Subject: [PATCH 1/2] Add specs for status quo --- spec/std/process/status_spec.cr | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/std/process/status_spec.cr b/spec/std/process/status_spec.cr index 86529b2cefd4..90bc275f3be5 100644 --- a/spec/std/process/status_spec.cr +++ b/spec/std/process/status_spec.cr @@ -105,6 +105,11 @@ describe Process::Status do Process::Status.new(Signal::INT.value).exit_signal.should eq Signal::INT last_signal = Signal.values[-1] 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 end it "#normal_exit? with signal code" do @@ -249,6 +254,10 @@ describe Process::Status do assert_prints Process::Status.new(Signal::HUP.value).to_s, "HUP" 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 end {% end %} end @@ -275,6 +284,11 @@ describe Process::Status do assert_prints Process::Status.new(Signal::HUP.value).inspect, "Process::Status[Signal::HUP]" last_signal = Signal.values[-1] 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 end {% end %} end From 7f0d4ddb339645799282c14eab12a4c121921c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 12 Dec 2024 11:27:25 +0100 Subject: [PATCH 2/2] Fix `Process::Status#to_s` for unknown signal value --- spec/std/process/status_spec.cr | 12 +++--------- src/enum.cr | 3 ++- src/process/status.cr | 12 +++++++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/spec/std/process/status_spec.cr b/spec/std/process/status_spec.cr index 90bc275f3be5..63136a2ddac7 100644 --- a/spec/std/process/status_spec.cr +++ b/spec/std/process/status_spec.cr @@ -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 @@ -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 @@ -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 diff --git a/src/enum.cr b/src/enum.cr index 8b6ca9eebbae..79b7d79a7479 100644 --- a/src/enum.cr +++ b/src/enum.cr @@ -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 %} diff --git a/src/process/status.cr b/src/process/status.cr index 5fd70e5ad203..28e6049238dc 100644 --- a/src/process/status.cr +++ b/src/process/status.cr @@ -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 %} @@ -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 @@ -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