test_wheel.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = [wheel]
  33. if sys.version_info >= (3, 10):
  34. packages += ["pytest>=6.2.4"]
  35. else:
  36. packages += ["pytest"]
  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 subprocess.call([python, "-m", "pip", "install"] + packages) != 0:
  43. shutil.rmtree(envdir)
  44. sys.exit(1)
  45. # Run the test suite.
  46. test_cmd = [python, "-m", "pytest", "tests"]
  47. if verbose:
  48. test_cmd.append("--verbose")
  49. # Put the location of the python DLL on the path, for deploy-stub test
  50. # This is needed because venv does not install a copy of the python DLL
  51. env = None
  52. if sys.platform == "win32":
  53. deploy_libs = os.path.join(envdir, "Lib", "site-packages", "deploy_libs")
  54. if os.path.isdir(deploy_libs):
  55. # We have to do this dance because os.environ is case insensitive
  56. env = dict(os.environ)
  57. for key, value in env.items():
  58. if key.upper() == "PATH":
  59. env[key] = deploy_libs + ";" + value
  60. break
  61. else:
  62. env["PATH"] = deploy_libs
  63. exit_code = subprocess.call(test_cmd, env=env)
  64. shutil.rmtree(envdir)
  65. if exit_code != 0:
  66. sys.exit(exit_code)
  67. if __name__ == "__main__":
  68. parser = OptionParser(usage="%prog [options] file...")
  69. parser.add_option('', '--verbose', dest = 'verbose', help = 'Enable verbose output', action = 'store_true', default = False)
  70. (options, args) = parser.parse_args()
  71. if not args:
  72. parser.print_usage()
  73. sys.exit(1)
  74. for arg in args:
  75. test_wheel(arg, verbose=options.verbose)