get_env 0.1.4 → 0.2.0

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: 2f2f663152b27c3a911a38ea2ab930a4035f808036c6fc721e5374993a668ccb
4
- data.tar.gz: 6f6db938e4c6f1e695d18c0e87914231c8c55e481102e719dfb82a1933e3e914
3
+ metadata.gz: 840c77f6c35baeec3560a6fb8b64667fffe6e477d344c0b4d92e2b18f9a89758
4
+ data.tar.gz: '0916cd555771f013375a4d42b190ee04736e101a0e1a9e963eb004cc899cb9bf'
5
5
  SHA512:
6
- metadata.gz: 0fd52dd3d9e3c1dd5f6ab569cb2e2b4483bf5b3d88c5a9ddee3072f8eedb946f20b1846a916b64728a639f084bb9465857d4a1e5eae08d6a9289649b3c29e135
7
- data.tar.gz: 1b39d837f70ca650f69d9ea631497208619303bdb9c75cb1d895ac2f68dc958ceb3f513b0590c2e135e193f95e49db14853fe4c4003495b47d7257606b87d2d8
6
+ metadata.gz: c636397567c57c93a12cbefc88bbed42e42fc6511a488971840af66a91b2aa8de92173412b9ba88ab9aee689c10307536181723f5e8d1a1fada132778ac0c3cd
7
+ data.tar.gz: b1b43cea47ab2af9a9f1adbfda13319e0598d4a73566529dcfc8e4496276903804f295feaf4e8a4ca2b0132917cd9d3a3c50ea29c31d3f8dc08698266690bf4c
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
- require "get_env/version"
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 != nil
24
+ elsif !default.nil?
23
25
  default
24
26
  elsif block_given?
25
27
  yield
@@ -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.1.4'
4
+ VERSION = '0.2.0'
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.1.4
4
+ version: 0.2.0
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-03-02 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
@@ -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
@@ -101,4 +122,7 @@ rubygems_version: 3.0.3
101
122
  signing_key:
102
123
  specification_version: 4
103
124
  summary: Read values from ENV in a reasonable way
104
- test_files: []
125
+ test_files:
126
+ - spec/spec_helper.rb
127
+ - spec/get_env_spec.rb
128
+ - spec/rubocop/cop/lint/no_env_spec.rb