comparison tests/test-doctest.py @ 1097:80b8188a6eb1

tests: add doctest test so we don't regress with those
author Sean Farley <sean@farley.io>
date Thu, 30 Nov 2017 14:48:54 -0800
parents
children
comparison
equal deleted inserted replaced
1096:c64b73b9783f 1097:80b8188a6eb1
1 # this is hack to make sure no escape characters are inserted into the output
2
3 from __future__ import absolute_import
4
5 import doctest
6 import os
7 import re
8 import sys
9
10 ispy3 = (sys.version_info[0] >= 3)
11
12 # add hggit/ to sys.path
13 sys.path.insert(0, os.path.join(os.environ["TESTDIR"], ".."))
14
15 if 'TERM' in os.environ:
16 del os.environ['TERM']
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 got2 = re.sub(r'''^hggit\.\w+\.(\w+): (['"])(.*?)\2''', r'\1: \3',
25 got2, re.MULTILINE)
26 got2 = re.sub(r'^hggit\.\w+\.(\w+): ', r'\1: ', got2, re.MULTILINE)
27 return any(doctest.OutputChecker.check_output(self, w, g, optionflags)
28 for w, g in [(want, got), (want2, got2)])
29
30 def testmod(name, optionflags=0, testtarget=None):
31 __import__(name)
32 mod = sys.modules[name]
33 if testtarget is not None:
34 mod = getattr(mod, testtarget)
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()
45
46 testmod('hggit.compat')
47 testmod('hggit.hg2git')
48 testmod('hggit.util')
49 testmod('hggit.git_handler')