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