2020-08-06 10:34:46 -04:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
2020-08-11 10:54:23 -04:00
|
|
|
require 'irb'
|
2020-08-19 06:13:13 -04:00
|
|
|
require 'readline'
|
2020-08-06 10:34:46 -04:00
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestHistory < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
IRB.conf[:RC_NAME_GENERATOR] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
IRB.conf[:RC_NAME_GENERATOR] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_history_save_1
|
2020-08-19 06:13:13 -04:00
|
|
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
2020-10-04 15:17:15 -04:00
|
|
|
assert_history_with_irbrc_and_irb_history(<<~EXPECTED_HISTORY, <<~IRBRC, <<~IRB_HISTORY) do |stdin|
|
|
|
|
exit
|
|
|
|
EXPECTED_HISTORY
|
2020-08-06 10:34:46 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
|
|
|
IRB.conf[:SAVE_HISTORY] = 1
|
2020-08-19 06:13:13 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
2020-08-06 10:34:46 -04:00
|
|
|
IRBRC
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
IRB_HISTORY
|
|
|
|
stdin.write("5\nexit\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_history_save_100
|
2020-08-19 06:13:13 -04:00
|
|
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
2020-10-04 15:17:15 -04:00
|
|
|
assert_history_with_irbrc_and_irb_history(<<~EXPECTED_HISTORY, <<~IRBRC, <<~IRB_HISTORY) do |stdin|
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
EXPECTED_HISTORY
|
2020-08-06 10:34:46 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
|
|
|
IRB.conf[:SAVE_HISTORY] = 100
|
2020-08-19 06:13:13 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
2020-08-06 10:34:46 -04:00
|
|
|
IRBRC
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
IRB_HISTORY
|
|
|
|
stdin.write("5\nexit\n")
|
|
|
|
end
|
2020-10-04 15:17:15 -04:00
|
|
|
end
|
2020-08-06 10:34:46 -04:00
|
|
|
|
2020-10-04 15:17:15 -04:00
|
|
|
def test_history_save_bignum
|
|
|
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
|
|
|
assert_history_with_irbrc_and_irb_history(<<~EXPECTED_HISTORY, <<~IRBRC, <<~IRB_HISTORY) do |stdin|
|
2020-08-06 10:34:46 -04:00
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
exit
|
2020-10-04 15:17:15 -04:00
|
|
|
EXPECTED_HISTORY
|
2020-08-07 10:42:51 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
|
|
|
IRB.conf[:SAVE_HISTORY] = 10 ** 19
|
2020-08-19 06:13:13 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
2020-08-07 10:42:51 -04:00
|
|
|
IRBRC
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
IRB_HISTORY
|
|
|
|
stdin.write("5\nexit\n")
|
|
|
|
end
|
2020-10-04 15:17:15 -04:00
|
|
|
end
|
2020-08-07 10:42:51 -04:00
|
|
|
|
2020-10-04 15:17:15 -04:00
|
|
|
def test_history_save_minus_as_infinity
|
|
|
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
|
|
|
assert_history_with_irbrc_and_irb_history(<<~EXPECTED_HISTORY, <<~IRBRC, <<~IRB_HISTORY) do |stdin|
|
2020-08-07 10:42:51 -04:00
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
exit
|
2020-10-04 15:17:15 -04:00
|
|
|
EXPECTED_HISTORY
|
2020-08-08 07:48:23 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
|
|
|
IRB.conf[:SAVE_HISTORY] = -1 # infinity
|
2020-08-19 06:13:13 -04:00
|
|
|
IRB.conf[:USE_READLINE] = true
|
2020-08-08 07:48:23 -04:00
|
|
|
IRBRC
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
IRB_HISTORY
|
|
|
|
stdin.write("5\nexit\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-06 10:34:46 -04:00
|
|
|
private
|
|
|
|
|
2020-10-04 15:17:15 -04:00
|
|
|
def assert_history_with_irbrc_and_irb_history(expected_history, irbrc, irb_history)
|
2020-08-06 10:34:46 -04:00
|
|
|
result = nil
|
|
|
|
result_history = nil
|
|
|
|
backup_irbrc = ENV.delete("IRBRC")
|
|
|
|
backup_home = ENV["HOME"]
|
|
|
|
Dir.mktmpdir("test_irb_history_#{$$}") do |tmpdir|
|
|
|
|
ENV["HOME"] = tmpdir
|
|
|
|
open(IRB.rc_file, "w") do |f|
|
|
|
|
f.write(irbrc)
|
|
|
|
end
|
|
|
|
open(IRB.rc_file("_history"), "w") do |f|
|
|
|
|
f.write(irb_history)
|
|
|
|
end
|
|
|
|
|
|
|
|
with_temp_stdio do |stdin, stdout|
|
2020-08-18 20:42:34 -04:00
|
|
|
yield(stdin, stdout)
|
|
|
|
stdin.close
|
|
|
|
stdout.flush
|
2020-10-04 15:17:15 -04:00
|
|
|
system('ruby', '-Ilib', '-Itest', '-W0', '-rirb', '-e', 'IRB.start(__FILE__)', in: stdin.path, out: stdout.path, err: stdout.path)
|
2020-08-18 20:42:34 -04:00
|
|
|
result = stdout.read
|
|
|
|
stdout.close
|
2020-08-06 10:34:46 -04:00
|
|
|
end
|
|
|
|
open(IRB.rc_file("_history"), "r") do |f|
|
|
|
|
result_history = f.read
|
|
|
|
end
|
|
|
|
end
|
2020-10-04 15:17:15 -04:00
|
|
|
assert_equal(expected_history, result_history, <<~MESSAGE)
|
|
|
|
expected:
|
|
|
|
#{expected_history}
|
|
|
|
but actual:
|
|
|
|
#{result_history}
|
|
|
|
and stdout and stderr ware
|
|
|
|
#{result}
|
|
|
|
MESSAGE
|
2020-08-06 10:34:46 -04:00
|
|
|
ensure
|
|
|
|
ENV["HOME"] = backup_home
|
|
|
|
ENV["IRBRC"] = backup_irbrc
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_temp_stdio
|
|
|
|
Tempfile.create("test_readline_stdin") do |stdin|
|
|
|
|
Tempfile.create("test_readline_stdout") do |stdout|
|
|
|
|
yield stdin, stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-20 00:24:51 -04:00
|
|
|
end if not RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)
|