Skip to content
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

Support local as an environment for Rails/UnknownEnv from Rails 7.1 onward. #894

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_rails_unknown_env_to_add_local.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#893](https://github.com/rubocop/rubocop-rails/issues/893): Support `local` as an environment for `Rails/UnknownEnv` from Rails 7.1 onward. ([@ghiculescu][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/unknown_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def unknown_env_name?(name)
end

def environments
cop_config['Environments']
@environments ||= begin
e = cop_config['Environments']
e += ['local'] if target_rails_version >= 7.1
e
end
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cop/rails/unknown_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
Rails.env.something?
^^^^^^^^^^ Unknown environment `something`.
Rails.env.local?
^^^^^^ Unknown environment `local`.
RUBY
end

Expand All @@ -36,6 +38,9 @@

'something' === Rails.env
^^^^^^^^^^^ Unknown environment `something`.

'local' === Rails.env
^^^^^^^ Unknown environment `local`.
RUBY
end
end
Expand All @@ -49,6 +54,9 @@
^^^^^^^^^^^^^ Unknown environment `developpment`.
Rails.env.something?
^^^^^^^^^^ Unknown environment `something`.

Rails.env.local?
^^^^^^ Unknown environment `local`.
RUBY
end

Expand All @@ -61,6 +69,9 @@

'something' === Rails.env
^^^^^^^^^^^ Unknown environment `something`.

'local' === Rails.env
^^^^^^^ Unknown environment `local`.
RUBY
end
end
Expand All @@ -72,4 +83,30 @@
Rails.env == 'production'
RUBY
end

context 'Rails 7.1' do
let(:config) do
RuboCop::Config.new(
{
'AllCops' => {
'TargetRailsVersion' => '7.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this PR and added support for local as part #906 as well. I think this approach is better, although it may make sense to cherry pick my commit introducing a :rails71 shared context (or I can open a separate PR for that, but it seems small) rather than a custom config, especially given the rest of this config is just the defaults.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extracted the shared context into its own PR in #1134.

},
'Rails/UnknownEnv' => {
'Environments' => %w[
development
production
test
]
}
}
)
end

it 'accepts local as an environment name on Rails 7.1' do
Comment on lines +87 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that #1134 has been merged, I think this could be changed to use the shared context and cop_config defined above. We also don't need to repeat "Rails 7.1" in the spec name, as it is already in the context name.

Suggested change
context 'Rails 7.1' do
let(:config) do
RuboCop::Config.new(
{
'AllCops' => {
'TargetRailsVersion' => '7.1'
},
'Rails/UnknownEnv' => {
'Environments' => %w[
development
production
test
]
}
}
)
end
it 'accepts local as an environment name on Rails 7.1' do
context 'in Rails 7.1 onwards', :rails71 do
it 'accepts local as an environment name' do

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghiculescu ping.

expect_no_offenses(<<~RUBY)
Rails.env.local?
Rails.env == 'local'
RUBY
end
end
end