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