lib/rubocop/cop/rspec/file_path.rb in rubocop-rspec-1.39.0 vs lib/rubocop/cop/rspec/file_path.rb in rubocop-rspec-1.40.0
- old
+ new
@@ -1,22 +1,27 @@
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
- # Checks that spec file paths are consistent with the test subject.
+ # Checks that spec file paths are consistent and well-formed.
#
- # Checks the path of the spec file and enforces that it reflects the
- # described class/module and its optionally called out method.
+ # 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.
+ #
# @example
# # bad
# whatever_spec.rb # describe MyClass
#
# # bad
@@ -39,10 +44,20 @@
# my_class_spec.rb # describe MyClass
#
# # good
# my_class_spec.rb # describe MyClass, '#method'
#
+ # @example 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'
+ #
class FilePath < Cop
include RuboCop::RSpec::TopLevelDescribe
MSG = 'Spec path should end with `%<suffix>s`.'
@@ -68,13 +83,19 @@
def routing_spec?(args)
args.any?(&method(:routing_metadata?))
end
def glob_for((described_class, method_name))
+ return glob_for_spec_suffix_only? if spec_suffix_only?
+
"#{expected_path(described_class)}#{name_glob(method_name)}*_spec.rb"
end
+ def glob_for_spec_suffix_only?
+ '*_spec.rb'
+ end
+
def name_glob(name)
return unless name&.str_type?
"*#{name.str_content.gsub(/\W/, '')}" unless ignore_methods?
end
@@ -103,14 +124,19 @@
end
def filename_ends_with?(glob)
filename =
RuboCop::PathUtil.relative_path(processed_source.buffer.name)
+ .gsub('../', '')
File.fnmatch?("*#{glob}", filename)
end
def relevant_rubocop_rspec_file?(_file)
true
+ end
+
+ def spec_suffix_only?
+ cop_config['SpecSuffixOnly']
end
end
end
end
end