get_env 0.2.0.pre3 → 0.2.0.pre4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bba82e71b1cff0bda4ff516323e391680d6c5d18d96981f1a4434c702ad5a544
4
- data.tar.gz: 26782f15c8f3e8cfb493d2629ec22fe12fe517fc969fcadc72e17c2f597a2f8a
3
+ metadata.gz: 2a89e2cc9d400fab0b30d9b74201547f3d08f6609b32869e9b32c5e1fb4984a2
4
+ data.tar.gz: b85bde1e2d563e9cf7481836eeea2eed8a9fc571022bb737bf74b7bd8636b449
5
5
  SHA512:
6
- metadata.gz: e2a32b34e46a3f38736eeeea7ed99694da08bb62d19ae2dbd213c75ebeabb59bd3e37a016eebfb1e72ff43570561ebf873193c80fe88dfe8f4724d02131a6cac
7
- data.tar.gz: 52054e2cdf526fd255fc12d77d94c19ea0ec1732721b501203bcdb50143324308ae97db90eac599c27890a3314cd5fd8580261a087bbd9aa204e0d472c06c1ba
6
+ metadata.gz: f3634e108a5c48ab5262a9855ab6f94cbf4a01443982698f674e14d39113fcdfa48e187904f73711394b1ef90e13df21165fdf3322ba9b303577f7928246271c
7
+ data.tar.gz: 56923245058d42eca837afb64b6289b4bda520e7c5955d107bb519ff8738499b630db739a38c1f92f1a43106b596b7edd3a08e8bbc0134c6dc45d31c186886b2
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'get_env/version'
4
- require 'get_env/cops'
5
4
 
6
5
  module GetEnv
7
6
  def self.[](key)
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../rubocop/cop/lint/no_env'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GetEnv
4
- VERSION = '0.2.0.pre3'
4
+ VERSION = '0.2.0.pre4'
5
5
  end
@@ -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
@@ -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.2.0.pre3
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: 2020-05-25 00:00:00.000000000 Z
14
+ date: 2020-05-26 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: bundler
@@ -86,12 +87,18 @@ description: Read values from ENV in a reasonable way
86
87
  email:
87
88
  - bartek@rainforestqa.com
88
89
  - jonathan@rainforestqa.com
90
+ - pp@rainforestqa.com
89
91
  executables: []
90
92
  extensions: []
91
93
  extra_rdoc_files: []
92
94
  files:
93
95
  - lib/get_env.rb
96
+ - lib/get_env/cops.rb
94
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
95
102
  homepage: https://github.com/rainforestapp/get_env
96
103
  licenses:
97
104
  - MIT
@@ -115,4 +122,7 @@ rubygems_version: 3.0.3
115
122
  signing_key:
116
123
  specification_version: 4
117
124
  summary: Read values from ENV in a reasonable way
118
- 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