From 921a0ed5549d82bdd2e3f5ed7c4dd98ea704d8d4 Mon Sep 17 00:00:00 2001 From: Theodore Ni <3806110+tjni@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:48:32 -0700 Subject: [PATCH] Support running tests in more environments 1. Support running tests in test runners that do not import setuptools prior to running the test. 2. Pass along PATH and PYTHONPATH when running setup.py in a new process with with a new environment. --- setuptools_git_versioning.py | 5 +++++ tests/lib/util.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/setuptools_git_versioning.py b/setuptools_git_versioning.py index ab4ff00..19d7965 100644 --- a/setuptools_git_versioning.py +++ b/setuptools_git_versioning.py @@ -15,6 +15,11 @@ from pprint import pformat from typing import TYPE_CHECKING, Any, Callable +# because we use distutils in this file, we need to ensure that setuptools is +# imported first so that it can do monkey patching. this is not always already +# done for us, for example, when running this in a test or as a module +import setuptools # noqa: F401 + if TYPE_CHECKING: # avoid importing 'packaging' because setuptools-git-versioning can be installed using sdist # where 'packaging' is not installed yet diff --git a/tests/lib/util.py b/tests/lib/util.py index 3706a8b..ab218cf 100644 --- a/tests/lib/util.py +++ b/tests/lib/util.py @@ -30,6 +30,13 @@ def rand_sha() -> str: def execute(cwd: str | os.PathLike, cmd: str, **kwargs) -> str: log.info(f"Executing '{cmd}' at '{cwd}'") + + if "env" in kwargs: + kwargs["env"]["PATH"] = os.environ["PATH"] + pythonpath = os.getenv("PYTHONPATH", None) + if pythonpath: + kwargs["env"]["PYTHONPATH"] = pythonpath + return subprocess.check_output(cmd, cwd=cwd, shell=True, universal_newlines=True, **kwargs) # nosec