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