Skip to content

Commit

Permalink
[Fix rubocop#893] Support local as an environment for `Rails/Unknow…
Browse files Browse the repository at this point in the history
…nEnv` from Rails 7.1 onward.

Fixes rubocop#893
  • Loading branch information
ghiculescu committed Dec 23, 2022
1 parent b930670 commit 3db5d93
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#893](https://github.com/rubocop/rubocop-rails/issues/893): Support `local` as an environment for `Rails/UnknownEnv` from Rails 7.1 onward. ([@ghiculescu][])

## 2.17.3 (2022-11-20)

### Bug fixes
Expand Down
4 changes: 3 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,9 @@ def unknown_env_name?(name)
end

def environments
cop_config['Environments']
e = cop_config['Environments']
e += ['local'] if target_rails_version >= 7.1
e
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'
},
'Rails/UnknownEnv' => {
'Environments' => %w[
development
production
test
]
}
}
)
end

it 'accepts local as an environment name on Rails 7.1' do
expect_no_offenses(<<~RUBY)
Rails.env.local?
Rails.env == 'local'
RUBY
end
end
end

0 comments on commit 3db5d93

Please sign in to comment.