rubocop-airbnb 4.0.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/config/default.yml +9 -8
  4. data/config/rubocop-performance.yml +3 -0
  5. data/config/rubocop-rails.yml +3 -0
  6. data/config/rubocop-rspec.yml +4 -9
  7. data/lib/rubocop/airbnb/version.rb +1 -1
  8. data/lib/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file.rb +1 -1
  9. data/lib/rubocop/cop/airbnb/const_assigned_in_wrong_file.rb +1 -1
  10. data/lib/rubocop/cop/airbnb/continuation_slash.rb +1 -1
  11. data/lib/rubocop/cop/airbnb/default_scope.rb +1 -1
  12. data/lib/rubocop/cop/airbnb/factory_attr_references_class.rb +1 -1
  13. data/lib/rubocop/cop/airbnb/factory_class_use_string.rb +1 -1
  14. data/lib/rubocop/cop/airbnb/mass_assignment_accessible_modifier.rb +1 -1
  15. data/lib/rubocop/cop/airbnb/module_method_in_wrong_file.rb +1 -1
  16. data/lib/rubocop/cop/airbnb/no_timeout.rb +1 -1
  17. data/lib/rubocop/cop/airbnb/opt_arg_parameters.rb +1 -1
  18. data/lib/rubocop/cop/airbnb/phrase_bundle_keys.rb +1 -1
  19. data/lib/rubocop/cop/airbnb/risky_activerecord_invocation.rb +1 -1
  20. data/lib/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace.rb +1 -1
  21. data/lib/rubocop/cop/airbnb/rspec_environment_modification.rb +1 -1
  22. data/lib/rubocop/cop/airbnb/simple_modifier_conditional.rb +1 -1
  23. data/lib/rubocop/cop/airbnb/simple_unless.rb +1 -1
  24. data/lib/rubocop/cop/airbnb/spec_constant_assignment.rb +3 -4
  25. data/lib/rubocop/cop/airbnb/unsafe_yaml_marshal.rb +1 -1
  26. data/rubocop-airbnb.gemspec +3 -3
  27. data/spec/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file_spec.rb +69 -102
  28. data/spec/rubocop/cop/airbnb/const_assigned_in_wrong_file_spec.rb +58 -101
  29. data/spec/rubocop/cop/airbnb/continuation_slash_spec.rb +77 -112
  30. data/spec/rubocop/cop/airbnb/default_scope_spec.rb +24 -31
  31. data/spec/rubocop/cop/airbnb/factory_attr_references_class_spec.rb +81 -121
  32. data/spec/rubocop/cop/airbnb/factory_class_use_string_spec.rb +12 -20
  33. data/spec/rubocop/cop/airbnb/mass_assignment_accessible_modifier_spec.rb +17 -22
  34. data/spec/rubocop/cop/airbnb/module_method_in_wrong_file_spec.rb +75 -114
  35. data/spec/rubocop/cop/airbnb/no_timeout_spec.rb +16 -22
  36. data/spec/rubocop/cop/airbnb/opt_arg_parameter_spec.rb +46 -73
  37. data/spec/rubocop/cop/airbnb/phrase_bundle_keys_spec.rb +5 -20
  38. data/spec/rubocop/cop/airbnb/risky_activerecord_invocation_spec.rb +19 -33
  39. data/spec/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace_spec.rb +109 -187
  40. data/spec/rubocop/cop/airbnb/rspec_environment_modification_spec.rb +31 -41
  41. data/spec/rubocop/cop/airbnb/simple_modifier_conditional_spec.rb +64 -88
  42. data/spec/rubocop/cop/airbnb/simple_unless_spec.rb +17 -27
  43. data/spec/rubocop/cop/airbnb/spec_constant_assignment_spec.rb +42 -60
  44. data/spec/rubocop/cop/airbnb/unsafe_yaml_marshal_spec.rb +24 -36
  45. data/spec/spec_helper.rb +2 -0
  46. metadata +8 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d15fbaae4390350367ffaf63eb8983bfd6b3f5b20b204a84396b1e52f7f6509
