Skip to content

Commit

Permalink
Set pip environment variables even if devmode is disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Apr 2, 2024
1 parent 048511f commit 7e6d503
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
59 changes: 34 additions & 25 deletions repo_helper/files/testing.py
Expand Up @@ -105,6 +105,29 @@ class ToxConfig(IniConfigurator):
"pytest",
]

def get_setenv(self, prefer_binary: bool = True, setuptools_stdlib=True) -> List[str]:
"""
Return environment variables to be set in the testenv.
:param prefer_binary: Whether pip should be configured to prefer older binary packages over newer source packages.
:param setuptools_stdlib: Whether setuptools should be configured to use the stdlib distutils.
"""

setenv = []

if self["enable_devmode"]:
setenv.append("PYTHONDEVMODE=1")

setenv.append("PIP_DISABLE_PIP_VERSION_CHECK=1")

if prefer_binary:
setenv.append("PIP_PREFER_BINARY=1")

if setuptools_stdlib:
setenv.append("SETUPTOOLS_USE_DISTUTILS=stdlib")

return setenv

def __init__(self, repo_path: pathlib.Path, templates: Environment):
self._globals = templates.globals

Expand Down Expand Up @@ -306,10 +329,7 @@ def testenv(self) -> None:
``[testenv]``.
"""

if self["enable_devmode"]:
self._ini["testenv"]["setenv"] = indent_join(
("PYTHONDEVMODE=1", "PIP_DISABLE_PIP_VERSION_CHECK=1", "SETUPTOOLS_USE_DISTUTILS=stdlib")
)
self._ini["testenv"]["setenv"] = indent_join(self.get_setenv(False))

if self["enable_tests"]:

Expand Down Expand Up @@ -391,19 +411,15 @@ def testenv_py312_dev(self) -> None:

for fixup_version in ["3.12-dev", "3.12", "3.13-dev"]:
if fixup_version in self["python_versions"]:
if self["enable_devmode"]:
env_name = f"testenv:py{fixup_version.replace('.', '')}"
if env_name in self._ini:
self._ini[env_name]["setenv"] = indent_join(
("PYTHONDEVMODE=1", "PIP_DISABLE_PIP_VERSION_CHECK=1")
)
setenv = self.get_setenv(False, False)
env_name = f"testenv:py{fixup_version.replace('.', '')}"
if env_name in self._ini:
self._ini[env_name]["setenv"] = indent_join(setenv)

for env in third_party_envs:
env_name = f"testenv:py{fixup_version.replace('.', '')}-{env}"
self._ini.add_section(env_name)
self._ini[env_name]["setenv"] = indent_join(
("PYTHONDEVMODE=1", "PIP_DISABLE_PIP_VERSION_CHECK=1")
)
for env in third_party_envs:
env_name = f"testenv:py{fixup_version.replace('.', '')}-{env}"
self._ini.add_section(env_name)
self._ini[env_name]["setenv"] = indent_join(setenv)

else:
self._ini.remove_section(f"testenv:py{fixup_version.replace('.', '')}")
Expand All @@ -416,10 +432,7 @@ def testenv__package(self) -> None:
"""

if self["enable_devmode"]:
self._ini["testenv:.package"]["setenv"] = indent_join(
("PYTHONDEVMODE=1", "PIP_DISABLE_PIP_VERSION_CHECK=1")
)

self._ini["testenv:.package"]["setenv"] = indent_join(self.get_setenv(False, False))
else:
self._ini.remove_section("testenv:.package")

Expand Down Expand Up @@ -456,11 +469,7 @@ def testenv_build(self) -> None:
``[testenv:build]``.
"""

if self["enable_devmode"]:
self._ini["testenv:build"]["setenv"] = indent_join(
("PYTHONDEVMODE=1", "PIP_DISABLE_PIP_VERSION_CHECK=1", "PIP_PREFER_BINARY=1")
)

self._ini["testenv:build"]["setenv"] = indent_join(self.get_setenv(setuptools_stdlib=False))
self._ini["testenv:build"]["skip_install"] = True
self._ini["testenv:build"]["changedir"] = "{toxinidir}"
self._ini["testenv:build"]["deps"] = indent_join([
Expand Down
6 changes: 6 additions & 0 deletions tests/test_files/test_testing_/test_make_tox_matrix.ini
Expand Up @@ -44,6 +44,9 @@ qa = mypy, lint
cov = py36-attrs19.3, coverage

[testenv]
setenv =
PIP_DISABLE_PIP_VERSION_CHECK=1
SETUPTOOLS_USE_DISTUTILS=stdlib
deps =
-r{toxinidir}/tests/requirements.txt
attrs19.3: attrs~=19.3.0
Expand All @@ -55,6 +58,9 @@ commands =
python -m pytest --cov=hello_world -r aR tests/ {posargs}

[testenv:build]
setenv =
PIP_DISABLE_PIP_VERSION_CHECK=1
PIP_PREFER_BINARY=1
skip_install = True
changedir = {toxinidir}
deps =
Expand Down
Expand Up @@ -36,6 +36,9 @@ qa = mypy, lint
cov = py36, coverage

[testenv]
setenv =
PIP_DISABLE_PIP_VERSION_CHECK=1
SETUPTOOLS_USE_DISTUTILS=stdlib
deps = -r{toxinidir}/tests/requirements.txt
commands =
python --version
Expand All @@ -50,6 +53,9 @@ deps = -r{toxinidir}/doc-source/requirements.txt
commands = sphinx-build -M {env:SPHINX_BUILDER:html} . ./build {posargs}

[testenv:build]
setenv =
PIP_DISABLE_PIP_VERSION_CHECK=1
PIP_PREFER_BINARY=1
skip_install = True
changedir = {toxinidir}
deps =
Expand Down

0 comments on commit 7e6d503

Please sign in to comment.