Skip to content

Commit

Permalink
Make sure run_command properly combines envs (#3104)
Browse files Browse the repository at this point in the history
Because dictionary's update method updates data in place and does not
return anything back, the run_command method executed baked commands
with env overrides with an empty env. This commit makes sure
environments are properly combined.
  • Loading branch information
tadeboro authored May 14, 2021
1 parent 288dade commit 2347df5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:
- tox_env: lint
- tox_env: docs
- tox_env: py36
PREFIX: PYTEST_REQPASS=430
PREFIX: PYTEST_REQPASS=433
- tox_env: py37
PREFIX: PYTEST_REQPASS=430
PREFIX: PYTEST_REQPASS=433
- tox_env: py38
PREFIX: PYTEST_REQPASS=430
PREFIX: PYTEST_REQPASS=433
- tox_env: py39
PREFIX: PYTEST_REQPASS=430
PREFIX: PYTEST_REQPASS=433
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=430
PREFIX: PYTEST_REQPASS=433
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=430
PREFIX: PYTEST_REQPASS=433
- tox_env: packaging
- tox_env: eco
- tox_env: dockerfile
Expand Down
33 changes: 33 additions & 0 deletions src/molecule/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,39 @@ def test_run_command_with_debug(mocker, patched_print_debug):
assert x == patched_print_debug.mock_calls


def test_run_command_baked_env(mocker):
run_mock = mocker.patch.object(util, "run")
run_mock.return_value = mocker.Mock(returncode=0)
cmd = util.BakedCommand(cmd=["ls"], env=None)

util.run_command(cmd, env=dict(myvar="myrealvalue"))

# call_args[1] contains kwargs
assert run_mock.call_args[1]["env"] == dict(myvar="myrealvalue")


def test_run_command_baked_cmd_env(mocker):
run_mock = mocker.patch.object(util, "run")
run_mock.return_value = mocker.Mock(returncode=0)
cmd = util.BakedCommand(cmd=["ls"], env=dict(myvar="myvalue"))

util.run_command(cmd)

# call_args[1] contains kwargs
assert run_mock.call_args[1]["env"] == dict(myvar="myvalue")


def test_run_command_baked_both_envs(mocker):
run_mock = mocker.patch.object(util, "run")
run_mock.return_value = mocker.Mock(returncode=0)
cmd = util.BakedCommand(cmd=["ls"], env=dict(myvar="myvalue"))

util.run_command(cmd, env=dict(myvar="myrealvalue"))

# call_args[1] contains kwargs
assert run_mock.call_args[1]["env"] == dict(myvar="myrealvalue")


def test_run_command_with_debug_handles_no_env(mocker, patched_print_debug):
cmd = "ls"
util.run_command(cmd, debug=True)
Expand Down
5 changes: 4 additions & 1 deletion src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ def run_command(
"See https://github.com/ansible-community/molecule/issues/2678"
)
elif cmd.__class__.__name__ == "BakedCommand":
env = cmd.env if not env else cmd.env.copy().update(env)
if cmd.env and env:
env = dict(cmd.env, **env)
else:
env = cmd.env or env
args = cmd.cmd
stdout = cmd.stdout
stderr = cmd.stderr
Expand Down

0 comments on commit 2347df5

Please sign in to comment.