2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'fast_spec_helper'
|
2020-01-01 13:55:28 +05:30
|
|
|
require_relative '../../../rubocop/cop/ignored_columns'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::IgnoredColumns do
|
2020-01-01 13:55:28 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'flags direct use of ignored_columns instead of the IgnoredColumns concern' do
|
|
|
|
expect_offense(<<~RUBY)
|
2020-01-01 13:55:28 +05:30
|
|
|
class Foo < ApplicationRecord
|
|
|
|
self.ignored_columns += %i[id]
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^ Use `IgnoredColumns` concern instead of adding to `self.ignored_columns`.
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
context 'when only CE model exist' do
|
|
|
|
let(:file_path) { full_path('app/models/bar.rb') }
|
|
|
|
|
|
|
|
it 'does not flag ignore_columns usage in CE model' do
|
|
|
|
expect_no_offenses(<<~RUBY, file_path)
|
|
|
|
class Bar < ApplicationRecord
|
|
|
|
ignore_columns :foo, remove_with: '14.3', remove_after: '2021-09-22'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flags ignore_column usage in EE model' do
|
|
|
|
expect_no_offenses(<<~RUBY, file_path)
|
|
|
|
class Baz < ApplicationRecord
|
|
|
|
ignore_column :bar, remove_with: '14.3', remove_after: '2021-09-22'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when only EE model exist' do
|
|
|
|
let(:file_path) { full_path('ee/app/models/ee/bar.rb') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:exist?).with(full_path('app/models/bar.rb')).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flags ignore_columns usage in EE model' do
|
|
|
|
expect_no_offenses(<<~RUBY, file_path)
|
|
|
|
class Bar < ApplicationRecord
|
|
|
|
ignore_columns :foo, remove_with: '14.3', remove_after: '2021-09-22'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flags ignore_column usage in EE model' do
|
|
|
|
expect_no_offenses(<<~RUBY, file_path)
|
|
|
|
class Bar < ApplicationRecord
|
|
|
|
ignore_column :foo, remove_with: '14.3', remove_after: '2021-09-22'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when CE and EE model exist' do
|
|
|
|
let(:file_path) { full_path('ee/app/models/ee/bar.rb') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:exist?).with(full_path('app/models/bar.rb')).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flags ignore_columns usage in EE model' do
|
|
|
|
expect_offense(<<~RUBY, file_path)
|
|
|
|
class Bar < ApplicationRecord
|
|
|
|
ignore_columns :foo, remove_with: '14.3', remove_after: '2021-09-22'
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If the model exists in CE and EE, [...]
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flags ignore_column usage in EE model' do
|
|
|
|
expect_offense(<<~RUBY, file_path)
|
|
|
|
class Bar < ApplicationRecord
|
|
|
|
ignore_column :foo, remove_with: '14.3', remove_after: '2021-09-22'
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If the model exists in CE and EE, [...]
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def full_path(path)
|
|
|
|
rails_root = '../../../'
|
|
|
|
|
|
|
|
File.expand_path(File.join(rails_root, path), __dir__)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|