##// END OF EJS Templates
doctest: normalize b'', u'' and exception output on Python 3...
Yuya Nishihara -
r34142:52ec9ac0 default
parent child Browse files
Show More
@@ -4,6 +4,7 b' from __future__ import absolute_import'
4
4
5 import doctest
5 import doctest
6 import os
6 import os
7 import re
7 import sys
8 import sys
8
9
9 ispy3 = (sys.version_info[0] >= 3)
10 ispy3 = (sys.version_info[0] >= 3)
@@ -11,6 +12,18 b' ispy3 = (sys.version_info[0] >= 3)'
11 if 'TERM' in os.environ:
12 if 'TERM' in os.environ:
12 del os.environ['TERM']
13 del os.environ['TERM']
13
14
15 class py3docchecker(doctest.OutputChecker):
16 def check_output(self, want, got, optionflags):
17 want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want) # py2: u''
18 got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got) # py3: b''
19 # py3: <exc.name>: b'<msg>' -> <name>: <msg>
20 # <exc.name>: <others> -> <name>: <others>
21 got2 = re.sub(r'''^mercurial\.\w+\.(\w+): (['"])(.*?)\2''', r'\1: \3',
22 got2, re.MULTILINE)
23 got2 = re.sub(r'^mercurial\.\w+\.(\w+): ', r'\1: ', got2, re.MULTILINE)
24 return any(doctest.OutputChecker.check_output(self, w, g, optionflags)
25 for w, g in [(want, got), (want2, got2)])
26
14 # TODO: migrate doctests to py3 and enable them on both versions
27 # TODO: migrate doctests to py3 and enable them on both versions
15 def testmod(name, optionflags=0, testtarget=None, py2=True, py3=False):
28 def testmod(name, optionflags=0, testtarget=None, py2=True, py3=False):
16 if not (not ispy3 and py2 or ispy3 and py3):
29 if not (not ispy3 and py2 or ispy3 and py3):
@@ -19,7 +32,16 b' def testmod(name, optionflags=0, testtar'
19 mod = sys.modules[name]
32 mod = sys.modules[name]
20 if testtarget is not None:
33 if testtarget is not None:
21 mod = getattr(mod, testtarget)
34 mod = getattr(mod, testtarget)
22 doctest.testmod(mod, optionflags=optionflags)
35
36 # minimal copy of doctest.testmod()
37 finder = doctest.DocTestFinder()
38 checker = None
39 if ispy3:
40 checker = py3docchecker()
41 runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags)
42 for test in finder.find(mod, name):
43 runner.run(test)
44 runner.summarize()
23
45
24 testmod('mercurial.changegroup')
46 testmod('mercurial.changegroup')
25 testmod('mercurial.changelog')
47 testmod('mercurial.changelog')
General Comments 0
You need to be logged in to leave comments. Login now