climate_control 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/{.travisci.yml → .travis.yml} +3 -2
- data/Gemfile +1 -1
- data/LICENSE.txt +1 -1
- data/NEWS +5 -0
- data/README.md +15 -2
- data/Rakefile +2 -2
- data/climate_control.gemspec +12 -12
- data/lib/climate_control.rb +11 -3
- data/lib/climate_control/environment.rb +21 -0
- data/lib/climate_control/errors.rb +3 -0
- data/lib/climate_control/modifier.rb +22 -14
- data/lib/climate_control/version.rb +1 -1
- data/spec/acceptance/climate_control_spec.rb +142 -7
- data/spec/spec_helper.rb +10 -8
- metadata +27 -37
- data/spec/lib/climate_control/modifier_spec.rb +0 -83
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 115ebcc91a993cd77c6f7d635c98cbcca37e304a
|
4
|
+
data.tar.gz: 99ea065056db3051e559fc120754bad454f5dbf4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af4aa1f76d2c0056d53f407a71df8a0135354d2b5aa126440c791252252cb02d54a4bb7f278bc320f1959e4985967d8c82e7275e9f81cd2f3893f594656fa08c
|
7
|
+
data.tar.gz: 70320c9af644facb62efbd6710ddf624f41f44ccdcaf92aa1e1156d2f3f7eff5c66c3cf238b6e6194da42225fe4f4995f503d6e6fbffbf009f80c7125566dd15
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/NEWS
CHANGED
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Climate Control can be used to temporarily assign environment variables
|
22
22
|
within a block:
|
23
23
|
|
24
24
|
```ruby
|
@@ -76,6 +76,19 @@ block, except for the overrides. Transparency is crucial because the code
|
|
76
76
|
executed within the block is not for `ClimateControl` to manage or modify. See
|
77
77
|
the tests for more detail about the specific behaviors.
|
78
78
|
|
79
|
+
## Why Use Climate Control?
|
80
|
+
|
81
|
+
By following guidelines regarding environment variables outlined by the
|
82
|
+
[twelve-factor app](http://12factor.net/config), testing code in an isolated
|
83
|
+
manner becomes more difficult:
|
84
|
+
|
85
|
+
* avoiding modifications and testing values, we introduce mystery guests
|
86
|
+
* making modifications and testing values, we introduce risk as environment
|
87
|
+
variables represent global state
|
88
|
+
|
89
|
+
Climate Control modifies environment variables only within the context of the
|
90
|
+
block, ensuring values are managed properly and consistently.
|
91
|
+
|
79
92
|
## Contributing
|
80
93
|
|
81
94
|
1. Fork it
|
@@ -86,4 +99,4 @@ the tests for more detail about the specific behaviors.
|
|
86
99
|
|
87
100
|
## License
|
88
101
|
|
89
|
-
climate_control is copyright 2012 Joshua Clayton and thoughtbot, inc. It is free software and may be redistributed under the terms specified in the LICENSE.txt file.
|
102
|
+
climate_control is copyright 2012-2017 Joshua Clayton and thoughtbot, inc. It is free software and may be redistributed under the terms specified in the [LICENSE.txt](https://github.com/thoughtbot/climate_control/blob/master/LICENSE.txt) file.
|
data/Rakefile
CHANGED
data/climate_control.gemspec
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "climate_control/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
7
|
+
gem.name = "climate_control"
|
8
8
|
gem.version = ClimateControl::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
9
|
+
gem.authors = ["Joshua Clayton"]
|
10
|
+
gem.email = ["joshua.clayton@gmail.com"]
|
11
11
|
gem.description = %q{Modify your ENV}
|
12
12
|
gem.summary = %q{Modify your ENV easily with ClimateControl}
|
13
|
-
gem.homepage =
|
13
|
+
gem.homepage = "https://github.com/thoughtbot/climate_control"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
-
gem.require_paths = [
|
18
|
+
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_dependency
|
21
|
-
gem.add_development_dependency
|
22
|
-
gem.add_development_dependency
|
23
|
-
gem.add_development_dependency
|
20
|
+
gem.add_dependency "activesupport", ">= 3.0"
|
21
|
+
gem.add_development_dependency "rspec", "~> 3.1.0"
|
22
|
+
gem.add_development_dependency "rake", "~> 10.3.2"
|
23
|
+
gem.add_development_dependency "simplecov", "~> 0.9.1"
|
24
24
|
end
|
data/lib/climate_control.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "climate_control/environment"
|
2
|
+
require "climate_control/errors"
|
3
|
+
require "climate_control/modifier"
|
4
|
+
require "climate_control/version"
|
3
5
|
|
4
6
|
module ClimateControl
|
7
|
+
@@env = ClimateControl::Environment.new
|
8
|
+
|
5
9
|
def self.modify(environment_overrides, &block)
|
6
|
-
Modifier.new(environment_overrides, &block).process
|
10
|
+
Modifier.new(env, environment_overrides, &block).process
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.env
|
14
|
+
@@env
|
7
15
|
end
|
8
16
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "thread"
|
2
|
+
require "active_support/core_ext/module/delegation"
|
3
|
+
|
4
|
+
module ClimateControl
|
5
|
+
class Environment
|
6
|
+
def initialize
|
7
|
+
@semaphore = Mutex.new
|
8
|
+
end
|
9
|
+
|
10
|
+
delegate :[]=, :to_hash, :[], :delete, to: :env
|
11
|
+
delegate :synchronize, to: :semaphore
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_reader :semaphore
|
16
|
+
|
17
|
+
def env
|
18
|
+
ENV
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,20 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/hash/keys"
|
2
2
|
|
3
3
|
module ClimateControl
|
4
4
|
class Modifier
|
5
|
-
def initialize(environment_overrides = {}, &block)
|
5
|
+
def initialize(env, environment_overrides = {}, &block)
|
6
6
|
@environment_overrides = environment_overrides.dup.stringify_keys!
|
7
7
|
@block = block
|
8
|
+
@env = env
|
8
9
|
end
|
9
10
|
|
10
11
|
def process
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
@env.synchronize do
|
13
|
+
begin
|
14
|
+
prepare_environment_for_block
|
15
|
+
run_block
|
16
|
+
ensure
|
17
|
+
cache_environment_after_block
|
18
|
+
delete_keys_that_do_not_belong
|
19
|
+
revert_changed_keys
|
20
|
+
end
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
@@ -32,7 +35,12 @@ module ClimateControl
|
|
32
35
|
|
33
36
|
def copy_overrides_to_environment
|
34
37
|
@environment_overrides.each do |key, value|
|
35
|
-
|
38
|
+
begin
|
39
|
+
@env[key] = value
|
40
|
+
rescue TypeError => e
|
41
|
+
raise UnassignableValueError,
|
42
|
+
"attempted to assign #{value} to #{key} but failed (#{e.message})"
|
43
|
+
end
|
36
44
|
end
|
37
45
|
end
|
38
46
|
|
@@ -49,22 +57,22 @@ module ClimateControl
|
|
49
57
|
end
|
50
58
|
|
51
59
|
def delete_keys_that_do_not_belong
|
52
|
-
(keys_to_remove - keys_changed_by_block).each {|key|
|
60
|
+
(keys_to_remove - keys_changed_by_block).each {|key| @env.delete(key) }
|
53
61
|
end
|
54
62
|
|
55
63
|
def revert_changed_keys
|
56
64
|
(@original_env.keys - keys_changed_by_block).each do |key|
|
57
|
-
|
65
|
+
@env[key] = @original_env[key]
|
58
66
|
end
|
59
67
|
end
|
60
68
|
|
61
69
|
def clone_environment
|
62
|
-
|
70
|
+
@env.to_hash
|
63
71
|
end
|
64
72
|
|
65
73
|
class OverlappingKeysWithChangedValues
|
66
74
|
def initialize(hash_1, hash_2)
|
67
|
-
@hash_1 = hash_1
|
75
|
+
@hash_1 = hash_1 || {}
|
68
76
|
@hash_2 = hash_2
|
69
77
|
end
|
70
78
|
|
@@ -1,14 +1,149 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
|
-
describe
|
4
|
-
it
|
3
|
+
describe "Climate control" do
|
4
|
+
it "allows modification of the environment" do
|
5
5
|
block_run = false
|
6
|
-
ClimateControl.modify FOO:
|
7
|
-
expect(ENV[
|
6
|
+
ClimateControl.modify FOO: "bar" do
|
7
|
+
expect(ENV["FOO"]).to eq "bar"
|
8
8
|
block_run = true
|
9
9
|
end
|
10
10
|
|
11
|
-
expect(ENV[
|
12
|
-
expect(block_run).to
|
11
|
+
expect(ENV["FOO"]).to be_nil
|
12
|
+
expect(block_run).to be true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "modifies the environment" do
|
16
|
+
with_modified_env VARIABLE_1: "bar", VARIABLE_2: "qux" do
|
17
|
+
expect(ENV["VARIABLE_1"]).to eq "bar"
|
18
|
+
expect(ENV["VARIABLE_2"]).to eq "qux"
|
19
|
+
end
|
20
|
+
|
21
|
+
expect(ENV["VARIABLE_1"]).to be_nil
|
22
|
+
expect(ENV["VARIABLE_2"]).to be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "allows for environment variables to be assigned within the block" do
|
26
|
+
with_modified_env VARIABLE_1: "modified" do
|
27
|
+
ENV["ASSIGNED_IN_BLOCK"] = "assigned"
|
28
|
+
end
|
29
|
+
|
30
|
+
expect(ENV["ASSIGNED_IN_BLOCK"]).to eq "assigned"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "reassigns previously set environment variables" do
|
34
|
+
ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"] = "original"
|
35
|
+
expect(ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"]).to eq "original"
|
36
|
+
|
37
|
+
with_modified_env VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV: "overridden" do
|
38
|
+
expect(ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"]).to eq "overridden"
|
39
|
+
end
|
40
|
+
|
41
|
+
expect(ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"]).to eq "original"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "persists the change when overriding the variable in the block" do
|
45
|
+
with_modified_env VARIABLE_MODIFIED_AND_THEN_ASSIGNED: "modified" do
|
46
|
+
ENV["VARIABLE_MODIFIED_AND_THEN_ASSIGNED"] = "assigned value"
|
47
|
+
end
|
48
|
+
|
49
|
+
expect(ENV["VARIABLE_MODIFIED_AND_THEN_ASSIGNED"]).to eq "assigned value"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "resets environment variables even if the block raises" do
|
53
|
+
expect {
|
54
|
+
with_modified_env FOO: "bar" do
|
55
|
+
raise "broken"
|
56
|
+
end
|
57
|
+
}.to raise_error
|
58
|
+
|
59
|
+
expect(ENV["FOO"]).to be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "preserves environment variables set within the block" do
|
63
|
+
ENV["CHANGED"] = "old value"
|
64
|
+
|
65
|
+
with_modified_env IRRELEVANT: "ignored value" do
|
66
|
+
ENV["CHANGED"] = "new value"
|
67
|
+
end
|
68
|
+
|
69
|
+
expect(ENV["CHANGED"]).to eq "new value"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "returns the value of the block" do
|
73
|
+
value = with_modified_env VARIABLE_1: "bar" do
|
74
|
+
"value inside block"
|
75
|
+
end
|
76
|
+
|
77
|
+
expect(value).to eq "value inside block"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "handles threads correctly" do
|
81
|
+
# failure path without mutex
|
82
|
+
# [thread_removing_env] BAZ is assigned
|
83
|
+
# 0.25s passes
|
84
|
+
# [other_thread] FOO is assigned and ENV is copied (which includes BAZ)
|
85
|
+
# 0.25s passes
|
86
|
+
# [thread_removing_env] thread resolves and BAZ is removed from env; other_thread still retains knowledge of BAZ
|
87
|
+
# 0.25s passes
|
88
|
+
# [other_thread] thread resolves, FOO is removed, BAZ is copied back to ENV
|
89
|
+
|
90
|
+
thread_removing_env = Thread.new do
|
91
|
+
with_modified_env BAZ: "buzz" do
|
92
|
+
sleep 0.5
|
93
|
+
end
|
94
|
+
|
95
|
+
expect(ENV["BAZ"]).to be_nil
|
96
|
+
end
|
97
|
+
|
98
|
+
other_thread = Thread.new do
|
99
|
+
sleep 0.25
|
100
|
+
with_modified_env FOO: "bar" do
|
101
|
+
sleep 0.5
|
102
|
+
end
|
103
|
+
|
104
|
+
expect(ENV["FOO"]).to be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
thread_removing_env.join
|
108
|
+
other_thread.join
|
109
|
+
|
110
|
+
expect(ENV["FOO"]).to be_nil
|
111
|
+
expect(ENV["BAZ"]).to be_nil
|
112
|
+
end
|
113
|
+
|
114
|
+
it "raises when the value cannot be assigned properly" do
|
115
|
+
Thing = Class.new
|
116
|
+
message = generate_type_error_for_object(Thing.new)
|
117
|
+
|
118
|
+
expect do
|
119
|
+
with_modified_env(FOO: Thing.new)
|
120
|
+
end.to raise_error ClimateControl::UnassignableValueError, /attempted to assign .*Thing.* to FOO but failed \(#{message}\)$/
|
121
|
+
end
|
122
|
+
|
123
|
+
def with_modified_env(options, &block)
|
124
|
+
ClimateControl.modify(options, &block)
|
125
|
+
end
|
126
|
+
|
127
|
+
def generate_type_error_for_object(object)
|
128
|
+
message = nil
|
129
|
+
|
130
|
+
begin
|
131
|
+
"1" + object
|
132
|
+
rescue TypeError => e
|
133
|
+
message = e.message
|
134
|
+
end
|
135
|
+
|
136
|
+
message
|
137
|
+
end
|
138
|
+
|
139
|
+
around do |example|
|
140
|
+
old_env = ENV.to_hash
|
141
|
+
|
142
|
+
example.run
|
143
|
+
|
144
|
+
ENV.clear
|
145
|
+
old_env.each do |key, value|
|
146
|
+
ENV[key] = value
|
147
|
+
end
|
13
148
|
end
|
14
149
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
begin
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start
|
4
|
+
rescue LoadError
|
5
|
+
warn "warning: simplecov gem not found; skipping coverage"
|
6
|
+
end
|
3
7
|
|
4
|
-
$LOAD_PATH << File.join(File.dirname(__FILE__),
|
8
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
|
5
9
|
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
6
10
|
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
|
11
|
-
Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) }
|
11
|
+
require "rubygems"
|
12
|
+
require "rspec"
|
13
|
+
require "climate_control"
|
metadata
CHANGED
@@ -1,80 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: climate_control
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Joshua Clayton
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2017-01-06 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '3.0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
33
|
+
version: 3.1.0
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
40
|
+
version: 3.1.0
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- - ~>
|
45
|
+
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
47
|
+
version: 10.3.2
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- - ~>
|
52
|
+
- - "~>"
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
54
|
+
version: 10.3.2
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: simplecov
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- - ~>
|
59
|
+
- - "~>"
|
68
60
|
- !ruby/object:Gem::Version
|
69
|
-
version: 0.
|
61
|
+
version: 0.9.1
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- - ~>
|
66
|
+
- - "~>"
|
76
67
|
- !ruby/object:Gem::Version
|
77
|
-
version: 0.
|
68
|
+
version: 0.9.1
|
78
69
|
description: Modify your ENV
|
79
70
|
email:
|
80
71
|
- joshua.clayton@gmail.com
|
@@ -82,8 +73,8 @@ executables: []
|
|
82
73
|
extensions: []
|
83
74
|
extra_rdoc_files: []
|
84
75
|
files:
|
85
|
-
- .gitignore
|
86
|
-
- .
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
87
78
|
- Gemfile
|
88
79
|
- LICENSE.txt
|
89
80
|
- NEWS
|
@@ -91,37 +82,36 @@ files:
|
|
91
82
|
- Rakefile
|
92
83
|
- climate_control.gemspec
|
93
84
|
- lib/climate_control.rb
|
85
|
+
- lib/climate_control/environment.rb
|
86
|
+
- lib/climate_control/errors.rb
|
94
87
|
- lib/climate_control/modifier.rb
|
95
88
|
- lib/climate_control/version.rb
|
96
89
|
- spec/acceptance/climate_control_spec.rb
|
97
|
-
- spec/lib/climate_control/modifier_spec.rb
|
98
90
|
- spec/spec_helper.rb
|
99
91
|
homepage: https://github.com/thoughtbot/climate_control
|
100
|
-
licenses:
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
101
95
|
post_install_message:
|
102
96
|
rdoc_options: []
|
103
97
|
require_paths:
|
104
98
|
- lib
|
105
99
|
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
-
none: false
|
107
100
|
requirements:
|
108
|
-
- -
|
101
|
+
- - ">="
|
109
102
|
- !ruby/object:Gem::Version
|
110
103
|
version: '0'
|
111
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
-
none: false
|
113
105
|
requirements:
|
114
|
-
- -
|
106
|
+
- - ">="
|
115
107
|
- !ruby/object:Gem::Version
|
116
108
|
version: '0'
|
117
109
|
requirements: []
|
118
110
|
rubyforge_project:
|
119
|
-
rubygems_version:
|
111
|
+
rubygems_version: 2.6.4
|
120
112
|
signing_key:
|
121
|
-
specification_version:
|
113
|
+
specification_version: 4
|
122
114
|
summary: Modify your ENV easily with ClimateControl
|
123
115
|
test_files:
|
124
116
|
- spec/acceptance/climate_control_spec.rb
|
125
|
-
- spec/lib/climate_control/modifier_spec.rb
|
126
117
|
- spec/spec_helper.rb
|
127
|
-
has_rdoc:
|
@@ -1,83 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ClimateControl::Modifier do
|
4
|
-
it 'modifies the environment' do
|
5
|
-
with_modified_env VARIABLE_1: 'bar', VARIABLE_2: 'qux' do
|
6
|
-
expect(ENV['VARIABLE_1']).to eq 'bar'
|
7
|
-
expect(ENV['VARIABLE_2']).to eq 'qux'
|
8
|
-
end
|
9
|
-
|
10
|
-
expect(ENV['VARIABLE_1']).to be_nil
|
11
|
-
expect(ENV['VARIABLE_2']).to be_nil
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'allows for environment variables to be assigned within the block' do
|
15
|
-
with_modified_env VARIABLE_1: 'modified' do
|
16
|
-
ENV['ASSIGNED_IN_BLOCK'] = 'assigned'
|
17
|
-
end
|
18
|
-
|
19
|
-
expect(ENV['ASSIGNED_IN_BLOCK']).to eq 'assigned'
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'reassigns previously set environment variables' do
|
23
|
-
ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV'] = 'original'
|
24
|
-
expect(ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV']).to eq 'original'
|
25
|
-
|
26
|
-
with_modified_env VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV: 'overridden' do
|
27
|
-
expect(ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV']).to eq 'overridden'
|
28
|
-
end
|
29
|
-
|
30
|
-
expect(ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV']).to eq 'original'
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'persists the change when overriding the variable in the block' do
|
34
|
-
with_modified_env VARIABLE_MODIFIED_AND_THEN_ASSIGNED: 'modified' do
|
35
|
-
ENV['VARIABLE_MODIFIED_AND_THEN_ASSIGNED'] = 'assigned value'
|
36
|
-
end
|
37
|
-
|
38
|
-
expect(ENV['VARIABLE_MODIFIED_AND_THEN_ASSIGNED']).to eq 'assigned value'
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'resets environment variables even if the block raises' do
|
42
|
-
expect {
|
43
|
-
with_modified_env FOO: 'bar' do
|
44
|
-
raise 'broken'
|
45
|
-
end
|
46
|
-
}.to raise_error
|
47
|
-
|
48
|
-
expect(ENV['FOO']).to be_nil
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'preserves environment variables set within the block' do
|
52
|
-
ENV['CHANGED'] = 'old value'
|
53
|
-
|
54
|
-
with_modified_env IRRELEVANT: 'ignored value' do
|
55
|
-
ENV['CHANGED'] = 'new value'
|
56
|
-
end
|
57
|
-
|
58
|
-
expect(ENV['CHANGED']).to eq 'new value'
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'returns the value of the block' do
|
62
|
-
value = with_modified_env VARIABLE_1: 'bar' do
|
63
|
-
'value inside block'
|
64
|
-
end
|
65
|
-
|
66
|
-
expect(value).to eq 'value inside block'
|
67
|
-
end
|
68
|
-
|
69
|
-
def with_modified_env(options, &block)
|
70
|
-
ClimateControl::Modifier.new(options, &block).process
|
71
|
-
end
|
72
|
-
|
73
|
-
around do |example|
|
74
|
-
old_env = ENV.to_hash
|
75
|
-
|
76
|
-
example.run
|
77
|
-
|
78
|
-
ENV.clear
|
79
|
-
old_env.each do |key, value|
|
80
|
-
ENV[key] = value
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|