summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/code_reuse
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2018-08-27 17:30:20 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2018-09-11 15:00:18 +0200
commit91c40973dc620430b173ea2df968587d2a708dff (patch)
treeeb871f8b742d8e165138d699be5e884a2c612b24 /spec/rubocop/cop/code_reuse
parentc56f2b96159afaf6f1e0831d0e7a756a40568cab (diff)
downloadgitlab-ce-91c40973dc620430b173ea2df968587d2a708dff.tar.gz
Added RuboCop cops to enforce code reuse rules
These Cops enforces the code reuse rules as defined in merge request https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21254.
Diffstat (limited to 'spec/rubocop/cop/code_reuse')
-rw-r--r--spec/rubocop/cop/code_reuse/active_record_spec.rb138
-rw-r--r--spec/rubocop/cop/code_reuse/finder_spec.rb77
-rw-r--r--spec/rubocop/cop/code_reuse/presenter_spec.rb117
-rw-r--r--spec/rubocop/cop/code_reuse/serializer_spec.rb117
-rw-r--r--spec/rubocop/cop/code_reuse/service_class_spec.rb89
-rw-r--r--spec/rubocop/cop/code_reuse/worker_spec.rb104
6 files changed, 642 insertions, 0 deletions
diff --git a/spec/rubocop/cop/code_reuse/active_record_spec.rb b/spec/rubocop/cop/code_reuse/active_record_spec.rb
new file mode 100644
index 00000000000..a30fc52d26f
--- /dev/null
+++ b/spec/rubocop/cop/code_reuse/active_record_spec.rb
@@ -0,0 +1,138 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../../rubocop/cop/code_reuse/active_record'
+
+describe RuboCop::Cop::CodeReuse::ActiveRecord do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ it 'flags the use of "where" without any arguments' do
+ expect_offense(<<~SOURCE)
+ def foo
+ User.where
+ ^^^^^ This method can only be used inside an ActiveRecord model
+ end
+ SOURCE
+ end
+
+ it 'flags the use of "where" with arguments' do
+ expect_offense(<<~SOURCE)
+ def foo
+ User.where(id: 10)
+ ^^^^^ This method can only be used inside an ActiveRecord model
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of "group" without any arguments' do
+ expect_no_offenses(<<~SOURCE)
+ def foo
+ project.group
+ end
+ SOURCE
+ end
+
+ it 'flags the use of "group" with arguments' do
+ expect_offense(<<~SOURCE)
+ def foo
+ project.group(:name)
+ ^^^^^ This method can only be used inside an ActiveRecord model
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of ActiveRecord models in a model' do
+ path = Rails.root.join('app', 'models', 'foo.rb').to_s
+
+ expect_no_offenses(<<~SOURCE, path)
+ def foo
+ project.group(:name)
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of ActiveRecord models in a spec' do
+ path = Rails.root.join('spec', 'foo_spec.rb').to_s
+
+ expect_no_offenses(<<~SOURCE, path)
+ def foo
+ project.group(:name)
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of ActiveRecord models in a background migration' do
+ path = Rails
+ .root
+ .join('lib', 'gitlab', 'background_migration', 'foo.rb')
+ .to_s
+
+ expect_no_offenses(<<~SOURCE, path)
+ def foo
+ project.group(:name)
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of ActiveRecord models in lib/gitlab/database' do
+ path = Rails.root.join('lib', 'gitlab', 'database', 'foo.rb').to_s
+
+ expect_no_offenses(<<~SOURCE, path)
+ def foo
+ project.group(:name)
+ end
+ SOURCE
+ end
+
+ it 'autocorrects offenses in instance methods by whitelisting them' do
+ corrected = autocorrect_source(<<~SOURCE)
+ def foo
+ User.where
+ end
+ SOURCE
+
+ expect(corrected).to eq(<<~SOURCE)
+ # rubocop: disable CodeReuse/ActiveRecord
+ def foo
+ User.where
+ end
+ # rubocop: enable CodeReuse/ActiveRecord
+ SOURCE
+ end
+
+ it 'autocorrects offenses in class methods by whitelisting them' do
+ corrected = autocorrect_source(<<~SOURCE)
+ def self.foo
+ User.where
+ end
+ SOURCE
+
+ expect(corrected).to eq(<<~SOURCE)
+ # rubocop: disable CodeReuse/ActiveRecord
+ def self.foo
+ User.where
+ end
+ # rubocop: enable CodeReuse/ActiveRecord
+ SOURCE
+ end
+
+ it 'autocorrects offenses in blocks by whitelisting them' do
+ corrected = autocorrect_source(<<~SOURCE)
+ get '/' do
+ User.where
+ end
+ SOURCE
+
+ expect(corrected).to eq(<<~SOURCE)
+ # rubocop: disable CodeReuse/ActiveRecord
+ get '/' do
+ User.where
+ end
+ # rubocop: enable CodeReuse/ActiveRecord
+ SOURCE
+ end
+end
diff --git a/spec/rubocop/cop/code_reuse/finder_spec.rb b/spec/rubocop/cop/code_reuse/finder_spec.rb
new file mode 100644
index 00000000000..b04e053a4c3
--- /dev/null
+++ b/spec/rubocop/cop/code_reuse/finder_spec.rb
@@ -0,0 +1,77 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../../rubocop/cop/code_reuse/finder'
+
+describe RuboCop::Cop::CodeReuse::Finder do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ it 'flags the use of a Finder inside another Finder' do
+ allow(cop)
+ .to receive(:in_finder?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooFinder
+ def execute
+ BarFinder.new.execute
+ ^^^^^^^^^^^^^ Finders can not be used inside a Finder.
+ end
+ end
+ SOURCE
+
+ expect(cop.offenses.size).to eq(1)
+ end
+
+ it 'flags the use of a Finder inside a model class method' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User
+ class << self
+ def second_method
+ BarFinder.new
+ ^^^^^^^^^^^^^ Finders can not be used inside model class methods.
+ end
+ end
+
+ def self.second_method
+ FooFinder.new
+ ^^^^^^^^^^^^^ Finders can not be used inside model class methods.
+ end
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of a Finder in a non Finder file' do
+ expect_no_offenses(<<~SOURCE)
+ class FooFinder
+ def execute
+ BarFinder.new.execute
+ end
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of a Finder in a regular class method' do
+ expect_no_offenses(<<~SOURCE)
+ class User
+ class << self
+ def second_method
+ BarFinder.new
+ end
+ end
+
+ def self.second_method
+ FooFinder.new
+ end
+ end
+ SOURCE
+ end
+end
diff --git a/spec/rubocop/cop/code_reuse/presenter_spec.rb b/spec/rubocop/cop/code_reuse/presenter_spec.rb
new file mode 100644
index 00000000000..4fe72619273
--- /dev/null
+++ b/spec/rubocop/cop/code_reuse/presenter_spec.rb
@@ -0,0 +1,117 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../../rubocop/cop/code_reuse/presenter'
+
+describe RuboCop::Cop::CodeReuse::Presenter do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ it 'flags the use of a Presenter in a Service class' do
+ allow(cop)
+ .to receive(:in_service_class?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooService
+ def execute
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a Service class.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Presenter in a Finder' do
+ allow(cop)
+ .to receive(:in_finder?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooFinder
+ def execute
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a Finder.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Service class in a Presenter' do
+ allow(cop)
+ .to receive(:in_presenter?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooPresenter
+ def execute
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a Presenter.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Presenter in a Serializer' do
+ allow(cop)
+ .to receive(:in_serializer?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooSerializer
+ def execute
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a Serializer.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Presenter in a model instance method' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User < ActiveRecord::Base
+ def execute
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a model.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Presenter in a model class method' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User < ActiveRecord::Base
+ def self.execute
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a model.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Presenter in a worker' do
+ allow(cop)
+ .to receive(:in_worker?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooWorker
+ def perform
+ FooPresenter.new.execute
+ ^^^^^^^^^^^^^^^^ Presenters can not be used in a worker.
+ end
+ end
+ SOURCE
+ end
+end
diff --git a/spec/rubocop/cop/code_reuse/serializer_spec.rb b/spec/rubocop/cop/code_reuse/serializer_spec.rb
new file mode 100644
index 00000000000..4530b15eed7
--- /dev/null
+++ b/spec/rubocop/cop/code_reuse/serializer_spec.rb
@@ -0,0 +1,117 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../../rubocop/cop/code_reuse/serializer'
+
+describe RuboCop::Cop::CodeReuse::Serializer do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ it 'flags the use of a Serializer in a Service class' do
+ allow(cop)
+ .to receive(:in_service_class?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooService
+ def execute
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a Service class.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Serializer in a Finder' do
+ allow(cop)
+ .to receive(:in_finder?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooFinder
+ def execute
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a Finder.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Serializer in a Presenter' do
+ allow(cop)
+ .to receive(:in_presenter?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooPresenter
+ def execute
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a Presenter.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Serializer in a Serializer' do
+ allow(cop)
+ .to receive(:in_serializer?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooSerializer
+ def execute
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a Serializer.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Serializer in a model instance method' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User < ActiveRecord::Base
+ def execute
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a model.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Serializer in a model class method' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User < ActiveRecord::Base
+ def self.execute
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a model.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Serializer in a worker' do
+ allow(cop)
+ .to receive(:in_worker?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooWorker
+ def perform
+ FooSerializer.new.execute
+ ^^^^^^^^^^^^^^^^^ Serializers can not be used in a worker.
+ end
+ end
+ SOURCE
+ end
+end
diff --git a/spec/rubocop/cop/code_reuse/service_class_spec.rb b/spec/rubocop/cop/code_reuse/service_class_spec.rb
new file mode 100644
index 00000000000..7b8d82f332e
--- /dev/null
+++ b/spec/rubocop/cop/code_reuse/service_class_spec.rb
@@ -0,0 +1,89 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../../rubocop/cop/code_reuse/service_class'
+
+describe RuboCop::Cop::CodeReuse::ServiceClass do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ it 'flags the use of a Service class in a Finder' do
+ allow(cop)
+ .to receive(:in_finder?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooFinder
+ def execute
+ FooService.new.execute
+ ^^^^^^^^^^^^^^ Service classes can not be used in a Finder.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Service class in a Presenter' do
+ allow(cop)
+ .to receive(:in_presenter?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooPresenter
+ def execute
+ FooService.new.execute
+ ^^^^^^^^^^^^^^ Service classes can not be used in a Presenter.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Service class in a Serializer' do
+ allow(cop)
+ .to receive(:in_serializer?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooSerializer
+ def execute
+ FooService.new.execute
+ ^^^^^^^^^^^^^^ Service classes can not be used in a Serializer.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a Service class in a model' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User < ActiveRecord::Model
+ class << self
+ def first
+ FooService.new.execute
+ ^^^^^^^^^^^^^^ Service classes can not be used in a model.
+ end
+ end
+
+ def second
+ FooService.new.execute
+ ^^^^^^^^^^^^^^ Service classes can not be used in a model.
+ end
+ end
+ SOURCE
+ end
+
+ it 'does not flag the use of a Service class in a regular class' do
+ expect_no_offenses(<<~SOURCE)
+ class Foo
+ def execute
+ FooService.new.execute
+ end
+ end
+ SOURCE
+ end
+end
diff --git a/spec/rubocop/cop/code_reuse/worker_spec.rb b/spec/rubocop/cop/code_reuse/worker_spec.rb
new file mode 100644
index 00000000000..97acaeb7643
--- /dev/null
+++ b/spec/rubocop/cop/code_reuse/worker_spec.rb
@@ -0,0 +1,104 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../../rubocop/cop/code_reuse/worker'
+
+describe RuboCop::Cop::CodeReuse::Worker do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ it 'flags the use of a worker in a controller' do
+ allow(cop)
+ .to receive(:in_controller?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooController
+ def index
+ FooWorker.perform_async
+ ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a controller.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a worker in an API' do
+ allow(cop)
+ .to receive(:in_api?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class Foo < Grape::API
+ resource :projects do
+ get '/' do
+ FooWorker.perform_async
+ ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Grape API.
+ end
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a worker in a Finder' do
+ allow(cop)
+ .to receive(:in_finder?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooFinder
+ def execute
+ FooWorker.perform_async
+ ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Finder.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a worker in a Presenter' do
+ allow(cop)
+ .to receive(:in_presenter?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooPresenter
+ def execute
+ FooWorker.perform_async
+ ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Presenter.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a worker in a Serializer' do
+ allow(cop)
+ .to receive(:in_serializer?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class FooSerializer
+ def execute
+ FooWorker.perform_async
+ ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Serializer.
+ end
+ end
+ SOURCE
+ end
+
+ it 'flags the use of a worker in a model class method' do
+ allow(cop)
+ .to receive(:in_model?)
+ .and_return(true)
+
+ expect_offense(<<~SOURCE)
+ class User < ActiveRecord::Base
+ def self.execute
+ FooWorker.perform_async
+ ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in model class methods.
+ end
+ end
+ SOURCE
+ end
+end