Added unit testing framework
authorAaron Bentley <abentley@panoramicfeedback.com>
Fri, 11 Mar 2005 21:49:42 +0000 (21:49 +0000)
committerAaron Bentley <abentley@panoramicfeedback.com>
Fri, 11 Mar 2005 21:49:42 +0000 (21:49 +0000)
libbe/cmdutil.py
libbe/plugin.py
test.py [new file with mode: 0644]

index 891e2738c7e2ef32172de0741f7f2df7574af89a..3fbfb50dac4a15703756ba20d0c538e1ba9d3352 100644 (file)
@@ -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 "<stdin>", 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")
+    <module 'becommands.list' from '/home/abentley/be/becommands/list.pyc'>
+    """
     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()
index 2dedac4a7ab716b9647ab370fcc28746172ef1ae..5f0fa4de53d42580153d9e15775a3bfc72ec04dc 100644 (file)
@@ -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")
+    <module 'becommands.list' from '/home/abentley/be/becommands/list.pyc'>
+    """
     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 (file)
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])