diff --git a/CHANGELOG.md b/CHANGELOG.md index 50f70697e..8a55546e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Drop Ruby 2.5 support. ([@ydah][]) * Add new `RSpec/ChangeByZero` cop. ([@ydah][]) +* Make `RSpec/FilePath` support ActiveSupport inflections, if defined. ([@jeromedalbert][]) ## 2.10.0 (2022-04-19) @@ -684,3 +685,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@oshiro3]: https://github.com/oshiro3 [@ydah]: https://github.com/ydah [@t3h2mas]: https://github.com/t3h2mas +[@jeromedalbert]: https://github.com/jeromedalbert diff --git a/lib/rubocop/cop/rspec/file_path.rb b/lib/rubocop/cop/rspec/file_path.rb index c2ab8f584..4a5848ea0 100644 --- a/lib/rubocop/cop/rspec/file_path.rb +++ b/lib/rubocop/cop/rspec/file_path.rb @@ -130,6 +130,13 @@ def expected_path(constant) end def camel_to_snake_case(string) + if defined?(ActiveSupport::Inflector) + if File.exist?('./config/initializers/inflections.rb') + require './config/initializers/inflections' + end + return ActiveSupport::Inflector.underscore(string) + end + string .gsub(/([^A-Z])([A-Z]+)/, '\1_\2') .gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2') diff --git a/rubocop-rspec.gemspec b/rubocop-rspec.gemspec index 174819130..836dfbd95 100644 --- a/rubocop-rspec.gemspec +++ b/rubocop-rspec.gemspec @@ -39,6 +39,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'rubocop', '~> 1.19' + spec.add_development_dependency 'activesupport' spec.add_development_dependency 'rack' spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec', '>= 3.4' diff --git a/spec/rubocop/cop/rspec/file_path_spec.rb b/spec/rubocop/cop/rspec/file_path_spec.rb index 68dfeb2e4..e188206d5 100644 --- a/spec/rubocop/cop/rspec/file_path_spec.rb +++ b/spec/rubocop/cop/rspec/file_path_spec.rb @@ -246,6 +246,27 @@ class Foo end end + context 'when ActiveSupport Inflector is defined', order: :defined do + before { require 'active_support/inflector' } + + it 'registers an offense for a bad path when there is no custom acronym' do + expect_offense(<<-RUBY, 'pvp_class_foo_spec.rb') + describe PvPClass, 'foo' do; end + ^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `pv_p_class*foo*_spec.rb`. + RUBY + end + + it 'does not register an offense when class name contains custom acronym' do + ActiveSupport::Inflector.inflections do |inflect| + inflect.acronym('PvP') + end + + expect_no_offenses(<<-RUBY, 'pvp_class_foo_spec.rb') + describe PvPClass, 'foo' do; end + RUBY + end + end + context 'when configured with IgnoreMethods' do let(:cop_config) { { 'IgnoreMethods' => true } }