test_wheel.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. """
  3. Tests a .whl file by installing it and pytest into a virtual environment and
  4. running the test suite.
  5. Requires pip to be installed, as well as 'virtualenv' on Python 2.
  6. """
  7. import os
  8. import sys
  9. import shutil
  10. import subprocess
  11. import tempfile
  12. from optparse import OptionParser
  13. def test_wheel(wheel, verbose=False, ignores=[]):
  14. envdir = tempfile.mkdtemp(prefix="venv-")
  15. print("Setting up virtual environment in {0}".format(envdir))
  16. sys.stdout.flush()
  17. # Create a virtualenv.
  18. if sys.version_info >= (3, 0):
  19. subprocess.call([sys.executable, "-B", "-m", "venv", "--clear", envdir])
  20. else:
  21. subprocess.call([sys.executable, "-B", "-m", "virtualenv", "--clear", envdir])
  22. # Determine the path to the Python interpreter.
  23. if sys.platform == "win32":
  24. python = os.path.join(envdir, "Scripts", "python.exe")
  25. else:
  26. python = os.path.join(envdir, "bin", "python")
  27. # Upgrade pip inside the environment too.
  28. if subprocess.call([python, "-m", "pip", "install", "-U", "pip"]) != 0:
  29. shutil.rmtree(envdir)
  30. sys.exit(1)
  31. # Install pytest into the environment, as well as our wheel.
  32. packages = [wheel]
  33. if sys.version_info >= (3, 10):
  34. packages += ["pytest>=6.2.4"]
  35. else:
  36. packages += ["pytest>=3.9.0"]
  37. if sys.version_info[0:2] == (3, 4):
  38. if sys.platform == "win32":
  39. packages += ["colorama==0.4.1"]
  40. # See https://github.com/python-attrs/attrs/pull/807
  41. packages += ["attrs<21"]
  42. if sys.version_info >= (3, 12):
  43. packages += ["setuptools"]
  44. if subprocess.call([python, "-m", "pip", "install"] + packages) != 0:
  45. shutil.rmtree(envdir)
  46. sys.exit(1)
  47. # Run the test suite.
  48. test_cmd = [python, "-m", "pytest", "tests"]
  49. if verbose:
  50. test_cmd.append("--verbose")
  51. for ignore in ignores:
  52. test_cmd.append("--ignore")
  53. test_cmd.append(ignore)
  54. # Put the location of the python DLL on the path, for deploy-stub test
  55. # This is needed because venv does not install a copy of the python DLL
  56. env = None
  57. if sys.platform == "win32":
  58. deploy_libs = os.path.join(envdir, "Lib", "site-packages", "deploy_libs")
  59. if os.path.isdir(deploy_libs):
  60. # We have to do this dance because os.environ is case insensitive
  61. env = dict(os.environ)
  62. for key, value in env.items():
  63. if key.upper() == "PATH":
  64. env[key] = deploy_libs + ";" + value
  65. break
  66. else:
  67. env["PATH"] = deploy_libs
  68. exit_code = subprocess.call(test_cmd, env=env)
  69. shutil.rmtree(envdir)
  70. if exit_code != 0:
  71. sys.exit(exit_code)
  72. if __name__ == "__main__":
  73. parser = OptionParser(usage="%prog [options] file...")
  74. parser.add_option('', '--verbose', dest = 'verbose', help = 'Enable verbose output', action = 'store_true', default = False)
  75. parser.add_option('', '--ignore', dest = 'ignores', help = 'Ignores given test directory (may be repeated)', action = 'append', default = [])
  76. (options, args) = parser.parse_args()
  77. if not args:
  78. parser.print_usage()
  79. sys.exit(1)
  80. for arg in args:
  81. test_wheel(arg, verbose=options.verbose, ignores=options.ignores)