test_wheel.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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):
  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. # Temp hack to patch issue pypa/pip#6885 in pip 19.2.2 and Python 3.8.
  32. if sys.platform == "win32" and "-cp38-cp38-" in wheel and os.path.isdir(os.path.join(envdir, "Lib", "site-packages", "pip-19.2.2.dist-info")):
  33. pep425tags = os.path.join(envdir, "Lib", "site-packages", "pip", "_internal", "pep425tags.py")
  34. if os.path.isfile(pep425tags):
  35. data = open(pep425tags, "r").read()
  36. data = data.replace(" m = 'm'\n", " m = ''\n")
  37. open(pep425tags, "w").write(data)
  38. # Install pytest into the environment, as well as our wheel.
  39. if subprocess.call([python, "-m", "pip", "install", "pytest", wheel]) != 0:
  40. shutil.rmtree(envdir)
  41. sys.exit(1)
  42. # Run the test suite.
  43. test_cmd = [python, "-m", "pytest", "tests"]
  44. if verbose:
  45. test_cmd.append("--verbose")
  46. exit_code = subprocess.call(test_cmd)
  47. shutil.rmtree(envdir)
  48. if exit_code != 0:
  49. sys.exit(exit_code)
  50. if __name__ == "__main__":
  51. parser = OptionParser(usage="%prog [options] file...")
  52. parser.add_option('', '--verbose', dest = 'verbose', help = 'Enable verbose output', action = 'store_true', default = False)
  53. (options, args) = parser.parse_args()
  54. if not args:
  55. parser.print_usage()
  56. sys.exit(1)
  57. for arg in args:
  58. test_wheel(arg, verbose=options.verbose)