diff options
author | James Lopez <james@gitlab.com> | 2018-04-12 08:57:27 +0000 |
---|---|---|
committer | James Lopez <james@gitlab.com> | 2018-04-12 08:57:27 +0000 |
commit | 63623dc7741a4680af17dd348319e1104f71a8c6 (patch) | |
tree | 24586af26d80f56d06d8b432a22fa6bfa7cae8c0 /spec/rubocop | |
parent | c393a44f2fa5e161c8055bc589f10d4ee2e1d8a0 (diff) | |
parent | 8e26fbfeff7fbd32879bfc530fb3eb5bcbd73a00 (diff) | |
download | gitlab-ce-63623dc7741a4680af17dd348319e1104f71a8c6.tar.gz |
Merge branch '10-7-stable-prepare-rc5' into '10-7-stable'
Prepare 10.7 RC5 release
See merge request gitlab-org/gitlab-ce!18308
Diffstat (limited to 'spec/rubocop')
-rw-r--r-- | spec/rubocop/cop/gitlab/has_many_through_scope_spec.rb | 74 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb | 48 |
2 files changed, 122 insertions, 0 deletions
diff --git a/spec/rubocop/cop/gitlab/has_many_through_scope_spec.rb b/spec/rubocop/cop/gitlab/has_many_through_scope_spec.rb new file mode 100644 index 00000000000..6d769c8e6fd --- /dev/null +++ b/spec/rubocop/cop/gitlab/has_many_through_scope_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/gitlab/has_many_through_scope' + +describe RuboCop::Cop::Gitlab::HasManyThroughScope do # rubocop:disable RSpec/FilePath + include CopHelper + + subject(:cop) { described_class.new } + + context 'in a model file' do + before do + allow(cop).to receive(:in_model?).and_return(true) + end + + context 'when the model does not use has_many :through' do + it 'does not register an offense' do + expect_no_offenses(<<-RUBY) + class User < ActiveRecord::Base + has_many :tags, source: 'UserTag' + end + RUBY + end + end + + context 'when the model uses has_many :through' do + context 'when the association has no scope defined' do + it 'registers an offense on the association' do + expect_offense(<<-RUBY) + class User < ActiveRecord::Base + has_many :tags, through: :user_tags + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG} + end + RUBY + end + end + + context 'when the association has a scope defined' do + context 'when the scope does not disable auto-loading' do + it 'registers an offense on the scope' do + expect_offense(<<-RUBY) + class User < ActiveRecord::Base + has_many :tags, -> { where(active: true) }, through: :user_tags + ^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG} + end + RUBY + end + end + + context 'when the scope has auto_include(false)' do + it 'does not register an offense' do + expect_no_offenses(<<-RUBY) + class User < ActiveRecord::Base + has_many :tags, -> { where(active: true).auto_include(false).reorder(nil) }, through: :user_tags + end + RUBY + end + end + end + end + end + + context 'outside of a migration spec file' do + it 'does not register an offense' do + expect_no_offenses(<<-RUBY) + class User < ActiveRecord::Base + has_many :tags, through: :user_tags + end + RUBY + end + end +end diff --git a/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb new file mode 100644 index 00000000000..2763f2bda21 --- /dev/null +++ b/spec/rubocop/cop/rspec/factories_in_migration_specs_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/rspec/factories_in_migration_specs' + +describe RuboCop::Cop::RSpec::FactoriesInMigrationSpecs do + include CopHelper + + let(:source_file) { 'spec/migrations/foo_spec.rb' } + + subject(:cop) { described_class.new } + + shared_examples 'an offensive factory call' do |namespace| + %i[build build_list create create_list].each do |forbidden_method| + namespaced_forbidden_method = "#{namespace}#{forbidden_method}(:user)" + + it "registers an offense for #{namespaced_forbidden_method}" do + expect_offense(<<-RUBY) + describe 'foo' do + let(:user) { #{namespaced_forbidden_method} } + #{'^' * namespaced_forbidden_method.size} Don't use FactoryBot.#{forbidden_method} in migration specs, use `table` instead. + end + RUBY + end + end + end + + context 'in a migration spec file' do + before do + allow(cop).to receive(:in_migration_spec?).and_return(true) + end + + it_behaves_like 'an offensive factory call', '' + it_behaves_like 'an offensive factory call', 'FactoryBot.' + end + + context 'outside of a migration spec file' do + it "does not register an offense" do + expect_no_offenses(<<-RUBY) + describe 'foo' do + let(:user) { create(:user) } + end + RUBY + end + end +end |