-
-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make RSpec/FilePath
support ActiveSupport inflections, if defined
#1266
Make RSpec/FilePath
support ActiveSupport inflections, if defined
#1266
Conversation
if File.exist?('./config/initializers/inflections.rb') | ||
require './config/initializers/inflections' | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inflection configuration must be loaded before ActiveSupport::Inflector.underscore
is executed, to take acronyms and other inflection rules into account.
That configuration is typically found in the config/initializers/inflections.rb
file that is generated when creating a Rails app.
Nice! |
cfb46fe
to
1dd8f53
Compare
@pirj Done |
a6210f7
to
9bb2204
Compare
RSpec/FilePath
support ActiveSupport inflections, if defined
9bb2204
to
57d1e78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
57d1e78
to
23cf2f3
Compare
The failing example is flaky, due to order dependency. If the example that define the "PvP" acronym runs first, that the other one fails. context 'when ActiveSupport Inflector is defined', order: :defined do |
23cf2f3
to
a60261d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit on the fence about adding Rails specific logic into RuboCop-RSpec, so not giving a 👍🏼 right away. I’ll think about it some more.
if defined?(ActiveSupport::Inflector) | ||
if File.exist?('./config/initializers/inflections.rb') | ||
require './config/initializers/inflections' | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m afraid this code is a bit slow. Could we do the defined?
/File.exists?
/require
thing just once and store the result perhaps a class variable?
if File.exist?('./config/initializers/inflections.rb') | ||
require './config/initializers/inflections' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this file path should be configurable.
ActiveSupport::Inflector.inflections do |inflect| | ||
inflect.acronym('PvP') | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we couldn’t do
ensure
ActiveSupport::Inflector.inflections.clear(:acronyms)
instead of having the order :defined
on the context block.
Any update on your thoughts? My guess is that Rails projects that use inflections are either probably just disabling this cop (my project does), so this PR is an attempt to fix that, and make the cop useful again. I have seen many gems whose goal is to work without Rails, but still provide a |
Apologies for the silence on my end. I do understand the use case, and it would be a neat addition for Rails projects, not having to configure rubocop-rspec with static map of inflections that the Rails application already knows how to generate. My problem is with putting Rails-specific logic into this gem. One might argue that we should then also put logic for other frameworks that might have similar inflection logic – an approach that wouldn’t scale. It would be neat if we could configure this cop with an “inflector block”, i.e. |
@bquorning I was a secret admirer/dreamer of a plain Ruby RuboCop configuration, especially after our DSL configuration extension and some later problems with merging arrays in YAML. Did you have something like: RuboCop::Cop::RSpec::FilePath.add_custom_transform { |string| ActiveSupport::Inflector.underscore(string) } in mind? I'm all for it. 😈 cc @Darhazer |
@@ -130,6 +130,13 @@ def expected_path(constant) | |||
end | |||
|
|||
def camel_to_snake_case(string) | |||
if defined?(ActiveSupport::Inflector) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other libraries that can be used to set inflectors:
I would suggest providing a script field in the YAML files that could be run such that String#underscore
could be defined if desired.
Rspec/FilePath:
LoadInfector: |-
require 'active_support/inflector'
require './config/initializers/inflections'
Alternatively, we could add a field for the Inflector gem and/or definition file:
Rspec/FilePath:
UseInflectorGem: ActiveSupport
InflectorDefinition: ./config/initializers/inflections.rb
Doing either of these should then allow if string.respond_to?(:underscore)
to be used instead of hard-coding a library and a file location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another alternative may simply be to make sure the file name matches the locations in which the class is defined, regardless of inflection.
Example:
app/services/my_html_module/xml_reader.rbmodule MyHTMLModule
class XMLReader
RSpec.describe MyHTMLModule::XMLReader do
end
This could sidestep the need for inflection.
This PR is stale, closing it. If someone wants to take a stab at it, feel free to open a new PR. |
Fixes #740. Implementation is based on the suggestion in this comment: #740 (comment).
This is especially useful for Rails applications (or any application using ActiveSupport inflections) that define mixed-case custom acronyms, for example:
It is also great to be re-using ActiveSupport's inflection configuration instead of having to repeat it in Rubocop as mentioned in #740.
Other common custom mixed-case acronyms could be StatsD, OAuth, RESTful, etc.
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.VersionAdded
indefault/config.yml
to the next minor version.If you have modified an existing cop's configuration options:
VersionChanged
inconfig/default.yml
to the next major version.