climate_control 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 65eb94d5a3f27af965178320b8b470c81b36cc646c55eda3b6471db78fca49a4
4
+ data.tar.gz: ba3abd5ba34a63c23f77195a7f9d4c8e5853f313812dc95b5d9ed37032f964c8
5
+ SHA512:
6
+ metadata.gz: 0a456cc8d8f3fb15cce8bf3401fbbf954b6ad2e7427a20a02fb35abf1da7852833ee8da7862ca4dfa2f98ef9ed62a29841c18d6ab5f40a1d3192073cd73bc621
7
+ data.tar.gz: 21879ffe4cc583c24fcb7e61fe1617f5126c5f606b4327cc6384cc81dfbff061fff6262c343054dee166f75c109769981354918c2e0481da4412dbf2cf211207
@@ -0,0 +1,23 @@
1
+ name: CI
2
+ on: [pull_request]
3
+ jobs:
4
+ tests:
5
+ name: Tests
6
+ runs-on: ubuntu-latest
7
+ strategy:
8
+ matrix:
9
+ ruby-version: [3.0, 2.7, 2.6, 2.5]
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - name: Set up Ruby ${{ matrix.ruby-version }}
13
+ uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby-version }}
16
+ - name: Install gems
17
+ run: |
18
+ bundle config path vendor/bundle
19
+ bundle install --jobs 4 --retry 3
20
+ - name: Run tests
21
+ run: bundle exec rake
22
+ - name: Run StandardRB
23
+ run: bundle exec standardrb
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ bin
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
@@ -0,0 +1,6 @@
1
+ # Code of Conduct
2
+
3
+ By participating in this project, you agree to abide by the
4
+ [thoughtbot code of conduct][1].
5
+
6
+ [1]: https://thoughtbot.com/open-source-code-of-conduct
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in climate_control.gemspec
4
4
  gemspec
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Joshua Clayton and thoughtbot, inc.
1
+ Copyright (c) 2012-2021 Joshua Clayton and thoughtbot, inc.
2
2
 
3
3
  MIT License
4
4
 
data/NEWS CHANGED
@@ -1,2 +1,19 @@
1
+ 1.0.0 (March 6, 2021)
2
+ Commit to supporting latest patch versions of Ruby 2.5+
3
+ Improve documentation
4
+ Format code with StandardRB
5
+ Bump gem dependencies
6
+
7
+ 0.2.0 (May 12, 2017)
8
+ Allow nested environment changes in the same thread
9
+
10
+ 0.1.0 (January 7, 2017)
11
+ Remove ActiveSupport dependency
12
+
13
+ 0.0.4 (January 6, 2017)
14
+ Improved thread safety
15
+ Handle TypeErrors during assignment
16
+ Improve documentation
17
+
1
18
  0.0.1 (November 28, 2012)
2
19
  Initial release
data/README.md CHANGED
@@ -18,14 +18,30 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- `ClimateControl` can be used to temporarily assign environment variables
21
+ Climate Control can be used to temporarily assign environment variables
22
22
  within a block:
23
23
 
24
24
  ```ruby
25
- ClimateControl::Modifier.new CONFIRMATION_INSTRUCTIONS_BCC: 'confirmation_bcc@example.com' do
25
+ ClimateControl.modify CONFIRMATION_INSTRUCTIONS_BCC: 'confirmation_bcc@example.com' do
26
26
  sign_up_as 'john@example.com'
27
+
28
+ confirm_account_for_email 'john@example.com'
29
+
30
+ expect(current_email).to bcc_to('confirmation_bcc@example.com')
31
+ end
32
+ ```
33
+
34
+ To modify multiple environment variables:
35
+
36
+ ```ruby
37
+ ClimateControl.modify CONFIRMATION_INSTRUCTIONS_BCC: 'confirmation_bcc@example.com',
38
+ MAIL_FROM: 'us@example.com' do
39
+ sign_up_as 'john@example.com'
40
+
27
41
  confirm_account_for_email 'john@example.com'
28
- current_email.should bcc_to('confirmation_bcc@example.com')
42
+
43
+ expect(current_email).to bcc_to('confirmation_bcc@example.com')
44
+ expect(current_email).to be_from('us@example.com')
29
45
  end
30
46
  ```
31
47
 
@@ -33,7 +49,7 @@ To use with RSpec, you could define this in your spec:
33
49
 
