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-10-05 05:57:47 -04:00
|
|
|
require 'irb/ext/save-history'
|
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
|
|
|
|
|
2020-10-05 05:57:47 -04:00
|
|
|
class TestInputMethod < ::IRB::InputMethod
|
|
|
|
HISTORY = Array.new
|
|
|
|
|
|
|
|
include IRB::HistorySavingAbility
|
|
|
|
|
|
|
|
attr_reader :list, :line_no
|
|
|
|
|
|
|
|
def initialize(list = [])
|
|
|
|
super("test")
|
|
|
|
@line_no = 0
|
|
|
|
@list = list
|
|
|
|
end
|
|
|
|
|
|
|
|
def gets
|
|
|
|
@list[@line_no]&.tap {@line_no += 1}
|
|
|
|
end
|
|
|
|
|
|
|
|
def eof?
|
|
|
|
@line_no >= @list.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def encoding
|
|
|
|
Encoding.default_external
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@line_no = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def winsize
|
|
|
|
[10, 20]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-06 10:34:46 -04:00
|
|
|
def test_history_save_1
|
2020-08-19 06:13:13 -04:00
|
|
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
2020-10-05 05:57:47 -04:00
|
|
|
IRB.conf[:SAVE_HISTORY] = 1
|
|
|
|
assert_history(<<~EXPECTED_HISTORY, <<~INITIAL_HISTORY, <<~INPUT)
|
2020-10-04 15:17:15 -04:00
|
|
|
exit
|
|
|
|
EXPECTED_HISTORY
|
2020-08-06 10:34:46 -04:00
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
2020-10-05 05:57:47 -04:00
|
|
|
INITIAL_HISTORY
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
INPUT
|
2020-08-06 10:34:46 -04:00
|
|
|
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-05 05:57:47 -04:00
|
|
|
IRB.conf[:SAVE_HISTORY] = 100
|
|
|
|
assert_history(<<~EXPECTED_HISTORY, <<~INITIAL_HISTORY, <<~INPUT)
|
2020-10-04 15:17:15 -04:00
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
EXPECTED_HISTORY
|
2020-08-06 10:34:46 -04:00
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
2020-10-05 05:57:47 -04:00
|
|
|
INITIAL_HISTORY
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
INPUT
|
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)
|
2020-10-05 05:57:47 -04:00
|
|
|
IRB.conf[:SAVE_HISTORY] = 10 ** 19
|
|
|
|
assert_history(<<~EXPECTED_HISTORY, <<~INITIAL_HISTORY, <<~INPUT)
|
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
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
2020-10-05 05:57:47 -04:00
|
|
|
INITIAL_HISTORY
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
INPUT
|
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)
|
2020-10-05 05:57:47 -04:00
|
|
|
IRB.conf[:SAVE_HISTORY] = -1 # infinity
|
|
|
|
assert_history(<<~EXPECTED_HISTORY, <<~INITIAL_HISTORY, <<~INPUT)
|
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
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
2020-10-05 05:57:47 -04:00
|
|
|
INITIAL_HISTORY
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
INPUT
|
2020-08-08 07:48:23 -04:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:17:23 -05:00
|
|
|
def test_history_concurrent_use
|
|
|
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
|
|
|
IRB.conf[:SAVE_HISTORY] = 1
|
|
|
|
assert_history(<<~EXPECTED_HISTORY, <<~INITIAL_HISTORY, <<~INPUT) do |history_file|
|
|
|
|
exit
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
EXPECTED_HISTORY
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
INITIAL_HISTORY
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
INPUT
|
|
|
|
assert_history(<<~EXPECTED_HISTORY2, <<~INITIAL_HISTORY2, <<~INPUT2)
|
|
|
|
exit
|
|
|
|
EXPECTED_HISTORY2
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
INITIAL_HISTORY2
|
|
|
|
5
|
|
|
|
exit
|
|
|
|
INPUT2
|
|
|
|
File.utime(File.atime(history_file), File.mtime(history_file) + 2, history_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-06 10:34:46 -04:00
|
|
|
private
|
|
|
|
|
2020-10-05 05:57:47 -04:00
|
|
|
def assert_history(expected_history, initial_irb_history, input)
|
|
|
|
backup_verbose, $VERBOSE = $VERBOSE, nil
|
2020-08-06 10:34:46 -04:00
|
|
|
backup_home = ENV["HOME"]
|
2021-03-18 05:07:18 -04:00
|
|
|
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
|
2020-10-05 05:57:47 -04:00
|
|
|
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
|
|
|
|
actual_history = nil
|
2020-08-06 10:34:46 -04:00
|
|
|
Dir.mktmpdir("test_irb_history_#{$$}") do |tmpdir|
|
|
|
|
ENV["HOME"] = tmpdir
|
|
|
|
open(IRB.rc_file("_history"), "w") do |f|
|
2020-10-05 05:57:47 -04:00
|
|
|
f.write(initial_irb_history)
|
2020-08-06 10:34:46 -04:00
|
|
|
end
|
|
|
|
|
2020-10-05 05:57:47 -04:00
|
|
|
io = TestInputMethod.new
|
|
|
|
io.class::HISTORY.clear
|
|
|
|
io.load_history
|
2021-03-02 15:17:23 -05:00
|
|
|
if block_given?
|
|
|
|
history = io.class::HISTORY.dup
|
|
|
|
yield IRB.rc_file("_history")
|
|
|
|
io.class::HISTORY.replace(history)
|
|
|
|
end
|
2020-10-05 05:57:47 -04:00
|
|
|
io.class::HISTORY.concat(input.split)
|
|
|
|
io.save_history
|
|
|
|
|
|
|
|
io.load_history
|
2020-08-06 10:34:46 -04:00
|
|
|
open(IRB.rc_file("_history"), "r") do |f|
|
2020-10-05 05:57:47 -04:00
|
|
|
actual_history = f.read
|
2020-08-06 10:34:46 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-05 05:57:47 -04:00
|
|
|
assert_equal(expected_history, actual_history, <<~MESSAGE)
|
2020-10-04 15:17:15 -04:00
|
|
|
expected:
|
|
|
|
#{expected_history}
|
|
|
|
but actual:
|
2020-10-05 05:57:47 -04:00
|
|
|
#{actual_history}
|
2020-10-04 15:17:15 -04:00
|
|
|
MESSAGE
|
2020-08-06 10:34:46 -04:00
|
|
|
ensure
|
2020-10-05 05:57:47 -04:00
|
|
|
$VERBOSE = backup_verbose
|
2020-08-06 10:34:46 -04:00
|
|
|
ENV["HOME"] = backup_home
|
2021-03-18 05:07:18 -04:00
|
|
|
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
|
2020-08-06 10:34:46 -04:00
|
|
|
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)
|