Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement exit_program (!!!) command #825

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/irb/cmd/exit_program.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative "nop"

module IRB
module ExtendCommand
class ExitProgram < Nop
category "Misc"
description "End the current program, optionally with a status to give to `Kernel.exit`"

def execute(arg = true)
Kernel.exit(arg)
end
end
end
end
8 changes: 7 additions & 1 deletion lib/irb/extend-command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ def irb_context
:irb_history, :History, "cmd/history",
[:history, NO_OVERRIDE],
[:hist, NO_OVERRIDE],
],

[
:irb_exit_program, :ExitProgram, 'cmd/exit_program',
[:exit_program, NO_OVERRIDE],
[:quit_program, NO_OVERRIDE],
]
]

Expand Down Expand Up @@ -285,7 +291,7 @@ def install_alias_method(to, from, override = NO_OVERRIDE)

if override == OVERRIDE_ALL or
(override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
(override == NO_OVERRIDE) && !respond_to?(to, true)
(override == NO_OVERRIDE) && !respond_to?(to, true)
target = self
(class << self; self; end).instance_eval{
if target.respond_to?(to, true) &&
Expand Down
5 changes: 3 additions & 2 deletions lib/irb/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ def IRB.init_config(ap_path)

@CONF[:COMMAND_ALIASES] = {
# Symbol aliases
:'$' => :show_source,
:'@' => :whereami,
:'$' => :show_source,
:'@' => :whereami,
:'!!!' => :exit_program,
}
end

Expand Down
71 changes: 71 additions & 0 deletions test/irb/cmd/test_exit_program.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true
require 'irb'

require_relative "../helper"

module TestIRB
class ExitProgramTest < IntegrationTestCase
def test_irb_exit_program
assert_exits_program(with_status: 0) do
type "irb_exit_program"
end
end

def test_exit_program
assert_exits_program(with_status: 0) do
type "exit_program"
end
end

def test_quit_program
assert_exits_program(with_status: 0) do
type "quit_program"
end
end

def test_triple_bang
assert_exits_program(with_status: 0) do
type "!!!"
end
end

def test_exit_code_zero
assert_exits_program(with_status: 0) do
type "!!! 0"
end
end

def test_exit_code_one
assert_exits_program(with_status: 1) do
type "!!! 1"
end
end

def test_exit_code_expression
assert_exits_program(with_status: 2) do
type "n = 1"
type "!!! n + 1"
end
end

private

def assert_exits_program(with_status:, &block)
write_ruby <<~'RUBY'
begin
binding.irb
puts "Did not raise #{SystemExit}!" # Interpolate so we don't match whereami context
rescue SystemExit => e
puts "Raised SystemExit with status #{e.status.inspect}"
end
RUBY

output = run_ruby_file(&block)

refute_includes(output, "Did not raise SystemExit!", "AN ERROR MESSAGE")
matching_status = output[/(?<=Raised SystemExit with status )(\d+)/]
refute_nil matching_status, "Did not find exit status in output: \n#{output}"
assert_equal with_status, matching_status.to_i, "Exited with wrong status code"
end
end
end