Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
8f13c1eb
Commit
8f13c1eb
authored
Jun 27, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a slightly cleaner approach to stub ENV
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
99bdfd18
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
9 deletions
+27
-9
spec/support/stub_env.rb
spec/support/stub_env.rb
+27
-9
No files found.
spec/support/stub_env.rb
View file @
8f13c1eb
# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb
module
StubENV
module
StubENV
def
stub_env
(
key
,
value
)
def
stub_env
(
key_or_hash
,
value
=
nil
)
allow
(
ENV
).
to
receive
(
:[]
).
and_call_original
unless
@env_already_stubbed
init_stub
unless
env_stubbed?
@env_already_stubbed
||=
true
if
key_or_hash
.
is_a?
Hash
key_or_hash
.
each
{
|
k
,
v
|
add_stubbed_value
(
k
,
v
)
}
else
add_stubbed_value
key_or_hash
,
value
end
end
private
STUBBED_KEY
=
'__STUBBED__'
.
freeze
def
add_stubbed_value
(
key
,
value
)
allow
(
ENV
).
to
receive
(
:[]
).
with
(
key
).
and_return
(
value
)
allow
(
ENV
).
to
receive
(
:[]
).
with
(
key
).
and_return
(
value
)
allow
(
ENV
).
to
receive
(
:fetch
).
with
(
key
).
and_return
(
value
)
allow
(
ENV
).
to
receive
(
:fetch
).
with
(
key
,
anything
())
do
|
_
,
default_val
|
value
||
default_val
end
end
def
env_stubbed?
ENV
[
STUBBED_KEY
]
end
end
end
# It's possible that the state of the class variables are not reset across
def
init_stub
# test runs.
allow
(
ENV
).
to
receive
(
:[]
).
and_call_original
RSpec
.
configure
do
|
config
|
allow
(
ENV
).
to
receive
(
:fetch
).
and_call_original
config
.
after
(
:each
)
do
add_stubbed_value
(
STUBBED_KEY
,
true
)
@env_already_stubbed
=
nil
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment