Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not allow EnvironmentVariableAccess in initializers #1229

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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