mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
[ruby/irb] Cache RDoc::RI::Driver.new
(https://github.com/ruby/irb/pull/911)
* Cache RDoc::RI::Driver.new to improve performance and to avoid flaky test
* Insert sleep to fix flaky rendering test that renders document dialog
da84e6cb56
This commit is contained in:
parent
5f334b67d2
commit
f53209f023
3 changed files with 28 additions and 17 deletions
|
@ -308,6 +308,20 @@ module IRB
|
||||||
@completor.doc_namespace(preposing, matched, postposing, bind: bind)
|
@completor.doc_namespace(preposing, matched, postposing, bind: bind)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rdoc_ri_driver
|
||||||
|
return @rdoc_ri_driver if defined?(@rdoc_ri_driver)
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'rdoc'
|
||||||
|
rescue LoadError
|
||||||
|
@rdoc_ri_driver = nil
|
||||||
|
else
|
||||||
|
options = {}
|
||||||
|
options[:extra_doc_dirs] = IRB.conf[:EXTRA_DOC_DIRS] unless IRB.conf[:EXTRA_DOC_DIRS].empty?
|
||||||
|
@rdoc_ri_driver = RDoc::RI::Driver.new(options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def show_doc_dialog_proc
|
def show_doc_dialog_proc
|
||||||
input_method = self # self is changed in the lambda below.
|
input_method = self # self is changed in the lambda below.
|
||||||
->() {
|
->() {
|
||||||
|
@ -331,9 +345,7 @@ module IRB
|
||||||
|
|
||||||
show_easter_egg = name&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
|
show_easter_egg = name&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
|
||||||
|
|
||||||
options = {}
|
driver = input_method.rdoc_ri_driver
|
||||||
options[:extra_doc_dirs] = IRB.conf[:EXTRA_DOC_DIRS] unless IRB.conf[:EXTRA_DOC_DIRS].empty?
|
|
||||||
driver = RDoc::RI::Driver.new(options)
|
|
||||||
|
|
||||||
if key.match?(dialog.name)
|
if key.match?(dialog.name)
|
||||||
if show_easter_egg
|
if show_easter_egg
|
||||||
|
@ -421,12 +433,9 @@ module IRB
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_document(matched, driver: nil)
|
def display_document(matched)
|
||||||
begin
|
driver = rdoc_ri_driver
|
||||||
require 'rdoc'
|
return unless driver
|
||||||
rescue LoadError
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
|
if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
|
||||||
IRB.__send__(:easter_egg)
|
IRB.__send__(:easter_egg)
|
||||||
|
@ -436,7 +445,6 @@ module IRB
|
||||||
namespace = retrieve_doc_namespace(matched)
|
namespace = retrieve_doc_namespace(matched)
|
||||||
return unless namespace
|
return unless namespace
|
||||||
|
|
||||||
driver ||= RDoc::RI::Driver.new
|
|
||||||
if namespace.is_a?(Array)
|
if namespace.is_a?(Array)
|
||||||
out = RDoc::Markup::Document.new
|
out = RDoc::Markup::Document.new
|
||||||
namespace.each do |m|
|
namespace.each do |m|
|
||||||
|
|
|
@ -88,17 +88,18 @@ module TestIRB
|
||||||
@driver = RDoc::RI::Driver.new(use_stdout: true)
|
@driver = RDoc::RI::Driver.new(use_stdout: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_document(target, bind)
|
def display_document(target, bind, driver = nil)
|
||||||
input_method = IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
|
input_method = IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
|
||||||
|
input_method.instance_variable_set(:@rdoc_ri_driver, driver) if driver
|
||||||
input_method.instance_variable_set(:@completion_params, ['', target, '', bind])
|
input_method.instance_variable_set(:@completion_params, ['', target, '', bind])
|
||||||
input_method.display_document(target, driver: @driver)
|
input_method.display_document(target)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_perfectly_matched_namespace_triggers_document_display
|
def test_perfectly_matched_namespace_triggers_document_display
|
||||||
omit unless has_rdoc_content?
|
omit unless has_rdoc_content?
|
||||||
|
|
||||||
out, err = capture_output do
|
out, err = capture_output do
|
||||||
display_document("String", binding)
|
display_document("String", binding, @driver)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_empty(err)
|
assert_empty(err)
|
||||||
|
@ -109,7 +110,7 @@ module TestIRB
|
||||||
def test_perfectly_matched_multiple_namespaces_triggers_document_display
|
def test_perfectly_matched_multiple_namespaces_triggers_document_display
|
||||||
result = nil
|
result = nil
|
||||||
out, err = capture_output do
|
out, err = capture_output do
|
||||||
result = display_document("{}.nil?", binding)
|
result = display_document("{}.nil?", binding, @driver)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_empty(err)
|
assert_empty(err)
|
||||||
|
@ -131,7 +132,7 @@ module TestIRB
|
||||||
def test_not_matched_namespace_triggers_nothing
|
def test_not_matched_namespace_triggers_nothing
|
||||||
result = nil
|
result = nil
|
||||||
out, err = capture_output do
|
out, err = capture_output do
|
||||||
result = display_document("Stri", binding)
|
result = display_document("Stri", binding, @driver)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_empty(err)
|
assert_empty(err)
|
||||||
|
@ -156,7 +157,7 @@ module TestIRB
|
||||||
def test_perfect_matching_handles_nil_namespace
|
def test_perfect_matching_handles_nil_namespace
|
||||||
out, err = capture_output do
|
out, err = capture_output do
|
||||||
# symbol literal has `nil` doc namespace so it's a good test subject
|
# symbol literal has `nil` doc namespace so it's a good test subject
|
||||||
assert_nil(display_document(":aiueo", binding))
|
assert_nil(display_document(":aiueo", binding, @driver))
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_empty(err)
|
assert_empty(err)
|
||||||
|
|
|
@ -256,9 +256,9 @@ class IRB::RenderingTest < Yamatanooroti::TestCase
|
||||||
start_terminal(3, 50, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
|
start_terminal(3, 50, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
|
||||||
write("{}.__id_")
|
write("{}.__id_")
|
||||||
write("\C-i")
|
write("\C-i")
|
||||||
|
sleep 0.2
|
||||||
close
|
close
|
||||||
screen = result.join("\n").sub(/\n*\z/, "\n")
|
screen = result.join("\n").sub(/\n*\z/, "\n")
|
||||||
# This assertion passes whether showdoc dialog completed or not.
|
|
||||||
assert_match(/start\ IRB\nirb\(main\):001> {}\.__id__\n }\.__id__(?:Press )?/, screen)
|
assert_match(/start\ IRB\nirb\(main\):001> {}\.__id__\n }\.__id__(?:Press )?/, screen)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -278,6 +278,7 @@ class IRB::RenderingTest < Yamatanooroti::TestCase
|
||||||
start_terminal(4, 19, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
|
start_terminal(4, 19, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
|
||||||
write("IR")
|
write("IR")
|
||||||
write("\C-i")
|
write("\C-i")
|
||||||
|
sleep 0.2
|
||||||
close
|
close
|
||||||
|
|
||||||
# This is because on macOS we display different shortcut for displaying the full doc
|
# This is because on macOS we display different shortcut for displaying the full doc
|
||||||
|
@ -315,6 +316,7 @@ class IRB::RenderingTest < Yamatanooroti::TestCase
|
||||||
start_terminal(4, 12, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
|
start_terminal(4, 12, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
|
||||||
write("IR")
|
write("IR")
|
||||||
write("\C-i")
|
write("\C-i")
|
||||||
|
sleep 0.2
|
||||||
close
|
close
|
||||||
assert_screen(<<~EOC)
|
assert_screen(<<~EOC)
|
||||||
start IRB
|
start IRB
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue