Skip to content

Commit

Permalink
Merge pull request #177 from no6v/bug17564-exit-after-source
Browse files Browse the repository at this point in the history
Bug17564 exit after source
  • Loading branch information
aycabta authored Jan 27, 2021
2 parents 97cb2e2 + 502c590 commit 7248da8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def eval_input
printf "Use \"exit\" to leave %s\n", @context.ap_name
end
else
print "\n"
print "\n" if @context.prompting?
end
end
l
Expand Down
40 changes: 22 additions & 18 deletions lib/irb/ext/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ def search_file_from_ruby_path(fn) # :nodoc:
# See Irb#suspend_input_method for more information.
def source_file(path)
irb.suspend_name(path, File.basename(path)) do
irb.suspend_input_method(FileInputMethod.new(path)) do
|back_io|
irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
irb.eval_input
else
begin
FileInputMethod.open(path) do |io|
irb.suspend_input_method(io) do
|back_io|
irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
irb.eval_input
rescue LoadAbort
print "load abort!!\n"
else
begin
irb.eval_input
rescue LoadAbort
print "load abort!!\n"
end
end
end
end
Expand All @@ -79,16 +81,18 @@ def load_file(path, priv = nil)
ws = WorkSpace.new
end
irb.suspend_workspace(ws) do
irb.suspend_input_method(FileInputMethod.new(path)) do
|back_io|
irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
irb.eval_input
else
begin
FileInputMethod.open(path) do |io|
irb.suspend_input_method(io) do
|back_io|
irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
irb.eval_input
rescue LoadAbort
print "load abort!!\n"
else
begin
irb.eval_input
rescue LoadAbort
print "load abort!!\n"
end
end
end
end
Expand Down
20 changes: 18 additions & 2 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,22 @@ def inspect

# Use a File for IO with irb, see InputMethod
class FileInputMethod < InputMethod
class << self
def open(file, &block)
begin
io = new(file)
block.call(io)
ensure
io&.close
end
end
end

# Creates a new input method object
def initialize(file)
super
@io = IRB::MagicFile.open(file)
@external_encoding = @io.external_encoding
end
# The file name of this input method, usually given during initialization.
attr_reader :file_name
Expand All @@ -137,7 +149,7 @@ def initialize(file)
#
# See IO#eof? for more information.
def eof?
@io.eof?
@io.closed? || @io.eof?
end

# Reads the next line from this input method.
Expand All @@ -150,13 +162,17 @@ def gets

# The external encoding for standard input.
def encoding
@io.external_encoding
@external_encoding
end

# For debug message
def inspect
'FileInputMethod'
end

def close
@io.close
end
end

begin
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def each_top_level_statement
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no
end
break if @io.eof?
raise TerminateLineInput if @io.eof?
@line = ''
@exp_line_no = @line_no

Expand Down
52 changes: 52 additions & 0 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,57 @@ def test_measure_with_custom
assert_empty err
assert_match(/\A=> 3\nCUSTOM is added\.\n=> nil\ncustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
end

def test_irb_source
IRB.init_config(nil)
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
input = TestInputMethod.new([
"a = 'bug17564'\n",
"a\n",
"irb_source '#{@tmpdir}/a.rb'\n",
"a\n",
])
IRB.conf[:VERBOSE] = false
IRB.conf[:PROMPT_MODE] = :SIMPLE
irb = IRB::Irb.new(IRB::WorkSpace.new, input)
IRB.conf[:MAIN_CONTEXT] = irb.context
out, err = capture_output do
irb.eval_input
end
assert_empty err
assert_pattern_list([
/=> "bug17564"\n/,
/=> "bug17564"\n/,
/ => "hi"\n/,
/ => nil\n/,
/=> "hi"\n/,
], out)
end

def test_irb_load
IRB.init_config(nil)
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
input = TestInputMethod.new([
"a = 'bug17564'\n",
"a\n",
"irb_load '#{@tmpdir}/a.rb'\n",
"a\n",
])
IRB.conf[:VERBOSE] = false
IRB.conf[:PROMPT_MODE] = :SIMPLE
irb = IRB::Irb.new(IRB::WorkSpace.new, input)
IRB.conf[:MAIN_CONTEXT] = irb.context
out, err = capture_output do
irb.eval_input
end
assert_empty err
assert_pattern_list([
/=> "bug17564"\n/,
/=> "bug17564"\n/,
/ => "hi"\n/,
/ => nil\n/,
/=> "bug17564"\n/,
], out)
end
end
end

0 comments on commit 7248da8

Please sign in to comment.