Skip to content

Commit 23cf2f3

Browse files
jeromedalbertpirj
authored andcommitted
Make RSpec/FilePath support ActiveSupport inflections, if defined
1 parent eb0add8 commit 23cf2f3

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Drop Ruby 2.5 support. ([@ydah][])
66
* Add new `RSpec/ChangeByZero` cop. ([@ydah][])
7+
* Make `RSpec/FilePath` support ActiveSupport inflections, if defined. ([@jeromedalbert][])
78

89
## 2.10.0 (2022-04-19)
910

@@ -684,3 +685,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
684685
[@oshiro3]: https://github.com/oshiro3
685686
[@ydah]: https://github.com/ydah
686687
[@t3h2mas]: https://github.com/t3h2mas
688+
[@jeromedalbert]: https://github.com/jeromedalbert

lib/rubocop/cop/rspec/file_path.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ def expected_path(constant)
130130
end
131131

132132
def camel_to_snake_case(string)
133+
if defined?(ActiveSupport::Inflector)
134+
if File.exist?('./config/initializers/inflections.rb')
135+
require './config/initializers/inflections'
136+
end
137+
return ActiveSupport::Inflector.underscore(string)
138+
end
139+
133140
string
134141
.gsub(/([^A-Z])([A-Z]+)/, '\1_\2')
135142
.gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2')

rubocop-rspec.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
3939

4040
spec.add_runtime_dependency 'rubocop', '~> 1.19'
4141

42+
spec.add_development_dependency 'activesupport'
4243
spec.add_development_dependency 'rack'
4344
spec.add_development_dependency 'rake'
4445
spec.add_development_dependency 'rspec', '>= 3.4'

spec/rubocop/cop/rspec/file_path_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,27 @@ class Foo
246246
end
247247
end
248248

249+
context 'when ActiveSupport Inflector is defined' do
250+
before { require 'active_support/inflector' }
251+
252+
it 'registers an offense for a bad path when there is no custom acronym' do
253+
expect_offense(<<-RUBY, 'pvp_class_foo_spec.rb')
254+
describe PvPClass, 'foo' do; end
255+
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `pv_p_class*foo*_spec.rb`.
256+
RUBY
257+
end
258+
259+
it 'does not register an offense when class name contains custom acronym' do
260+
ActiveSupport::Inflector.inflections do |inflect|
261+
inflect.acronym('PvP')
262+
end
263+
264+
expect_no_offenses(<<-RUBY, 'pvp_class_foo_spec.rb')
265+
describe PvPClass, 'foo' do; end
266+
RUBY
267+
end
268+
end
269+
249270
context 'when configured with IgnoreMethods' do
250271
let(:cop_config) { { 'IgnoreMethods' => true } }
251272

0 commit comments

Comments
 (0)