Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-11-07 18:06:33 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-11-07 18:06:33 +0300
commit90a06a20be61bb6d48d77746091492831153e075 (patch)
treebdba99289605f8b5acf12159d02aeb23f8690202 /rubocop
parent84a0e65ac88c7a3db86a0e4347606ba093490bef (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/rspec/any_instance_of.rb78
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 79 insertions, 0 deletions
diff --git a/rubocop/cop/rspec/any_instance_of.rb b/rubocop/cop/rspec/any_instance_of.rb
new file mode 100644
index 00000000000..a939af36c13
--- /dev/null
+++ b/rubocop/cop/rspec/any_instance_of.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module RSpec
+ # This cop checks for `allow_any_instance_of` or `expect_any_instance_of`
+ # usage in specs.
+ #
+ # @example
+ #
+ # # bad
+ # allow_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
+ #
+ # # bad
+ # expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
+ #
+ # # good
+ # allow_next_instance_of(User) do |instance|
+ # allow(instance).to receive(:invalidate_issue_cache_counts)
+ # end
+ #
+ # # good
+ # expect_next_instance_of(User) do |instance|
+ # expect(instance).to receive(:invalidate_issue_cache_counts)
+ # end
+ #
+ class AnyInstanceOf < RuboCop::Cop::Cop
+ MESSAGE_EXPECT = 'Do not use `expect_any_instance_of` method, use `expect_next_instance_of` instead.'
+ MESSAGE_ALLOW = 'Do not use `allow_any_instance_of` method, use `allow_next_instance_of` instead.'
+
+ def_node_search :expect_any_instance_of?, <<~PATTERN
+ (send (send nil? :expect_any_instance_of ...) ...)
+ PATTERN
+ def_node_search :allow_any_instance_of?, <<~PATTERN
+ (send (send nil? :allow_any_instance_of ...) ...)
+ PATTERN
+
+ def on_send(node)
+ if expect_any_instance_of?(node)
+ add_offense(node, location: :expression, message: MESSAGE_EXPECT)
+ elsif allow_any_instance_of?(node)
+ add_offense(node, location: :expression, message: MESSAGE_ALLOW)
+ end
+ end
+
+ def autocorrect(node)
+ replacement =
+ if expect_any_instance_of?(node)
+ replacement_any_instance_of(node, 'expect')
+ elsif allow_any_instance_of?(node)
+ replacement_any_instance_of(node, 'allow')
+ end
+
+ lambda do |corrector|
+ corrector.replace(node.loc.expression, replacement)
+ end
+ end
+
+ private
+
+ def replacement_any_instance_of(node, rspec_prefix)
+ method_call =
+ node.receiver.source.sub(
+ "#{rspec_prefix}_any_instance_of",
+ "#{rspec_prefix}_next_instance_of")
+
+ block = <<~RUBY.chomp
+ do |instance|
+ #{rspec_prefix}(instance).#{node.method_name} #{node.children.last.source}
+ end
+ RUBY
+
+ "#{method_call} #{block}"
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 70679aa1e78..159892ae0c1 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -32,6 +32,7 @@ require_relative 'cop/migration/timestamps'
require_relative 'cop/migration/update_column_in_batches'
require_relative 'cop/migration/update_large_table'
require_relative 'cop/project_path_helper'
+require_relative 'cop/rspec/any_instance_of'
require_relative 'cop/rspec/be_success_matcher'
require_relative 'cop/rspec/env_assignment'
require_relative 'cop/rspec/factories_in_migration_specs'