|
| 1 | +# frozen_string_literal: false |
| 2 | +require 'test/unit' |
| 3 | + |
| 4 | +module TestIRB |
| 5 | + class TestHistory < Test::Unit::TestCase |
| 6 | + def setup |
| 7 | + IRB.conf[:RC_NAME_GENERATOR] = nil |
| 8 | + end |
| 9 | + |
| 10 | + def teardown |
| 11 | + IRB.conf[:RC_NAME_GENERATOR] = nil |
| 12 | + end |
| 13 | + |
| 14 | + def test_history_save_1 |
| 15 | + result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin| |
| 16 | + IRB.conf[:USE_READLINE] = true |
| 17 | + IRB.conf[:SAVE_HISTORY] = 1 |
| 18 | + IRBRC |
| 19 | + 1 |
| 20 | + 2 |
| 21 | + 3 |
| 22 | + 4 |
| 23 | + IRB_HISTORY |
| 24 | + stdin.write("5\nexit\n") |
| 25 | + end |
| 26 | + |
| 27 | + assert_equal(<<~HISTORY_FILE, result_history_file) |
| 28 | + exit |
| 29 | + HISTORY_FILE |
| 30 | + end |
| 31 | + |
| 32 | + def test_history_save_100 |
| 33 | + result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin| |
| 34 | + IRB.conf[:USE_READLINE] = true |
| 35 | + IRB.conf[:SAVE_HISTORY] = 100 |
| 36 | + IRBRC |
| 37 | + 1 |
| 38 | + 2 |
| 39 | + 3 |
| 40 | + 4 |
| 41 | + IRB_HISTORY |
| 42 | + stdin.write("5\nexit\n") |
| 43 | + end |
| 44 | + |
| 45 | + assert_equal(<<~HISTORY_FILE, result_history_file) |
| 46 | + 1 |
| 47 | + 2 |
| 48 | + 3 |
| 49 | + 4 |
| 50 | + 5 |
| 51 | + exit |
| 52 | + HISTORY_FILE |
| 53 | + end |
| 54 | + |
| 55 | + private |
| 56 | + |
| 57 | + def launch_irb_with_irbrc_and_irb_history(irbrc, irb_history) |
| 58 | + result = nil |
| 59 | + result_history = nil |
| 60 | + backup_irbrc = ENV.delete("IRBRC") |
| 61 | + backup_home = ENV["HOME"] |
| 62 | + Dir.mktmpdir("test_irb_history_#{$$}") do |tmpdir| |
| 63 | + ENV["HOME"] = tmpdir |
| 64 | + open(IRB.rc_file, "w") do |f| |
| 65 | + f.write(irbrc) |
| 66 | + end |
| 67 | + open(IRB.rc_file("_history"), "w") do |f| |
| 68 | + f.write(irb_history) |
| 69 | + end |
| 70 | + |
| 71 | + with_temp_stdio do |stdin, stdout| |
| 72 | + replace_stdio(stdin.path, stdout.path) do |
| 73 | + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] |
| 74 | + cmds = %W[ruby] + bundle_exec + %W[-W0 -rirb -e 'IRB.start(__FILE__)'] |
| 75 | + yield(stdin, stdout) |
| 76 | + stdin.close |
| 77 | + system(cmds.join(' ')) |
| 78 | + stdout.flush |
| 79 | + result = stdout.read |
| 80 | + stdout.close |
| 81 | + end |
| 82 | + end |
| 83 | + open(IRB.rc_file("_history"), "r") do |f| |
| 84 | + result_history = f.read |
| 85 | + end |
| 86 | + end |
| 87 | + [result, result_history] |
| 88 | + ensure |
| 89 | + ENV["HOME"] = backup_home |
| 90 | + ENV["IRBRC"] = backup_irbrc |
| 91 | + end |
| 92 | + |
| 93 | + def with_temp_stdio |
| 94 | + Tempfile.create("test_readline_stdin") do |stdin| |
| 95 | + Tempfile.create("test_readline_stdout") do |stdout| |
| 96 | + yield stdin, stdout |
| 97 | + if /mswin|mingw/ =~ RUBY_PLATFORM |
| 98 | + # needed since readline holds refs to tempfiles, can't delete on Windows |
| 99 | + #Readline.input = STDIN |
| 100 | + #Readline.output = STDOUT |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def replace_stdio(stdin_path, stdout_path) |
| 107 | + open(stdin_path, "r") do |stdin| |
| 108 | + open(stdout_path, "w") do |stdout| |
| 109 | + orig_stdin = STDIN.dup |
| 110 | + orig_stdout = STDOUT.dup |
| 111 | + orig_stderr = STDERR.dup |
| 112 | + STDIN.reopen(stdin) |
| 113 | + STDOUT.reopen(stdout) |
| 114 | + STDERR.reopen(stdout) |
| 115 | + begin |
| 116 | + #Readline.input = STDIN |
| 117 | + #Readline.output = STDOUT |
| 118 | + yield |
| 119 | + ensure |
| 120 | + STDERR.reopen(orig_stderr) |
| 121 | + STDIN.reopen(orig_stdin) |
| 122 | + STDOUT.reopen(orig_stdout) |
| 123 | + orig_stdin.close |
| 124 | + orig_stdout.close |
| 125 | + orig_stderr.close |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments