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