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