[ruby/irb] Change show_source tests into integration tests

* Remove trailing spaces

* Migrate show_source tests to integration tests

Because show_source tests often need to define class and/or methods,
they can easily leak state to other tests. Changing them to integration
tests will ensure that they are run in a clean environment.

* Fix NoMethodError caused by SourceFinder#method_target

3c39f13397
This commit is contained in:
Peter Zhu 2023-11-28 12:22:46 -05:00
Родитель 476a231e7e
Коммит fadd28c7ba
3 изменённых файлов: 265 добавлений и 228 удалений

Просмотреть файл

@ -77,6 +77,8 @@ module IRB
target_method = target_method.super_method if target_method
end
target_method.nil? ? nil : target_method.source_location
rescue NameError
nil
end
end
end

Просмотреть файл

@ -0,0 +1,263 @@
# frozen_string_literal: false
require 'irb'
require_relative "../helper"
module TestIRB
class ShowSourceTest < IntegrationTestCase
def setup
super
write_rc <<~'RUBY'
IRB.conf[:USE_PAGER] = false
RUBY
end
def test_show_source
write_ruby <<~'RUBY'
binding.irb
RUBY
out = run_ruby_file do
type "show_source IRB.conf"
type "exit"
end
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_alias
write_ruby <<~'RUBY'
binding.irb
RUBY
out = run_ruby_file do
type "$ IRB.conf"
type "exit"
end
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_string
write_ruby <<~'RUBY'
binding.irb
RUBY
out = run_ruby_file do
type "show_source 'IRB.conf'"
type "exit"
end
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_method_s
write_ruby <<~RUBY
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bar#foo -s"
type "exit"
end
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end\r\n], out)
end
def test_show_source_method_s_with_incorrect_signature
write_ruby <<~RUBY
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bar#fooo -s"
type "exit"
end
assert_match(%r[Error: Couldn't locate a super definition for Bar#fooo], out)
end
def test_show_source_private_method
write_ruby <<~RUBY
class Bar
private def foo
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bar#foo"
type "exit"
end
assert_match(%r[#{@ruby_file.to_path}:2\s+private def foo\r\n end\r\n], out)
end
def test_show_source_private_singleton_method
write_ruby <<~RUBY
class Bar
private def foo
end
end
binding.irb
RUBY
out = run_ruby_file do
type "bar = Bar.new"
type "show_source bar.foo"
type "exit"
end
assert_match(%r[#{@ruby_file.to_path}:2\s+private def foo\r\n end\r\n], out)
end
def test_show_source_method_multiple_s
write_ruby <<~RUBY
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
class Bob < Bar
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bob#foo -ss"
type "exit"
end
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end\r\n], out)
end
def test_show_source_method_no_instance_method
write_ruby <<~RUBY
class Baz
end
class Bar < Baz
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bar#foo -s"
type "exit"
end
assert_match(%r[Error: Couldn't locate a super definition for Bar#foo], out)
end
def test_show_source_method_exceeds_super_chain
write_ruby <<~RUBY
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bar#foo -ss"
type "exit"
end
assert_match(%r[Error: Couldn't locate a super definition for Bar#foo], out)
end
def test_show_source_method_accidental_characters
write_ruby <<~'RUBY'
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "show_source Bar#foo -sddddd"
type "exit"
end
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end], out)
end
def test_show_source_receiver_super
write_ruby <<~RUBY
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
binding.irb
RUBY
out = run_ruby_file do
type "bar = Bar.new"
type "show_source bar.foo -s"
type "exit"
end
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end], out)
end
end
end

Просмотреть файл

@ -449,234 +449,6 @@ module TestIRB
end
end
class ShowSourceTest < CommandTestCase
def test_show_source
out, err = execute_lines(
"show_source IRB.conf\n",
)
assert_empty err
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_method
out, err = execute_lines(
"p show_source('IRB.conf')\n",
)
assert_empty err
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_method_s
code = <<~RUBY
class Baz
def foo
end
end
class Bar < Baz
def foo
super
end
end
RUBY
File.write("#{@tmpdir}/bazbar.rb", code)
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbar.rb'\n",
"show_source Bar#foo -s",
)
assert_match(%r[bazbar.rb:2\n\n def foo\n end\n\n=> nil\n], out)
File.delete("#{@tmpdir}/bazbar.rb")
end
def test_show_source_method_multiple_s
code = <<~RUBY
class Baz
def fob
end
end
class Bar < Baz
def fob
super
end
end
class Bob < Bar
def fob
super
end
end
RUBY
File.write("#{@tmpdir}/bazbarbob.rb", code)
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbarbob.rb'\n",
"show_source Bob#fob -ss",
)
assert_match(%r[bazbarbob.rb:2\n\n def fob\n end\n\n=> nil\n], out)
File.delete("#{@tmpdir}/bazbarbob.rb")
end
def test_show_source_method_no_instance_method
code = <<~RUBY
class Baz
end
class Bar < Baz
def fee
super
end
end
RUBY
File.write("#{@tmpdir}/bazbar.rb", code)
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbar.rb'\n",
"show_source Bar#fee -s",
)
assert_match(%r[Error: Couldn't locate a super definition for Bar#fee\n], out)
File.delete("#{@tmpdir}/bazbar.rb")
end
def test_show_source_method_exceeds_super_chain
code = <<~RUBY
class Baz
def fow
end
end
class Bar < Baz
def fow
super
end
end
RUBY
File.write("#{@tmpdir}/bazbar.rb", code)
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbar.rb'\n",
"show_source Bar#fow -ss",
)
assert_match(%r[Error: Couldn't locate a super definition for Bar#fow\n], out)
File.delete("#{@tmpdir}/bazbar.rb")
end
def test_show_source_method_accidental_characters
code = <<~RUBY
class Baz
def fol
end
end
class Bar < Baz
def fol
super
end
end
RUBY
File.write("#{@tmpdir}/bazbar.rb", code)
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbar.rb'\n",
"show_source Bar#fol -sddddd",
)
assert_match(%r[bazbar.rb:2\n\n def fol\n end\n\n=> nil\n], out)
File.delete("#{@tmpdir}/bazbar.rb")
end
def test_show_source_receiver_super
code = <<~RUBY
class Baz
def fot
end
end
class Bar < Baz
def fot
super
end
end
RUBY
File.write("#{@tmpdir}/bazbar.rb", code)
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbar.rb'\n",
"bar = Bar.new",
"show_source bar.fot -s"
)
assert_match(%r[bazbar.rb:2\n\n def fot\n end\n\n=> nil\n], out)
File.delete("#{@tmpdir}/bazbar.rb")
end
def test_show_source_string
out, err = execute_lines(
"show_source 'IRB.conf'\n",
)
assert_empty err
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_alias
out, err = execute_lines(
"$ 'IRB.conf'\n",
conf: { COMMAND_ALIASES: { :'$' => :show_source } }
)
assert_empty err
assert_match(%r[/irb\/init\.rb], out)
end
def test_show_source_end_finder
eval(code = <<-EOS, binding, __FILE__, __LINE__ + 1)
def show_source_test_method
unless true
end
end unless defined?(show_source_test_method)
EOS
out, err = execute_lines(
"show_source '#{self.class.name}#show_source_test_method'\n",
)
assert_empty err
assert_include(out, code)
end
def test_show_source_private_instance
eval(code = <<-EOS, binding, __FILE__, __LINE__ + 1)
class PrivateInstanceTest
private def show_source_test_method
unless true
end
end unless private_method_defined?(:show_source_test_method)
end
EOS
out, err = execute_lines(
"show_source '#{self.class.name}::PrivateInstanceTest#show_source_test_method'\n",
)
assert_empty err
assert_include(out, code.lines[1..-2].join)
end
def test_show_source_private
eval(code = <<-EOS, binding, __FILE__, __LINE__ + 1)
class PrivateTest
private def show_source_test_method
unless true
end
end unless private_method_defined?(:show_source_test_method)
end
Instance = PrivateTest.new unless defined?(Instance)
EOS
out, err = execute_lines(
"show_source '#{self.class.name}::Instance.show_source_test_method'\n",
)
assert_empty err
assert_include(out, code.lines[1..-4].join)
end
end
class WorkspaceCommandTestCase < CommandTestCase
def setup
super