automate test system
authorAlexander Solovyov <piranha@piranha.org.ua>
Sun, 11 Oct 2009 14:19:47 +0300
changeset 96 82aaeccffbec
parent 95 5d92ba281fe8
child 97 46afc08b68ff
automate test system
test.py
test_cmd.py
test_opts.py
tests/runtests
tests/test
tests/test.out
tests/test.py
tests/test2
tests/test_cmd
tests/test_cmd.out
tests/test_cmd.py
tests/test_opts
tests/test_opts.err
tests/test_opts.out
tests/test_opts.py
--- a/test.py	Sun Oct 11 11:53:36 2009 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-
-from opster import dispatch, command
-
-
-@command(usage='[-t]', shortlist=True)
-def simple(ui,
-           test=('t', False, 'just test execution')):
-    '''Just simple command to do nothing.
-
-    I assure you! Nothing to look here. ;-)
-    '''
-    ui.write(str(locals()))
-    ui.write('\n')
-
-cplx_opts = [('p', 'pass', False, 'don\'t run the command'),
-             ('', 'exit', 0, 'exit with supplied code (default: 0)'),
-             ('n', 'name', '', 'optional name')]
-
-@command(cplx_opts, usage='[-p] [--exit value] ...', name='complex', hide=True)
-def complex_(ui, *args, **opts):
-    '''That's more complex command indented to do something
-
-    Let's try to do that (what?!)
-    '''
-    if opts.get('pass'):
-        return
-    # test ui
-    ui.write('write\n')
-    ui.note('note\n')
-    ui.info('info\n')
-    ui.warn('warn\n')
-    if opts.get('exit'):
-        sys.exit(opts['exit'])
-
-def ui_middleware(func):
-    def extract_dict(source, *keys):
-        dest = {}
-        for k in keys:
-            dest[k] = source.pop(k, None)
-        return dest
-
-    def inner(*args, **kwargs):
-        opts = extract_dict(kwargs, 'verbose', 'quiet')
-        if func.__name__ == 'help_inner':
-            return func(*args, **kwargs)
-        ui = UI(**opts)
-        return func(ui, *args, **kwargs)
-    return inner
-
-class UI(object):
-    '''User interface helper.
-
-    Intended to ease handling of quiet/verbose output and more.
-
-    You have three methods to handle program messages output:
-
-      - ``UI.info`` is printed by default, but hidden with quiet option
-      - ``UI.note`` is printed only if output is verbose
-      - ``UI.write`` is printed in any case
-
-    Additionally there is ``UI.warn`` method, which prints to stderr.
-    '''
-
-    options = [('v', 'verbose', False, 'enable additional output'),
-               ('q', 'quiet', False, 'suppress output')]
-
-    def __init__(self, verbose=False, quiet=False):
-        self.verbose = verbose
-        # disabling quiet in favor of verbose is more safe
-        self.quiet = (not verbose and quiet)
-
-    def write(self, *messages):
-        for m in messages:
-            sys.stdout.write(m)
-
-    def warn(self, *messages):
-        for m in messages:
-            sys.stderr.write(m)
-
-    info = lambda self, *m: not self.quiet and self.write(*m)
-    note = lambda self, *m: self.verbose and self.write(*m)
-
-
-if __name__ == '__main__':
-    dispatch(globaloptions=UI.options, middleware=ui_middleware)
--- a/test_cmd.py	Sun Oct 11 11:53:36 2009 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-#!/usr/bin/env python
-
-import opster
-
-config_opts=[('c', 'config', 'webshops.ini', 'config file to use')]
-
-@opster.command(config_opts)
-def initdb(config):
-    """Initialize database"""
-    pass
-
-@opster.command(config_opts)
-def runserver(listen=('l', 'localhost', 'ip to listen on'),
-              port=('p', 5000, 'port to listen on'),
-              **opts):
-    """Run development server"""
-    print locals()
-
-if __name__ == '__main__':
-    opster.dispatch()
--- a/test_opts.py	Sun Oct 11 11:53:36 2009 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-
-from opster import command
-
-opts = [('l', 'listen', 'localhost', 'ip to listen on'),
-        ('p', 'port', 8000, 'port to listen on'),
-        ('d', 'daemonize', False, 'daemonize process'),
-        ('', 'pid-file', '', 'name of file to write process ID to')]
-
-@command(opts, usage='[-l HOST] DIR')
-def main(*dirs, **opts):
-    '''This is some command
-
-    It looks very similar to some serve command
-    '''
-    print locals()
-
-@command(usage='[-l HOST] DIR')
-def another(dirname,
-            listen=('l', 'localhost', 'ip to listen on'),
-            port=('p', 8000, 'port to listen on'),
-            daemonize=('d', False, 'daemonize process'),
-            pid_file=('', '', 'name of file to write process ID to')):
-    '''Command with option declaration as keyword arguments
-
-    Otherwise it's the same as previous command
-    '''
-    print locals()
-
-if __name__ == '__main__':
-    #main(argv=sys.argv[1:])
-    another(argv=sys.argv[1:])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/runtests	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+BASE=$(dirname $0)/..
+STDOUT=$BASE/tests/tmp.out
+STDERR=$BASE/tests/tmp.err
+
+for f in `ls .`; do
+    if [ -x "$f" -a $f != "runtests" ]; then
+        echo ---------------- $f
+        PYTHONPATH=$BASE $BASE/tests/$f >$STDOUT 2>$STDERR
+        diff -N $BASE/tests/$f.out $STDOUT
+        diff -N $BASE/tests/$f.err $STDERR
+    fi
+done
+
+rm -f $STDOUT
+rm -f $STDERR
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+python test.py
+python test.py simple
+python test.py help complex
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test.out	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,21 @@
+usage: test.py <command> [options]
+
+commands:
+
+ simple  Just simple command to do nothing.
+{'test': False, 'ui': <__main__.UI object at 0x100511750>}
+test.py complex [-p] [--exit value] ...
+
+That's more complex command indented to do something
+
+    Let's try to do that (what?!)
+
+options:
+
+ -p --pass     don't run the command
+    --exit     exit with supplied code (default: 0)
+ -n --name     optional name
+ -v --verbose  enable additional output
+ -q --quiet    suppress output
+ -h --help     display help
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test.py	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,88 @@
+#!/usr/bin/env python
+
+import sys
+
+from opster import dispatch, command
+
+
+@command(usage='[-t]', shortlist=True)
+def simple(ui,
+           test=('t', False, 'just test execution')):
+    '''Just simple command to do nothing.
+
+    I assure you! Nothing to look here. ;-)
+    '''
+    ui.write(str(locals()))
+    ui.write('\n')
+
+cplx_opts = [('p', 'pass', False, 'don\'t run the command'),
+             ('', 'exit', 0, 'exit with supplied code (default: 0)'),
+             ('n', 'name', '', 'optional name')]
+
+@command(cplx_opts, usage='[-p] [--exit value] ...', name='complex', hide=True)
+def complex_(ui, *args, **opts):
+    '''That's more complex command indented to do something
+
+    Let's try to do that (what?!)
+    '''
+    if opts.get('pass'):
+        return
+    # test ui
+    ui.write('write\n')
+    ui.note('note\n')
+    ui.info('info\n')
+    ui.warn('warn\n')
+    if opts.get('exit'):
+        sys.exit(opts['exit'])
+
+def ui_middleware(func):
+    def extract_dict(source, *keys):
+        dest = {}
+        for k in keys:
+            dest[k] = source.pop(k, None)
+        return dest
+
+    def inner(*args, **kwargs):
+        opts = extract_dict(kwargs, 'verbose', 'quiet')
+        if func.__name__ == 'help_inner':
+            return func(*args, **kwargs)
+        ui = UI(**opts)
+        return func(ui, *args, **kwargs)
+    return inner
+
+class UI(object):
+    '''User interface helper.
+
+    Intended to ease handling of quiet/verbose output and more.
+
+    You have three methods to handle program messages output:
+
+      - ``UI.info`` is printed by default, but hidden with quiet option
+      - ``UI.note`` is printed only if output is verbose
+      - ``UI.write`` is printed in any case
+
+    Additionally there is ``UI.warn`` method, which prints to stderr.
+    '''
+
+    options = [('v', 'verbose', False, 'enable additional output'),
+               ('q', 'quiet', False, 'suppress output')]
+
+    def __init__(self, verbose=False, quiet=False):
+        self.verbose = verbose
+        # disabling quiet in favor of verbose is more safe
+        self.quiet = (not verbose and quiet)
+
+    def write(self, *messages):
+        for m in messages:
+            sys.stdout.write(m)
+
+    def warn(self, *messages):
+        for m in messages:
+            sys.stderr.write(m)
+
+    info = lambda self, *m: not self.quiet and self.write(*m)
+    note = lambda self, *m: self.verbose and self.write(*m)
+
+
+if __name__ == '__main__':
+    dispatch(globaloptions=UI.options, middleware=ui_middleware)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test2	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+from opster import command
+
+a = 0
+
+@command(usage="%name [-p|--pptx] [-x|--xslx] [-w|--wrdx] [-a|--abstract]")
+def main(pptx=('p', False, 'should we generate pptx related code'),
+         xslx=('x', False, 'should we generate xslx related code'),
+         wrdx=('w', False, 'should we generate wrdx related code'),
+         abstract=('a', False, 'should we generate abstract ifaces')):
+    """
+    Code generation tool. Run without params to regenerate all the code
+    """
+    global a
+    a = 42
+
+if __name__ == '__main__':
+    main(argv=[])
+    assert a == 42, "WTF???"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_cmd	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+python test_cmd.py
+python test_cmd.py help runserver
+python test_cmd.py initdb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_cmd.out	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,18 @@
+usage: test_cmd.py <command> [options]
+
+commands:
+
+ help       Show help for a given help topic or a help overview
+ initdb     Initialize database
+ runserver  Run development server
+test_cmd.py runserver [OPTIONS] 
+
+Run development server
+
+options:
+
+ -l --listen  ip to listen on (default: localhost)
+ -p --port    port to listen on (default: 5000)
+ -c --config  config file to use (default: webshops.ini)
+ -h --help    display help
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_cmd.py	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+import opster
+
+config_opts=[('c', 'config', 'webshops.ini', 'config file to use')]
+
+@opster.command(config_opts)
+def initdb(config):
+    """Initialize database"""
+    pass
+
+@opster.command(config_opts)
+def runserver(listen=('l', 'localhost', 'ip to listen on'),
+              port=('p', 5000, 'port to listen on'),
+              **opts):
+    """Run development server"""
+    print locals()
+
+if __name__ == '__main__':
+    opster.dispatch()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_opts	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+python test_opts.py --help
+python test_opts.py
+python test_opts.py .
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_opts.err	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,1 @@
+another: invalid arguments
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_opts.out	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,29 @@
+test_opts.py [-l HOST] DIR
+
+Command with option declaration as keyword arguments
+
+    Otherwise it's the same as previous command
+
+options:
+
+ -l --listen     ip to listen on (default: localhost)
+ -p --port       port to listen on (default: 8000)
+ -d --daemonize  daemonize process
+    --pid-file   name of file to write process ID to
+ -h --help       show help
+
+test_opts.py [-l HOST] DIR
+
+Command with option declaration as keyword arguments
+
+    Otherwise it's the same as previous command
+
+options:
+
+ -l --listen     ip to listen on (default: localhost)
+ -p --port       port to listen on (default: 8000)
+ -d --daemonize  daemonize process
+    --pid-file   name of file to write process ID to
+ -h --help       show help
+
+{'pid_file': '', 'daemonize': False, 'dirname': '.', 'port': 8000, 'listen': 'localhost'}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_opts.py	Sun Oct 11 14:19:47 2009 +0300
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+import sys
+
+from opster import command
+
+opts = [('l', 'listen', 'localhost', 'ip to listen on'),
+        ('p', 'port', 8000, 'port to listen on'),
+        ('d', 'daemonize', False, 'daemonize process'),
+        ('', 'pid-file', '', 'name of file to write process ID to')]
+
+@command(opts, usage='[-l HOST] DIR')
+def main(*dirs, **opts):
+    '''This is some command
+
+    It looks very similar to some serve command
+    '''
+    print locals()
+
+@command(usage='[-l HOST] DIR')
+def another(dirname,
+            listen=('l', 'localhost', 'ip to listen on'),
+            port=('p', 8000, 'port to listen on'),
+            daemonize=('d', False, 'daemonize process'),
+            pid_file=('', '', 'name of file to write process ID to')):
+    '''Command with option declaration as keyword arguments
+
+    Otherwise it's the same as previous command
+    '''
+    print locals()
+
+if __name__ == '__main__':
+    #main(argv=sys.argv[1:])
+    another(argv=sys.argv[1:])