Skip to content

Commit

Permalink
Add Rubocop for RSpec
Browse files Browse the repository at this point in the history
* Add the rubocop-rspec and rubocop-rake gems
* Enable Rubocop on `spec/*`
* Autocorrect safe corrections
* Add other Rubocop failures to todo list
  • Loading branch information
jrmhaig committed Jul 10, 2021
1 parent ae782e4 commit 2e7cb08
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 92 deletions.
13 changes: 8 additions & 5 deletions .rubocop.yml
@@ -1,18 +1,21 @@
require:
- rubocop-rspec
- rubocop-rake

inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- 'spec/**/*'
TargetRubyVersion: 2.5
Metrics/LineLength:
NewCops: enable
Layout/LineLength:
Max: 99
Style/FileName:
Naming/FileName:
Enabled: false
Style/ModuleFunction:
Enabled: false
Style/Encoding:
Enabled: false
Documentation:
Style/Documentation:
Enabled: false
Metrics/MethodLength:
Max: 15
98 changes: 97 additions & 1 deletion .rubocop_todo.yml
@@ -1,16 +1,34 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2021-07-10 17:27:59 UTC using RuboCop version 1.16.1.
# on 2021-07-10 17:38:28 UTC using RuboCop version 1.16.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 10
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 110

# Offense count: 1
Lint/MissingSuper:
Exclude:
- 'lib/amoeba/macros/base.rb'

# Offense count: 17
Lint/UselessAssignment:
Exclude:
- 'spec/support/data.rb'

# Offense count: 8
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
Metrics/BlockLength:
Max: 339

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Expand All @@ -22,3 +40,81 @@ Metrics/ClassLength:
Naming/HeredocDelimiterNaming:
Exclude:
- 'lib/amoeba/config.rb'

# Offense count: 2
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339
Naming/VariableNumber:
Exclude:
- 'spec/support/data.rb'

# Offense count: 1
RSpec/BeforeAfterAll:
Exclude:
- 'spec/spec_helper.rb'
- 'spec/rails_helper.rb'
- 'spec/support/**/*.rb'
- 'spec/lib/amoeba_spec.rb'

# Offense count: 12
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'spec/lib/amoeba_spec.rb'

# Offense count: 1
# Configuration parameters: IgnoredMetadata.
RSpec/DescribeClass:
Exclude:
- 'spec/lib/amoeba_spec.rb'

# Offense count: 3
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 134

# Offense count: 1
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 7
RSpec/MultipleExpectations:
Max: 55

# Offense count: 18
# Configuration parameters: IgnoreSharedExamples.
RSpec/NamedSubject:
Exclude:
- 'spec/lib/amoeba_spec.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Strict, EnforcedStyle, AllowedExplicitMatchers.
# SupportedStyles: inflected, explicit
RSpec/PredicateMatcher:
Exclude:
- 'spec/lib/amoeba_spec.rb'

# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Exclude:
- 'spec/lib/amoeba_spec.rb'
- 'spec/spec_helper.rb'
- 'spec/support/data.rb'
- 'spec/support/models.rb'
- 'spec/support/schema.rb'

# Offense count: 5
# Cop supports --auto-correct.
Style/StringConcatenation:
Exclude:
- 'spec/lib/amoeba_spec.rb'
- 'spec/spec_helper.rb'
- 'spec/support/models.rb'
2 changes: 2 additions & 0 deletions amoeba.gemspec
Expand Up @@ -28,6 +28,8 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
s.add_development_dependency 'rspec', '>= 3.0.0'
s.add_development_dependency 'rubocop', '~> 1.16'
s.add_development_dependency 'rubocop-rake', '~> 0.6'
s.add_development_dependency 'rubocop-rspec', '~> 2.4'

if RUBY_PLATFORM == 'java'
s.add_development_dependency 'activerecord-jdbc-adapter', '~> 61.0'
Expand Down
4 changes: 2 additions & 2 deletions lib/amoeba/config.rb
Expand Up @@ -112,7 +112,7 @@ def fill_hash_value_for(config_key, key, val)
def include_association(value = nil, options = {})
enable
@config[:excludes] = {}
value = value.is_a?(Array) ? Hash[value.map! { |v| [v, options] }] : { value => options }
value = value.is_a?(Array) ? value.map! { |v| [v, options] }.to_h : { value => options }
push_value_to_hash(value, :includes)
end

