-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcheck_doctest_examples.py
60 lines (48 loc) · 1.81 KB
/
check_doctest_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
"""Quick hack to conditionally evaluate doctest fragments."""
# Standard library modules.
import doctest
import glob
import logging
import os
import sys
# External dependencies.
import coloredlogs
from humanfriendly import format_path
# Modules included in our package.
from deb_pkg_tools.printer import CustomPrettyPrinter
# Initialize a logger.
logger = logging.getLogger('check-doctest-examples')
SAMPLES_DIRECTORY = '/var/lib/deb-pkg-tools/samples'
def main():
"""Command line interface."""
coloredlogs.install()
if not os.path.isdir(SAMPLES_DIRECTORY):
logger.info("Samples directory (%s) doesn't exist, skipping doctest checks ..", SAMPLES_DIRECTORY)
else:
failures = 0
for name in sorted(glob.glob('deb_pkg_tools/*.py')):
failures += testfile(name, verbose='-v' in sys.argv)
if failures > 0:
sys.exit(1)
def testfile(filename, verbose=False):
"""Evaluate and report on the doctest fragments in a single Python file."""
logger.info("Checking %s", format_path(filename))
printer = CustomPrettyPrinter()
filename = os.path.abspath(filename)
cwd_save = os.getcwd()
os.chdir(SAMPLES_DIRECTORY)
results = doctest.testfile(filename=filename,
module_relative=False,
globs=dict(repr=printer.pformat),
optionflags=doctest.NORMALIZE_WHITESPACE,
verbose=verbose)
if results.attempted > 0:
if results.failed == 0:
logger.info("Evaluated %i doctests, all passed!", results.attempted)
else:
logger.error("Evaluated %i doctests, %i failed!", results.attempted, results.failed)
os.chdir(cwd_save)
return results.failed
if __name__ == '__main__':
main()