Skip to content

Commit

Permalink
Fix environment variables propagation. (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldemarmiesse authored Apr 19, 2021
1 parent feca4f4 commit 28feb56
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 1 addition & 3 deletions python_on_whales/components/buildx/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import json
import os
import tempfile
from enum import Enum
from pathlib import Path
Expand Down Expand Up @@ -170,8 +169,7 @@ def bake(
full_cmd.add_simple_arg("--file", file)
full_cmd.add_args_list("--set", format_dict_for_cli(set))
targets = to_list(targets)
env = dict(os.environ)
env.update(variables)
env = dict(variables)
if print:
return json.loads(run(full_cmd + targets, env=env))
else:
Expand Down
4 changes: 1 addition & 3 deletions python_on_whales/components/stack/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import Dict, List, Optional, Union

Expand Down Expand Up @@ -81,8 +80,7 @@ def deploy(
full_cmd.add_flag("--with-registry-auth", with_registry_auth)
full_cmd.append(name)

env = dict(os.environ)
env.update(read_env_files([Path(x) for x in env_files]))
env = read_env_files([Path(x) for x in env_files])
env.update(variables)

run(full_cmd, capture_stdout=False, capture_stderr=False, env=env)
Expand Down
9 changes: 5 additions & 4 deletions python_on_whales/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import subprocess
import warnings
from pathlib import Path
Expand Down Expand Up @@ -102,11 +103,11 @@ def run(
env: Dict[str, str] = {},
) -> Union[str, Tuple[str, str]]:
args = [str(x) for x in args]
subprocess_env = dict(os.environ)
subprocess_env.update(env)
if args[1] == "buildx":
install_buildx_if_needed(args[0])
env["DOCKER_CLI_EXPERIMENTAL"] = "enabled"
if env == {}:
env = None
subprocess_env["DOCKER_CLI_EXPERIMENTAL"] = "enabled"
if capture_stdout:
stdout_dest = subprocess.PIPE
else:
Expand All @@ -116,7 +117,7 @@ def run(
else:
stderr_dest = None
completed_process = subprocess.run(
args, input=input, stdout=stdout_dest, stderr=stderr_dest, env=env
args, input=input, stdout=stdout_dest, stderr=stderr_dest, env=subprocess_env
)

if completed_process.returncode != 0:
Expand Down
11 changes: 11 additions & 0 deletions tests/python_on_whales/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@

def test_project_root():
assert (python_on_whales.utils.PROJECT_ROOT / "setup.py").exists()


def test_environment_variables_propagation(monkeypatch):
monkeypatch.setenv("SOME_VARIABLE", "dododada")

stdout = python_on_whales.utils.run(
["bash", "-c", "echo $SOME_VARIABLE && echo $OTHER_VARIABLE"],
capture_stdout=True,
env={"OTHER_VARIABLE": "dudu"},
)
assert stdout == "dododada\ndudu"

0 comments on commit 28feb56

Please sign in to comment.