Mercurial > evolve
comparison tests/test-doctest.py @ 6235:318b81560f8c
tests: port test-doctest.py from core hg
We already have a doctest string for evolve.obshistory.cyclic(), but of course
it didn't actually run. Something like test-doctest.py is first needed to run
doctests via run-tests.py.
author | Anton Shestakov <av6@dwimlabs.net> |
---|---|
date | Thu, 12 May 2022 20:02:40 +0400 |
parents | |
children | 7ad8107d953a |
comparison
equal
deleted
inserted
replaced
6234:7e2dd2159414 | 6235:318b81560f8c |
---|---|
1 # this is hack to make sure no escape characters are inserted into the output | |
2 | |
3 from __future__ import absolute_import | |
4 from __future__ import print_function | |
5 | |
6 import doctest | |
7 import os | |
8 import re | |
9 import subprocess | |
10 import sys | |
11 | |
12 ispy3 = sys.version_info[0] >= 3 | |
13 | |
14 if 'TERM' in os.environ: | |
15 del os.environ['TERM'] | |
16 | |
17 | |
18 class py3docchecker(doctest.OutputChecker): | |
19 def check_output(self, want, got, optionflags): | |
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'' | |
22 # py3: <exc.name>: b'<msg>' -> <name>: <msg> | |
23 # <exc.name>: <others> -> <name>: <others> | |
24 # TODO: more output massaging | |
25 return any( | |
26 doctest.OutputChecker.check_output(self, w, g, optionflags) | |
27 for w, g in [(want, got), (want2, got2)] | |
28 ) | |
29 | |
30 | |
31 def testmod(name, optionflags=0, testtarget=None): | |
32 __import__(name) | |
33 mod = sys.modules[name] | |
34 if testtarget is not None: | |
35 mod = getattr(mod, testtarget) | |
36 | |
37 # minimal copy of doctest.testmod() | |
38 finder = doctest.DocTestFinder() | |
39 checker = None | |
40 if ispy3: | |
41 checker = py3docchecker() | |
42 runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags) | |
43 for test in finder.find(mod, name): | |
44 runner.run(test) | |
45 runner.summarize() | |
46 | |
47 | |
48 DONT_RUN = [] | |
49 | |
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. | |
52 # testmod is called once per item in the list, so an empty list will cause the | |
53 # module to not be tested. | |
54 testmod_arg_overrides = { | |
55 # 'mercurial.statprof': DONT_RUN, # >>> is an example, not a doctest | |
56 } | |
57 | |
58 fileset = 'set:(**.py)' | |
59 | |
60 cwd = os.path.dirname(os.environ["TESTDIR"]) | |
61 | |
62 # run-tests.py makes an installation of core Mercurial in /tmp/, but evolve is | |
63 # not installed together with it, and evolve modules need to be imported to run | |
64 # doctests. We insert it at the start to make sure wider install of evolve does not take priority. | |
65 sys.path.insert(0, cwd) | |
66 | |
67 if not os.path.isdir(os.path.join(cwd, ".hg")): | |
68 sys.exit(0) | |
69 | |
70 files = subprocess.check_output( | |
71 "hg files --print0 \"%s\"" % fileset, | |
72 shell=True, | |
73 cwd=cwd, | |
74 ).split(b'\0') | |
75 | |
76 if sys.version_info[0] >= 3: | |
77 cwd = os.fsencode(cwd) | |
78 | |
79 mods_tested = set() | |
80 for f in files: | |
81 if not f: | |
82 continue | |
83 | |
84 with open(os.path.join(cwd, f), "rb") as fh: | |
85 if not re.search(br'\n\s*>>>', fh.read()): | |
86 continue | |
87 | |
88 if ispy3: | |
89 f = f.decode() | |
90 | |
91 modname = f.replace('.py', '').replace('\\', '.').replace('/', '.') | |
92 | |
93 # Third-party modules aren't our responsibility to test. | |
94 if modname.startswith('hgext3rd.evolve.thirdparty.'): | |
95 continue | |
96 | |
97 for kwargs in testmod_arg_overrides.get(modname, [{}]): | |
98 mods_tested.add((modname, '%r' % (kwargs,))) | |
99 if modname.startswith('tests.'): | |
100 # On py2, we can't import from tests.foo, but it works on both py2 | |
101 # and py3 with the way that PYTHONPATH is setup to import without | |
102 # the 'tests.' prefix, so we do that. | |
103 modname = modname[len('tests.'):] | |
104 | |
105 testmod(modname, **kwargs) | |
106 | |
107 # Meta-test: let's make sure that we actually ran what we expected to, above. | |
108 # Each item in the set is a 2-tuple of module name and stringified kwargs passed | |
109 # to testmod. | |
110 expected_mods_tested = set( | |
111 [ | |
112 ('hgext3rd.evolve.obshistory', '{}'), | |
113 ] | |
114 ) | |
115 | |
116 unexpectedly_run = mods_tested.difference(expected_mods_tested) | |
117 not_run = expected_mods_tested.difference(mods_tested) | |
118 | |
119 if unexpectedly_run: | |
120 print('Unexpectedly ran (probably need to add to list):') | |
121 for r in sorted(unexpectedly_run): | |
122 print(' %r' % (r,)) | |
123 if not_run: | |
124 print('Expected to run, but was not run (doctest removed?):') | |
125 for r in sorted(not_run): | |
126 print(' %r' % (r,)) |