4
- data.tar.gz: eb4b2cb7e6b3fb6a347c8c8aa1d3c189a5787d14b0d59db5a5993d4fe400a299
3
+ metadata.gz: badf32420d149c7d0f156905b025e12753c8d2138feca349ce157dd8f52acbb3
4
+ data.tar.gz: 7da82ffbd161c9e168091daeb0c9c0571c7a72eb33b635646f68ddeacf10a2e4
5
5
  SHA512:
6
- metadata.gz: bc207a5af59c1eecfeaba405a3a216fae0afa3b54288b35249de115b9f6c1d5036f9ae3f692823ee0a5a3af64718cd7aef072f0e6402c6c4b305b7f6f13bdd5f
7
- data.tar.gz: e99bd2c461379d8a13f20a880b2963b0d0241d8affa7028d67cff807573ffe9fe8053bd7d8bbe65174bf0d3021dd9c7a75c4456f9dd1c58e5fe340280554d5d0
6
+ metadata.gz: d7eb9157f6279bc33ab656247532e9332160c3a2934c00bbace9684e23397e17300d1346bb874dae2bed24c599aa3685a30166da9735f7adf5529c55122dcc4d
7
+ data.tar.gz: d975235537cee012fa313562ee517ca34b7143d37ff975dbd3e0ccc4ffb9a3794982ab2871fe477fdedf2ef62ac0cf061e5383d0cdea7dafdbfbdd33144d3772
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 6.0.0
2
+ * Recover code analysis using `TargetRubyVersion` from Ruby 2.0 to 2.4
3
+ * Drop support for Ruby 2.5
4
+ * Update rubocop to 1.32.0
5
+
6
+ # 5.0.0
7
+ * Add support for Ruby 3.1
8
+ * Drop support for Ruby 2.4
9
+ * Update rubocop to 1.22.0
10
+ * Update rubocop-rspec to 2.0.0
11
+ * Remove deprecated cops InvalidPredicateMatcher and CustomIncludeMethods
12
+
1
13
  # 4.0.0
2
14
  * Add support for Ruby 3.0
3
15
  * Run CI against Ruby 2.7
data/config/default.yml CHANGED
@@ -15,14 +15,6 @@ AllCops:
15
15
  - 'vendor/**/*'
16
16
  - Vagrantfile
17
17
  - Guardfile
18
- RSpec:
19
- Patterns:
20
- - _spec.rb
21
- - "(?:^|/)spec/"
22
- RSpec/FactoryBot:
23
- Patterns:
24
- - spec/factories/**/*.rb
25
- - features/support/factories/**/*.rb
26
18
 
27
19
  # While Rubocop has released a bunch of new cops, not all of these cops have been evaluated as
28
20
  # part of this styleguide. To prevent new, unevaluated cops from imposing on this styleguide, we
@@ -33,6 +25,15 @@ AllCops:
33
25
  # https://github.com/rubocop/rubocop/blob/1e55b1aa5e4c5eaeccad5d61f08b7930ed6bc341/config/default.yml#L89-L101
34
26
  DisabledByDefault: true
35
27
 
28
+ RSpec:
29
+ Include:
30
+ - _spec.rb
31
+ - "(?:^|/)spec/"
32
+ RSpec/FactoryBot:
33
+ Include:
34
+ - spec/factories/**/*.rb
35
+ - features/support/factories/**/*.rb
36
+
36
37
  inherit_from:
37
38
  - './rubocop-airbnb.yml'
38
39
  - './rubocop-bundler.yml'
@@ -1,3 +1,6 @@
1
+ require:
2
+ - rubocop-performance
3
+
1
4
  Performance/Caller:
2
5
  Enabled: false
3
6
 
@@ -1,3 +1,6 @@
1
+ require:
2
+ - rubocop-rails
3
+
1
4
  # before_action doesn't seem to exist, so this doesn't make sense.
2
5
  Rails/ActionFilter:
3
6
  Enabled: false
@@ -56,7 +56,6 @@ RSpec/DescribedClass:
56
56
  RSpec/EmptyExampleGroup:
