Skip to content

Commit d52a032

Browse files
committed
Share history test setup
1 parent 30036fc commit d52a032

3 files changed

Lines changed: 48 additions & 58 deletions

File tree

test/irb/history_test_case.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: false
2+
require "irb"
3+
require "fileutils"
4+
require "tmpdir"
5+
6+
require_relative "helper"
7+
8+
module TestIRB
9+
class HistoryTestCase < TestCase
10+
def setup
11+
@conf_backup = IRB.conf.dup
12+
@original_verbose, $VERBOSE = $VERBOSE, nil
13+
@tmpdir = Dir.mktmpdir("test_irb_history_")
14+
setup_envs(home: @tmpdir)
15+
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
16+
save_encodings
17+
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
18+
end
19+
20+
def teardown
21+
IRB.conf.replace(@conf_backup)
22+
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
23+
teardown_envs
24+
restore_encodings
25+
$VERBOSE = @original_verbose
26+
FileUtils.rm_rf(@tmpdir)
27+
end
28+
end
29+
end

test/irb/test_history.rb

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# frozen_string_literal: false
2-
require 'irb'
32
require "tempfile"
43

5-
require_relative "helper"
4+
require_relative "history_test_case"
65

76
return if RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)
87

@@ -14,26 +13,7 @@ module TestIRB
1413
Readline = ::Reline
1514
end
1615

17-
class HistoryTest < TestCase
18-
def setup
19-
@conf_backup = IRB.conf.dup
20-
@original_verbose, $VERBOSE = $VERBOSE, nil
21-
@tmpdir = Dir.mktmpdir("test_irb_history_")
22-
setup_envs(home: @tmpdir)
23-
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
24-
save_encodings
25-
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
26-
end
27-
28-
def teardown
29-
IRB.conf.replace(@conf_backup)
30-
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
31-
teardown_envs
32-
restore_encodings
33-
$VERBOSE = @original_verbose
34-
FileUtils.rm_rf(@tmpdir)
35-
end
36-
16+
class HistoryTest < HistoryTestCase
3717
class TestInputMethodWithRelineHistory < TestInputMethod
3818
# When IRB.conf[:USE_MULTILINE] is true, IRB::RelineInputMethod uses Reline::History
3919
HISTORY = Reline::History.new(Reline.core.config)

test/irb/test_history_windows.rb

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# frozen_string_literal: false
2-
require "irb"
3-
require "fileutils"
4-
require "tmpdir"
5-
6-
require_relative "helper"
2+
require_relative "history_test_case"
73

84
module TestIRB
9-
class HistoryWindowsTest < TestCase
5+
class HistoryWindowsTest < HistoryTestCase
106
class TestInputMethodWithHistory < TestInputMethod
117
HISTORY = []
128

@@ -19,42 +15,27 @@ def gets
1915
end
2016
end
2117

22-
def setup
23-
@conf_backup = IRB.conf.dup
24-
@original_verbose, $VERBOSE = $VERBOSE, nil
25-
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
26-
end
27-
28-
def teardown
29-
IRB.conf.replace(@conf_backup)
30-
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
31-
TestInputMethodWithHistory::HISTORY.clear
32-
$VERBOSE = @original_verbose
33-
end
34-
3518
def test_history_is_saved_in_readonly_attributed_directory
3619
omit "Windows-only" unless windows?
3720

38-
Dir.mktmpdir("test_irb_history_windows_") do |tmpdir|
39-
history_dir = File.join(tmpdir, "history")
40-
history_file = File.join(history_dir, "irb_history")
41-
FileUtils.mkdir_p(history_dir)
42-
43-
with_readonly_attribute(history_dir) do
44-
assert_nothing_raised do
45-
File.write(File.join(history_dir, "manual_write"), "ok")
46-
end
21+
history_dir = File.join(@tmpdir, "history")
22+
history_file = File.join(history_dir, "irb_history")
23+
FileUtils.mkdir_p(history_dir)
4724

48-
_output, warning = capture_output do
49-
run_irb_with_history(history_file)
50-
end
25+
with_readonly_attribute(history_dir) do
26+
assert_nothing_raised do
27+
File.write(File.join(history_dir, "manual_write"), "ok")
28+
end
5129

52-
assert_equal("", warning)
53-
assert_equal(<<~HISTORY, File.read(history_file))
54-
puts 'history_entry'
55-
exit
56-
HISTORY
30+
_output, warning = capture_output do
31+
run_irb_with_history(history_file)
5732
end
33+
34+
assert_equal("", warning)
35+
assert_equal(<<~HISTORY, File.read(history_file))
36+
puts 'history_entry'
37+
exit
38+
HISTORY
5839
end
5940
end
6041

0 commit comments

Comments
 (0)