33module RuboCop
44 module Cop
55 module RSpec
6- # Checks that spec file paths are consistent with the test subject .
6+ # Checks that spec file paths are consistent and well-formed .
77 #
8- # Checks the path of the spec file and enforces that it reflects the
9- # described class/module and its optionally called out method.
8+ # By default, this checks that spec file paths are consistent with the
9+ # test subject and and enforces that it reflects the described
10+ # class/module and its optionally called out method.
1011 #
1112 # With the configuration option `IgnoreMethods` the called out method will
1213 # be ignored when determining the enforced path.
@@ -15,6 +16,10 @@ module RSpec
1516 # be specified that should not as usual be transformed from CamelCase to
1617 # snake_case (e.g. 'RuboCop' => 'rubocop' ).
1718 #
19+ # With the configuration option `SpecSuffixOnly` test files will only
20+ # be checked to ensure they end in '_spec.rb'. This option disables
21+ # checking for consistency in the test subject or test methods.
22+ #
1823 # @example
1924 # # bad
2025 # whatever_spec.rb # describe MyClass
@@ -41,6 +46,16 @@ module RSpec
4146 # # good
4247 # my_class_spec.rb # describe MyClass, '#method'
4348 #
49+ # @example when configuration is `SpecSuffixOnly: true`
50+ # # good
51+ # whatever_spec.rb # describe MyClass
52+ #
53+ # # good
54+ # my_class_spec.rb # describe MyClass
55+ #
56+ # # good
57+ # my_class_spec.rb # describe MyClass, '#method'
58+ #
4459 class FilePath < Cop
4560 include RuboCop ::RSpec ::TopLevelDescribe
4661
@@ -70,9 +85,15 @@ def routing_spec?(args)
7085 end
7186
7287 def glob_for ( ( described_class , method_name ) )
88+ return glob_for_spec_suffix_only? if spec_suffix_only?
89+
7390 "#{ expected_path ( described_class ) } #{ name_glob ( method_name ) } *_spec.rb"
7491 end
7592
93+ def glob_for_spec_suffix_only?
94+ '*_spec.rb'
95+ end
96+
7697 def name_glob ( name )
7798 return unless name &.str_type?
7899
@@ -111,6 +132,10 @@ def filename_ends_with?(glob)
111132 def relevant_rubocop_rspec_file? ( _file )
112133 true
113134 end
135+
136+ def spec_suffix_only?
137+ cop_config [ 'SpecSuffixOnly' ]
138+ end
114139 end
115140 end
116141 end
0 commit comments