Expand All @@ -123,7 +123,7 @@ def include_associations(*values)
def exclude_association(value = nil, options = {})
enable
@config[:includes] = {}
value = value.is_a?(Array) ? Hash[value.map! { |v| [v, options] }] : { value => options }
value = value.is_a?(Array) ? value.map! { |v| [v, options] }.to_h : { value => options }
push_value_to_hash(value, :excludes)
end

Expand Down
76 changes: 45 additions & 31 deletions spec/lib/amoeba_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe 'amoeba' do
context 'dup' do
before :each do
before do
require ::File.dirname(__FILE__) + '/../support/data.rb'
end

Expand Down Expand Up @@ -73,12 +73,17 @@
expect(new_post.supercats.map(&:ramblings)).to include('Copy of zomg')
expect(new_post.supercats.map(&:other_ramblings).uniq.length).to eq(1)
expect(new_post.supercats.map(&:other_ramblings).uniq).to include('La la la')
expect(new_post.contents).to eq("Here's a copy: #{old_post.contents.gsub(/dog/, 'cat')} (copied version)")
expect(new_post.contents).to eq("Here's a copy: #{old_post.contents.gsub(/dog/,
'cat')} (copied version)")
expect(new_post.comments.length).to eq(5)
expect(new_post.comments.select { |c| c.nerf == 'ratatat' && c.contents.nil? }.length).to eq(1)
expect(new_post.comments.select do |c|
c.nerf == 'ratatat' && c.contents.nil?
end.length).to eq(1)
expect(new_post.comments.select { |c| c.nerf == 'ratatat' }.length).to eq(2)
expect(new_post.comments.select { |c| c.nerf == 'bonk' }.length).to eq(1)
expect(new_post.comments.select { |c| c.nerf == 'bonkers' && c.contents.nil? }.length).to eq(1)
expect(new_post.comments.select do |c|
c.nerf == 'bonkers' && c.contents.nil?
end.length).to eq(1)

new_post.widgets.map(&:id).each do |id|
expect(old_post.widgets.map(&:id)).not_to include(id)
Expand All @@ -96,7 +101,9 @@
expect(new_author.errors.messages).to be_empty
expect(new_author.posts.first.custom_things.length).to eq(3)
expect(new_author.posts.first.custom_things.select { |ct| ct.value == [] }.length).to eq(1)
expect(new_author.posts.first.custom_things.select { |ct| ct.value == [1, 2] }.length).to eq(1)
expect(new_author.posts.first.custom_things.select do |ct|
ct.value == [1, 2]
end.length).to eq(1)
expect(new_author.posts.first.custom_things.select { |ct| ct.value == [78] }.length).to eq(1)
# }}}
# Products {{{
Expand Down Expand Up @@ -177,87 +184,89 @@
end

context 'Using a if condition' do
subject { post.amoeba_dup.save! }

before(:all) do
require ::File.dirname(__FILE__) + '/../support/data.rb'
end

before { ::Post.fresh_amoeba }

subject { post.amoeba_dup.save! }
let(:post) { Post.first }

it 'includes an association with truthy condition' do
::Post.amoeba do
include_association :comments, if: :truthy?
end
expect { subject }.to change { Comment.count }.by(3)
expect { subject }.to change(Comment, :count).by(3)
end

it 'does not include an association with a falsey condition' do
::Post.amoeba do
include_association :comments, if: :falsey?
end
expect { subject }.not_to change { Comment.count }
expect { subject }.not_to change(Comment, :count)
end

it 'excludes an association with a truthy condition' do
::Post.amoeba do
exclude_association :comments, if: :truthy?
end
expect { subject }.not_to change { Comment.count }
expect { subject }.not_to change(Comment, :count)
end

it 'does not exclude an association with a falsey condition' do
::Post.amoeba do
exclude_association :comments, if: :falsey?
end
expect { subject }.to change { Comment.count }.by(3)
expect { subject }.to change(Comment, :count).by(3)
end

it 'includes associations from a given array with a truthy condition' do
::Post.amoeba do
include_association [:comments], if: :truthy?
end
expect { subject }.to change { Comment.count }.by(3)
expect { subject }.to change(Comment, :count).by(3)
end

