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