test_wheel.py 2.1 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. # Install pytest into the environment, as well as our wheel.
  32. packages = ["pytest", wheel]
  33. if sys.version_info[0:2] == (3, 4):
  34. if sys.platform == "win32":
  35. packages += ["colorama==0.4.1"]
  36. # See https://github.com/python-attrs/attrs/pull/807
  37. packages += ["attrs<21"]
  38. if subprocess.call([python, "-m", "pip", "install"] + packages) != 0:
  39. shutil.rmtree(envdir)
  40. sys.exit(1)
  41. # Run the test suite.
  42. test_cmd = [python, "-m", "pytest", "tests"]
  43. if verbose:
  44. test_cmd.append("--verbose")
  45. exit_code = subprocess.call(test_cmd)
  46. shutil.rmtree(envdir)
  47. if exit_code != 0:
  48. sys.exit(exit_code)
  49. if __name__ == "__main__":
  50. parser = OptionParser(usage="%prog [options] file...")
  51. parser.add_option('', '--verbose', dest = 'verbose', help = 'Enable verbose output', action = 'store_true', default = False)
  52. (options, args) = parser.parse_args()
  53. if not args:
  54. parser.print_usage()
  55. sys.exit(1)
  56. for arg in args:
  57. test_wheel(arg, verbose=options.verbose)