Class: RuboCop::Cop::RSpec::SpecFilePathFormat

Inherits:
Base
  • Object
show all
Includes:
FileHelp, Namespace, TopLevelGroup
Defined in:
lib/rubocop/cop/rspec/spec_file_path_format.rb

Overview

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

Examples:

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

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

‘CustomTransform: RSpec=>rspec` (default)

# good
rubocop_spec.rb          # describe RuboCop
rspec_spec.rb            # describe RSpec

‘IgnoreMethods: false` (default)

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

‘IgnoreMethods: true`

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

‘IgnoreMetadata: type=>routing` (default)

# good
whatever_spec.rb         # describe MyClass, type: :routing do; end

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from FileHelp

#expanded_file_path

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_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

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

Instance Method Details

#example_group_arguments(node) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/rspec/spec_file_path_format.rb', line 43

def_node_matcher :example_group_arguments, <<~PATTERN
  (block $(send #rspec? #ExampleGroups.all $_ $...) ...)
PATTERN

#metadata_key_value(node) ⇒ Object



48
# File 'lib/rubocop/cop/rspec/spec_file_path_format.rb', line 48

def_node_search :metadata_key_value, '(pair (sym $_key) (sym $_value))'

#on_top_level_example_group(node) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/rspec/spec_file_path_format.rb', line 50

def on_top_level_example_group(node)
  return unless top_level_groups.one?

  example_group_arguments(node) do |send_node, class_name, arguments|
    next if !class_name.const_type? || ignore_metadata?(arguments)

    ensure_correct_file_path(send_node, class_name, arguments)
  end
end