summaryrefslogtreecommitdiff
path: root/test/irb/test_init.rb
blob: 645678701263787cdff25ea0a6df394d796fff9c (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# frozen_string_literal: false
require "irb"
require "fileutils"

require_relative "helper"

module TestIRB
  class InitTest < TestCase
    def setup
      # IRBRC is for RVM...
      @backup_env = %w[HOME XDG_CONFIG_HOME IRBRC].each_with_object({}) do |env, hash|
        hash[env] = ENV.delete(env)
      end
      ENV["HOME"] = @tmpdir = File.realpath(Dir.mktmpdir("test_irb_init_#{$$}"))
    end

    def reset_rc_name_generators
      IRB.instance_variable_set(:@existing_rc_name_generators, nil)
    end

    def teardown
      ENV.update(@backup_env)
      FileUtils.rm_rf(@tmpdir)
      IRB.conf.delete(:SCRIPT)
      reset_rc_name_generators
    end

    def test_setup_with_argv_preserves_global_argv
      argv = ["foo", "bar"]
      with_argv(argv) do
        IRB.setup(eval("__FILE__"), argv: %w[-f])
        assert_equal argv, ARGV
      end
    end

    def test_setup_with_minimum_argv_does_not_change_dollar0
      orig = $0.dup
      IRB.setup(eval("__FILE__"), argv: %w[-f])
      assert_equal orig, $0
    end

    def test_rc_files
      tmpdir = @tmpdir
      Dir.chdir(tmpdir) do
        home = ENV['HOME'] = "#{tmpdir}/home"
        xdg_config_home = ENV['XDG_CONFIG_HOME'] = "#{tmpdir}/xdg"
        reset_rc_name_generators
        assert_empty(IRB.irbrc_files)
        assert_equal("#{home}/.irb_history", IRB.rc_file('_history'))
        FileUtils.mkdir_p(home)
        FileUtils.mkdir_p("#{xdg_config_home}/irb")
        FileUtils.mkdir_p("#{home}/.config/irb")
        reset_rc_name_generators
        assert_empty(IRB.irbrc_files)
        assert_equal("#{xdg_config_home}/irb/irb_history", IRB.rc_file('_history'))
        home_irbrc = "#{home}/.irbrc"
        config_irbrc = "#{home}/.config/irb/irbrc"
        xdg_config_irbrc = "#{xdg_config_home}/irb/irbrc"
        [home_irbrc, config_irbrc, xdg_config_irbrc].each do |file|
          FileUtils.touch(file)
        end
        current_dir_irbrcs = %w[.irbrc irbrc _irbrc $irbrc].map { |file| "#{tmpdir}/#{file}" }
        current_dir_irbrcs.each { |file| FileUtils.touch(file) }
        reset_rc_name_generators
        assert_equal([xdg_config_irbrc, home_irbrc, *current_dir_irbrcs], IRB.irbrc_files)
        assert_equal(xdg_config_irbrc.sub(/rc$/, '_history'), IRB.rc_file('_history'))
        ENV['XDG_CONFIG_HOME'] = nil
        reset_rc_name_generators
        assert_equal([home_irbrc, config_irbrc, *current_dir_irbrcs], IRB.irbrc_files)
        assert_equal(home_irbrc.sub(/rc$/, '_history'), IRB.rc_file('_history'))
        ENV['XDG_CONFIG_HOME'] = ''
        reset_rc_name_generators
        assert_equal([home_irbrc, config_irbrc] + current_dir_irbrcs, IRB.irbrc_files)
        assert_equal(home_irbrc.sub(/rc$/, '_history'), IRB.rc_file('_history'))
        ENV['XDG_CONFIG_HOME'] = xdg_config_home
        ENV['IRBRC'] = "#{tmpdir}/.irbrc"
        reset_rc_name_generators
        assert_equal([ENV['IRBRC'], xdg_config_irbrc, home_irbrc] + (current_dir_irbrcs - [ENV['IRBRC']]), IRB.irbrc_files)
        assert_equal(ENV['IRBRC'] + '_history', IRB.rc_file('_history'))
        ENV['IRBRC'] = ENV['HOME'] = ENV['XDG_CONFIG_HOME'] = nil
        reset_rc_name_generators
        assert_equal(current_dir_irbrcs, IRB.irbrc_files)
        assert_nil(IRB.rc_file('_history'))
      end
    end

    def test_duplicated_rc_files
      tmpdir = @tmpdir
      Dir.chdir(tmpdir) do
        ENV['XDG_CONFIG_HOME'] = "#{ENV['HOME']}/.config"
        FileUtils.mkdir_p("#{ENV['XDG_CONFIG_HOME']}/irb")
        env_irbrc = ENV['IRBRC'] = "#{tmpdir}/_irbrc"
        xdg_config_irbrc = "#{ENV['XDG_CONFIG_HOME']}/irb/irbrc"
        home_irbrc = "#{ENV['HOME']}/.irbrc"
        current_dir_irbrc = "#{tmpdir}/irbrc"
        [env_irbrc, xdg_config_irbrc, home_irbrc, current_dir_irbrc].each do |file|
          FileUtils.touch(file)
        end
        reset_rc_name_generators
        assert_equal([env_irbrc, xdg_config_irbrc, home_irbrc, current_dir_irbrc], IRB.irbrc_files)
      end
    end

    def test_sigint_restore_default
      pend "This test gets stuck on Solaris for unknown reason; contribution is welcome" if RUBY_PLATFORM =~ /solaris/
      bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
      # IRB should restore SIGINT handler
      status = assert_in_out_err(bundle_exec + %w[-W0 -rirb -e Signal.trap("SIGINT","DEFAULT");binding.irb;loop{Process.kill("SIGINT",$$)} -- -f --], "exit\n", //, //)
      Process.kill("SIGKILL", status.pid) if !status.exited? && !status.stopped? && !status.signaled?
    end

    def test_sigint_restore_block
      bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
      # IRB should restore SIGINT handler
      status = assert_in_out_err(bundle_exec + %w[-W0 -rirb -e x=false;Signal.trap("SIGINT"){x=true};binding.irb;loop{Process.kill("SIGINT",$$);if(x);break;end} -- -f --], "exit\n", //, //)
      Process.kill("SIGKILL", status.pid) if !status.exited? && !status.stopped? && !status.signaled?
    end

    def test_no_color_environment_variable
      orig_no_color = ENV['NO_COLOR']
      orig_use_colorize = IRB.conf[:USE_COLORIZE]
      IRB.conf[:USE_COLORIZE] = true

      assert IRB.conf[:USE_COLORIZE]

      ENV['NO_COLOR'] = 'true'
      IRB.setup(__FILE__)
      refute IRB.conf[:USE_COLORIZE]

      ENV['NO_COLOR'] = ''
      IRB.setup(__FILE__)
      assert IRB.conf[:USE_COLORIZE]

      ENV['NO_COLOR'] = nil
      IRB.setup(__FILE__)
      assert IRB.conf[:USE_COLORIZE]
    ensure
      ENV['NO_COLOR'] = orig_no_color
      IRB.conf[:USE_COLORIZE] = orig_use_colorize
    end

    def test_use_autocomplete_environment_variable
      orig_use_autocomplete_env = ENV['IRB_USE_AUTOCOMPLETE']
      orig_use_autocomplete_conf = IRB.conf[:USE_AUTOCOMPLETE]

      ENV['IRB_USE_AUTOCOMPLETE'] = nil
      IRB.setup(__FILE__)
      assert IRB.conf[:USE_AUTOCOMPLETE]

      ENV['IRB_USE_AUTOCOMPLETE'] = ''
      IRB.setup(__FILE__)
      assert IRB.conf[:USE_AUTOCOMPLETE]

      ENV['IRB_USE_AUTOCOMPLETE'] = 'false'
      IRB.setup(__FILE__)
      refute IRB.conf[:USE_AUTOCOMPLETE]

      ENV['IRB_USE_AUTOCOMPLETE'] = 'true'
      IRB.setup(__FILE__)
      assert IRB.conf[:USE_AUTOCOMPLETE]
    ensure
      ENV["IRB_USE_AUTOCOMPLETE"] = orig_use_autocomplete_env
      IRB.conf[:USE_AUTOCOMPLETE] = orig_use_autocomplete_conf
    end

    def test_completor_environment_variable
      orig_use_autocomplete_env = ENV['IRB_COMPLETOR']
      orig_use_autocomplete_conf = IRB.conf[:COMPLETOR]

      ENV['IRB_COMPLETOR'] = nil
      IRB.setup(__FILE__)
      assert_equal(:regexp, IRB.conf[:COMPLETOR])

      ENV['IRB_COMPLETOR'] = 'regexp'
      IRB.setup(__FILE__)
      assert_equal(:regexp, IRB.conf[:COMPLETOR])

      ENV['IRB_COMPLETOR'] = 'type'
      IRB.setup(__FILE__)
      assert_equal(:type, IRB.conf[:COMPLETOR])

      ENV['IRB_COMPLETOR'] = 'regexp'
      IRB.setup(__FILE__, argv: ['--type-completor'])
      assert_equal :type, IRB.conf[:COMPLETOR]

      ENV['IRB_COMPLETOR'] = 'type'
      IRB.setup(__FILE__, argv: ['--regexp-completor'])
      assert_equal :regexp, IRB.conf[:COMPLETOR]
    ensure
      ENV['IRB_COMPLETOR'] = orig_use_autocomplete_env
      IRB.conf[:COMPLETOR] = orig_use_autocomplete_conf
    end

    def test_completor_setup_with_argv
      orig_completor_conf = IRB.conf[:COMPLETOR]

      # Default is :regexp
      IRB.setup(__FILE__, argv: [])
      assert_equal :regexp, IRB.conf[:COMPLETOR]

      IRB.setup(__FILE__, argv: ['--type-completor'])
      assert_equal :type, IRB.conf[:COMPLETOR]

      IRB.setup(__FILE__, argv: ['--regexp-completor'])
      assert_equal :regexp, IRB.conf[:COMPLETOR]
    ensure
      IRB.conf[:COMPLETOR] = orig_completor_conf
    end

    def test_noscript
      argv = %w[--noscript -- -f]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_nil IRB.conf[:SCRIPT]
      assert_equal(['-f'], argv)

      argv = %w[--noscript -- a]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_nil IRB.conf[:SCRIPT]
      assert_equal(['a'], argv)

      argv = %w[--noscript a]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_nil IRB.conf[:SCRIPT]
      assert_equal(['a'], argv)

      argv = %w[--script --noscript a]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_nil IRB.conf[:SCRIPT]
      assert_equal(['a'], argv)

      argv = %w[--noscript --script a]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_equal('a', IRB.conf[:SCRIPT])
      assert_equal([], argv)
    end

    def test_dash
      argv = %w[-]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_equal('-', IRB.conf[:SCRIPT])
      assert_equal([], argv)

      argv = %w[-- -]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_equal('-', IRB.conf[:SCRIPT])
      assert_equal([], argv)

      argv = %w[-- - -f]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_equal('-', IRB.conf[:SCRIPT])
      assert_equal(['-f'], argv)
    end

    def test_option_tracer
      argv = %w[--tracer]
      IRB.setup(eval("__FILE__"), argv: argv)
      assert_equal(true, IRB.conf[:USE_TRACER])
    end

    private

    def with_argv(argv)
      orig = ARGV.dup
      ARGV.replace(argv)
      yield
    ensure
      ARGV.replace(orig)
    end
  end

  class ConfigValidationTest < TestCase
    def setup
      @original_home = ENV["HOME"]
      @original_irbrc = ENV["IRBRC"]
      # To prevent the test from using the user's .irbrc file
      ENV["HOME"] = Dir.mktmpdir
      IRB.instance_variable_set(:@existing_rc_name_generators, nil)
      super
    end

    def teardown
      super
      ENV["IRBRC"] = @original_irbrc
      ENV["HOME"] = @original_home
      File.unlink(@irbrc)
    end

    def test_irb_name_converts_non_string_values_to_string
      assert_no_irb_validation_error(<<~'RUBY')
        IRB.conf[:IRB_NAME] = :foo
      RUBY

      assert_equal "foo", IRB.conf[:IRB_NAME]
    end

    def test_irb_rc_name_only_takes_callable_objects
      assert_irb_validation_error(<<~'RUBY', "IRB.conf[:IRB_RC] should be a callable object. Got :foo.")
        IRB.conf[:IRB_RC] = :foo
      RUBY
    end

    def test_back_trace_limit_only_accepts_integers
      assert_irb_validation_error(<<~'RUBY', "IRB.conf[:BACK_TRACE_LIMIT] should be an integer. Got \"foo\".")
        IRB.conf[:BACK_TRACE_LIMIT] = "foo"
      RUBY
    end

    def test_prompt_only_accepts_hash
      assert_irb_validation_error(<<~'RUBY', "IRB.conf[:PROMPT] should be a Hash. Got \"foo\".")
        IRB.conf[:PROMPT] = "foo"
      RUBY
    end

    def test_eval_history_only_accepts_integers
      assert_irb_validation_error(<<~'RUBY', "IRB.conf[:EVAL_HISTORY] should be an integer. Got \"foo\".")
        IRB.conf[:EVAL_HISTORY] = "foo"
      RUBY
    end

    private

    def assert_irb_validation_error(rc_content, error_message)
      write_rc rc_content

      assert_raise_with_message(TypeError, error_message) do
        IRB.setup(__FILE__)
      end
    end

    def assert_no_irb_validation_error(rc_content)
      write_rc rc_content

      assert_nothing_raised do
        IRB.setup(__FILE__)
      end
    end

    def write_rc(content)
      @irbrc = Tempfile.new('irbrc')
      @irbrc.write(content)
      @irbrc.close
      ENV['IRBRC'] = @irbrc.path
    end
  end

  class InitIntegrationTest < IntegrationTestCase
    def setup
      super

      write_ruby <<~'RUBY'
        binding.irb
      RUBY
    end

    def test_load_error_in_rc_file_is_warned
      write_rc <<~'IRBRC'
        require "file_that_does_not_exist"
      IRBRC

      output = run_ruby_file do
        type "'foobar'"
        type "exit"
      end

      # IRB session should still be started
      assert_includes output, "foobar"
      assert_includes output, 'cannot load such file -- file_that_does_not_exist (LoadError)'
    end

    def test_normal_errors_in_rc_file_is_warned
      write_rc <<~'IRBRC'
        raise "I'm an error"
      IRBRC

      output = run_ruby_file do
        type "'foobar'"
        type "exit"
      end

      # IRB session should still be started
      assert_includes output, "foobar"
      assert_includes output, 'I\'m an error (RuntimeError)'
    end
  end
end