#!/usr/bin/env python # Copyright (c) 2007 RADLogic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """Run doctests in the given module. This grabs a bunch of code from pydoc. Usage: python run_doctest.py where item is a module name or path to a script/module/package (see pydoc --help for more details on what is acceptable here) Note: If item is a python filename in the current directory use ./name.py to make it unambiguous that item is a file. """ __author__ = 'Tim Wegener ' __date__ = '$Date: 2007/03/27 03:27:45 $' __version__ = '$Revision: 0.5 $' __credits__ = 'pydoc author: Ka-Ping Yee ' import os import sys import inspect import doctest from pydoc import ispackage, ispath, importfile, resolve def test_object(thing, forceload=0): """Run doctest on the given object.""" try: obj, name = resolve(thing, forceload) except ImportError: raise ImportError("No tests found for %r" % thing) doctest.testmod(obj, verbose=True) def test_dir(dirname, pkgpath='', done=None): """Run doctest over the given directory.""" if done is None: done = {} for filename in os.listdir(dirname): path = os.path.join(dirname, filename) if ispackage(path): test_dir(path, pkgpath + filename + '.', done) elif os.path.isfile(path): modname = inspect.getmodulename(path) if modname: if modname == '__init__': modname = pkgpath[:-1] # remove trailing period else: modname = pkgpath + modname if modname not in done: done[modname] = 1 test_object(modname) def main(): """Provide command line front end.""" args = sys.argv[1:] for arg in args: if ispath(arg) and not os.path.exists(arg): sys.stderr.write("Error: File does not exist: '%s'\n" % arg) sys.exit(1) scriptdir = os.path.dirname(os.path.abspath(arg)) if scriptdir not in sys.path: sys.path.insert(0, scriptdir) added_path = True else: added_path = False # Note: pydoc.ispath won't pick up a file in the current directory, # without any path hierarchy delimiters present. if ispath(arg) and os.path.isfile(arg): arg = importfile(arg) if ispath(arg) and os.path.isdir(arg): test_dir(arg) else: sys.stderr.write("Testing: %s\n" % arg) test_object(arg) if added_path: sys.path.remove(scriptdir) if __name__ == '__main__': main()