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

Fix tests leaking readline history #384

Merged
merged 5 commits into from
Dec 11, 2017
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
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-01-31 11:12:41 -0200 using RuboCop version 0.47.1.
# on 2017-12-10 20:04:42 -0300 using RuboCop version 0.51.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -19,7 +19,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 17
# Offense count: 18
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 19
Expand Down
2 changes: 1 addition & 1 deletion lib/byebug/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def at_end
private

def processor
@processor ||= self.class.processor.new(self)
@processor ||= self.class.processor.new(self, self.class.interface)
end

#
Expand Down
20 changes: 9 additions & 11 deletions lib/byebug/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ module Byebug
# are debugging, in the directory where you invoke byebug.
#
def run_init_script
rc_dirs.each { |dir| run_rc_file(dir) }
rc_dirs.each do |dir|
rc_file = File.expand_path(File.join(dir, init_file))
next unless File.exist?(rc_file)

run_rc_file(rc_file)
end
end

def self.load_settings
Expand Down Expand Up @@ -80,17 +85,10 @@ def self.handle_post_mortem
#
# Runs a initialization script file
#
def run_rc_file(base_path)
old_interface = Context.interface

rc_file = File.expand_path(File.join(base_path, init_file))
return unless File.exist?(rc_file)

Context.interface = ScriptInterface.new(rc_file)
def run_rc_file(rc_file)
interface = ScriptInterface.new(rc_file)

ScriptProcessor.new(nil).process_commands
ensure
Context.interface = old_interface
ScriptProcessor.new(nil, interface).process_commands
end

#
Expand Down
10 changes: 4 additions & 6 deletions lib/byebug/processors/command_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ class CommandProcessor
include Helpers::EvalHelper

attr_accessor :prev_line
attr_reader :context
attr_reader :context, :interface

def initialize(context)
def initialize(context, interface = LocalInterface.new)
@context = context
@interface = interface

@proceed = false
@prev_line = nil
end

def interface
@interface ||= Context.interface
end

def printer
@printer ||= Printers::Plain.new
end
Expand Down Expand Up @@ -119,6 +116,7 @@ def before_repl

def after_repl
interface.autosave
interface.close
end

#
Expand Down
4 changes: 0 additions & 4 deletions lib/byebug/processors/control_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ module Byebug
# Processes commands when there's not program running
#
class ControlProcessor < CommandProcessor
def initialize(context = nil)
@context = context
end

#
# Available commands
#
Expand Down
4 changes: 0 additions & 4 deletions lib/byebug/processors/script_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def repl
end
end

def after_repl
interface.close
end

#
# Prompt shown before reading a command.
#
Expand Down
5 changes: 3 additions & 2 deletions lib/byebug/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def start_control(host = nil, ctrl_port = PORT + 1)

@control_thread = DebugThread.new do
while (session = server.accept)
Context.interface = RemoteInterface.new(session)
context = Byebug.current_context
interface = RemoteInterface.new(session)

ControlProcessor.new(Byebug.current_context).process_commands
ControlProcessor.new(context, interface).process_commands
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/byebug/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def run

break if quit

ControlProcessor.new.process_commands
ControlProcessor.new(nil, interface).process_commands
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def setup
@interface = SpecificInterface.new
end

def teardown
@interface.history.clear
end

def test_reads_simple_commands
@interface.fake_input_queue = ['a_command']

Expand Down
22 changes: 22 additions & 0 deletions test/processors/script_processor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'test_helper'

module Byebug
#
# Tests script processor in isolation
#
class ScriptProcessorTest < Minitest::Test
include TestUtils

def test_script_processor_clears_history
with_init_file('set callstyle long') do
interface = ScriptInterface.new(Byebug.init_file)

previous_history = Readline::HISTORY.to_a

ScriptProcessor.new(nil, interface).process_commands

assert_equal previous_history, Readline::HISTORY.to_a
end
end
end
end