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