1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import os, sys, re |
---|
4 | |
---|
5 | modulename = None |
---|
6 | for i in xrange(1, len(sys.argv)): |
---|
7 | if not sys.argv[i].startswith('-'): |
---|
8 | modulename = sys.argv[i] |
---|
9 | break |
---|
10 | |
---|
11 | if modulename is None: |
---|
12 | raise AssertionError("no test module specified") |
---|
13 | |
---|
14 | __import__(modulename) |
---|
15 | srcfile = sys.modules[modulename].__file__ |
---|
16 | srcdir = os.path.dirname(os.path.realpath(srcfile)) |
---|
17 | for i in modulename.split('.'): |
---|
18 | srcdir = os.path.dirname(srcdir) |
---|
19 | |
---|
20 | if os.path.normcase(srcdir).endswith('.egg'): |
---|
21 | srcdir = os.path.dirname(srcdir) |
---|
22 | elif os.path.normcase(os.path.basename(srcdir)) == 'site-packages': |
---|
23 | srcdir = os.path.dirname(srcdir) |
---|
24 | if re.search(r'python.+\..+', os.path.normcase(os.path.basename(srcdir))): |
---|
25 | srcdir = os.path.dirname(srcdir) |
---|
26 | if os.path.normcase(os.path.basename(srcdir)) == 'lib': |
---|
27 | srcdir = os.path.dirname(srcdir) |
---|
28 | |
---|
29 | srcdir = os.path.normcase(os.path.normpath(srcdir)) |
---|
30 | cwd = os.path.normcase(os.path.normpath(os.getcwd())) |
---|
31 | |
---|
32 | same = (srcdir == cwd) |
---|
33 | if not same: |
---|
34 | try: |
---|
35 | same = os.path.samefile(srcdir, cwd) |
---|
36 | except AttributeError, e: |
---|
37 | e # hush pyflakes |
---|
38 | |
---|
39 | if not same: |
---|
40 | msg = ("We seem to be testing the code at %r\n" |
---|
41 | "(according to the source filename %r),\n" |
---|
42 | "but expected to be testing the code at %r.\n" |
---|
43 | % (srcdir, srcfile, cwd)) |
---|
44 | if (not isinstance(cwd, unicode) and |
---|
45 | cwd.decode(sys.getfilesystemencoding(), 'replace') != os.path.normcase(os.path.normpath(os.getcwdu()))): |
---|
46 | msg += ("However, this may be a false alarm because the current directory path\n" |
---|
47 | "is not representable in the filesystem encoding. This script needs to be\n" |
---|
48 | "run from the source directory to be tested, at a non-Unicode path.") |
---|
49 | else: |
---|
50 | msg += "This script needs to be run from the source directory to be tested." |
---|
51 | |
---|
52 | raise AssertionError(msg) |
---|
53 | |
---|
54 | from twisted.scripts.trial import run |
---|
55 | run() |
---|