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

Inherits:
RuboCop::Cop show all
Includes:
RSpec::SpecOnly, RSpec::TopLevelDescribe
Defined in:
lib/rubocop/cop/rspec/file_path.rb

Overview

Checks that spec file paths are consistent with the test subject.

Checks the path of the spec file and enforces that it reflects the described class/module and its optionally called out method.

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’ ).

Examples:

my_class/method_spec.rb  # describe MyClass, '#method'
my_class_method_spec.rb  # describe MyClass, '#method'
my_class_spec.rb         # describe MyClass

Constant Summary collapse

MESSAGE =
'Spec path should end with `%s`'.freeze
METHOD_STRING_MATCHER =
/^[\#\.].+/
ROUTING_PAIR =
s(:pair, s(:sym, :type), s(:sym, :routing))

Constants included from RSpec::SpecOnly

RSpec::SpecOnly::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods included from RSpec::SpecOnly

#relevant_file?

Methods included from RSpec::TopLevelDescribe

#on_send

Instance Method Details

#on_top_level_describe(node, args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec/file_path.rb', line 26

def on_top_level_describe(node, args)
  return if routing_spec?(args)

  return unless single_top_level_describe?
  object = args.first.const_name
  return unless object

  path_matcher = matcher(object, args.at(1))
  return if source_filename =~ regexp_from_glob(path_matcher)

  add_offense(node, :expression, format(MESSAGE, path_matcher))
end