Skip to content

Commit 8bcd6fa

Browse files
authored
Merge pull request #1229 from markokajzer/master
do not allow EnvironmentVariableAccess in initializers
2 parents cb01e4d + c0d3fda commit 8bcd6fa

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

changelog/change_fix_do_not_allow.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1229](https://github.com/rubocop/rubocop-rails/pull/1229): Make `Rails/EnvironmentVariableAccess` aware of initializers. ([@markokajzer][])

config/default.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,10 @@ Rails/EnvironmentVariableAccess:
445445
# TODO: Set to `pending` status in RuboCop Rails 2 series when migration doc will be written.
446446
Enabled: false
447447
VersionAdded: '2.10'
448-
VersionChanged: '2.11'
448+
VersionChanged: '<<next>>'
449449
Include:
450450
- app/**/*.rb
451+
- config/initializers/**/*.rb
451452
- lib/**/*.rb
452453
Exclude:
453454
- lib/**/*.rake

docs/modules/ROOT/pages/cops_rails.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ ENV["FOO"] = "bar"
20682068
| Name | Default value | Configurable values
20692069
20702070
| Include
2071-
| `+app/**/*.rb+`, `+lib/**/*.rb+`
2071+
| `+app/**/*.rb+`, `+config/initializers/**/*.rb+`, `+lib/**/*.rb+`
20722072
| Array
20732073
20742074
| Exclude

spec/rubocop/cop/rails/environment_variable_access_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@
2828
Foo::ENV.fetch("BAR")
2929
RUBY
3030
end
31+
32+
it 'registers an offense when reading from an `ENV` key in the config/initializers folder' do
33+
file_path = 'config/initializers/foo.rb'
34+
35+
expect_offense(<<~RUBY, file_path)
36+
ACCESS_TOKEN = ENV["ACCESS_TOKEN"]
37+
^^^ Do not read from `ENV` directly post initialization.
38+
RUBY
39+
end
40+
41+
it 'does not register an offense when reading from an `ENV` key in the config folder' do
42+
file_path = '/config/foo.rb'
43+
44+
expect_no_offenses(<<~RUBY, file_path)
45+
ACCESS_TOKEN = ENV["ACCESS_TOKEN"]
46+
RUBY
47+
end
3148
end
3249

3350
context 'when allowing reads' do

0 commit comments

Comments
 (0)