stub_env 0.2.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.travis.yml +4 -1
- data/README.md +45 -2
- data/Rakefile +7 -0
- data/gemfiles/rspec2.gemfile +5 -0
- data/gemfiles/rspec3.gemfile +5 -0
- data/lib/stub_env/helpers.rb +21 -2
- data/spec/stub_env/helpers_spec.rb +48 -7
- data/stub_env.gemspec +1 -1
- metadata +6 -6
- data/.git-ignore +0 -2
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/Gemfile.lock +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ad5f1af2fe9363c6302afaf0e30c7ab56eae7c3
|
4
|
+
data.tar.gz: b21a53e9b11bc30d6ad7141f9bb3dfc28e3ec7bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 032e44c16aae9a2ab047b62d128115938d852747ab20b969e3e4d91afb14257c7ff1c0ec1057fecd0d3af367c7c9e433ebd27b49e982321a45953005a1ca3b87
|
7
|
+
data.tar.gz: faec12e2fd6a424df41f2b5a07e4bbfbb3ef7f9897ade218bc5aa48b0c19ae66cceeb1b2e348d9925c1ddfc6c779681d1209fd0c9785fca8960c21729c221be2
|
data/.gitignore
ADDED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -22,10 +22,53 @@ RSpec.configure do |config|
|
|
22
22
|
end
|
23
23
|
```
|
24
24
|
|
25
|
-
|
25
|
+
There are 2 possible usages. The first is to stub a single value:
|
26
26
|
|
27
27
|
```
|
28
28
|
stub_env('key', 'value')
|
29
|
+
|
30
|
+
puts ENV['key'] # => "value"
|
31
|
+
```
|
32
|
+
|
33
|
+
These will work in series, so you can stub multiple values by making multiple calls:
|
34
|
+
|
35
|
+
```
|
36
|
+
stub_env('key1', 'value1')
|
37
|
+
stub_env('key2', 'value2')
|
38
|
+
|
39
|
+
puts ENV['key1'] # => "value1"
|
40
|
+
puts ENV['key2'] # => "value2"
|
41
|
+
```
|
42
|
+
|
43
|
+
The second method is to stub multiple values using a hash:
|
44
|
+
|
45
|
+
```
|
46
|
+
stub_env({'key' => 'value', 'key2' => 'value2'})
|
47
|
+
|
48
|
+
puts ENV['key1'] # => "value1"
|
49
|
+
puts ENV['key2'] # => "value2"
|
29
50
|
```
|
30
51
|
|
31
|
-
|
52
|
+
You can also use this multiple times, and in combination with the other method:
|
53
|
+
|
54
|
+
```
|
55
|
+
stub_env({'key1' => 'value1', 'key2' => 'value2'})
|
56
|
+
stub_env('key3', 'value3')
|
57
|
+
stub_env({'key4' => 'value4'})
|
58
|
+
|
59
|
+
puts ENV['key1'] # => "value1"
|
60
|
+
puts ENV['key2'] # => "value2"
|
61
|
+
puts ENV['key3'] # => "value3"
|
62
|
+
puts ENV['key4'] # => "value4"
|
63
|
+
```
|
64
|
+
|
65
|
+
All ENV values not stubbed will return their original values.
|
66
|
+
|
67
|
+
```
|
68
|
+
# Given ENV['unstubbed_key'] = 'unstubbed_value'
|
69
|
+
stub_env('key', 'value')
|
70
|
+
|
71
|
+
puts ENV['key'] # => "value"
|
72
|
+
puts ENV['unstubbed_key'] # => "unstubbed_value"
|
73
|
+
|
74
|
+
```
|
data/Rakefile
ADDED
data/lib/stub_env/helpers.rb
CHANGED
@@ -1,9 +1,28 @@
|
|
1
1
|
module StubEnv
|
2
2
|
module Helpers
|
3
3
|
|
4
|
-
def stub_env
|
4
|
+
def stub_env key_or_hash, value=nil
|
5
|
+
init_stub unless env_stubbed?
|
6
|
+
if value
|
7
|
+
add_stubbed_value key_or_hash, value
|
8
|
+
else
|
9
|
+
key_or_hash.each { |k,v| add_stubbed_value k,v }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def add_stubbed_value key, value
|
16
|
+
allow(ENV).to receive(:[]).with(key).and_return(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def env_stubbed?
|
20
|
+
ENV.respond_to?(:stubbed?) && ENV.stubbed?
|
21
|
+
end
|
22
|
+
|
23
|
+
def init_stub
|
24
|
+
allow(ENV).to receive(:stubbed?).and_return(true)
|
5
25
|
allow(ENV).to receive(:[]).and_call_original
|
6
|
-
allow(ENV).to receive(:[]).with(key).and_return(value)
|
7
26
|
end
|
8
27
|
|
9
28
|
end
|
@@ -2,20 +2,61 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe StubEnv::Helpers do
|
4
4
|
include described_class
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
ENV['UNSTUBBED'] = 'unstubbed'
|
8
|
+
end
|
5
9
|
|
6
10
|
describe "#stub_env" do
|
7
11
|
|
8
|
-
|
9
|
-
|
10
|
-
|
12
|
+
context "with a single stubbed variable" do
|
13
|
+
before :each do
|
14
|
+
stub_env('TEST', 'success')
|
15
|
+
end
|
16
|
+
it "stubs out environment variables" do
|
17
|
+
expect(ENV['TEST']).to eq 'success'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "leaves original environment variables unstubbed" do
|
21
|
+
expect(ENV['UNSTUBBED']).to eq 'unstubbed'
|
22
|
+
end
|
11
23
|
end
|
12
24
|
|
13
|
-
|
14
|
-
|
25
|
+
context "with multiple stubbed variables" do
|
26
|
+
before :each do
|
27
|
+
stub_env('TEST', 'success')
|
28
|
+
stub_env('TEST2', 'another success')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "stubs out the first variable" do
|
32
|
+
expect(ENV['TEST']).to eq 'success'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "stubs out the second variable" do
|
36
|
+
expect(ENV['TEST2']).to eq 'another success'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "leaves original environment variables unstubbed" do
|
40
|
+
expect(ENV['UNSTUBBED']).to eq 'unstubbed'
|
41
|
+
end
|
15
42
|
end
|
16
43
|
|
17
|
-
|
18
|
-
|
44
|
+
context "with multiple stubbed variables in a hash" do
|
45
|
+
before :each do
|
46
|
+
stub_env({'TEST' => 'success', 'TEST2' => 'another success'})
|
47
|
+
end
|
48
|
+
|
49
|
+
it "stubs out the first variable" do
|
50
|
+
expect(ENV['TEST']).to eq 'success'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "stubs out the second variable" do
|
54
|
+
expect(ENV['TEST2']).to eq 'another success'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "leaves original environment variables unstubbed" do
|
58
|
+
expect(ENV['UNSTUBBED']).to eq 'unstubbed'
|
59
|
+
end
|
19
60
|
end
|
20
61
|
end
|
21
62
|
end
|
data/stub_env.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stub_env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Liam Bennett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -65,14 +65,14 @@ executables: []
|
|
65
65
|
extensions: []
|
66
66
|
extra_rdoc_files: []
|
67
67
|
files:
|
68
|
-
- ".
|
69
|
-
- ".ruby-gemset"
|
70
|
-
- ".ruby-version"
|
68
|
+
- ".gitignore"
|
71
69
|
- ".travis.yml"
|
72
70
|
- Gemfile
|
73
|
-
- Gemfile.lock
|
74
71
|
- LICENSE.txt
|
75
72
|
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- gemfiles/rspec2.gemfile
|
75
|
+
- gemfiles/rspec3.gemfile
|
76
76
|
- lib/stub_env.rb
|
77
77
|
- lib/stub_env/helpers.rb
|
78
78
|
- spec/spec_helper.rb
|
data/.git-ignore
DELETED
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
stub_env
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.1.3
|
data/Gemfile.lock
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
stub_env (0.0.1)
|
5
|
-
rspec (>= 3.0, < 4.0)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
diff-lcs (1.2.5)
|
11
|
-
rake (10.3.2)
|
12
|
-
rspec (3.1.0)
|
13
|
-
rspec-core (~> 3.1.0)
|
14
|
-
rspec-expectations (~> 3.1.0)
|
15
|
-
rspec-mocks (~> 3.1.0)
|
16
|
-
rspec-core (3.1.7)
|
17
|
-
rspec-support (~> 3.1.0)
|
18
|
-
rspec-expectations (3.1.2)
|
19
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
-
rspec-support (~> 3.1.0)
|
21
|
-
rspec-mocks (3.1.3)
|
22
|
-
rspec-support (~> 3.1.0)
|
23
|
-
rspec-support (3.1.2)
|
24
|
-
|
25
|
-
PLATFORMS
|
26
|
-
ruby
|
27
|
-
|
28
|
-
DEPENDENCIES
|
29
|
-
bundler (~> 1.0)
|
30
|
-
rake (~> 10.0)
|
31
|
-
stub_env!
|