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

Inherits:
Cop
  • Object
show all
Includes:
RSpec::TopLevelDescribe
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`.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods included from RSpec::TopLevelDescribe

#on_send

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_top_level_describe(node, args) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/rspec/file_path.rb', line 67

def on_top_level_describe(node, args)
  return unless const_described?(node) && single_top_level_describe?
  return if routing_spec?(args)

  glob = glob_for(args)

  return if filename_ends_with?(glob)

  add_offense(
    node,
    message: format(MSG, suffix: glob)
  )
end