question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Testing] Passing env through invoke() does not work

See original GitHub issue

Explanation

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:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
amy-leicommented, Jun 30, 2020

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 printing environ.get("TMP_DIR") inside of read_config), the actual value of CONFIG_FILE used inside of the function body will not reflect that.

I made some changes to read_config so that it updates the value of CONFIG_FILE before evaluating the rest of the body, and the output is now DEBUG 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-amylei3

0reactions
LucasVanHaarencommented, Jul 2, 2020

I made some changes to read_config so that it updates the value of CONFIG_FILE before evaluating the rest of the body, and the output is now DEBUG config file loaded from /tmp/tmpjn_b9xq8/config.json. Does this match the behavior you were expecting? […]

Yes, it’s definitely the expecting behavior 👌 Thank you all for your time.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found