annotate tests/test-doctest.py @ 6900:1c7dcb1db06d mercurial-6.6

test-compat: merge mercurial-6.7 into mercurial-6.6
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 24 Oct 2024 17:57:13 +0400
parents 16fd24f6cf22
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
6864
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
70 files_cmd = 'hg files --print0 "%s"' % fileset
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
71
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
72 # we prefer system hg for reading the repository, unless we're on python2
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
73 # because then we assume that system hg is too old (this is not always true,
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
74 # but it's an easy check and works well enough for us)
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
75 if ispy3 and 'HGTEST_RESTOREENV':
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
76 files_cmd = '. $HGTEST_RESTOREENV; ' + files_cmd
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
77
16fd24f6cf22 tests: use system hg by restoring environment in test-doctest.py
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 6236
diff changeset
78 files = subprocess.check_output(files_cmd, shell=True, cwd=cwd).split(b'\0')
6235
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
79
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
80 if sys.version_info[0] >= 3:
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
81 cwd = os.fsencode(cwd)
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
82
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
83 mods_tested = set()
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
84 for f in files:
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
85 if not f:
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 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
89 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
90 continue
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
91
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
92 if ispy3:
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
93 f = f.decode()
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
94
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
95 modname = f.replace('.py', '').replace('\\', '.').replace('/', '.')
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 # 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
98 if modname.startswith('hgext3rd.evolve.thirdparty.'):
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
99 continue
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
100
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
101 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
102 mods_tested.add((modname, '%r' % (kwargs,)))
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
103 if modname.startswith('tests.'):
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
104 # 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
105 # 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
106 # the 'tests.' prefix, so we do that.
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
107 modname = modname[len('tests.'):]
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
108
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
109 testmod(modname, **kwargs)
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
110
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
111 # 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
112 # 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
113 # to testmod.
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
114 expected_mods_tested = set(
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 ('hgext3rd.evolve.obshistory', '{}'),
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 6235
diff changeset
117 ('hgext3rd.topic.common', '{}'),
6235
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 )
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
120
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
121 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
122 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
123
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
124 if unexpectedly_run:
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
125 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
126 for r in sorted(unexpectedly_run):
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
127 print(' %r' % (r,))
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
128 if not_run:
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
129 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
130 for r in sorted(not_run):
318b81560f8c tests: port test-doctest.py from core hg
Anton Shestakov <av6@dwimlabs.net>
parents:
diff changeset
131 print(' %r' % (r,))