get_env 0.1.3 → 0.2.0.pre4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/get_env.rb +4 -2
- data/lib/get_env/cops.rb +3 -0
- data/lib/get_env/version.rb +3 -1
- data/lib/rubocop/cop/lint/no_env.rb +43 -0
- data/spec/get_env_spec.rb +67 -0
- data/spec/rubocop/cop/lint/no_env_spec.rb +76 -0
- data/spec/spec_helper.rb +14 -0
- metadata +30 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a89e2cc9d400fab0b30d9b74201547f3d08f6609b32869e9b32c5e1fb4984a2
|
4
|
+
data.tar.gz: b85bde1e2d563e9cf7481836eeea2eed8a9fc571022bb737bf74b7bd8636b449
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3634e108a5c48ab5262a9855ab6f94cbf4a01443982698f674e14d39113fcdfa48e187904f73711394b1ef90e13df21165fdf3322ba9b303577f7928246271c
|
7
|
+
data.tar.gz: 56923245058d42eca837afb64b6289b4bda520e7c5955d107bb519ff8738499b630db739a38c1f92f1a43106b596b7edd3a08e8bbc0134c6dc45d31c186886b2
|
data/lib/get_env.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
|
+
require 'get_env/version'
|
3
4
|
|
4
5
|
module GetEnv
|
5
6
|
def self.[](key)
|
6
7
|
return nil if key.nil?
|
8
|
+
|
7
9
|
v = ENV[key].to_i
|
8
10
|
return v if v.to_s == ENV[key]
|
9
11
|
|
@@ -19,7 +21,7 @@ module GetEnv
|
|
19
21
|
def self.fetch(key, default = nil)
|
20
22
|
if ENV.has_key?(key)
|
21
23
|
self[key]
|
22
|
-
elsif default
|
24
|
+
elsif !default.nil?
|
23
25
|
default
|
24
26
|
elsif block_given?
|
25
27
|
yield
|
data/lib/get_env/cops.rb
ADDED
data/lib/get_env/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# @example
|
7
|
+
# # bad
|
8
|
+
# ENV[]
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# ENV.fetch(..)
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# GetEnv[]
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# GetEnv.fetch(...)
|
18
|
+
class NoENV < Cop
|
19
|
+
MSG = 'Use `GetEnv` instead of `ENV`.'
|
20
|
+
|
21
|
+
def_node_matcher :is_ENV_index?, '(send (const nil? :ENV) :[] _key)'
|
22
|
+
def_node_matcher :is_ENV_fetch?, '(send (const nil? :ENV) :fetch _key ...)'
|
23
|
+
|
24
|
+
def on_const(node)
|
25
|
+
parent = node.parent
|
26
|
+
return unless is_ENV_index?(parent) || is_ENV_fetch?(parent)
|
27
|
+
|
28
|
+
add_offense(node)
|
29
|
+
end
|
30
|
+
|
31
|
+
def autocorrect(node)
|
32
|
+
lambda do |corrector|
|
33
|
+
if Gem::Version.new(RuboCop::Version.version) >= Gem::Version.new('0.82.0')
|
34
|
+
corrector.replace(node, 'GetEnv')
|
35
|
+
else
|
36
|
+
corrector.replace(node.loc.expression, 'GetEnv')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe GetEnv do
|
4
|
+
before(:all) do
|
5
|
+
ENV['SOME_STRING'] = 'foo'
|
6
|
+
ENV['SOME_INT'] = '1'
|
7
|
+
ENV['SOME_FLOAT'] = '1.23'
|
8
|
+
ENV['SOME_TRUE'] = 'true'
|
9
|
+
ENV['SOME_FALSE'] = 'false'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "has a version number" do
|
13
|
+
expect(GetEnv::VERSION).not_to be nil
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#[]' do
|
17
|
+
it 'handles nil' do
|
18
|
+
expect(GetEnv[nil]).to eq(nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'handles missing value' do
|
22
|
+
expect(GetEnv['klfadjslfkadjsflkadjs']).to eq(nil)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'returns strings' do
|
26
|
+
expect(GetEnv['SOME_STRING']).to eq('foo')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'casts ints' do
|
30
|
+
expect(GetEnv['SOME_INT']).to eq(1)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'casts floats' do
|
34
|
+
expect(GetEnv['SOME_FLOAT']).to eq(1.23)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'casts bools' do
|
38
|
+
expect(GetEnv['SOME_TRUE']).to be(true)
|
39
|
+
expect(GetEnv['SOME_FALSE']).to be(false)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#fetch' do
|
44
|
+
it 'casts types' do
|
45
|
+
expect(GetEnv.fetch('SOME_FLOAT')).to eq(1.23)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'returns the value when found' do
|
49
|
+
expect(GetEnv.fetch('SOME_FLOAT', 3.21)).to eq(1.23)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns the defaults when not found' do
|
53
|
+
expect(GetEnv.fetch('SOME_NON_EXISTANT_FLOAT', 3.21)).to eq(3.21)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'raises an exception if not found and no default is specified' do
|
57
|
+
expect do
|
58
|
+
GetEnv.fetch('SOME_NON_EXISTANT_FLOAT')
|
59
|
+
end.to raise_error KeyError, /key not found: "SOME_NON_EXISTANT_FLOAT"/
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'accepts a block for the default value' do
|
63
|
+
v = GetEnv.fetch('SOME_NON_EXISTANT_FLOAT') { 3.21 }
|
64
|
+
expect(v).to eq(3.21)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubocop'
|
4
|
+
require 'rubocop/rspec/support'
|
5
|
+
require_relative '../../../../lib/rubocop/cop/lint/no_env'
|
6
|
+
|
7
|
+
RSpec.describe RuboCop::Cop::Lint::NoENV do
|
8
|
+
include RuboCop::RSpec::ExpectOffense
|
9
|
+
|
10
|
+
let(:config) { RuboCop::Config.new }
|
11
|
+
|
12
|
+
subject(:cop) { described_class.new(config) }
|
13
|
+
|
14
|
+
describe '#[]' do
|
15
|
+
it 'registers an offense when using `ENV`' do
|
16
|
+
expect_offense(<<~RUBY)
|
17
|
+
FOO = ENV['FOO']
|
18
|
+
^^^ Use `GetEnv` instead of `ENV`.
|
19
|
+
RUBY
|
20
|
+
|
21
|
+
expect_correction(<<~RUBY)
|
22
|
+
FOO = GetEnv['FOO']
|
23
|
+
RUBY
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'does not register an offense when using `GetEnv`' do
|
27
|
+
expect_no_offenses(<<~RUBY)
|
28
|
+
FOO = GetEnv['FOO']
|
29
|
+
RUBY
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#fetch' do
|
34
|
+
it 'registers an offense when using `ENV`' do
|
35
|
+
expect_offense(<<~RUBY)
|
36
|
+
do_the_thing(ENV.fetch('FOO'))
|
37
|
+
^^^ Use `GetEnv` instead of `ENV`.
|
38
|
+
RUBY
|
39
|
+
|
40
|
+
expect_correction(<<~RUBY)
|
41
|
+
do_the_thing(GetEnv.fetch('FOO'))
|
42
|
+
RUBY
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'registers an offense when using `ENV` with a default value' do
|
46
|
+
expect_offense(<<~RUBY)
|
47
|
+
a = x + ENV.fetch('FOO', 42)
|
48
|
+
^^^ Use `GetEnv` instead of `ENV`.
|
49
|
+
RUBY
|
50
|
+
|
51
|
+
expect_correction(<<~RUBY)
|
52
|
+
a = x + GetEnv.fetch('FOO', 42)
|
53
|
+
RUBY
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'does not register an offense when using `GetEnv`' do
|
57
|
+
expect_no_offenses(<<~RUBY)
|
58
|
+
do_the_thing(GetEnv.fetch('FOO'))
|
59
|
+
RUBY
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'does not register an offense when using `GetEnv` with a default value' do
|
63
|
+
expect_no_offenses(<<~RUBY)
|
64
|
+
a = x + GetEnv.fetch('FOO', 42)
|
65
|
+
RUBY
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'does not register an offense when using `ENV` for a method not defined by `GetEnv`' do
|
70
|
+
expect_no_offenses(<<~RUBY)
|
71
|
+
ENV.map do |var|
|
72
|
+
foo << var.lowercase
|
73
|
+
end
|
74
|
+
RUBY
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "get_env"
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
# Enable flags like --only-failures and --next-failure
|
6
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
7
|
+
|
8
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
9
|
+
config.disable_monkey_patching!
|
10
|
+
|
11
|
+
config.expect_with :rspec do |c|
|
12
|
+
c.syntax = :expect
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: get_env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.pre4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Mathieu
|
8
8
|
- Bartek Kruszczynski
|
9
9
|
- Jonathan Barber
|
10
|
+
- Paul Padier
|
10
11
|
autorequire:
|
11
12
|
bindir: bin
|
12
13
|
cert_chain: []
|
13
|
-
date:
|
14
|
+
date: 2020-05-26 00:00:00.000000000 Z
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: bundler
|
@@ -68,16 +69,36 @@ dependencies:
|
|
68
69
|
- - ">="
|
69
70
|
- !ruby/object:Gem::Version
|
70
71
|
version: '0'
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: rubocop
|
74
|
+
requirement: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
71
86
|
description: Read values from ENV in a reasonable way
|
72
87
|
email:
|
73
88
|
- bartek@rainforestqa.com
|
74
89
|
- jonathan@rainforestqa.com
|
90
|
+
- pp@rainforestqa.com
|
75
91
|
executables: []
|
76
92
|
extensions: []
|
77
93
|
extra_rdoc_files: []
|
78
94
|
files:
|
79
95
|
- lib/get_env.rb
|
96
|
+
- lib/get_env/cops.rb
|
80
97
|
- lib/get_env/version.rb
|
98
|
+
- lib/rubocop/cop/lint/no_env.rb
|
99
|
+
- spec/get_env_spec.rb
|
100
|
+
- spec/rubocop/cop/lint/no_env_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
81
102
|
homepage: https://github.com/rainforestapp/get_env
|
82
103
|
licenses:
|
83
104
|
- MIT
|
@@ -93,13 +114,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
114
|
version: '0'
|
94
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
116
|
requirements:
|
96
|
-
- - "
|
117
|
+
- - ">"
|
97
118
|
- !ruby/object:Gem::Version
|
98
|
-
version:
|
119
|
+
version: 1.3.1
|
99
120
|
requirements: []
|
100
|
-
|
101
|
-
rubygems_version: 2.7.8
|
121
|
+
rubygems_version: 3.0.3
|
102
122
|
signing_key:
|
103
123
|
specification_version: 4
|
104
124
|
summary: Read values from ENV in a reasonable way
|
105
|
-
test_files:
|
125
|
+
test_files:
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/rubocop/cop/lint/no_env_spec.rb
|
128
|
+
- spec/get_env_spec.rb
|