Skip to content

Commit 30036fc

Browse files
committed
Use Windows history regression test
1 parent a32c2ff commit 30036fc

2 files changed

Lines changed: 92 additions & 27 deletions

File tree

test/irb/test_history.rb

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,6 @@ def test_history_concurrent_use_not_present
169169
assert_equal(%w"line0 line1 line2", File.read(history_file).split)
170170
end
171171

172-
def test_history_save_uses_actual_write_result_not_directory_writable_predicate
173-
IRB.conf[:SAVE_HISTORY] = 1
174-
io = TestInputMethodWithRelineHistory.new
175-
io.class::HISTORY.clear
176-
io.class::HISTORY << "line1"
177-
178-
history_file = IRB.rc_file("_history")
179-
FileUtils.rm_f(history_file)
180-
181-
original_writable = Pathname.instance_method(:writable?)
182-
Pathname.class_eval do
183-
define_method(:writable?) { false }
184-
end
185-
186-
assert_warn("") do
187-
io.save_history
188-
end
189-
190-
assert_equal("line1\n", File.read(history_file))
191-
ensure
192-
if original_writable
193-
Pathname.class_eval do
194-
define_method(:writable?, original_writable)
195-
end
196-
end
197-
end
198-
199172
def test_history_different_encodings
200173
IRB.conf[:SAVE_HISTORY] = 2
201174
IRB.conf[:LC_MESSAGES] = IRB::Locale.new("en_US.ASCII")

test/irb/test_history_windows.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 HistoryWindowsTest < TestCase
10+
class TestInputMethodWithHistory < TestInputMethod
11+
HISTORY = []
12+
13+
include IRB::HistorySavingAbility
14+
15+
def gets
16+
super&.tap do |line|
17+
HISTORY << line unless line.empty?
18+
end
19+
end
20+
end
21+
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+
35+
def test_history_is_saved_in_readonly_attributed_directory
36+
omit "Windows-only" unless windows?
37+
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
47+
48+
_output, warning = capture_output do
49+
run_irb_with_history(history_file)
50+
end
51+
52+
assert_equal("", warning)
53+
assert_equal(<<~HISTORY, File.read(history_file))
54+
puts 'history_entry'
55+
exit
56+
HISTORY
57+
end
58+
end
59+
end
60+
61+
private
62+
63+
def with_readonly_attribute(path)
64+
assert(system("attrib", "+R", windows_path(path)), "failed to set read-only attribute on #{path}")
65+
yield
66+
ensure
67+
system("attrib", "-R", windows_path(path)) if path && File.directory?(path)
68+
end
69+
70+
def windows_path(path)
71+
return path unless File::ALT_SEPARATOR
72+
73+
path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
74+
end
75+
76+
def run_irb_with_history(history_file)
77+
IRB.init_config(nil)
78+
IRB.init_error
79+
IRB.conf[:PROMPT_MODE] = :SIMPLE
80+
IRB.conf[:SAVE_HISTORY] = 100
81+
IRB.conf[:HISTORY_FILE] = history_file
82+
IRB.conf[:USE_PAGER] = false
83+
84+
input = TestInputMethodWithHistory.new(["puts 'history_entry'", "exit"])
85+
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
86+
irb.context.return_format = "=> %s\n"
87+
irb.run(IRB.conf)
88+
ensure
89+
TestInputMethodWithHistory::HISTORY.clear
90+
end
91+
end
92+
end

0 commit comments

Comments
 (0)