summaryrefslogtreecommitdiff
path: root/test/irb/test_history.rb
blob: 3591f88f5907115ffe7a6a7f8d15c915884e5ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: false
require 'test/unit'
require 'irb'
require 'readline'

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
      omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
      _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
        IRB.conf[:USE_READLINE] = true
        IRB.conf[:SAVE_HISTORY] = 1
        IRB.conf[:USE_READLINE] = true
      IRBRC
        1
        2
        3
        4
      IRB_HISTORY
        stdin.write("5\nexit\n")
      end

      assert_equal(<<~HISTORY_FILE, result_history_file)
        exit
      HISTORY_FILE
    end

    def test_history_save_100
      omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
      _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
        IRB.conf[:USE_READLINE] = true
        IRB.conf[:SAVE_HISTORY] = 100
        IRB.conf[:USE_READLINE] = true
      IRBRC
        1
        2
        3
        4
      IRB_HISTORY
        stdin.write("5\nexit\n")
      end

      assert_equal(<<~HISTORY_FILE, result_history_file)
        1
        2
        3
        4
        5
        exit
      HISTORY_FILE
    end

    def test_history_save_bignum
      omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
      _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
        IRB.conf[:USE_READLINE] = true
        IRB.conf[:SAVE_HISTORY] = 10 ** 19
        IRB.conf[:USE_READLINE] = true
      IRBRC
        1
        2
        3
        4
      IRB_HISTORY
        stdin.write("5\nexit\n")
      end

      assert_equal(<<~HISTORY_FILE, result_history_file)
        1
        2
        3
        4
        5
        exit
      HISTORY_FILE
    end

    def test_history_save_minus_as_infinity
      omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
      _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
        IRB.conf[:USE_READLINE] = true
        IRB.conf[:SAVE_HISTORY] = -1 # infinity
        IRB.conf[:USE_READLINE] = true
      IRBRC
        1
        2
        3
        4
      IRB_HISTORY
        stdin.write("5\nexit\n")
      end

      assert_equal(<<~HISTORY_FILE, result_history_file)
        1
        2
        3
        4
        5
        exit
      HISTORY_FILE
    end

    private

    def launch_irb_with_irbrc_and_irb_history(irbrc, irb_history)
      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|
          yield(stdin, stdout)
          stdin.close
          stdout.flush
          system('ruby', '-Ilib', '-Itest', '-W0', '-rirb', '-e', 'IRB.start(__FILE__)', in: stdin.path, out: stdout.path)
          result = stdout.read
          stdout.close
        end
        open(IRB.rc_file("_history"), "r") do |f|
          result_history = f.read
        end
      end
      [result, result_history]
    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
end if not RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)