57
57
  Description: Checks if an example group does not include any tests.
58
58
  Enabled: true
59
- CustomIncludeMethods: []
60
59
 
61
60
  RSpec/EmptyLineAfterExampleGroup:
62
61
  Description: Checks if there is an empty line after example group blocks.
@@ -155,10 +154,6 @@ RSpec/InstanceVariable:
155
154
  Description: 'Checks for the usage of instance variables.'
156
155
  Enabled: false
157
156
 
158
- RSpec/InvalidPredicateMatcher:
159
- Description: Checks invalid usage for predicate matcher.
160
- Enabled: false
161
-
162
157
  RSpec/ItBehavesLike:
163
158
  Description: Checks that only one `it_behaves_like` style is used.
164
159
  Enabled: false
@@ -314,18 +309,18 @@ RSpec/VoidExpect:
314
309
  Description: This cop checks void `expect()`.
315
310
  Enabled: false
316
311
 
317
- Capybara/CurrentPathExpectation:
312
+ RSpec/Capybara/CurrentPathExpectation:
318
313
  Description: Checks that no expectations are set on Capybara's `current_path`.
319
314
  Enabled: false
320
315
 
321
- Capybara/FeatureMethods:
316
+ RSpec/Capybara/FeatureMethods:
322
317
  Description: Checks for consistent method usage in feature specs.
323
318
  Enabled: false
324
319
 
325
- FactoryBot/AttributeDefinedStatically:
320
+ RSpec/FactoryBot/AttributeDefinedStatically:
326
321
  Description: Always declare attribute values as blocks.
327
322
  Enabled: false
328
323
 
329
- FactoryBot/CreateList:
324
+ RSpec/FactoryBot/CreateList:
330
325
  Description: Checks for create_list usage.
331
326
  Enabled: true
@@ -3,6 +3,6 @@
3
3
  module RuboCop
4
4
  module Airbnb
5
5
  # Version information for the the Airbnb RuboCop plugin.
6
- VERSION = '4.0.0'
6
+ VERSION = '6.0.0'
7
7
  end
8
8
  end
@@ -56,7 +56,7 @@ module RuboCop
56
56
  # class Bar # nested class
57
57
  # end
58
58
  # end
59
- class ClassOrModuleDeclaredInWrongFile < Cop
59
+ class ClassOrModuleDeclaredInWrongFile < Base
60
60
  include Inflections
61
61
  include RailsAutoloading
62
62
 
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # module Foo
23
23
  # BAZ = 42
24
24
  # end
25
- class ConstAssignedInWrongFile < Cop
25
+ class ConstAssignedInWrongFile < Base
26
26
  include Inflections
27
27
  include RailsAutoloading
28
28
 
@@ -1,7 +1,7 @@
1
1
  module RuboCop
2
2
  module Cop
3
3
  module Airbnb
4
- class ContinuationSlash < Cop
4
+ class ContinuationSlash < Base
5
5
  MSG = 'Slash continuation should be reserved for closed string continuation. ' \
6
6
  'Many times it is used to get around other existing rules.'.freeze
7
7
 
@@ -3,7 +3,7 @@ module RuboCop
3
3
  module Airbnb
4
4
  # Cop to help prevent the scorge of Default Scopes from ActiveRecord.
5
5
  # Once in place they are almost impossible to remove.
6
- class DefaultScope < Cop
6
+ class DefaultScope < Base
7
7
  MSG = 'Avoid `default_scope`. Default scopes make it difficult to '\
8
8
  'refactor data access patterns since the scope becomes part '\
9
9
  'of every query unless explicitly excluded, even when it is '\
@@ -4,7 +4,7 @@ module RuboCop
4
4
  # Cop to enforce "attr { CONST }" instead of "attr CONST" in factories,
5
5
  # because the latter forces autoload, which slows down spec startup time and
6
6
  # Zeus reload time after touching a model.
7
- class FactoryAttrReferencesClass < Cop
7
+ class FactoryAttrReferencesClass < Base
8
8
  MSG = "Instead of attr_name MyClass::MY_CONST, use attr_name { MyClass::MY_CONST }. " \
