Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BK-COP] Update cops to use Base rather than Cop as base class. #107

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ Gemspec/RequiredRubyVersion:

RSpec/MultipleExpectations:
Enabled: false

RSpec/ExampleLength:
Max: 15

2 changes: 1 addition & 1 deletion lib/anony/cops/define_deletion_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Lint
#
# @example Bad
# class MyNewThing < ApplicationRecord; end
class DefineDeletionStrategy < Cop
class DefineDeletionStrategy < Base
MSG = "Define .anonymise for %<model>s, see https://github.com/gocardless/" \
"anony/blob/#{Anony::VERSION}/README.md for details".freeze

Expand Down
2 changes: 1 addition & 1 deletion lib/anony/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Anony
VERSION = "1.3.1"
VERSION = "1.4.0"
end
2 changes: 0 additions & 2 deletions spec/anony/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

it_behaves_like "overwritten anonymisable model"

# rubocop:disable RSpec/ExampleLength
it "applies the correct changes to each column" do
expect { instance.anonymise! }.
to change(instance, :first_name).to(/[\h-]{36}/).
Expand All @@ -41,7 +40,6 @@
and change(instance, :company_name).to("anonymised-Microsoft").
and change(instance, :onboarded_at).to be_within(1).of(Time.now)
end
# rubocop:enable RSpec/ExampleLength

it "populates the result fields hash with only anonymised fields" do
result = instance.anonymise!
Expand Down
86 changes: 29 additions & 57 deletions spec/rubocop/cop/lint/define_deletion_strategy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,108 +1,86 @@
# frozen_string_literal: true

require "spec_helper"
require "rubocop"
require "rubocop/rspec/cop_helper"
require "rubocop/rspec/support"
require "spec_helper"
require "anony/cops/define_deletion_strategy"

RSpec.describe RuboCop::Cop::Lint::DefineDeletionStrategy do
include CopHelper

let(:cop) { described_class.new(config) }
RSpec.describe RuboCop::Cop::Lint::DefineDeletionStrategy, :config do
include RuboCop::RSpec::ExpectOffense

let(:config) { RuboCop::Config.new(described_class.cop_name => cop_config) }

let(:cop_config) { {} }

before { inspect_source(source) }
let(:error_msg) do
"Define .anonymise for %s, see https://github.com/gocardless/anony/" \
"blob/#{Anony::VERSION}/README.md for details"
end

context "when it isn't a model" do
let(:source) do
<<~RUBY
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
class Service
end
RUBY
end

it { expect(cop.offenses).to be_empty }
end

context "when it doesn't directly subclass ApplicationRecord" do
let(:source) do
<<~RUBY
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
module Foo
class ApplicationRecord; end
end

class Service < Foo::ApplicationRecord; end
RUBY
end

it { expect(cop.offenses).to be_empty }
end

context "when a model already defines anonymisation rules" do
let(:source) do
<<~RUBY
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
class Employee < ApplicationRecord
anonymise do
destroy
end
end
RUBY
end

it { expect(cop.offenses).to be_empty }
end

context "when a model does not define anonymisation rules" do
subject(:offenses) { cop.offenses }

shared_examples_for "an offense" do
it { expect(offenses.count).to eq(1) }

it "has the correct name" do
expect(offenses.first.cop_name).to eq(cop.name)
end

it "has the correct message" do
expect(offenses.first.message).
to eq("Define .anonymise for Employee, see https://github.com/gocardless/anony/" \
"blob/#{Anony::VERSION}/README.md for details")
end
end

let(:source) do
<<~RUBY
it "registers an offense" do
expect_offense(<<~RUBY)
class Employee < ApplicationRecord
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{error_msg % 'Employee'}
end
RUBY
end

it_behaves_like "an offense"

context "with a custom model superclasss" do
context "with a custom model superclass" do
let(:cop_config) { { "ModelSuperclass" => "Acme::Record" } }

let(:source) do
<<~RUBY
it "registers an offense" do
expect_offense(<<~RUBY)
class Employee < Acme::Record
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{error_msg % 'Employee'}
end
RUBY
end

it_behaves_like "an offense"
end
end

context "when it uses multiple super classes" do
context "when it uses multiple superclasses" do
subject(:offenses) { cop.offenses }

let(:cop_config) { { "ModelSuperclass" => ["Acme::Record", "Another::Record"] } }
let(:cop_config) { { "ModelSuperclass" => %w[Acme::Record Another::Record] } }

context "when models defines anonymisation rules" do
let(:source) do
<<~RUBY
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
class Employee < Acme::Record
anonymise do
destroy
Expand All @@ -116,26 +94,20 @@ class Boss < Another::Record
end
RUBY
end

it { expect(cop.offenses).to be_empty }
end

context "when models are missing anonymisation rules" do
let(:source) do
<<~RUBY
it "registers an offense" do
expect_offense(<<~RUBY)
class Employee < Another::Record
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{error_msg % 'Employee'}
end

class Boss < Another::Record
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{error_msg % 'Boss'}
end
RUBY
end

it { expect(offenses.count).to eq(2) }

it "has the correct name" do
expect(offenses.first.cop_name).to eq(cop.name)
end
end
end
end
Loading