stub_env 0.2.0 → 1.0.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
  SHA1:
3
- metadata.gz: 093240b4f1b5a4dc0e224b0406c42d4d8f684a8c
4
- data.tar.gz: c80388dd7be2993298dbaf97214b1defc5b36b17
3
+ metadata.gz: 1ad5f1af2fe9363c6302afaf0e30c7ab56eae7c3
4
+ data.tar.gz: b21a53e9b11bc30d6ad7141f9bb3dfc28e3ec7bd
5
5
  SHA512:
6
- metadata.gz: dd995aaa5cdc69c97eff45366b13ab7c3afc79ea5d0a74f7cc80f6c2db08808cee273ba6716bc9cfa052d7c2b393527fe04611fb9ecd117a76f0efe7bd05ce16
7
- data.tar.gz: 675f86b45cc42feb299777d0105bd2f91758d12055772568296c3ed338a8bf656aa1d10dedaad91399f9b1d7f0ffbadf3dcd6783d4b4574c1d633e4874a0bf6a
6
+ metadata.gz: 032e44c16aae9a2ab047b62d128115938d852747ab20b969e3e4d91afb14257c7ff1c0ec1057fecd0d3af367c7c9e433ebd27b49e982321a45953005a1ca3b87
7
+ data.tar.gz: faec12e2fd6a424df41f2b5a07e4bbfbb3ef7f9897ade218bc5aa48b0c19ae66cceeb1b2e348d9925c1ddfc6c779681d1209fd0c9785fca8960c21729c221be2
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .ruby-gemset
2
+ .ruby-version
3
+ Gemfile.lock
4
+ *.gem
data/.travis.yml CHANGED
@@ -3,4 +3,7 @@ rvm:
3
3
  - 1.9.3
4
4
  - "2.0"
5
5
  - "2.1"
6
- - ruby-head
6
+ - ruby-head
7
+ gemfile:
8
+ - gemfiles/rspec2.gemfile
9
+ - gemfiles/rspec3.gemfile
data/README.md CHANGED
@@ -22,10 +22,53 @@ RSpec.configure do |config|
22
22
  end
23
23
  ```
24
24
 
25
- Then in your examples use:
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
- All ENV values not stubbed will return their original values.
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
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task test: [:spec]
7
+ task default: :test
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: ".."
4
+
5
+ gem "rspec", "~> 2.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec path: ".."
4
+
5
+ gem "rspec", "~> 3.0"
@@ -1,9 +1,28 @@
1
1
  module StubEnv
2
2
  module Helpers
3
3
 
4
- def stub_env key, value
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
- before :each do
9
- ENV['UNSTUBBED'] = 'unstubbed'
10
- stub_env('TEST', 'success')
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
- it "stubs out environment variables" do
14
- expect(ENV['TEST']).to eq 'success'
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
- it "leaves original environment variables unstubbed" do
18
- expect(ENV['UNSTUBBED']).to eq 'unstubbed'
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "stub_env"
5
- gem.version = "0.2.0"
5
+ gem.version = "1.0.0"
6
6
 
7
7
  gem.authors = ["Liam Bennett"]
8
8
  gem.email = ["liam@littleowllabs.com"]
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.2.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: 2014-11-05 00:00:00.000000000 Z
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
- - ".git-ignore"
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
@@ -1,2 +0,0 @@
1
- .ruby-gemset
2
- .ruby-version
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!