|
| 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