File tree Expand file tree Collapse file tree 7 files changed +72
-2
lines changed
spec/rubocop/cop/betterment Expand file tree Collapse file tree 7 files changed +72
-2
lines changed Original file line number Diff line number Diff line change 1
1
PATH
2
2
remote: .
3
3
specs:
4
- betterlint (1.18 .0 )
4
+ betterlint (1.19 .0 )
5
5
rubocop (~> 1.71 )
6
6
rubocop-graphql (~> 1.5 )
7
7
rubocop-performance (~> 1.23 )
Original file line number Diff line number Diff line change 130
130
` ` `
131
131
132
132
All three `params.permit` calls will be flagged.
133
+
134
+ # ## Betterment/EnvironmentConfiguration
135
+
136
+ This cop identifies references to `ENV` outside of the config directory.
137
+
138
+ Environment variables should be parsed at boot time. If an environment variable is missing or invalid,
139
+ the application should fail to boot.
140
+
141
+ If there isn't a better place to assign your environment variable, Rails provides the `config.x` namespace
142
+ for [custom configuration](https://guides.rubyonrails.org/configuring.html#custom-configuration):
143
+
144
+ ` ` ` ruby
145
+ config.x.whatever = ENV.fetch('WHATEVER')
146
+ ` ` `
147
+
148
+ Here's how you'd reference this configuration parameter at runtime :
149
+
150
+ ` ` ` ruby
151
+ Rails.configuration.x.whatever
152
+ ` ` `
153
+
133
154
# ## Betterment/InternalsProtection
134
155
135
156
This cop is not enabled by default, and must be enabled from your `.rubocop.yml` file :
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
6
6
Gem ::Specification . new do |s |
7
7
s . name = "betterlint"
8
- s . version = "1.18 .0"
8
+ s . version = "1.19 .0"
9
9
s . authors = [ "Development" ]
10
10
11
11
s . summary = "Betterment rubocop configuration"
Original file line number Diff line number Diff line change @@ -39,6 +39,13 @@ Betterment/DirectDelayedEnqueue:
39
39
40
40
Betterment/DynamicParams :
41
41
StyleGuide : ' #bettermentdynamicparams'
42
+
43
+ Betterment/EnvironmentConfiguration :
44
+ StyleGuide : ' #bettermentenvironmentconfiguration'
45
+ Exclude :
46
+ - config/**/*.rb
47
+ - spec/**/*.rb
48
+ - test/**/*.rb
42
49
43
50
Betterment/HardcodedID :
44
51
AutoCorrect : false
Original file line number Diff line number Diff line change 6
6
require 'rubocop/cop/betterment/authorization_in_controller'
7
7
require 'rubocop/cop/betterment/direct_delayed_enqueue'
8
8
require 'rubocop/cop/betterment/dynamic_params'
9
+ require 'rubocop/cop/betterment/environment_configuration'
9
10
require 'rubocop/cop/betterment/fetch_boolean'
10
11
require 'rubocop/cop/betterment/hardcoded_id'
11
12
require 'rubocop/cop/betterment/implicit_redirect_type'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Betterment
6
+ class EnvironmentConfiguration < Base
7
+ MSG =
8
+ "Environment variables should be parsed at boot time and assigned " \
9
+ "to `Rails.configuration` or some other configurable object."
10
+
11
+ # @!method env?(node)
12
+ def_node_matcher :env? , '(const nil? :ENV)'
13
+
14
+ def on_const ( node )
15
+ add_offense ( node ) if env? ( node )
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec . describe RuboCop ::Cop ::Betterment ::EnvironmentConfiguration , :config do
6
+ it 'does not allow access to the ENV' do
7
+ expect_offense ( <<~RUBY )
8
+ ENV['RAILS_ENV']
9
+ ^^^ Environment variables should be parsed at boot time [...]
10
+
11
+ ENV['RAILS_ENV'] ||= 'test'
12
+ ^^^ Environment variables should be parsed at boot time [...]
13
+
14
+ ENV.fetch('FOO')
15
+ ^^^ Environment variables should be parsed at boot time [...]
16
+
17
+ ENV.fetch('FOO', nil)
18
+ ^^^ Environment variables should be parsed at boot time [...]
19
+ RUBY
20
+ end
21
+ end
You can’t perform that action at this time.
0 commit comments