diff --git a/pmb/helpers/run.py b/pmb/helpers/run.py index efded208..7ffe00c1 100644 --- a/pmb/helpers/run.py +++ b/pmb/helpers/run.py @@ -1,6 +1,5 @@ # Copyright 2023 Oliver Smith # SPDX-License-Identifier: GPL-3.0-or-later -import shlex import pmb.helpers.run_core diff --git a/pmb/helpers/run_core.py b/pmb/helpers/run_core.py index 37d4db34..10c52afb 100644 --- a/pmb/helpers/run_core.py +++ b/pmb/helpers/run_core.py @@ -341,11 +341,13 @@ def core(args, log_message, cmd, working_dir=None, output="log", sanity_checks(output, output_return, check) # Preserve proxy environment variables + env = {} for var in ["FTP_PROXY", "ftp_proxy", "HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "HTTP_PROXY_AUTH"]: if var in os.environ: - # Prepend setting var to cmd string - cmd = f"{var}={os.environ[var]} {cmd}" + env[var] = os.environ[var] + if env: + cmd = ["sh", "-c", flat_cmd(cmd, env=env)] if args.sudo_timer and sudo: sudo_timer_start() diff --git a/test/test_run_core.py b/test/test_run_core.py index aa2aaf65..d120fc0f 100644 --- a/test/test_run_core.py +++ b/test/test_run_core.py @@ -1,10 +1,11 @@ # Copyright 2023 Oliver Smith # SPDX-License-Identifier: GPL-3.0-or-later """ Test pmb.helpers.run_core """ -import re -import sys -import subprocess +import os import pytest +import re +import subprocess +import sys import time import pmb_test # noqa @@ -127,7 +128,7 @@ def test_foreground_tui(): assert func(["echo", "test"]) == 0 -def test_core(args): +def test_core(args, monkeypatch): # Background func = pmb.helpers.run_core.core msg = "test" @@ -157,6 +158,11 @@ def test_core(args): func(args, msg, ["sleep", "1"], output="log") assert re.search(r"^Command failed \(exit code -?\d*\): ", str(e.value)) + # Preserve proxy environment variables + monkeypatch.setattr(os, "environ", {"FTP_PROXY": "testproxy"}) + ret = func(args, msg, ["sh", "-c", 'echo "$FTP_PROXY"'], output_return=True) + assert ret == "testproxy\n" + @pytest.mark.skip_ci def test_sudo_timer(args):