Skip to content

Commit

Permalink
Make Rails/EnvironmentVariableAccess aware of initializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
markokajzer committed Jan 10, 2024
1 parent cb01e4d commit c0d3fda
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/change_fix_do_not_allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1229](https://github.com/rubocop/rubocop-rails/pull/1229): Make `Rails/EnvironmentVariableAccess` aware of initializers. ([@markokajzer][])
3 changes: 2 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,10 @@ Rails/EnvironmentVariableAccess:
# TODO: Set to `pending` status in RuboCop Rails 2 series when migration doc will be written.
Enabled: false
VersionAdded: '2.10'
VersionChanged: '2.11'
VersionChanged: '<<next>>'
Include:
- app/**/*.rb
- config/initializers/**/*.rb
- lib/**/*.rb
Exclude:
- lib/**/*.rake
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ ENV["FOO"] = "bar"
| Name | Default value | Configurable values
| Include
| `+app/**/*.rb+`, `+lib/**/*.rb+`
| `+app/**/*.rb+`, `+config/initializers/**/*.rb+`, `+lib/**/*.rb+`
| Array
| Exclude
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/rails/environment_variable_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
Foo::ENV.fetch("BAR")
RUBY
end

it 'registers an offense when reading from an `ENV` key in the config/initializers folder' do
file_path = 'config/initializers/foo.rb'

expect_offense(<<~RUBY, file_path)
ACCESS_TOKEN = ENV["ACCESS_TOKEN"]
^^^ Do not read from `ENV` directly post initialization.
RUBY
end

it 'does not register an offense when reading from an `ENV` key in the config folder' do
file_path = '/config/foo.rb'

expect_no_offenses(<<~RUBY, file_path)
ACCESS_TOKEN = ENV["ACCESS_TOKEN"]
RUBY
end
end

context 'when allowing reads' do
Expand Down

0 comments on commit c0d3fda

Please sign in to comment.