Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop irb:rdbg from saving duplicated history when there's no prior history file #853

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/irb/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def support_history_saving?
end

def reset_history_counter
@loaded_history_lines = self.class::HISTORY.size if defined? @loaded_history_lines
@loaded_history_lines = self.class::HISTORY.size
end

def load_history
Expand Down
51 changes: 47 additions & 4 deletions test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ def with_temp_stdio

class IRBHistoryIntegrationTest < IntegrationTestCase
def test_history_saving_with_debug
if ruby_core?
omit "This test works only under ruby/irb"
end

write_history ""

write_ruby <<~'RUBY'
Expand Down Expand Up @@ -293,6 +289,53 @@ def foo
HISTORY
end

def test_history_saving_with_debug_without_prior_history
tmpdir = Dir.mktmpdir("test_irb_history_")
# Intentionally not creating the file so we test the reset counter logic
history_file = File.join(tmpdir, "irb_history")

write_rc <<~RUBY
IRB.conf[:HISTORY_FILE] = "#{history_file}"
RUBY

write_ruby <<~'RUBY'
def foo
end

binding.irb

foo
RUBY

output = run_ruby_file do
type "'irb session'"
type "next"
type "'irb:debug session'"
type "step"
type "irb_info"
type "puts Reline::HISTORY.to_a.to_s"
type "q!"
end

assert_include(output, "InputMethod: RelineInputMethod")
# check that in-memory history is preserved across sessions
assert_include output, %q(
["'irb session'", "next", "'irb:debug session'", "step", "irb_info", "puts Reline::HISTORY.to_a.to_s"]
).strip

assert_equal <<~HISTORY, File.read(history_file)
'irb session'
next
'irb:debug session'
step
irb_info
puts Reline::HISTORY.to_a.to_s
q!
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix, the history would be:

+ 'irb session'
+ next
+ 'irb session'
+ next
+ 'irb:debug session'
+ 'irb session'
+ next
+ 'irb:debug session'
+ step
+ 'irb session'
+ next
+ 'irb:debug session'
+ step
+ irb_info
+ puts Reline::HISTORY.to_a.to_s
  'irb session'
  next
  'irb:debug session'
  step
  irb_info
  puts Reline::HISTORY.to_a.to_s
  q!

HISTORY
ensure
FileUtils.rm_rf(tmpdir)
end

def test_history_saving_with_nested_sessions
write_history ""

Expand Down
Loading