[Testing] Passing env through invoke() does not work
See original GitHub issueExplanation
I’m building a multi-commands cli with persistent configuration stored in ~/.config/myapp
Each time cli runs, it parses this file and passes it to context to be available to sub-commands.
The init command builds and writes configuration on disk.
For testing I want to use isolated_filesystem() and write config on it, using invoke() with env var.
src/config.py
if 'TMP_DIR' in environ:
CONFIG_DIRECTORY = Path(environ['TMP_DIR'])
else:
CONFIG_DIRECTORY = Path.home().joinpath(".config/myapp")
CONFIG_FILE = CONFIG_DIRECTORY.joinpath("config.json")
test/init_test.py
def test_init(runner):
with runner.isolated_filesystem() as fs:
# Adding config location to env
env = environ.copy()
env['TMP_DIR'] = fs
# Running init cmd with env
res = runner.invoke(cli, ['--verbose', 'init', '.'], env=env)
assert res.exit_code == 0
assert fs in res.output
Expected Behavior
Init command should exit successfully and print to stdout the directory where config file was created like this:
working directory initialized in /tmp/tmplmw_fea6
Actual Behavior
TMP_DIR
env is not passed to init command, so config is loaded from ~/.config/myapp
even in testing.
Environment
I use virtualenv with:
- Python version: 3.6.9
- Click version: 7.1.1
- Pytest version: 5.4.1
Thank you in advance 😄
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Powershell Invoke-Command passing environment variables
Note that assigning environment variables like this ( $env:VAR=<value> ) won't persist once your session ends. Use the Environment.
Read more >Gotchas — bats-core 1 documentation
My negated statement (e.g. ! true) does not fail the test, even when it should. I cannot register a test multiple times via...
Read more >How to monkeypatch/mock modules and environments - Pytest
1. Modifying the behavior of a function or the property of a class for a test e.g. there is an API call or...
Read more >Invoking Lambda functions locally - AWS Documentation
You can invoke your AWS Lambda function locally by using the sam local invoke AWS SAM CLI command and providing the function's logical...
Read more >Testing Rails Applications - Ruby on Rails Guides
In this case, we can modify our test environment by changing the options ... an expression is not changed before and after invoking...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Seems the issue is that the value of
CONFIG_FILE
was already set the moment you imported from config, and this happens before the env gets updated. So although the value is indeed there (can check by printingenviron.get("TMP_DIR")
inside ofread_config
), the actual value ofCONFIG_FILE
used inside of the function body will not reflect that.I made some changes to
read_config
so that it updates the value ofCONFIG_FILE
before evaluating the rest of the body, and the output is nowDEBUG config file loaded from /tmp/tmpjn_b9xq8/config.json
. Does this match the behavior you were expecting? Here is the code forked from your replit: https://repl.it/join/ljsynukg-amylei3Yes, it’s definitely the expecting behavior 👌 Thank you all for your time.