Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add cop to detect do..end block with Capybara matcher

Christophe Bliard2f87928f013f
diff --git a/.rubocop.yml b/.rubocop.yml
index 4016ed01383..e1e6f24c12e 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -1,6 +1,7 @@
require:
- rubocop-rails
- rubocop-rspec
+ - ./lib_static/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect.rb
- ./lib_static/rubocop/cop/open_project/use_service_result_factory_methods.rb
inherit_mode:
diff --git a/lib_static/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect.rb b/lib_static/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect.rb
new file mode 100644
index 00000000000..178b7a27a96
--- /dev/null
+++ b/lib_static/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect.rb
@@ -0,0 +1,112 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module RuboCop::Cop::OpenProject
+ # As +do .. end+ block has less precedence than method call, a +do .. end+
+ # block at the end of a rspec matcher will be an argument to the +to+ method,
+ # which is not handled by Capybara matchers (teamcapybara/capybara/#2616).
+ #
+ # # bad
+ # expect(page).to have_selector("input") do |input|
+ # input.value == "hello world"
+ # end
+ #
+ # # good
+ # expect(page).to have_selector("input") { |input| input.value == "hello world" }
+ #
+ # # good
+ # expect(page).to have_selector("input", value: "hello world")
+ #
+ # # good
+ # match_input_with_hello_world = have_selector("input") do |input|
+ # input.value == "hello world"
+ # end
+ # expect(page).to match_input_with_hello_world
+ #
+ # # good
+ # expect(foo).to have_received(:bar) do |arg|
+ # arg == :baz
+ # end
+ #
+ class NoDoEndBlockWithRSpecCapybaraMatcherInExpect < RuboCop::Cop::Base
+ extend RuboCop::Cop::AutoCorrector
+
+ CAPYBARA_MATCHER_METHODS = %w[selector css xpath text title current_path link button
+ field checked_field unchecked_field select table
+ sibling ancestor].flat_map do |matcher_type|
+ ["have_#{matcher_type}", "have_no_#{matcher_type}"]
+ end
+
+ MSG = 'The `do .. end` block is associated with `to` and not with Capybara matcher `%<matcher_method>s`.'.freeze
+ MSG_IMPLICIT_FAILURE = 'That does not work.'.freeze
+
+ def_node_matcher :expect_to_with_block?, <<~PATTERN
+ # ruby-parse output
+ (block
+ (send
+ (send nil? :expect ...)
+ :to
+ ...
+ )
+ ...
+ )
+ PATTERN
+
+ def_node_matcher :rspec_matcher, <<~PATTERN
+ (send
+ (send nil? :expect...)
+ :to
+ (:send nil? $_matcher_method ...)
+ )
+ PATTERN
+
+ def on_block(node)
+ return unless expect_to_with_block?(node)
+ return unless capybara_matcher?(node)
+
+ add_offense(offense_range(node), message: offense_message(node))
+ end
+
+ private
+
+ def capybara_matcher?(node)
+ matcher_name = node.send_node.arguments.first.method_name.to_s
+ CAPYBARA_MATCHER_METHODS.include?(matcher_name)
+ end
+
+ def offense_range(node)
+ node.send_node.loc.selector.join(node.loc.end)
+ end
+
+ def offense_message(node)
+ rspec_matcher(node.send_node) do |matcher_method|
+ format(MSG, matcher_method:)
+ end
+ end
+ end
+end
diff --git a/spec/lib/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect_spec.rb b/spec/lib/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect_spec.rb
new file mode 100644
index 00000000000..be912e50f25
--- /dev/null
+++ b/spec/lib/rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect_spec.rb
@@ -0,0 +1,64 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'rubocop'
+require 'rubocop/rspec/shared_contexts'
+require 'spec_helper'
+require 'rubocop/rspec/support'
+require 'rubocop/cop/open_project/no_do_end_block_with_rspec_capybara_matcher_in_expect'
+
+RSpec.describe RuboCop::Cop::OpenProject::NoDoEndBlockWithRSpecCapybaraMatcherInExpect do
+ include RuboCop::RSpec::ExpectOffense
+ include_context 'config'
+
+ context 'when using `do .. end` syntax with rspec matcher' do
+ it 'registers an offense' do
+ expect_offense(<<~RUBY)
+ expect(page).to have_selector("input") do |input|
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The `do .. end` block is associated with `to` and not with Capybara matcher `have_selector`.
+ end
+ RUBY
+ end
+
+ it 'matches only Capybara matchers' do
+ expect_no_offenses(<<~RUBY)
+ expect(foo).to have_received(:bar) do |value|
+ value == 'hello world'
+ end
+ RUBY
+ end
+ end
+
+ context 'when using `{ .. }` syntax with rspec matcher' do
+ it 'does not register an offense' do
+ expect_no_offenses(<<~RUBY)
+ expect(page).to have_selector("input") { |input| }
+ RUBY
+ end
+ end
+end