File: run-doctests.py

package info (click to toggle)
cram 0.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 336 kB
  • sloc: python: 908; makefile: 46; sh: 20
file content (40 lines) | stat: -rw-r--r-- 1,182 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env python

import doctest
import os
import sys

def _getmodules(pkgdir):
    """Import and yield modules in pkgdir"""
    for root, dirs, files in os.walk(pkgdir):
        if '__pycache__' in dirs:
            dirs.remove('__pycache__')
        for fn in files:
            if not fn.endswith('.py') or fn == '__main__.py':
                continue

            modname = fn.replace(os.sep, '.')[:-len('.py')]
            if modname.endswith('.__init__'):
                modname = modname[:-len('.__init__')]
            modname = '.'.join(['cram', modname])
            if '.' in modname:
                fromlist = [modname.rsplit('.', 1)[1]]
            else:
                fromlist = []

            yield __import__(modname, {}, {}, fromlist)

def rundoctests(pkgdir):
    """Run doctests in the given package directory"""
    totalfailures = totaltests = 0
    for module in _getmodules(pkgdir):
        failures, tests = doctest.testmod(module)
        totalfailures += failures
        totaltests += tests
    return totalfailures != 0

if __name__ == '__main__':
    try:
        sys.exit(rundoctests(sys.argv[1]))
    except KeyboardInterrupt:
        pass