test_virtualenv.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os
  2. import subprocess
  3. import sys
  4. from urllib.error import URLError
  5. from urllib.request import urlopen
  6. import pytest
  7. @pytest.fixture(autouse=True)
  8. def pytest_virtualenv_works(venv):
  9. """
  10. pytest_virtualenv may not work. if it doesn't, skip these
  11. tests. See #1284.
  12. """
  13. venv_prefix = venv.run(["python", "-c", "import sys; print(sys.prefix)"]).strip()
  14. if venv_prefix == sys.prefix:
  15. pytest.skip("virtualenv is broken (see pypa/setuptools#1284)")
  16. def test_clean_env_install(venv_without_setuptools, setuptools_wheel):
  17. """
  18. Check setuptools can be installed in a clean environment.
  19. """
  20. cmd = ["python", "-m", "pip", "install", str(setuptools_wheel)]
  21. venv_without_setuptools.run(cmd)
  22. def access_pypi():
  23. # Detect if tests are being run without connectivity
  24. if not os.environ.get('NETWORK_REQUIRED', False): # pragma: nocover
  25. try:
  26. urlopen('https://pypi.org', timeout=1)
  27. except URLError:
  28. # No network, disable most of these tests
  29. return False
  30. return True
  31. @pytest.mark.skipif(
  32. 'platform.python_implementation() == "PyPy"',
  33. reason="https://github.com/pypa/setuptools/pull/2865#issuecomment-965834995",
  34. )
  35. @pytest.mark.skipif(not access_pypi(), reason="no network")
  36. # ^-- Even when it is not necessary to install a different version of `pip`
  37. # the build process will still try to download `wheel`, see #3147 and #2986.
  38. @pytest.mark.parametrize(
  39. 'pip_version',
  40. [
  41. None,
  42. pytest.param(
  43. 'pip<20.1',
  44. marks=pytest.mark.xfail(
  45. 'sys.version_info >= (3, 12)',
  46. reason="pip 23.1.2 required for Python 3.12 and later",
  47. ),
  48. ),
  49. pytest.param(
  50. 'pip<21',
  51. marks=pytest.mark.xfail(
  52. 'sys.version_info >= (3, 12)',
  53. reason="pip 23.1.2 required for Python 3.12 and later",
  54. ),
  55. ),
  56. pytest.param(
  57. 'pip<22',
  58. marks=pytest.mark.xfail(
  59. 'sys.version_info >= (3, 12)',
  60. reason="pip 23.1.2 required for Python 3.12 and later",
  61. ),
  62. ),
  63. pytest.param(
  64. 'pip<23',
  65. marks=pytest.mark.xfail(
  66. 'sys.version_info >= (3, 12)',
  67. reason="pip 23.1.2 required for Python 3.12 and later",
  68. ),
  69. ),
  70. pytest.param(
  71. 'https://github.com/pypa/pip/archive/main.zip',
  72. marks=pytest.mark.xfail(reason='#2975'),
  73. ),
  74. ],
  75. )
  76. def test_pip_upgrade_from_source(
  77. pip_version, venv_without_setuptools, setuptools_wheel, setuptools_sdist
  78. ):
  79. """
  80. Check pip can upgrade setuptools from source.
  81. """
  82. # Install pip/wheel, in a venv without setuptools (as it
  83. # should not be needed for bootstrapping from source)
  84. venv = venv_without_setuptools
  85. venv.run(["pip", "install", "-U", "wheel"])
  86. if pip_version is not None:
  87. venv.run(["python", "-m", "pip", "install", "-U", pip_version, "--retries=1"])
  88. with pytest.raises(subprocess.CalledProcessError):
  89. # Meta-test to make sure setuptools is not installed
  90. venv.run(["python", "-c", "import setuptools"])
  91. # Then install from wheel.
  92. venv.run(["pip", "install", str(setuptools_wheel)])
  93. # And finally try to upgrade from source.
  94. venv.run(["pip", "install", "--no-cache-dir", "--upgrade", str(setuptools_sdist)])
  95. def test_no_missing_dependencies(bare_venv, request):
  96. """
  97. Quick and dirty test to ensure all external dependencies are vendored.
  98. """
  99. setuptools_dir = request.config.rootdir
  100. bare_venv.run(['python', 'setup.py', '--help'], cwd=setuptools_dir)