9
9
  "This enables faster spec startup time and Zeus reload time.".freeze
10
10
 
@@ -3,7 +3,7 @@ module RuboCop
3
3
  module Airbnb
4
4
  # Cop to tell developers to use :class => "MyClass" instead of :class => MyClass,
5
5
  # because the latter slows down reloading zeus.
6
- class FactoryClassUseString < Cop
6
+ class FactoryClassUseString < Base
7
7
  MSG = 'Instead of :class => MyClass, use :class => "MyClass". ' \
8
8
  "This enables faster spec startup time and faster Zeus reload time.".freeze
9
9
 
@@ -3,7 +3,7 @@ module RuboCop
3
3
  module Airbnb
4
4
  # Modifying Mass assignment restrictions eliminates the entire point of disabling
5
5
  # mass assignment. It's a lazy, potentially dangerous approach that should be discouraged.
6
- class MassAssignmentAccessibleModifier < Cop
6
+ class MassAssignmentAccessibleModifier < Base
7
7
  MSG = 'Do no override and objects mass assignment restrictions.'.freeze
8
8
 
9
9
  def on_send(node)
@@ -59,7 +59,7 @@ module RuboCop
59
59
  # end
60
60
  # end
61
61
  # end
62
- class ModuleMethodInWrongFile < Cop
62
+ class ModuleMethodInWrongFile < Base
63
63
  include Inflections
64
64
  include RailsAutoloading
65
65
 
@@ -1,7 +1,7 @@
1
1
  module RuboCop
2
2
  module Cop
3
3
  module Airbnb
4
- class NoTimeout < Cop
4
+ class NoTimeout < Base
5
5
  MSG =
6
6
  'Do not use Timeout.timeout. In combination with Rails autoloading, ' \
7
7
  'timeout can cause Segmentation Faults in version of Ruby we use. ' \
@@ -3,7 +3,7 @@ module RuboCop
3
3
  module Airbnb
4
4
  # Cop to enforce use of options hash over default arguments
5
5
  # https://github.com/airbnb/ruby#no-default-args
6
- class OptArgParameters < Cop
6
+ class OptArgParameters < Base
7
7
  MSG =
8
8
  'Do not use default positional arguments. '\
9
9
  'Use keyword arguments or an options hash instead.'.freeze
@@ -24,7 +24,7 @@ module RuboCop
24
24
  # ),
25
25
  # }
26
26
  # end
27
- class PhraseBundleKeys < Cop
27
+ class PhraseBundleKeys < Base
28
28
  MESSAGE =
29
29
  'Phrase bundle keys should match their translation keys.'.freeze
30
30
 
@@ -2,7 +2,7 @@ module RuboCop
2
2
  module Cop
3
3
  module Airbnb
4
4
  # Disallow ActiveRecord calls that pass interpolated or added strings as an argument.
