From 13c4ac425bf7df2df8929622496bb3fd5df2e801 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Thu, 13 Jul 2023 20:19:48 +0200 Subject: [PATCH] pmb.helpers.run_core: fix proxy env var logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix that the list "cmd" was turned into a string if one of the proxy vars was set in the environment. Add a test for this code path. Before this patch: $ FTP_PROXY=test pmbootstrap -v --details-to-stdout status … % cd /home/user/.local/var/pmbootstrap/cache_git/pmaports; git remote -v run: FTP_PROXY=test ['git', 'remote', '-v'] ERROR: [Errno 2] No such file or directory: "FTP_PROXY=test ['git', 'remote', '-v']" Fixes: 1a00c04f ("pmb.helpers.run_core: always configure proxy vars if set in environment") Reviewed-by: Clayton Craft Link: https://lists.sr.ht/~postmarketos/pmbootstrap-devel/%3C20230713182337.6185-3-ollieparanoid@postmarketos.org%3E --- pmb/helpers/run.py | 1 - pmb/helpers/run_core.py | 6 ++++-- test/test_run_core.py | 14 ++++++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) 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):