From 71597577918f8d8206256293a7c985bc800a1363 Mon Sep 17 00:00:00 2001 From: Aaron Bentley Date: Fri, 11 Mar 2005 21:49:42 +0000 Subject: Added unit testing framework --- libbe/cmdutil.py | 19 +++++++++++++++++++ libbe/plugin.py | 29 ++++++++++++++++++++++++++--- test.py | 20 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 test.py diff --git a/libbe/cmdutil.py b/libbe/cmdutil.py index 891e273..3fbfb50 100644 --- a/libbe/cmdutil.py +++ b/libbe/cmdutil.py @@ -55,6 +55,17 @@ def iter_commands(): yield name.replace("_", "-"), module def get_command(command_name): + """Retrieves the module for a user command + + >>> get_command("asdf") + Traceback (most recent call last): + File "", line 1, in ? + File "/home/abentley/be/libbe/cmdutil.py", line 60, in get_command + raise UserError("Unknown command %s" % command_name) + UserError: Unknown command asdf + >>> get_command("list") + + """ cmd = plugin.get_plugin("becommands", command_name.replace("-", "_")) if cmd is None: raise UserError("Unknown command %s" % command_name) @@ -65,3 +76,11 @@ def execute(cmd, args): def help(cmd, args): return get_command(cmd).help() + +def _test(): + import doctest + import sys + doctest.testmod() + +if __name__ == "__main__": + _test() diff --git a/libbe/plugin.py b/libbe/plugin.py index 2dedac4..5f0fa4d 100644 --- a/libbe/plugin.py +++ b/libbe/plugin.py @@ -9,7 +9,13 @@ def my_import(mod_name): return module def iter_plugins(prefix): - modfiles = os.listdir(os.path.join(sys.path[0], prefix)) + """ + >>> "list" in [n for n,m in iter_plugins("becommands")] + True + >>> "plugin" in [n for n,m in iter_plugins("libbe")] + True + """ + modfiles = os.listdir(os.path.join(plugin_path, prefix)) modfiles.sort() for modfile in modfiles: if modfile.endswith(".py") and modfile != "__init__.py": @@ -17,9 +23,26 @@ def iter_plugins(prefix): def get_plugin(prefix, name): + """ + >>> get_plugin("becommands", "asdf") is None + True + >>> get_plugin("becommands", "list") + + """ dirprefix = '/'.join(prefix.split('.')) - command_path = os.path.join(sys.path[0], dirprefix, name+".py") + command_path = os.path.join(plugin_path, dirprefix, name+".py") if os.path.isfile(command_path): return my_import(prefix + "." + name) return None - + +plugin_path = sys.path[0] +while not os.path.isfile(os.path.join(plugin_path, "libbe/plugin.py")): + plugin_path = os.path.realpath(os.path.dirname(plugin_path)) +if plugin_path not in sys.path: + sys.path.append(plugin_path) +def _test(): + import doctest + doctest.testmod() + +if __name__ == "__main__": + _test() diff --git a/test.py b/test.py new file mode 100644 index 0000000..8913c1f --- /dev/null +++ b/test.py @@ -0,0 +1,20 @@ +from libbe import plugin +import doctest +import sys +if len(sys.argv) > 1: + match = False + mod = plugin.get_plugin("libbe", sys.argv[1]) + if mod is not None: + doctest.testmod(mod) + match = True + mod = plugin.get_plugin("becommands", sys.argv[1]) + if mod is not None: + doctest.testmod(mod) + match = True + if not match: + print "No modules match \"%s\"" % sys.argv[1] +else: + for module in plugin.iter_plugins("libbe"): + doctest.testmod(module[1]) + for module in plugin.iter_plugins("becommands"): + doctest.testmod(module[1]) -- cgit