5
- class RiskyActiverecordInvocation < Cop
5
+ class RiskyActiverecordInvocation < Base
6
6
  VULNERABLE_AR_METHODS = [
7
7
  :delete_all,
8
8
  :destroy_all,
@@ -20,7 +20,7 @@ module RuboCop
20
20
  #
21
21
  # describe Foo::Bar
22
22
  # end
23
- class RspecDescribeOrContextUnderNamespace < Cop
23
+ class RspecDescribeOrContextUnderNamespace < Base
24
24
  DESCRIBE_OR_CONTEXT_UNDER_NAMESPACE_MSG =
25
25
  'Declaring a `module` in a spec can break autoloading because subsequent references ' \
26
26
  'to it will not cause it to be loaded from the app. This could cause flaky tests.'.freeze
@@ -28,7 +28,7 @@ module RuboCop
28
28
  # before(:each) do
29
29
  # stub_env(:production)
30
30
  # end
31
- class RspecEnvironmentModification < Cop
31
+ class RspecEnvironmentModification < Base
32
32
  def_node_matcher :allow_or_expect_rails_env, <<-PATTERN
33
33
  (send (send nil? {:expect :allow} (send (const nil? :Rails) :env)) :to ...)
34
34
  PATTERN
@@ -3,7 +3,7 @@ module RuboCop
3
3
  module Airbnb
4
4
  # Cop to tackle prevent more complicated modifier if/unless statements
5
5
  # https://github.com/airbnb/ruby#only-simple-if-unless
6
- class SimpleModifierConditional < Cop
6
+ class SimpleModifierConditional < Base
7
7
  MSG = 'Modifier if/unless usage is okay when the body is simple, ' \
8
8
  'the condition is simple, and the whole thing fits on one line. ' \
9
9
  'Otherwise, avoid modifier if/unless.'.freeze
@@ -3,7 +3,7 @@ module RuboCop
3
3
  module Airbnb
4
4
  # Cop to tackle prevent unless statements with multiple conditions
5
5
  # https://github.com/airbnb/ruby#unless-with-multiple-conditions
6
- class SimpleUnless < Cop
6
+ class SimpleUnless < Base
7
7
  MSG = 'Unless usage is okay when there is only one conditional'.freeze
8
8
 
9
9
  def_node_matcher :multiple_conditionals?, '(if ({and or :^} ...) ...)'
@@ -25,16 +25,15 @@ module RuboCop
25
25
  # describe Something do
26
26
  # before { stub_const('MyClass::PAYLOAD', [1, 2, 3])
27
27
  # end
28
- class SpecConstantAssignment < Cop
29
- include RuboCop::RSpec::TopLevelDescribe
28
+ class SpecConstantAssignment < Base
30
29
  MESSAGE = "Defining constants inside of specs can cause spurious behavior. " \
31
- "It is almost always preferable to use `let` statements, "\
30
+ "It is almost always preferable to use `let` statements, " \
32
31
  "anonymous class/module definitions, or stub_const".freeze
33
32
 
34
33
  def on_casgn(node)
35
34
  return unless in_spec_file?(node)
36
35
  parent_module_name = node.parent_module_name
37
- if node.parent_module_name && parent_module_name != 'Object'
36
+ if parent_module_name && parent_module_name != "Object"
38
37
  return
39
38
  end
40
39
  add_offense(node, message: MESSAGE)
@@ -2,7 +2,7 @@ module RuboCop
2
2
  module Cop
3
3
  module Airbnb
4
4
  # Disallow use of YAML/Marshal methods that can trigger RCE on untrusted input
5
- class UnsafeYamlMarshal < Cop
5
+ class UnsafeYamlMarshal < Base
6
6
  MSG = 'Using unsafe YAML parsing methods on untrusted input can lead ' \
7
7
  'to remote code execution. Use `safe_load`, `parse`, `parse_file`, or ' \
8
8
  '`parse_stream` instead'.freeze
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
15
15
  spec.license = 'MIT'
16
16
  spec.version = RuboCop::Airbnb::VERSION
17
17
  spec.platform = Gem::Platform::RUBY
18
- spec.required_ruby_version = '>= 2.4'
18
+ spec.required_ruby_version = '>= 2.5'
19
19
 
20
20
  spec.require_paths = ['lib']
21
21
  spec.files = Dir[
@@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
25
25
  'Gemfile',
26
26
  ]
27
27
 
28
- spec.add_dependency('rubocop', '~> 0.93.1')
28
+ spec.add_dependency('rubocop', '~> 1.32.0')
29
29
  spec.add_dependency('rubocop-performance', '~> 1.10.2')
30
30
  spec.add_dependency('rubocop-rails', '~> 2.9.1')
31
- spec.add_dependency('rubocop-rspec', '~> 1.44.1')
31
+ spec.add_dependency('rubocop-rspec', '~> 2.0.0')
32
32
  spec.add_development_dependency('rspec', '~> 3.5')
33
33
  end
@@ -1,6 +1,4 @@
1
- describe RuboCop::Cop::Airbnb::ClassOrModuleDeclaredInWrongFile do
2
- subject(:cop) { described_class.new(config) }
3
-
1
+ describe RuboCop::Cop::Airbnb::ClassOrModuleDeclaredInWrongFile, :config do
4
2
  let(:config) do
5
3
  RuboCop::Config.new(
6
4
  {
@@ -25,150 +23,119 @@ describe RuboCop::Cop::Airbnb::ClassOrModuleDeclaredInWrongFile do
25
23
  end
26
24
 
27
25
  it 'rejects if class declaration is in a file with non-matching name' do
28
- source = [
29
- 'module Foo',
30
- ' module Bar',
31
- ' class Baz',
32
- ' end',
33
- ' end',
34
- 'end',
35
- ].join("\n")
36
-
37
26
  File.open "#{models_dir}/qux.rb", "w" do |file|
38
- inspect_source(source, file)
27
+ expect_offense(<<~RUBY, file)
28
+ module Foo
29
+ ^^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class/module, move its definition to a file that matches its name. Module Foo should be defined in foo.rb.
30
+ module Bar
31
+ ^^^^^^^^^^ In order for Rails autoloading [...]
32
+ class Baz
33
+ ^^^^^^^^^ In order for Rails autoloading [...]
34
+ end
35
+ end
36
+ end
37
+ RUBY
39
38
  end
40
-
41
- expect(cop.offenses.size).to eq(3)
42
- expect(cop.offenses.map(&:line).sort).to eq([1, 2, 3])
43
- expect(cop.offenses.first.message).to include('Module Foo should be defined in foo.rb.')
44
39
  end
45
40
 
46
41
  it 'rejects if class declaration is in a file with matching name but wrong parent dir' do
47
- source = [
48
- 'module Foo',
49
- ' module Bar',
50
- ' class Baz',
51
- ' end',
52
- ' end',
53
- 'end',
54
- ].join("\n")
55
-
56
42
  File.open "#{models_dir}/baz.rb", "w" do |file|
57
- inspect_source(source, file)
43
+ expect_offense(<<~RUBY, file)
44
+ module Foo
45
+ ^^^^^^^^^^ In order for Rails autoloading [...]
46
+ module Bar
47
+ ^^^^^^^^^^ In order for Rails autoloading [...]
48
+ class Baz
49
+ ^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class/module, move its definition to a file that matches its name. Class Baz should be defined in foo/bar/baz.rb.
50
+ end
51
+ end
52
+ end
53
+ RUBY
58
54
  end
59
-
60
- expect(cop.offenses.size).to eq(3)
61
- expect(cop.offenses.map(&:line).sort).to eq([1, 2, 3])
62
- expect(cop.offenses.last.message).to include('Class Baz should be defined in foo/bar/baz.rb.')
63
55
  end
64
56
 
65
57
  it 'accepts if class declaration is in a file with matching name and right parent dir' do
66
- source = [
67
- 'module Foo',
68
- ' module Bar',
69
- ' class Baz',
70
- ' end',
71
- ' end',
72
- 'end',
73
- ].join("\n")
74
-
75
58
  FileUtils.mkdir_p "#{models_dir}/foo/bar"
76
59
  File.open "#{models_dir}/foo/bar/baz.rb", "w" do |file|
77
- inspect_source(source, file)
60
+ expect_no_offenses(<<~RUBY, file)
61
+ module Foo
62
+ module Bar
63
+ class Baz
64
+ end
65
+ end
66
+ end
67
+ RUBY
78
68
  end
79
-
80
- expect(cop.offenses).to be_empty
81
69
  end
82
70
 
83
71
  it 'rejects if class declaration is in wrong dir and parent module uses ::' do
84
- source = [
85
- 'module Foo::Bar',
86
- ' class Baz',
87
- ' end',
88
- 'end',
89
- ].join("\n")
90
-
91
72
  FileUtils.mkdir_p "#{models_dir}/bar"
92
73
  File.open "#{models_dir}/bar/baz.rb", "w" do |file|
93
- inspect_source(source, file)
74
+ expect_offense(<<~RUBY, file)
75
+ module Foo::Bar
76
+ ^^^^^^^^^^^^^^^ In order for Rails autoloading [...]
77
+ class Baz
78
+ ^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class/module, move its definition to a file that matches its name. Class Baz should be defined in foo/bar/baz.rb.
79
+ end
80
+ end
81
+ RUBY
94
82
  end
95
-
96
- expect(cop.offenses.map(&:line).sort).to eq([1, 2])
97
- expect(cop.offenses.last.message).to include('Class Baz should be defined in foo/bar/baz.rb.')
98
83
  end
99
84
 
100
85
  it 'accepts if class declaration is in a file with matching name and parent module uses ::' do
101
- source = [
102
- 'module Foo::Bar',
103
- ' class Baz',
104
- ' end',
105
- 'end',
106
- ].join("\n")
107
-
108
86
  FileUtils.mkdir_p "#{models_dir}/foo/bar"
109
87
  File.open "#{models_dir}/foo/bar/baz.rb", "w" do |file|
110
- inspect_source(source, file)
88
+ expect_no_offenses(<<~RUBY, file)
89
+ module Foo::Bar
90
+ class Baz
91
+ end
92
+ end
93
+ RUBY
111
94
  end
112
-
113
- expect(cop.offenses).to be_empty
114
95
  end
115
96
 
116
97
  it 'accepts class declaration where the containing class uses an acronym' do
117
- source = [
118
- 'module CSVFoo',
119
- ' class Baz',
120
- ' end',
121
- 'end',
122
- ].join("\n")
123
-
124
98
  FileUtils.mkdir_p "#{models_dir}/csv_foo"
125
99
  File.open "#{models_dir}/csv_foo/baz.rb", "w" do |file|
126
- inspect_source(source, file)
100
+ expect_no_offenses(<<~RUBY)
101
+ module CSVFoo
102
+ class Baz
103
+ end
104
+ end
105
+ RUBY
127
106
  end
128
-
129
- expect(cop.offenses).to be_empty
130
107
  end
131
108
 
132
109
  it 'ignores class/module declaration in a rake task' do
133
- source = [
134
- 'class Baz',
135
- 'end',
136
- ].join("\n")
137
-
138
110
  File.open "#{models_dir}/foo.rake", "w" do |file|
139
- inspect_source(source, file)
111
+ expect_no_offenses(<<~RUBY, file)
112
+ class Baz
113
+ end
114
+ RUBY
140
115
  end
141
-
142
- expect(cop.offenses).to be_empty
143
116
  end
144
117
 
145
118
  it 'suggests moving error classes into the file that defines the owning scope' do
146
- source = [
147
- 'module Foo',
148
- ' class BarError < StandardError; end',
149
- 'end',
150
- ].join("\n")
151
-
152
119
  File.open "#{models_dir}/bar.rb", "w" do |file|
153
- inspect_source(source, file)
120
+ expect_offense(<<~RUBY, file)
121
+ module Foo
122
+ ^^^^^^^^^^ In order for Rails autoloading [...]
123
+ class BarError < StandardError; end
124
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class, move its definition to a file that defines the owning module. Class BarError should be defined in foo.rb.
125
+ end
126
+ RUBY
154
127
  end
155
-
156
- expect(cop.offenses.map(&:line)).to include(2)
157
- expect(cop.offenses.map(&:message)).to include(%r{Class BarError should be defined in foo\.rb.})
158
128
  end
159
129
 
160
130
  it 'recognizes error class based on the superclass name' do
161
- source = [
162
- 'module Foo',
163
- ' class Bar < StandardError; end',
164
- 'end',
165
- ].join("\n")
166
-
167
131
  File.open "#{models_dir}/bar.rb", "w" do |file|
168
- inspect_source(source, file)
132
+ expect_offense(<<~RUBY, file)
133
+ module Foo
134
+ ^^^^^^^^^^ In order for Rails autoloading [...]
135
+ class Bar < StandardError; end
136
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class, move its definition to a file that defines the owning module. Class Bar should be defined in foo.rb.
137
+ end
138
+ RUBY
169
139
  end
170
-
171
- expect(cop.offenses.map(&:line)).to include(2)
172
- expect(cop.offenses.map(&:message)).to include(%r{Class Bar should be defined in foo\.rb.})
173
140
  end
174
141
  end