Skip to content

Commit

Permalink
Make RSpec/FilePath support ActiveSupport inflections, if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromedalbert authored and pirj committed May 2, 2022
1 parent eb0add8 commit a60261d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/rubocop/cop/rspec/file_path.rb
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions rubocop-rspec.gemspec
Expand Up @@ -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'
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/rspec/file_path_spec.rb
Expand Up @@ -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 } }

Expand Down

0 comments on commit a60261d

Please sign in to comment.