Skip to content

Commit

Permalink
Support stubbing of the ENV variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nepalez committed Mar 15, 2020
1 parent 369c022 commit 5897aa1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1 deletion.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Not released

### Added

- Support for stubbing ENV variables (nepalez)

```yaml
---
- env: GOOGLE_CLOUD_KEY
value: foo

- env: GOOGLE_CLOUD_PASSWORD
value: bar
```
This would stub selected variables only, not touching the others
## [0.3.0] - [2020-03-08]
### Added
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ For constants:
- `const` for stubbed constant
- `value` for a value of the constant

For environment variables:

- `env` for the name of a variable
`value` for a value of the variable

For http requests:

- `url` or `uri` for the URI of the request (treats values like `/.../` as regular expressions)
Expand Down Expand Up @@ -186,9 +191,14 @@ For http requests:
arguments:
- "Profile with id: 1 not found" # for error message
# Here we stubbing a constant
- const: NOTIFIER_TIMEOUT_SEC
value: 10
# This is a stub for ENV['DEFAULT_EMAIL']
- env: DEFAULT_EMAIL
value: [email protected]
# Examples for stubbing HTTP
- uri: /example.com/foo/ # regexp!
method: delete
Expand Down
3 changes: 2 additions & 1 deletion lib/fixturama/changes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Changes
require_relative "changes/base"
require_relative "changes/chain"
require_relative "changes/const"
require_relative "changes/env"
require_relative "changes/request"
require_relative "changes/seed"

Expand All @@ -20,6 +21,7 @@ class Changes
class: Chain,
const: Const,
count: Seed,
env: Env,
headers: Request,
http_method: Request,
object: Chain,
Expand All @@ -31,7 +33,6 @@ class Changes
type: Seed,
uri: Request,
url: Request,
value: Const,
}.freeze

# Adds new change to the registry
Expand Down
35 changes: 35 additions & 0 deletions lib/fixturama/changes/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Fixturama::Changes
#
# @private
# Stub an environment variable
#
class Env < Base
# All changes has the same +key+
# They will be merged before stubbing (see +call+)
def key
"ENV"
end

# When we merge 2 env-s, we just merge their options
def merge(other)
return self unless other.is_a?(self.class)
dup.tap { |env| env.options.update(other.options) }
end

def call(example)
original = Hash ENV
example.send(:stub_const, "ENV", original.merge(options))
self
end

protected

attr_reader :options

private

def initialize(**options)
@options = { options[:env].to_s => options[:value].to_s }
end
end
end
1 change: 1 addition & 0 deletions lib/fixturama/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module Fixturama
# The current version of the gem
VERSION = "0.3.0"
end
18 changes: 18 additions & 0 deletions spec/fixturama/stub_fixture/_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ def pay(_)
end
end

context "when ENV stubbed" do
before do
ENV["FOO"] = "foo"
ENV["BAR"] = "bar"
ENV["QUX"] = "qux"
stub_fixture "#{__dir__}/stub.yml"
end

it "stubs the ENV" do
expect(ENV["FOO"]).to eq "oof"
expect(ENV["BAR"]).to eq "rab"
end

it "preserves unstubbed ENV" do
expect(ENV["QUX"]).to eq "qux"
end
end

context "when http request stubbed" do
before { stub_fixture "#{__dir__}/stub.yml" }

Expand Down
6 changes: 6 additions & 0 deletions spec/fixturama/stub_fixture/stub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@
responses:
- status: 200
- status: 404 # for any request except for the first one

- env: FOO
value: oof

- env: BAR
value: rab

0 comments on commit 5897aa1

Please sign in to comment.