Class: RuboCop::Cop::RSpec::FilePath

Inherits:
Base
  • Object
show all
Includes:
TopLevelGroup
Defined in:
lib/rubocop/cop/rspec/file_path.rb

Overview

Checks that spec file paths are consistent and well-formed.

By default, this checks that spec file paths are consistent with the test subject and and enforces that it reflects the described class/module and its optionally called out method.

With the configuration option `IgnoreMethods` the called out method will be ignored when determining the enforced path.

With the configuration option `CustomTransform` modules or classes can be specified that should not as usual be transformed from CamelCase to snake_case (e.g. 'RuboCop' => 'rubocop' ).

With the configuration option `SpecSuffixOnly` test files will only be checked to ensure they end in '_spec.rb'. This option disables checking for consistency in the test subject or test methods.

Examples:

# bad
whatever_spec.rb         # describe MyClass

# bad
my_class_spec.rb         # describe MyClass, '#method'

# good
my_class_spec.rb         # describe MyClass

# good
my_class_method_spec.rb  # describe MyClass, '#method'

# good
my_class/method_spec.rb  # describe MyClass, '#method'

when configuration is `IgnoreMethods: true`

# bad
whatever_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass, '#method'

when configuration is `SpecSuffixOnly: true`

# good
whatever_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass, '#method'

Constant Summary collapse

MSG =
'Spec path should end with `%<suffix>s`.'

Instance Method Summary collapse

Methods included from TopLevelGroup

#on_new_investigation, #top_level_groups

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#example_group(node) ⇒ Object


65
66
67
68
69
# File 'lib/rubocop/cop/rspec/file_path.rb', line 65

def_node_matcher :example_group, <<~PATTERN
  (block
    $(send #rspec? _example_group $_ $...) ...
  )
PATTERN

#on_top_level_example_group(node) ⇒ Object


74
75
76
77
78
79
80
81
82
# File 'lib/rubocop/cop/rspec/file_path.rb', line 74

def on_top_level_example_group(node)
  return unless top_level_groups.one?

  example_group(node) do |send_node, example_group, arguments|
    next if routing_spec?(arguments)

    ensure_correct_file_path(send_node, example_group, arguments)
  end
end

#routing_metadata?(node) ⇒ Object


72
# File 'lib/rubocop/cop/rspec/file_path.rb', line 72

def_node_search :routing_metadata?, '(pair (sym :type) (sym :routing))'