Skip to content

Commit 5b43516

Browse files
committed
[GR-18163] Process.exit should use "exit" for the message
PullRequest: truffleruby/3254
2 parents 81842ba + 8c6dd44 commit 5b43516

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

spec/ruby/core/exception/system_exit_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
require_relative '../../spec_helper'
22

33
describe "SystemExit" do
4+
describe "#initialize" do
5+
it "accepts a status and message" do
6+
exc = SystemExit.new(42, "message")
7+
exc.status.should == 42
8+
exc.message.should == "message"
9+
10+
exc = SystemExit.new(true, "message")
11+
exc.status.should == 0
12+
exc.message.should == "message"
13+
14+
exc = SystemExit.new(false, "message")
15+
exc.status.should == 1
16+
exc.message.should == "message"
17+
end
18+
19+
it "accepts a status only" do
20+
exc = SystemExit.new(42)
21+
exc.status.should == 42
22+
exc.message.should == "SystemExit"
23+
24+
exc = SystemExit.new(true)
25+
exc.status.should == 0
26+
exc.message.should == "SystemExit"
27+
28+
exc = SystemExit.new(false)
29+
exc.status.should == 1
30+
exc.message.should == "SystemExit"
31+
end
32+
33+
it "accepts a message only" do
34+
exc = SystemExit.new("message")
35+
exc.status.should == 0
36+
exc.message.should == "message"
37+
end
38+
39+
it "accepts no arguments" do
40+
exc = SystemExit.new
41+
exc.status.should == 0
42+
exc.message.should == "SystemExit"
43+
end
44+
end
45+
446
it "sets the exit status and exits silently when raised" do
547
code = 'raise SystemExit.new(7)'
648
result = ruby_exe(code, args: "2>&1", exit_status: 7)

spec/ruby/shared/process/exit.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
end
2222
end
2323

24+
it "raises a SystemExit with message 'exit'" do
25+
-> { @object.exit }.should raise_error(SystemExit) { |e|
26+
e.message.should == "exit"
27+
}
28+
end
29+
2430
it "tries to convert the passed argument to an Integer using #to_int" do
2531
obj = mock('5')
2632
obj.should_receive(:to_int).and_return(5)

src/main/ruby/truffleruby/core/exception.rb

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -247,55 +247,30 @@ def initialize(*args)
247247
end
248248

249249
class SystemExit < Exception
250+
def initialize(status = true, message = nil)
251+
case status
252+
when true
253+
status = Process::EXIT_SUCCESS
254+
when false
255+
status = Process::EXIT_FAILURE
256+
else
257+
converted = Truffle::Type.rb_check_to_integer(status, :to_int)
258+
if Primitive.nil?(converted)
259+
message = status
260+
status = Process::EXIT_SUCCESS
261+
else
262+
status = converted
263+
end
264+
end
250265

251-
##
252-
# Process exit status if this exception is raised
253-
254-
##
255-
# Creates a SystemExit exception with optional status and message. If the
256-
# status is omitted, Process::EXIT_SUCCESS is used.
257-
#--
258-
# *args is used to simulate optional prepended argument like MRI
259-
260-
def initialize(*args)
261-
status = if args.empty?
262-
Process::EXIT_SUCCESS
263-
else
264-
case args[0]
265-
when true
266-
args.shift
267-
Process::EXIT_SUCCESS
268-
when false
269-
args.shift
270-
Process::EXIT_FAILURE
271-
else
272-
converted = Truffle::Type.rb_check_to_integer(args[0], :to_int)
273-
if Primitive.nil?(converted)
274-
Process::EXIT_SUCCESS
275-
else
276-
args.shift
277-
if converted == 0
278-
Process::EXIT_SUCCESS
279-
else
280-
converted
281-
end
282-
end
283-
end
284-
end
285-
super(*args)
266+
message ? super(message) : super()
286267

287268
Primitive.system_exit_set_status(self, status)
288269
end
289270

290-
##
291-
# Returns true is exiting successfully, false if not. A successful exit is
292-
# one with a status equal to 0 (zero). Any other status is considered a
293-
# unsuccessful exit.
294-
295271
def success?
296272
status == Process::EXIT_SUCCESS
297273
end
298-
299274
end
300275

301276

src/main/ruby/truffleruby/core/process.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ module Constants
6969

7070
FFI = Truffle::FFI
7171

72-
# Terminate with given status code.
73-
#
7472
def self.exit(code=0)
7573
case code
7674
when true
@@ -81,7 +79,7 @@ def self.exit(code=0)
8179
code = Truffle::Type.coerce_to code, Integer, :to_int
8280
end
8381

84-
raise SystemExit, code
82+
raise SystemExit.new(code, 'exit')
8583
end
8684

8785
def self.exit!(code=1)

0 commit comments

Comments
 (0)