Closed dzirtusss closed 8 months ago
This is a tricky one, in some use cases you want the environment to be populated through to your test suite, it really should be Figaro's job to clean the environment at the right point.
We even use certain environment variables to control how RSpec works so I certainly can't support making this a default!
We also cannot use Bundler.with_clean_env
as we do not depend on Bundler, and it might not be present. However I've just added a flag in #2632 which allows using Ruby's own "clean" env support where required, fancy taking a look?
I just ran into this very old issue today and was able to come up with a straight forward fix for anyone else who happens to run into it.
You can clear out ENV variables in Figaro by adding an empty value to the test environment. In our case we were expecting the ENV variable to be empty so adding it explicitly to the test environment was also a win for clarity.
# config/application.yml
development:
SOME_KEY: dev
test:
SOME_KEY:
Figaro.env.some_key # => nil
As indicated we released #2632 to allow handling situations like this, using a clean environment in the rake task as an opt in, so I'm closing this very old issue 😂
Subject of the issue
When using RSpec with Figaro (maybe other similar gems as well) following happen:
Figaro has env config as following:
when
rake spec
, env is started indevelopment
andDEV_ONLY_VAR
got populated and joined withENV
later
rake spec
spawns a testing process here with simplesystem(...)
call, which causesDEV_ONLY_VAR
to leak to test env as initialENV
var.This was discussed in here and in here but no actual fix was provided mostly because problem originates from rspec's rake task.
IMO,
system(...)
should be replaced withBundler.clean_system(...)
which will execute process with initial shell env (before dev env changes applied).Currently, I have a personal patch as following (which does the same but with monkey-patching):
If this seems to be a desired default behavior, I guess task logic should be updated. Alternatively, it will be ok to have this as a configurable option.
Your environment
Steps to reproduce
described in subject
Expected behavior
starts
system
in rake task with clean envActual behavior
starts
system
in rake task with dev env