pywin32_testall.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """A test runner for pywin32"""
  2. import sys
  3. import os
  4. import site
  5. import subprocess
  6. # locate the dirs based on where this script is - it may be either in the
  7. # source tree, or in an installed Python 'Scripts' tree.
  8. this_dir = os.path.dirname(__file__)
  9. site_packages = [site.getusersitepackages(), ] + site.getsitepackages()
  10. # Run a test using subprocess and wait for the result.
  11. # If we get an returncode != 0, we know that there was an error.
  12. def run_test(script, cmdline_rest=""):
  13. dirname, scriptname = os.path.split(script)
  14. # some tests prefer to be run from their directory.
  15. cmd = [sys.executable, "-u", scriptname] + cmdline_rest.split()
  16. print(script)
  17. popen = subprocess.Popen(cmd, shell=True, cwd=dirname,
  18. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  19. data = popen.communicate()[0]
  20. if sys.version_info > (3,):
  21. sys.stdout.write(data.decode('latin-1'))
  22. else:
  23. sys.stdout.write(data)
  24. sys.stdout.flush()
  25. if popen.returncode:
  26. print("****** %s failed: %s" % (script, popen.returncode))
  27. sys.exit(popen.returncode)
  28. def find_and_run(possible_locations, script, cmdline_rest=""):
  29. for maybe in possible_locations:
  30. if os.path.isfile(os.path.join(maybe, script)):
  31. run_test(os.path.abspath(os.path.join(maybe, script)), cmdline_rest)
  32. break
  33. else:
  34. raise RuntimeError("Failed to locate the test script '%s' in one of %s"
  35. % (script, possible_locations))
  36. if __name__ == '__main__':
  37. import argparse
  38. code_directories = [this_dir] + site_packages
  39. parser = argparse.ArgumentParser(description="A script to trigger tests in all subprojects of PyWin32.")
  40. parser.add_argument("-no-user-interaction",
  41. default=False,
  42. action='store_true',
  43. help="Run all tests without user interaction")
  44. parser.add_argument("-skip-adodbapi",
  45. default=False,
  46. action='store_true',
  47. help="Skip the adodbapi tests; useful for CI where there's no provider")
  48. args = parser.parse_args()
  49. # win32
  50. maybes = [os.path.join(directory, "win32", "test") for directory in code_directories]
  51. command = ('testall.py', )
  52. if args.no_user_interaction:
  53. command += ("-no-user-interaction", )
  54. find_and_run(maybes, *command)
  55. # win32com
  56. maybes = [os.path.join(directory, "win32com", "test") for directory in [os.path.join(this_dir, "com"), ] + site_packages]
  57. find_and_run(maybes, 'testall.py', "2")
  58. # adodbapi
  59. if not args.skip_adodbapi:
  60. maybes = [os.path.join(directory, "adodbapi", "test") for directory in code_directories]
  61. find_and_run(maybes, 'adodbapitest.py')
  62. # This script has a hard-coded sql server name in it, (and markh typically
  63. # doesn't have a different server to test on) but there is now supposed to be a server out there on the Internet
  64. # just to run these tests, so try it...
  65. find_and_run(maybes, 'test_adodbapi_dbapi20.py')
  66. if sys.version_info > (3,):
  67. print("** The tests have some issues on py3k - not all failures are a problem...")