34
50
  ```ruby
35
51
  def with_modified_env(options, &block)
36
- ClimateControl::Modifier.new(options, &block)
52
+ ClimateControl.modify(options, &block)
37
53
  end
38
54
  ```
39
55
 
@@ -50,7 +66,7 @@ describe Thing, 'name' do
50
66
  end
51
67
 
52
68
  def with_modified_env(options, &block)
53
- ClimateControl::Modifier.new(options, &block)
69
+ ClimateControl.modify(options, &block)
54
70
  end
55
71
  end
56
72
  ```
@@ -63,7 +79,7 @@ describe Thing, 'name' do
63
79
  # ... tests
64
80
 
65
81
  around do |example|
66
- ClimateControl::Modifier.new FOO: 'bar' do
82
+ ClimateControl.modify FOO: 'bar' do
67
83
  example.run
68
84
  end
69
85
  end
@@ -76,6 +92,19 @@ block, except for the overrides. Transparency is crucial because the code
76
92
  executed within the block is not for `ClimateControl` to manage or modify. See
77
93
  the tests for more detail about the specific behaviors.
78
94
 
95
+ ## Why Use Climate Control?
96
+
97
+ By following guidelines regarding environment variables outlined by the
98
+ [twelve-factor app](http://12factor.net/config), testing code in an isolated
99
+ manner becomes more difficult:
100
+
101
+ * avoiding modifications and testing values, we introduce mystery guests
102
+ * making modifications and testing values, we introduce risk as environment
103
+ variables represent global state
104
+
105
+ Climate Control modifies environment variables only within the context of the
106
+ block, ensuring values are managed properly and consistently.
107
+
79
108
  ## Contributing
80
109
 
81
110
  1. Fork it
@@ -84,6 +113,8 @@ the tests for more detail about the specific behaviors.
84
113
  4. Push to the branch (`git push origin my-new-feature`)
85
114
  5. Create new Pull Request
86
115
 
116
+ This project uses [StandardRB](https://github.com/testdouble/standard) to ensure formatting.
117
+
87
118
  ## License
88
119
 
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.
120
+ climate_control is copyright 2012-2021 Joshua Clayton and thoughtbot, inc. It is free software and may be redistributed under the terms specified in the [LICENSE](https://github.com/thoughtbot/climate_control/blob/main/LICENSE) file.
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:rspec)
5
5
 
@@ -1,24 +1,23 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'climate_control/version'
3
+ require "climate_control/version"
5
4
 
6
5
  Gem::Specification.new do |gem|
7
- gem.name = 'climate_control'
8
- gem.version = ClimateControl::VERSION
9
- gem.authors = ['Joshua Clayton']
10
- gem.email = ['joshua.clayton@gmail.com']
11
- gem.description = %q{Modify your ENV}
12
- gem.summary = %q{Modify your ENV easily with ClimateControl}
13
- gem.homepage = 'https://github.com/thoughtbot/climate_control'
6
+ gem.name = "climate_control"
7
+ gem.version = ClimateControl::VERSION
8
+ gem.authors = ["Joshua Clayton"]
9
+ gem.email = ["joshua.clayton@gmail.com"]
10
+ gem.description = "Modify your ENV"
11
+ gem.summary = "Modify your ENV easily with ClimateControl"
12
+ gem.homepage = "https://github.com/thoughtbot/climate_control"
13
+ gem.license = "MIT"
14
14
 
15
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ['lib']
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
19
18
 
20
- gem.add_dependency 'activesupport', '>= 3.0'
21
- gem.add_development_dependency 'rspec', '~> 2.11'
22
- gem.add_development_dependency 'rake', '~> 0.9.2'
23
- gem.add_development_dependency 'simplecov', '~> 0.7.1'
19
+ gem.add_development_dependency "rspec", "~> 3.10.0"
20
+ gem.add_development_dependency "rake", "~> 12.3.3"
21
+ gem.add_development_dependency "simplecov", "~> 0.9.1"
22
+ gem.add_development_dependency "standard", "~> 1.0.0"
24
23
  end
@@ -1,5 +1,16 @@
1
- require 'climate_control/modifier'
2
- require 'climate_control/version'
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
+
9
+ def self.modify(environment_overrides, &block)
10
+ Modifier.new(env, environment_overrides, &block).process
11
+ end
12
+
13
+ def self.env
14
+ @@env
15
+ end
5
16
  end
@@ -0,0 +1,33 @@
1
+ require "forwardable"
2
+
3
+ module ClimateControl
4
+ class Environment
5
+ extend Forwardable
6
+
7
+ def initialize
8
+ @semaphore = Mutex.new
9
+ @owner = nil
10
+ end
11
+
12
+ def_delegators :env, :[]=, :to_hash, :[], :delete
13
+
14
+ def synchronize
15
+ if @owner == Thread.current
16
+ return yield if block_given?
17
+ end
18
+
19
+ @semaphore.synchronize do
20
+ @owner = Thread.current
21
+ yield if block_given?
22
+ ensure
23
+ @owner = nil
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def env
30
+ ENV
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ClimateControl
2
+ class UnassignableValueError < ArgumentError; end
3
+ end
@@ -1,18 +1,13 @@
1
- require 'active_support/core_ext/hash/keys'
2
-
3
1
  module ClimateControl
4
2
  class Modifier
5
- def initialize(environment_overrides = {}, &block)
6
- @environment_overrides = environment_overrides.dup.stringify_keys!
3
+ def initialize(env, environment_overrides = {}, &block)
4
+ @environment_overrides = stringify_keys(environment_overrides)
7
5
  @block = block
8
-
9
- process
6
+ @env = env
10
7
  end
11
8
 
12
- private
13
-
14
9
  def process
15
- begin
10
+ @env.synchronize do
16
11
  prepare_environment_for_block
17
12
  run_block
18
13
  ensure
@@ -22,6 +17,8 @@ module ClimateControl
22
17
  end
23
18
  end
24
19
 
20
+ private
21
+
25
22
  def prepare_environment_for_block
26
23
  @original_env = clone_environment
27
24
  copy_overrides_to_environment
@@ -34,7 +31,10 @@ module ClimateControl
34
31
 
35
32
  def copy_overrides_to_environment
36
33
  @environment_overrides.each do |key, value|
37
- ENV[key] = value
34
+ @env[key] = value
35
+ rescue TypeError => e
36
+ raise UnassignableValueError,
37
+ "attempted to assign #{value} to #{key} but failed (#{e.message})"
38
38
  end
39
39
  end
40
40
 
@@ -51,22 +51,28 @@ module ClimateControl
51
51
  end
52
52
 
53
53
  def delete_keys_that_do_not_belong
54
- (keys_to_remove - keys_changed_by_block).each {|key| ENV.delete(key) }
54
+ (keys_to_remove - keys_changed_by_block).each { |key| @env.delete(key) }
55
55
  end
56
56
 
57
57
  def revert_changed_keys
58
58
  (@original_env.keys - keys_changed_by_block).each do |key|
59
- ENV[key] = @original_env[key]
59
+ @env[key] = @original_env[key]
60
60
  end
61
61
  end
62
62
 
63
63
  def clone_environment
64
- ENV.to_hash
64
+ @env.to_hash
65
+ end
66
+
67
+ def stringify_keys(env)
68
+ env.each_with_object({}) do |(key, value), hash|
69
+ hash[key.to_s] = value
70
+ end
65
71
  end
66
72
 
67
73
  class OverlappingKeysWithChangedValues
68
74
  def initialize(hash_1, hash_2)
69
- @hash_1 = hash_1
75
+ @hash_1 = hash_1 || {}
70
76
  @hash_2 = hash_2
71
77
  end
72
78
 
@@ -1,3 +1,3 @@
1
1
  module ClimateControl
2
- VERSION = '0.0.1'
2
+ VERSION = "1.0.0".freeze
3
3
  end
@@ -0,0 +1,163 @@
1
+ require "spec_helper"
2
+
3
+ Thing = Class.new
4
+
5
+ describe "Climate control" do
6
+ it "allows modification of the environment" do
7
+ block_run = false
8
+ ClimateControl.modify FOO: "bar" do
9
+ expect(ENV["FOO"]).to eq "bar"
10
+ block_run = true
11
+ end
12
+
13
+ expect(ENV["FOO"]).to be_nil
14
+ expect(block_run).to be true
15
+ end
16
+
17
+ it "modifies the environment" do
18
+ with_modified_env VARIABLE_1: "bar", VARIABLE_2: "qux" do
19
+ expect(ENV["VARIABLE_1"]).to eq "bar"
20
+ expect(ENV["VARIABLE_2"]).to eq "qux"
21
+ end
22
+
23
+ expect(ENV["VARIABLE_1"]).to be_nil
24
+ expect(ENV["VARIABLE_2"]).to be_nil
25
+ end
26
+
27
+ it "allows for environment variables to be assigned within the block" do
28
+ with_modified_env VARIABLE_1: "modified" do
29
+ ENV["ASSIGNED_IN_BLOCK"] = "assigned"
30
+ end
31
+
32
+ expect(ENV["ASSIGNED_IN_BLOCK"]).to eq "assigned"
33
+ end
34
+
35
+ it "reassigns previously set environment variables" do
36
+ ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"] = "original"
37
+ expect(ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"]).to eq "original"
38
+
39
+ with_modified_env VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV: "overridden" do
40
+ expect(ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"]).to eq "overridden"
41
+ end
42
+
43
+ expect(ENV["VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV"]).to eq "original"
44
+ end
45
+
46
+ it "persists the change when overriding the variable in the block" do
47
+ with_modified_env VARIABLE_MODIFIED_AND_THEN_ASSIGNED: "modified" do
48
+ ENV["VARIABLE_MODIFIED_AND_THEN_ASSIGNED"] = "assigned value"
49
+ end
50
+
51
+ expect(ENV["VARIABLE_MODIFIED_AND_THEN_ASSIGNED"]).to eq "assigned value"
52
+ end
53
+
54
+ it "resets environment variables even if the block raises" do
55
+ expect {
56
+ with_modified_env FOO: "bar" do
57
+ raise "broken"
58
+ end
59
+ }.to raise_error("broken")
60
+
61
+ expect(ENV["FOO"]).to be_nil
62
+ end
63
+
64
+ it "preserves environment variables set within the block" do
65
+ ENV["CHANGED"] = "old value"
66
+
67
+ with_modified_env IRRELEVANT: "ignored value" do
68
+ ENV["CHANGED"] = "new value"
69
+ end
70
+
71
+ expect(ENV["CHANGED"]).to eq "new value"
72
+ end
73
+
74
+ it "returns the value of the block" do
75
+ value = with_modified_env VARIABLE_1: "bar" do
76
+ "value inside block"
77
+ end
78
+
79
+ expect(value).to eq "value inside block"
80
+ end
81
+
82
+ it "handles threads correctly" do
83
+ # failure path without mutex
84
+ # [thread_removing_env] BAZ is assigned
85
+ # 0.25s passes
86
+ # [other_thread] FOO is assigned and ENV is copied (which includes BAZ)
87
+ # 0.25s passes
88
+ # [thread_removing_env] thread resolves and BAZ is removed from env; other_thread still retains knowledge of BAZ
89
+ # 0.25s passes
90
+ # [other_thread] thread resolves, FOO is removed, BAZ is copied back to ENV
91
+
92
+ thread_removing_env = Thread.new {
93
+ with_modified_env BAZ: "buzz" do
94
+ sleep 0.5
95
+ end
96
+
97
+ expect(ENV["BAZ"]).to be_nil
98
+ }
99
+
100
+ other_thread = Thread.new {
101
+ sleep 0.25
102
+ with_modified_env FOO: "bar" do
103
+ sleep 0.5
104
+ end
105
+
106
+ expect(ENV["FOO"]).to be_nil
107
+ }
108
+
109
+ thread_removing_env.join
110
+ other_thread.join
111
+
112
+ expect(ENV["FOO"]).to be_nil
113
+ expect(ENV["BAZ"]).to be_nil
114
+ end
115
+
116
+ it "is re-entrant" do
117
+ ret = with_modified_env(FOO: "foo") {
118
+ with_modified_env(BAR: "bar") do
119
+ "bar"
120
+ end
121
+ }
122
+
123
+ expect(ret).to eq("bar")
124
+
125
+ expect(ENV["FOO"]).to be_nil
126
+ expect(ENV["BAR"]).to be_nil
127
+ end
128
+
129
+ it "raises when the value cannot be assigned properly" do
130
+ message = generate_type_error_for_object(Thing.new)
131
+
132
+ expect {
133
+ with_modified_env(FOO: Thing.new)
134
+ }.to raise_error ClimateControl::UnassignableValueError, /attempted to assign .*Thing.* to FOO but failed \(#{message}\)$/
135
+ end
136
+
137
+ def with_modified_env(options, &block)
138
+ ClimateControl.modify(options, &block)
139
+ end
140
+
141
+ def generate_type_error_for_object(object)
142
+ message = nil
143
+
144
+ begin
145
+ "1" + object
146
+ rescue TypeError => e
147
+ message = e.message
148
+ end
149
+
150
+ message
151
+ end
152
+
153
+ around do |example|
154
+ old_env = ENV.to_hash
155
+
156
+ example.run
157
+
158
+ ENV.clear
159
+ old_env.each do |key, value|
160
+ ENV[key] = value
161
+ end
162
+ end
163
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,13 @@
1
- require 'simplecov'
2
- SimpleCov.start
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__), '..', 'lib')
8
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
5
9
  $LOAD_PATH << File.join(File.dirname(__FILE__))
6
10
 
7
- require 'rubygems'
8
- require 'rspec'
9
- require 'climate_control'
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.1
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Joshua Clayton
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-28 00:00:00.000000000 Z
11
+ date: 2021-03-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: activesupport
14
+ name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '3.0'
22
- type: :runtime
19
+ version: 3.10.0
20
+ type: :development
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
- version: '3.0'
26
+ version: 3.10.0
30
27
  - !ruby/object:Gem::Dependency
31
- name: rspec
28
+ name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
- version: '2.11'
33
+ version: 12.3.3
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: '2.11'
40
+ version: 12.3.3
46
41
  - !ruby/object:Gem::Dependency
47
- name: rake
42
+ name: simplecov
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: 0.9.2
47
+ version: 0.9.1
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: 0.9.2
54
+ version: 0.9.1
62
55
  - !ruby/object:Gem::Dependency
63
- name: simplecov
56
+ name: standard
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ~>
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
- version: 0.7.1
61
+ version: 1.0.0
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.7.1
68
+ version: 1.0.0
78
69
  description: Modify your ENV
79
70
  email:
80
71
  - joshua.clayton@gmail.com
@@ -82,44 +73,45 @@ executables: []
82
73
  extensions: []
83
74
  extra_rdoc_files: []
84
75
  files:
85
- - .gitignore
86
- - .travisci.yml
76
+ - ".github/workflows/ci.yml"
77
+ - ".gitignore"
78
+ - CODE_OF_CONDUCT.md
87
79
  - Gemfile
88
- - LICENSE.txt
80
+ - LICENSE
89
81
  - NEWS
90
82
  - README.md
91
83
  - Rakefile
92
84
  - climate_control.gemspec
93
85
  - lib/climate_control.rb
86
+ - lib/climate_control/environment.rb
87
+ - lib/climate_control/errors.rb
94
88
  - lib/climate_control/modifier.rb
95
89
  - lib/climate_control/version.rb
96
- - spec/lib/climate_control/modifier_spec.rb
90
+ - spec/acceptance/climate_control_spec.rb
97
91
  - spec/spec_helper.rb
98
92
  homepage: https://github.com/thoughtbot/climate_control
99
- licenses: []
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
100
96
  post_install_message:
101
97
  rdoc_options: []
102
98
  require_paths:
103
99
  - lib
104
100
  required_ruby_version: !ruby/object:Gem::Requirement
105
- none: false
106
101
  requirements:
107
- - - ! '>='
102
+ - - ">="
108
103
  - !ruby/object:Gem::Version
109
104
  version: '0'
110
105
  required_rubygems_version: !ruby/object:Gem::Requirement
111
- none: false
112
106
  requirements:
113
- - - ! '>='
107
+ - - ">="
114
108
  - !ruby/object:Gem::Version
115
109
  version: '0'
116
110
  requirements: []
117
- rubyforge_project:
118
- rubygems_version: 1.8.23
111
+ rubygems_version: 3.1.4
119
112
  signing_key:
120
- specification_version: 3
113
+ specification_version: 4
121
114
  summary: Modify your ENV easily with ClimateControl
122
115
  test_files:
123
- - spec/lib/climate_control/modifier_spec.rb
116
+ - spec/acceptance/climate_control_spec.rb
124
117
  - spec/spec_helper.rb
125
- has_rdoc:
data/.travisci.yml DELETED
@@ -1,9 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.9.2
4
- - 1.9.3
5
- before_install:
6
- - gem update --system
7
- branches:
8
- only:
9
- - master
@@ -1,75 +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
- def with_modified_env(options, &block)
62
- ClimateControl::Modifier.new(options, &block)
63
- end
64
-
65
- around do |example|
66
- old_env = ENV.to_hash
67
-
68
- example.run
69
-
70
- ENV.clear
71
- old_env.each do |key, value|
72
- ENV[key] = value
73
- end
74
- end
75
- end