it 'does not include associations from a given array with a falsey condition' do
::Post.amoeba do
include_association [:comments], if: :falsey?
end
expect { subject }.not_to change { Comment.count }
expect { subject }.not_to change(Comment, :count)
end

it 'does exclude associations from a given array with a truthy condition' do
::Post.amoeba do
exclude_association [:comments], if: :truthy?
end
expect { subject }.not_to change { Comment.count }
expect { subject }.not_to change(Comment, :count)
end

it 'does not exclude associations from a given array with a falsey condition' do
::Post.amoeba do
exclude_association [:comments], if: :falsey?
end
expect { subject }.to change { Comment.count }.by(3)
expect { subject }.to change(Comment, :count).by(3)
end
end

context 'override' do
before :each do
before do
::Image.fresh_amoeba
::Image.amoeba do
override ->(old, new) { new.product_id = 13 if old.filename == 'test.jpg' }
end
end

it 'should override fields' do
it 'overrides fields' do
image = ::Image.create(filename: 'test.jpg', product_id: 12)
image_dup = image.amoeba_dup
expect(image_dup.save).to be_truthy
expect(image_dup.product_id).to eq(13)
end

it 'should not override fields' do
it 'does not override fields' do
image = ::Image.create(filename: 'test2.jpg', product_id: 12)
image_dup = image.amoeba_dup
expect(image_dup.save).to be_truthy
Expand All @@ -266,7 +275,7 @@
end

context 'nullify' do
before :each do
before do
::Image.fresh_amoeba
::Image.amoeba do
nullify :product_id
Expand All @@ -276,14 +285,14 @@
let(:image) { ::Image.create(filename: 'test.jpg', product_id: 12) }
let(:image_dup) { image.amoeba_dup }

it 'should nullify fields' do
it 'nullifies fields' do
expect(image_dup.save).to be_truthy
expect(image_dup.product_id).to be_nil
end
end

context 'strict propagate' do
it 'should call #reset_amoeba' do
it 'calls #reset_amoeba' do
expect(::SuperBlackBox).to receive(:reset_amoeba).and_call_original
box = ::SuperBlackBox.create(title: 'Super Black Box', price: 9.99, length: 1, metal: '1')
new_box = box.amoeba_dup
Expand Down Expand Up @@ -312,13 +321,16 @@

context 'preprocessing fields' do
subject { super_admin.amoeba_dup }
let(:super_admin) { ::SuperAdmin.create!(email: 'user@example.com', active: true, password: 'password') }

it 'should accept "set" to set false to attribute' do
let(:super_admin) do
::SuperAdmin.create!(email: 'user@example.com', active: true, password: 'password')
end

it 'accepts "set" to set false to attribute' do
expect(subject.active).to be false
end

it 'should skip "prepend" if it equal to false' do
it 'skips "prepend" if it equal to false' do
expect(subject.password).to eq('password')
end
end
Expand All @@ -336,6 +348,8 @@
end

context 'inheritance extended' do
subject { stage.amoeba_dup }

let(:stage) do
stage = CustomStage.new(title: 'My Stage', external_id: 213)
stage.listeners.build(name: 'John')
Expand All @@ -346,19 +360,19 @@
stage
end

subject { stage.amoeba_dup }

it 'contains parent association and own associations', :aggregate_failures do
subject
expect { subject.save! }.to change(Listener, :count).by(2).
and change(Specialist, :count).by(1).
and change(CustomRule, :count).by(1)
expect { subject.save! }.to change(Listener, :count).by(2)
.and change(Specialist, :count).by(1)
.and change(
CustomRule, :count
).by(1)

expect(subject.title).to eq 'My Stage'
expect(subject.external_id).to be_nil
expect(subject.listeners.find_by(name: 'John')).to_not be_nil
expect(subject.listeners.find_by(name: 'Helen')).to_not be_nil
expect(subject.specialists.find_by(name: 'Jack')).to_not be_nil
expect(subject.listeners.find_by(name: 'John')).not_to be_nil
expect(subject.listeners.find_by(name: 'Helen')).not_to be_nil
expect(subject.specialists.find_by(name: 'Jack')).not_to be_nil
expect(subject.custom_rules.first.description).to eq 'Kill all humans'
end
end
Expand Down

0 comments on commit 2e7cb08

Please sign in to comment.