├── Gemfile ├── .gitignore ├── lib ├── stub_env.rb └── stub_env │ └── helpers.rb ├── spec ├── spec_helper.rb ├── stub_env │ └── helpers_spec.rb └── support │ └── helper_tests.rb ├── gemfiles ├── rspec2.gemfile └── rspec3.gemfile ├── Rakefile ├── .travis.yml ├── stub_env.gemspec ├── LICENSE.txt └── README.md /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .ruby-gemset 2 | .ruby-version 3 | Gemfile.lock 4 | *.gem -------------------------------------------------------------------------------- /lib/stub_env.rb: -------------------------------------------------------------------------------- 1 | require "stub_env/helpers" 2 | 3 | module StubEnv 4 | end -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'stub_env' 2 | 3 | Dir[("./spec/support/**/*.rb")].each { |f| require f } -------------------------------------------------------------------------------- /gemfiles/rspec2.gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec path: ".." 4 | 5 | gem "rspec", "~> 2.0" -------------------------------------------------------------------------------- /gemfiles/rspec3.gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec path: ".." 4 | 5 | gem "rspec", "~> 3.0" -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 1.9.3 4 | - 2.0 5 | - 2.1 6 | - 2.2 7 | - 2.3 8 | - 2.4 9 | - 2.5 10 | - 2.6 11 | - ruby-head 12 | install: 13 | - "gem install bundler" 14 | - "bundle install --jobs=3 --retry=3" 15 | gemfile: 16 | - gemfiles/rspec2.gemfile 17 | - gemfiles/rspec3.gemfile 18 | -------------------------------------------------------------------------------- /spec/stub_env/helpers_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe StubEnv::Helpers do 4 | include described_class 5 | 6 | describe '#stub_env' do 7 | include_examples 'stub_env tests' 8 | 9 | methods = RSpec::Mocks::Configuration.public_instance_methods 10 | if methods.include?(:verify_partial_doubles=) 11 | context 'with the verify partial doubles option set' do 12 | before :each do 13 | RSpec.configuration.mock_with :rspec do |mocks| 14 | mocks.verify_partial_doubles = true 15 | end 16 | end 17 | 18 | include_examples 'stub_env tests' 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /stub_env.gemspec: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | Gem::Specification.new do |gem| 4 | gem.name = "stub_env" 5 | gem.version = "1.0.4" 6 | 7 | gem.authors = ["Liam Bennett"] 8 | gem.email = ["liam@littleowllabs.com"] 9 | gem.summary = "Stub ENV values in RSpec tests" 10 | gem.description = "RSpec helper for stubbing ENV values" 11 | gem.homepage = "https://github.com/littleowllabs/stub_env" 12 | gem.license = "MIT" 13 | 14 | gem.add_dependency "rspec", ">= 2.0", "< 4.0" 15 | 16 | gem.add_development_dependency "bundler", "~> 2.0" 17 | gem.add_development_dependency "rake", "~> 12.0" 18 | 19 | gem.files = `git ls-files`.split($\) 20 | gem.test_files = gem.files.grep(/^(spec)/) 21 | end 22 | -------------------------------------------------------------------------------- /lib/stub_env/helpers.rb: -------------------------------------------------------------------------------- 1 | module StubEnv 2 | module Helpers 3 | def stub_env(key_or_hash, value = nil) 4 | init_stub unless env_stubbed? 5 | if key_or_hash.is_a? Hash 6 | key_or_hash.each { |k, v| add_stubbed_value(k, v) } 7 | else 8 | add_stubbed_value key_or_hash, value 9 | end 10 | end 11 | 12 | private 13 | 14 | STUBBED_KEY = '__STUBBED__' 15 | 16 | def add_stubbed_value(key, value) 17 | allow(ENV).to receive(:[]).with(key).and_return(value) 18 | allow(ENV).to receive(:fetch).with(key).and_return(value) 19 | allow(ENV).to receive(:fetch).with(key, anything()) do |_, default_val| 20 | value || default_val 21 | end 22 | end 23 | 24 | def env_stubbed? 25 | ENV[STUBBED_KEY] 26 | end 27 | 28 | def init_stub 29 | allow(ENV).to receive(:[]).and_call_original 30 | allow(ENV).to receive(:fetch).and_call_original 31 | add_stubbed_value(STUBBED_KEY, true) 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright © 2014 LittlOwlLabs 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Stub Env 2 | ======== 3 | 4 | [![Gem Version](https://img.shields.io/gem/v/stub_env.svg?style=flat)](http://rubygems.org/gems/stub_env) 5 | [![Build Status](https://img.shields.io/travis/ljkbennett/stub_env/master.svg?style=flat)](https://travis-ci.org/littleowllabs/stub_env) 6 | 7 | This project provides a simple helper method to stub ENV values within RSpec tests. 8 | 9 | To start using it, install the gem in your project in the test group. 10 | 11 | ```ruby 12 | gem 'stub_env' 13 | ``` 14 | 15 | Then add the following to your rails_config.rb 16 | 17 | ```ruby 18 | RSpec.configure do |config| 19 | config.include StubEnv::Helpers 20 | end 21 | ``` 22 | 23 | There are 2 possible usages. The first is to stub a single value: 24 | 25 | ```ruby 26 | stub_env('key', 'value') 27 | 28 | puts ENV['key'] # => "value" 29 | puts ENV.fetch('key') # => "value" 30 | ``` 31 | 32 | These will work in series, so you can stub multiple values by making multiple calls: 33 | 34 | ```ruby 35 | stub_env('key1', 'value1') 36 | stub_env('key2', 'value2') 37 | 38 | puts ENV['key1'] # => "value1" 39 | puts ENV['key2'] # => "value2" 40 | puts ENV.fetch('key1') # => "value1" 41 | puts ENV.fetch('key2') # => "value2" 42 | ``` 43 | 44 | The second method is to stub multiple values using a hash: 45 | 46 | ```ruby 47 | stub_env({'key' => 'value', 'key2' => 'value2'}) 48 | 49 | puts ENV['key1'] # => "value1" 50 | puts ENV['key2'] # => "value2" 51 | puts ENV.fetch('key1') # => "value1" 52 | puts ENV.fetch('key2') # => "value2" 53 | ``` 54 | 55 | You can also use this multiple times, and in combination with the other method: 56 | 57 | ```ruby 58 | stub_env({'key1' => 'value1', 'key2' => 'value2'}) 59 | stub_env('key3', 'value3') 60 | stub_env({'key4' => 'value4'}) 61 | 62 | puts ENV['key1'] # => "value1" 63 | puts ENV['key2'] # => "value2" 64 | puts ENV['key3'] # => "value3" 65 | puts ENV['key4'] # => "value4" 66 | puts ENV.fetch('key1') # => "value1" 67 | puts ENV.fetch('key2') # => "value2" 68 | puts ENV.fetch('key3') # => "value3" 69 | puts ENV.fetch('key4') # => "value4" 70 | ``` 71 | 72 | All ENV values not stubbed will return their original values. 73 | 74 | ```ruby 75 | # Given ENV['unstubbed_key'] = 'unstubbed_value' 76 | stub_env('key', 'value') 77 | 78 | puts ENV['key'] # => "value" 79 | puts ENV['unstubbed_key'] # => "unstubbed_value" 80 | puts ENV.fetch('key') # => "value" 81 | puts ENV.fetch('unstubbed_key') # => "unstubbed_value" 82 | 83 | ``` 84 | -------------------------------------------------------------------------------- /spec/support/helper_tests.rb: -------------------------------------------------------------------------------- 1 | shared_examples 'stub_env tests' do 2 | before :each do 3 | ENV['UNSTUBBED'] = 'unstubbed' 4 | end 5 | 6 | describe '#[]' do 7 | context 'with a single stubbed variable' do 8 | before :each do 9 | stub_env('TEST', 'success') 10 | end 11 | it 'stubs out environment variables' do 12 | expect(ENV['TEST']).to eq 'success' 13 | end 14 | 15 | it 'leaves original environment variables unstubbed' do 16 | expect(ENV['UNSTUBBED']).to eq 'unstubbed' 17 | end 18 | end 19 | 20 | context 'with multiple stubbed variables' do 21 | before :each do 22 | stub_env('TEST', 'success') 23 | stub_env('TEST2', 'another success') 24 | end 25 | 26 | it 'stubs out the first variable' do 27 | expect(ENV['TEST']).to eq 'success' 28 | end 29 | 30 | it 'stubs out the second variable' do 31 | expect(ENV['TEST2']).to eq 'another success' 32 | end 33 | 34 | it 'leaves original environment variables unstubbed' do 35 | expect(ENV['UNSTUBBED']).to eq 'unstubbed' 36 | end 37 | end 38 | 39 | context 'with multiple stubbed variables in a hash' do 40 | before :each do 41 | stub_env({'TEST' => 'success', 'TEST2' => 'another success'}) 42 | end 43 | 44 | it 'stubs out the first variable' do 45 | expect(ENV['TEST']).to eq 'success' 46 | end 47 | 48 | it 'stubs out the second variable' do 49 | expect(ENV['TEST2']).to eq 'another success' 50 | end 51 | 52 | it 'leaves original environment variables unstubbed' do 53 | expect(ENV['UNSTUBBED']).to eq 'unstubbed' 54 | end 55 | end 56 | 57 | context 'with existing environment variables' do 58 | before :each do 59 | ENV['TO_OVERWRITE'] = 'to overwrite' 60 | end 61 | 62 | it 'returns the original value' do 63 | expect(ENV['TO_OVERWRITE']).to eq 'to overwrite' 64 | end 65 | 66 | it 'allows the original value to be stubbed' do 67 | stub_env('TO_OVERWRITE', 'overwritten') 68 | expect(ENV['TO_OVERWRITE']).to eq 'overwritten' 69 | end 70 | 71 | it 'allows the original value to be stubbed with nil' do 72 | stub_env('TO_OVERWRITE', nil) 73 | expect(ENV['TO_OVERWRITE']).to be_nil 74 | end 75 | end 76 | end 77 | 78 | describe '#fetch' do 79 | context 'with a single stubbed variable' do 80 | before :each do 81 | stub_env('TEST', 'success') 82 | end 83 | it 'stubs out environment variables' do 84 | expect(ENV.fetch('TEST')).to eq 'success' 85 | end 86 | 87 | it 'leaves original environment variables unstubbed' do 88 | expect(ENV.fetch('UNSTUBBED')).to eq 'unstubbed' 89 | end 90 | end 91 | 92 | context 'with multiple stubbed variables' do 93 | before :each do 94 | stub_env('TEST', 'success') 95 | stub_env('TEST2', 'another success') 96 | end 97 | 98 | it 'stubs out the first variable' do 99 | expect(ENV.fetch('TEST')).to eq 'success' 100 | end 101 | 102 | it 'stubs out the second variable' do 103 | expect(ENV.fetch('TEST2')).to eq 'another success' 104 | end 105 | 106 | it 'leaves original environment variables unstubbed' do 107 | expect(ENV.fetch('UNSTUBBED')).to eq 'unstubbed' 108 | end 109 | end 110 | 111 | context 'with multiple stubbed variables in a hash' do 112 | before :each do 113 | stub_env({'TEST' => 'success', 'TEST2' => 'another success'}) 114 | end 115 | 116 | it 'stubs out the first variable' do 117 | expect(ENV.fetch('TEST')).to eq 'success' 118 | end 119 | 120 | it 'stubs out the second variable' do 121 | expect(ENV.fetch('TEST2')).to eq 'another success' 122 | end 123 | 124 | it 'leaves original environment variables unstubbed' do 125 | expect(ENV.fetch('UNSTUBBED')).to eq 'unstubbed' 126 | end 127 | end 128 | 129 | context 'with existing environment variables' do 130 | before :each do 131 | ENV['TO_OVERWRITE'] = 'to overwrite' 132 | end 133 | 134 | it 'returns the original value' do 135 | expect(ENV.fetch('TO_OVERWRITE')).to eq 'to overwrite' 136 | end 137 | 138 | context 'stubbed with a value' do 139 | it 'allows the original value to be stubbed' do 140 | stub_env('TO_OVERWRITE', 'overwritten') 141 | expect(ENV.fetch('TO_OVERWRITE')).to eq 'overwritten' 142 | end 143 | 144 | it 'returns the stubbed value even if a default is specified' do 145 | stub_env('TO_OVERWRITE', 'overwritten') 146 | expect(ENV.fetch('TO_OVERWRITE', 'DEFAULT')).to eq 'overwritten' 147 | end 148 | end 149 | 150 | context 'stubbed with nil' do 151 | before :each do 152 | stub_env('TO_OVERWRITE', nil) 153 | end 154 | 155 | it 'returns nil' do 156 | expect(ENV.fetch('TO_OVERWRITE')).to be_nil 157 | end 158 | 159 | it 'returns the default if specified' do 160 | expect(ENV.fetch('TO_OVERWRITE', 'DEFAULT')).to eq 'DEFAULT' 161 | end 162 | end 163 | end 164 | end 165 | end 166 | --------------------------------------------------------------------------------