Show More
@@ -1,175 +1,175 | |||
|
1 | 1 | # this is hack to make sure no escape characters are inserted into the output |
|
2 | 2 | |
|
3 | 3 | from __future__ import absolute_import |
|
4 | 4 | from __future__ import print_function |
|
5 | 5 | |
|
6 | 6 | import doctest |
|
7 |
import os |
|
|
7 | import os | |
|
8 | 8 | import re |
|
9 | 9 | import subprocess |
|
10 | 10 | import sys |
|
11 | 11 | |
|
12 | 12 | ispy3 = sys.version_info[0] >= 3 |
|
13 | 13 | |
|
14 | 14 | if 'TERM' in os.environ: |
|
15 | 15 | del os.environ['TERM'] |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | class py3docchecker(doctest.OutputChecker): |
|
19 | 19 | def check_output(self, want, got, optionflags): |
|
20 | 20 | want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want) # py2: u'' |
|
21 | 21 | got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got) # py3: b'' |
|
22 | 22 | # py3: <exc.name>: b'<msg>' -> <name>: <msg> |
|
23 | 23 | # <exc.name>: <others> -> <name>: <others> |
|
24 | 24 | got2 = re.sub( |
|
25 | 25 | r'''^mercurial\.\w+\.(\w+): (['"])(.*?)\2''', |
|
26 | 26 | r'\1: \3', |
|
27 | 27 | got2, |
|
28 | 28 | re.MULTILINE, |
|
29 | 29 | ) |
|
30 | 30 | got2 = re.sub(r'^mercurial\.\w+\.(\w+): ', r'\1: ', got2, re.MULTILINE) |
|
31 | 31 | return any( |
|
32 | 32 | doctest.OutputChecker.check_output(self, w, g, optionflags) |
|
33 | 33 | for w, g in [(want, got), (want2, got2)] |
|
34 | 34 | ) |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | def testmod(name, optionflags=0, testtarget=None): |
|
38 | 38 | __import__(name) |
|
39 | 39 | mod = sys.modules[name] |
|
40 | 40 | if testtarget is not None: |
|
41 | 41 | mod = getattr(mod, testtarget) |
|
42 | 42 | |
|
43 | 43 | # minimal copy of doctest.testmod() |
|
44 | 44 | finder = doctest.DocTestFinder() |
|
45 | 45 | checker = None |
|
46 | 46 | if ispy3: |
|
47 | 47 | checker = py3docchecker() |
|
48 | 48 | runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags) |
|
49 | 49 | for test in finder.find(mod, name): |
|
50 | 50 | runner.run(test) |
|
51 | 51 | runner.summarize() |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | DONT_RUN = [] |
|
55 | 55 | |
|
56 | 56 | # Exceptions to the defaults for a given detected module. The value for each |
|
57 | 57 | # module name is a list of dicts that specify the kwargs to pass to testmod. |
|
58 | 58 | # testmod is called once per item in the list, so an empty list will cause the |
|
59 | 59 | # module to not be tested. |
|
60 | 60 | testmod_arg_overrides = { |
|
61 | 61 | 'i18n.check-translation': DONT_RUN, # may require extra installation |
|
62 | 62 | 'mercurial.dagparser': [{'optionflags': doctest.NORMALIZE_WHITESPACE}], |
|
63 | 63 | 'mercurial.keepalive': DONT_RUN, # >>> is an example, not a doctest |
|
64 | 64 | 'mercurial.posix': DONT_RUN, # run by mercurial.platform |
|
65 | 65 | 'mercurial.statprof': DONT_RUN, # >>> is an example, not a doctest |
|
66 | 66 | 'mercurial.util': [{}, {'testtarget': 'platform'}], # run twice! |
|
67 | 67 | 'mercurial.windows': DONT_RUN, # run by mercurial.platform |
|
68 | 68 | 'tests.test-url': [{'optionflags': doctest.NORMALIZE_WHITESPACE}], |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | fileset = 'set:(**.py)' |
|
72 | 72 | |
|
73 | 73 | cwd = os.path.dirname(os.environ["TESTDIR"]) |
|
74 | 74 | |
|
75 | 75 | if not os.path.isdir(os.path.join(cwd, ".hg")): |
|
76 | 76 | sys.exit(0) |
|
77 | 77 | |
|
78 | 78 | files = subprocess.check_output( |
|
79 | 79 | "hg files --print0 \"%s\"" % fileset, shell=True, cwd=cwd, |
|
80 | 80 | ).split(b'\0') |
|
81 | 81 | |
|
82 | 82 | if sys.version_info[0] >= 3: |
|
83 | 83 | cwd = os.fsencode(cwd) |
|
84 | 84 | |
|
85 | 85 | mods_tested = set() |
|
86 | 86 | for f in files: |
|
87 | 87 | if not f: |
|
88 | 88 | continue |
|
89 | 89 | |
|
90 | 90 | with open(os.path.join(cwd, f), "rb") as fh: |
|
91 | 91 | if not re.search(br'\n\s*>>>', fh.read()): |
|
92 | 92 | continue |
|
93 | 93 | |
|
94 | 94 | if ispy3: |
|
95 | 95 | f = f.decode() |
|
96 | 96 | |
|
97 | 97 | modname = f.replace('.py', '').replace('\\', '.').replace('/', '.') |
|
98 | 98 | |
|
99 | 99 | # Third-party modules aren't our responsibility to test, and the modules in |
|
100 | 100 | # contrib generally do not have doctests in a good state, plus they're hard |
|
101 | 101 | # to import if this test is running with py2, so we just skip both for now. |
|
102 | 102 | if modname.startswith('mercurial.thirdparty.') or modname.startswith( |
|
103 | 103 | 'contrib.' |
|
104 | 104 | ): |
|
105 | 105 | continue |
|
106 | 106 | |
|
107 | 107 | for kwargs in testmod_arg_overrides.get(modname, [{}]): |
|
108 | 108 | mods_tested.add((modname, '%r' % (kwargs,))) |
|
109 | 109 | if modname.startswith('tests.'): |
|
110 | 110 | # On py2, we can't import from tests.foo, but it works on both py2 |
|
111 | 111 | # and py3 with the way that PYTHONPATH is setup to import without |
|
112 | 112 | # the 'tests.' prefix, so we do that. |
|
113 | 113 | modname = modname[len('tests.') :] |
|
114 | 114 | |
|
115 | 115 | testmod(modname, **kwargs) |
|
116 | 116 | |
|
117 | 117 | # Meta-test: let's make sure that we actually ran what we expected to, above. |
|
118 | 118 | # Each item in the set is a 2-tuple of module name and stringified kwargs passed |
|
119 | 119 | # to testmod. |
|
120 | 120 | expected_mods_tested = set( |
|
121 | 121 | [ |
|
122 | 122 | ('hgext.convert.convcmd', '{}'), |
|
123 | 123 | ('hgext.convert.cvsps', '{}'), |
|
124 | 124 | ('hgext.convert.filemap', '{}'), |
|
125 | 125 | ('hgext.convert.p4', '{}'), |
|
126 | 126 | ('hgext.convert.subversion', '{}'), |
|
127 | 127 | ('hgext.fix', '{}'), |
|
128 | 128 | ('hgext.mq', '{}'), |
|
129 | 129 | ('mercurial.changelog', '{}'), |
|
130 | 130 | ('mercurial.cmdutil', '{}'), |
|
131 | 131 | ('mercurial.color', '{}'), |
|
132 | 132 | ('mercurial.config', '{}'), |
|
133 | 133 | ('mercurial.dagparser', "{'optionflags': 4}"), |
|
134 | 134 | ('mercurial.encoding', '{}'), |
|
135 | 135 | ('mercurial.fancyopts', '{}'), |
|
136 | 136 | ('mercurial.formatter', '{}'), |
|
137 | 137 | ('mercurial.hg', '{}'), |
|
138 | 138 | ('mercurial.hgweb.hgwebdir_mod', '{}'), |
|
139 | 139 | ('mercurial.match', '{}'), |
|
140 | 140 | ('mercurial.mdiff', '{}'), |
|
141 | 141 | ('mercurial.minirst', '{}'), |
|
142 | 142 | ('mercurial.parser', '{}'), |
|
143 | 143 | ('mercurial.patch', '{}'), |
|
144 | 144 | ('mercurial.pathutil', '{}'), |
|
145 | 145 | ('mercurial.pycompat', '{}'), |
|
146 | 146 | ('mercurial.revlogutils.deltas', '{}'), |
|
147 | 147 | ('mercurial.revset', '{}'), |
|
148 | 148 | ('mercurial.revsetlang', '{}'), |
|
149 | 149 | ('mercurial.simplemerge', '{}'), |
|
150 | 150 | ('mercurial.smartset', '{}'), |
|
151 | 151 | ('mercurial.store', '{}'), |
|
152 | 152 | ('mercurial.subrepo', '{}'), |
|
153 | 153 | ('mercurial.templater', '{}'), |
|
154 | 154 | ('mercurial.ui', '{}'), |
|
155 | 155 | ('mercurial.util', "{'testtarget': 'platform'}"), |
|
156 | 156 | ('mercurial.util', '{}'), |
|
157 | 157 | ('mercurial.utils.dateutil', '{}'), |
|
158 | 158 | ('mercurial.utils.stringutil', '{}'), |
|
159 | 159 | ('tests.drawdag', '{}'), |
|
160 | 160 | ('tests.test-run-tests', '{}'), |
|
161 | 161 | ('tests.test-url', "{'optionflags': 4}"), |
|
162 | 162 | ] |
|
163 | 163 | ) |
|
164 | 164 | |
|
165 | 165 | unexpectedly_run = mods_tested.difference(expected_mods_tested) |
|
166 | 166 | not_run = expected_mods_tested.difference(mods_tested) |
|
167 | 167 | |
|
168 | 168 | if unexpectedly_run: |
|
169 | 169 | print('Unexpectedly ran (probably need to add to list):') |
|
170 | 170 | for r in sorted(unexpectedly_run): |
|
171 | 171 | print(' %r' % (r,)) |
|
172 | 172 | if not_run: |
|
173 | 173 | print('Expected to run, but was not run (doctest removed?):') |
|
174 | 174 | for r in sorted(not_run): |
|
175 | 175 | print(' %r' % (r,)) |
General Comments 0
You need to be logged in to leave comments.
Login now