Skip to content

Commit 1eb9593

Browse files
committed
[Fix #893] Support local as an environment for Rails/UnknownEnv from Rails 7.1 onward.
Fixes #893
1 parent c0e3784 commit 1eb9593

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#893](https://github.com/rubocop/rubocop-rails/issues/893): Support `local` as an environment for `Rails/UnknownEnv` from Rails 7.1 onward. ([@ghiculescu][])

lib/rubocop/cop/rails/unknown_env.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ def unknown_env_name?(name)
8686
end
8787

8888
def environments
89-
cop_config['Environments']
89+
@environments ||= begin
90+
e = cop_config['Environments']
91+
e += ['local'] if target_rails_version >= 7.1
92+
e
93+
end
9094
end
9195
end
9296
end

spec/rubocop/cop/rails/unknown_env_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
2525
Rails.env.something?
2626
^^^^^^^^^^ Unknown environment `something`.
27+
Rails.env.local?
28+
^^^^^^ Unknown environment `local`.
2729
RUBY
2830
end
2931

@@ -36,6 +38,9 @@
3638
3739
'something' === Rails.env
3840
^^^^^^^^^^^ Unknown environment `something`.
41+
42+
'local' === Rails.env
43+
^^^^^^^ Unknown environment `local`.
3944
RUBY
4045
end
4146
end
@@ -49,6 +54,9 @@
4954
^^^^^^^^^^^^^ Unknown environment `developpment`.
5055
Rails.env.something?
5156
^^^^^^^^^^ Unknown environment `something`.
57+
58+
Rails.env.local?
59+
^^^^^^ Unknown environment `local`.
5260
RUBY
5361
end
5462

@@ -61,6 +69,9 @@
6169
6270
'something' === Rails.env
6371
^^^^^^^^^^^ Unknown environment `something`.
72+
73+
'local' === Rails.env
74+
^^^^^^^ Unknown environment `local`.
6475
RUBY
6576
end
6677
end
@@ -72,4 +83,30 @@
7283
Rails.env == 'production'
7384
RUBY
7485
end
86+
87+
context 'Rails 7.1' do
88+
let(:config) do
89+
RuboCop::Config.new(
90+
{
91+
'AllCops' => {
92+
'TargetRailsVersion' => '7.1'
93+
},
94+
'Rails/UnknownEnv' => {
95+
'Environments' => %w[
96+
development
97+
production
98+
test
99+
]
100+
}
101+
}
102+
)
103+
end
104+
105+
it 'accepts local as an environment name on Rails 7.1' do
106+
expect_no_offenses(<<~RUBY)
107+
Rails.env.local?
108+
Rails.env == 'local'
109+
RUBY
110+
end
111+
end
75112
end

0 commit comments

Comments
 (0)