Skip to content

Commit

Permalink
Get unit tests running stably under xdist. (pex-tool#2529)
Browse files Browse the repository at this point in the history
This was mostly already working save for a few tests that needed
adjustment to be able to run consistently in isolation or under xdist
or both.
  • Loading branch information
jsirois authored Sep 14, 2024
1 parent 1c9ac9e commit 565092f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
11 changes: 5 additions & 6 deletions testing/bin/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ def main():
else:
logger.info("No test control environment variables set.")

args = [sys.executable, "-m", "pytest", "-n", "auto"]
if options.it:
pytest_args = ["-n", "auto", "tests/integration"]
args.append("tests/integration")
else:
pytest_args = ["tests", "--ignore", "tests/integration"]
args.extend(["tests", "--ignore", "tests/integration"])
args.extend(passthrough_args or ["-vvs"])

return subprocess.call(
args=[sys.executable, "-m", "pytest"] + pytest_args + passthrough_args or ["-vvs"],
cwd=pex_project_dir(),
)
return subprocess.call(args=args, cwd=pex_project_dir())


if __name__ == "__main__":
Expand Down
6 changes: 5 additions & 1 deletion tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ def create_venv(
):
# type: (...) -> List[str]
venv_dir = os.path.join(tmpdir, rel_path)
interpreter.execute(["-m", "venv", venv_dir])

# N.B.: We don't need pip in the venv for this test and sometimes system interpreters
# don't have ensurepip support.
interpreter.execute(["-m", "venv", "--without-pip", venv_dir])

return glob.glob(os.path.join(venv_dir, "bin", "python*"))

assert not real_interpreter.is_venv
Expand Down
10 changes: 8 additions & 2 deletions tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import site
import subprocess
import sys
import sysconfig
import tempfile
import textwrap
from contextlib import contextmanager
Expand Down Expand Up @@ -230,8 +231,13 @@ def test_site_libs(tmpdir):
def test_site_libs_symlink(tmpdir):
# type: (Any) -> None

sys_path_entry = os.path.join(str(tmpdir), "lib")
os.mkdir(sys_path_entry)
# N.B.: PythonIdentity.get() used below in the core test ends up consulting
# `packaging.tags.sys_tags()` which in turn consults `sysconfig`; so we need to make sure that
# bit of stdlib is on the sys.path. We get this by grabbing its sys.path entry, which contains
# the whole stdlib in addition to it.
assert sysconfig.__file__
sys_path_entry = os.path.dirname(sysconfig.__file__)

sys_path_entry_link = os.path.join(str(tmpdir), "lib-link")
os.symlink(sys_path_entry, sys_path_entry_link)

Expand Down
26 changes: 19 additions & 7 deletions tests/tools/commands/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,15 @@ def test_extract_deterministic_wheels(pex, pex_tools_env):
def test_extract_lifecycle(pex, pex_tools_env, tmpdir):
# type: (str, Dict[str, str], Any) -> None

# Since we'll be locking down indexes to just find-links, we need to include the wheel .whl
# needed by vendored Pip.
vendored_pip_dists_dir = os.path.join(str(tmpdir), "vendored-pip-dists")
# Since we'll be locking down indexes to just find-links, we need to include setuptools and
# wheel build deps for sdists.
build_reqs_dists_dir = os.path.join(str(tmpdir), "vendored-pip-dists")
get_pip(resolver=ConfiguredResolver.default()).spawn_download_distributions(
download_dir=vendored_pip_dists_dir,
requirements=[str(PipVersion.VENDORED.wheel_requirement)],
download_dir=build_reqs_dists_dir,
requirements=[
str(PipVersion.VENDORED.setuptools_requirement),
str(PipVersion.VENDORED.wheel_requirement),
],
build_configuration=BuildConfiguration.create(allow_builds=False),
).wait()

Expand Down Expand Up @@ -253,7 +256,7 @@ def test_extract_lifecycle(pex, pex_tools_env, tmpdir):
"--find-links",
find_links_url,
"--find-links",
vendored_pip_dists_dir,
build_reqs_dists_dir,
"example",
"-c",
"example",
Expand All @@ -265,7 +268,16 @@ def test_extract_lifecycle(pex, pex_tools_env, tmpdir):

_, pip = ensure_python_venv(PY310)
subprocess.check_call(
args=[pip, "install", "--no-index", "--find-links", find_links_url, "example"]
args=[
pip,
"install",
"--no-index",
"--find-links",
find_links_url,
"--find-links",
build_reqs_dists_dir,
"example",
]
)
example_console_script = os.path.join(os.path.dirname(pip), "example")

Expand Down
9 changes: 5 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ install_command =
# minimal setup.py since hatchling is buying us less and less and getting in the way more and
# more.
py{py27,py35,27,35}: python -m pip install {opts} {toxinidir} {packages}

docs,check,typecheck,py{py36,py37,py38,py39,py310,36,37,38,39,310,311,312,313}: \
python -m pip install {opts} -e {toxinidir} {packages}
!py{py27,py35,27,35}: python -m pip install {opts} -e {toxinidir} {packages}


commands =
!integration: python testing/bin/run_tests.py {posargs:-vvs}
integration: python testing/bin/run_tests.py --it {posargs:-vvs}
deps =
integration: pytest-xdist==1.34.0
pytest-xdist==1.34.0; python_version == "2.7"
pytest-xdist==2.2.1; python_version == "3.5"
pytest-xdist==2.5.0; python_version >= "3.6" and python_version < "3.8"
pytest-xdist==3.6.1; python_version >= "3.8"
ansicolors==1.1.8
coloredlogs==15.0.1
# The more-itertools project is an indirect requirement of pytest and its broken for
Expand Down

0 comments on commit 565092f

Please sign in to comment.