Debian Codegraph @

tests/test-doctest.py

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