selftest.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. """Test the copy of nose in this directory, by running that nose against itself.
  3. You can test nose using nose in other ways, but if you don't use this script,
  4. you might have one installation of nose testing another installation, which is
  5. not supported.
  6. """
  7. # More detail:
  8. # In the absence of some sort of deep renaming magic, nose can't reasonably
  9. # test a different installation of itself, given the existence of the global
  10. # module registry sys.modules .
  11. # If installed system-wide with setuptools, setuptools (via the site-packages
  12. # easy-install.pth) takes you at your word and ensures that the installed nose
  13. # comes first on sys.path . So the only way to test a copy of nose other than
  14. # the installed one is to install that version (e.g. by running python setup.py
  15. # develop).
  16. # This script provides a way of running nose on nose's own tests without
  17. # installing the version to be tested, nor uninstalling the currently-installed
  18. # version.
  19. import glob
  20. import os
  21. import sys
  22. if __name__ == "__main__":
  23. this_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  24. lib_dirs = [this_dir]
  25. test_dir = this_dir
  26. if sys.version_info >= (3,):
  27. # Under Python 3.x, we need to 'build' the source (using 2to3, etc)
  28. # first. 'python3 setup.py build_tests' will put everything under
  29. # build/tests (including nose itself, since some tests are inside the
  30. # nose source)
  31. # The 'py3where' argument in setup.cfg will take care of making sure we
  32. # pull our tests only from the build/tests directory. We just need to
  33. # make sure the right things are on sys.path.
  34. lib_dirs = glob.glob(os.path.join(this_dir, 'build', 'lib*'))
  35. test_dir = os.path.join(this_dir, 'build', 'tests')
  36. if not os.path.isdir(test_dir):
  37. raise AssertionError("Error: %s does not exist. Use the setup.py 'build_tests' command to create it." % (test_dir,))
  38. try:
  39. import pkg_resources
  40. env = pkg_resources.Environment(search_path=lib_dirs)
  41. distributions = env["nose"]
  42. assert len(distributions) == 1, (
  43. "Incorrect usage of selftest.py; please see DEVELOPERS.txt")
  44. dist = distributions[0]
  45. dist.activate()
  46. except ImportError:
  47. pass
  48. # Always make sure our chosen test dir is first on the path
  49. sys.path.insert(0, test_dir)
  50. import nose
  51. nose.run_exit()