diff --git a/changelog/change_rails_unknown_env_to_add_local.md b/changelog/change_rails_unknown_env_to_add_local.md new file mode 100644 index 0000000000..795ec30513 --- /dev/null +++ b/changelog/change_rails_unknown_env_to_add_local.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/unknown_env.rb b/lib/rubocop/cop/rails/unknown_env.rb index c3d8d467eb..854d8e5a79 100644 --- a/lib/rubocop/cop/rails/unknown_env.rb +++ b/lib/rubocop/cop/rails/unknown_env.rb @@ -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 diff --git a/spec/rubocop/cop/rails/unknown_env_spec.rb b/spec/rubocop/cop/rails/unknown_env_spec.rb index 8819e9fd1e..6e9ae6de19 100644 --- a/spec/rubocop/cop/rails/unknown_env_spec.rb +++ b/spec/rubocop/cop/rails/unknown_env_spec.rb @@ -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 @@ -36,6 +38,9 @@ 'something' === Rails.env ^^^^^^^^^^^ Unknown environment `something`. + + 'local' === Rails.env + ^^^^^^^ Unknown environment `local`. RUBY end end @@ -49,6 +54,9 @@ ^^^^^^^^^^^^^ Unknown environment `developpment`. Rails.env.something? ^^^^^^^^^^ Unknown environment `something`. + + Rails.env.local? + ^^^^^^ Unknown environment `local`. RUBY end @@ -61,6 +69,9 @@ 'something' === Rails.env ^^^^^^^^^^^ Unknown environment `something`. + + 'local' === Rails.env + ^^^^^^^ Unknown environment `local`. RUBY end end @@ -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