From 6ed9d68907bae7f4804eb06e0e0e295497ceb032 Mon Sep 17 00:00:00 2001 From: Yuji Nakayama Date: Sat, 12 Jun 2021 01:35:52 +0900 Subject: [PATCH 1/2] Add SnippetExtractor spec examples for end-less methods support Ref: https://github.com/rspec/rspec-core/issues/2892 --- .../core/formatters/snippet_extractor_spec.rb | 60 +++++++++++++++++++ spec/support/aruba_support.rb | 10 +--- spec/support/helper_methods.rb | 7 +++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/spec/rspec/core/formatters/snippet_extractor_spec.rb b/spec/rspec/core/formatters/snippet_extractor_spec.rb index 32032240ab..406c025ebd 100644 --- a/spec/rspec/core/formatters/snippet_extractor_spec.rb +++ b/spec/rspec/core/formatters/snippet_extractor_spec.rb @@ -1,7 +1,11 @@ require 'rspec/core/formatters/snippet_extractor' +require 'support/helper_methods' +require 'tempfile' module RSpec::Core::Formatters RSpec.describe SnippetExtractor do + include RSpecHelpers + subject(:expression_lines) do SnippetExtractor.extract_expression_lines_at(file_path, line_number, max_line_count) end @@ -182,6 +186,62 @@ def obj.foo(arg) end end + context 'when the expression line includes an "end"-less method definition', :if => RUBY_VERSION.to_f >= 3.0 do + include RSpec::Support::InSubProcess + + let(:source) do + in_sub_process do + load(file.path) + end + end + + let(:file) do + file = Tempfile.new('source.rb') + + file.write(unindent(<<-END)) + obj = Object.new + + def obj.foo = raise + + obj.foo + END + + file.close + + file + end + + after do + file.unlink + end + + it 'returns only the line' do + expect(expression_lines).to eq([ + 'def obj.foo = raise' + ]) + end + end + + context 'when the expression is a setter method definition', :unless => argument_error_points_invoker do + let(:source) do + obj = Object.new + + def obj.foo=(arg1, arg2) + @foo = arg1 + end + + obj.foo = 1 + end + + it 'returns all the lines without confusing it with "end"-less method' do + expect(expression_lines).to eq([ + ' def obj.foo=(arg1, arg2)', + ' @foo = arg1', + ' end' + ]) + end + end + context "when the expression ends with multiple paren-only lines of same type" do let(:source) do do_something_fail(:foo, (:bar diff --git a/spec/support/aruba_support.rb b/spec/support/aruba_support.rb index 5f8383f2db..00d4a74316 100644 --- a/spec/support/aruba_support.rb +++ b/spec/support/aruba_support.rb @@ -1,3 +1,5 @@ +require 'support/helper_methods' + if RSpec::Support::Ruby.jruby? && RSpec::Support::Ruby.jruby_version == "9.1.17.0" # A regression appeared in require_relative in JRuby 9.1.17.0 where require some # how ends up private, this monkey patch uses `send` @@ -29,6 +31,7 @@ module ArubaLoader RSpec.shared_context "aruba support" do include Aruba::Api + include RSpecHelpers let(:stderr) { StringIO.new } let(:stdout) { StringIO.new } @@ -70,13 +73,6 @@ def write_file_formatted(file_name, contents) formatted_contents = unindent(contents.sub(/\A\n/, "")) write_file file_name, formatted_contents end - - # Intended for use with indented heredocs. - # taken from Ruby Tapas: - # https://rubytapas.dpdcart.com/subscriber/post?id=616#files - def unindent(s) - s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "") - end end RSpec.configure do |c| diff --git a/spec/support/helper_methods.rb b/spec/support/helper_methods.rb index c4a7027b85..8de71dc0bd 100644 --- a/spec/support/helper_methods.rb +++ b/spec/support/helper_methods.rb @@ -14,6 +14,13 @@ def ignoring_warnings result end + # Intended for use with indented heredocs. + # taken from Ruby Tapas: + # https://rubytapas.dpdcart.com/subscriber/post?id=616#files + def unindent(s) + s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "") + end + # In Ruby 2.7 taint was removed and has no effect, whilst SAFE warns that it # has no effect and will become a normal varible in 3.0. Other engines do not # implement SAFE. From 50f7fb26ed78a504090cc6852e1e82e98d944e94 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sat, 6 Nov 2021 08:56:14 +0000 Subject: [PATCH 2/2] Isolate require tempfile --- spec/rspec/core/formatters/snippet_extractor_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/rspec/core/formatters/snippet_extractor_spec.rb b/spec/rspec/core/formatters/snippet_extractor_spec.rb index 406c025ebd..2bc796a8d6 100644 --- a/spec/rspec/core/formatters/snippet_extractor_spec.rb +++ b/spec/rspec/core/formatters/snippet_extractor_spec.rb @@ -1,6 +1,5 @@ require 'rspec/core/formatters/snippet_extractor' require 'support/helper_methods' -require 'tempfile' module RSpec::Core::Formatters RSpec.describe SnippetExtractor do @@ -189,6 +188,11 @@ def obj.foo(arg) context 'when the expression line includes an "end"-less method definition', :if => RUBY_VERSION.to_f >= 3.0 do include RSpec::Support::InSubProcess + around(:example) do |example| + require 'tempfile' + example.call + end + let(:source) do in_sub_process do load(file.path)