#!/usr/local/bin/python

import glob
import importlib
import os
import inspect
import sys

from lib import BaseTest

try:
    from termcolor import colored
except ImportError:
    def colored(s, **kwargs):
        return s


def run(include_long_tests=False, tests=None):
    """
    Run system test.
    """
    if tests is None:
        tests = glob.glob("t*_*")
    fails = []
    numTests = numFailed = numSkipped = 0

    for test in tests:

        testModule = importlib.import_module(test)

        for name in dir(testModule):
            o = getattr(testModule, name)

            if not (inspect.isclass(o) and issubclass(o, BaseTest) and o is not BaseTest):
                continue

            t = o()
            if t.longTest and not include_long_tests or not t.fixture_available():
                numSkipped += 1
                continue

            numTests += 1

            sys.stdout.write("%s:%s... " % (test, o.__name__))

            try:
                t.test()
            except BaseException, e:
                numFailed += 1
                fails.append((test, t, e, testModule))
                sys.stdout.write(colored("FAIL\n", color="red"))
            else:
                sys.stdout.write(colored("OK\n", color="green"))

            t.shutdown()

    print "TESTS: %d SUCCESS: %d FAIL: %d SKIP: %d" % (numTests, numTests - numFailed, numFailed, numSkipped)

    if len(fails) > 0:
        print "\nFAILURES (%d):" % (len(fails), )

        for (test, t, e, testModule) in fails:
            print "%s:%s %s" % (test, t.__class__.__name__, testModule.__doc__.strip() + ": " + t.__doc__.strip())
            print "ERROR: %s" % (e, )
            print "=" * 60

        sys.exit(1)

if __name__ == "__main__":
    os.chdir(os.path.realpath(os.path.dirname(sys.argv[0])))
    include_long_tests = False
    tests = None
    if len(sys.argv) > 1:
        if sys.argv[1] == "--long":
            include_long_tests = True
            tests = sys.argv[2:]
        else:
            tests = sys.argv[1:]
    run(include_long_tests, tests)

@@ -72,16 +72,31 @@
 inspect%0A
+import fnmatch%0A
 import s
@@ -277,16 +277,30 @@
 sts=None
+, filters=None
 ):%0A    %22
@@ -342,21 +342,17 @@
  if 
+not 
 tests
- is None
 :%0A  
@@ -706,32 +706,316 @@
       continue%0A%0A
+            if filters:%0A                matches = False%0A%0A                for filt in filters:%0A                    if fnmatch.fnmatch(o.__name__, filt):%0A                        matches = True%0A                        break%0A%0A                if not matches:%0A                    continue%0A%0A
             t = 
@@ -2168,23 +2168,23 @@
     
-if len(
+args = 
 sys.argv
 ) %3E 
@@ -2183,36 +2183,44 @@
 argv
-) %3E 
+%5B
 1:
+%5D
 %0A    
-    if sys.argv%5B1
+if len(args) %3E 0 and args%5B0
 %5D ==
@@ -2230,20 +2230,16 @@
 -long%22:%0A
-    
         
@@ -2276,79 +2276,187 @@
     
-    tests = sys.argv%5B2:%5D%0A        else:%0A            tests = sys.argv%5B1:%5D
+args = args%5B1:%5D%0A%0A    tests = %5B%5D%0A    filters = %5B%5D%0A%0A    for arg in args:%0A        if arg.startswith('t'):%0A            tests.append(arg)%0A        else:%0A            filters.append(arg)%0A
 %0A   
@@ -2485,10 +2485,19 @@
 s, tests
+, filters
 )%0A

Allow running system tests by mask.