~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to contrib/test

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-01-25 16:42:52 UTC
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: james.westby@ubuntu.com-20110125164252-rl1pybasx1nsqgoy
Tags: upstream-1.5.3
ImportĀ upstreamĀ versionĀ 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
import os
4
 
import re
5
 
import signal
6
 
import sys
7
 
import string
8
 
import subprocess
9
 
import unittest
10
 
 
11
 
sys.path.insert(0, os.path.abspath("."))
12
 
 
13
 
from distutils.spawn import find_executable
14
 
 
15
 
 
16
 
class TestRunner(object):
17
 
 
18
 
    def _load_unittest(self, relpath):
19
 
        """Load unit tests from a Python module with the given relative path."""
20
 
        assert relpath.endswith(".py"), (
21
 
            "%s does not appear to be a Python module" % relpath)
22
 
        modpath = relpath.replace(os.path.sep, ".")[:-3]
23
 
        module = __import__(modpath, None, None, [""])
24
 
 
25
 
        # If the module has a 'suite' or 'test_suite' function, use that
26
 
        # to load the tests.
27
 
        if hasattr(module, "suite"):
28
 
            return module.suite()
29
 
        elif hasattr(module, "test_suite"):
30
 
            return module.test_suite()
31
 
        else:
32
 
            return unittest.defaultTestLoader.loadTestsFromModule(module)
33
 
 
34
 
    def _collect_tests(self, testpath, test_pattern):
35
 
        """Return the set of unittests."""
36
 
        suite = unittest.TestSuite()
37
 
        if test_pattern:
38
 
            pattern = re.compile('.*%s.*' % test_pattern)
39
 
        else:
40
 
            pattern = None 
41
 
 
42
 
        if testpath:
43
 
            module_suite = self._load_unittest(testpath)
44
 
            if pattern:
45
 
                for inner_suite in module_suite._tests:
46
 
                    for test in inner_suite._tests:
47
 
                        if pattern.match(test.id()):
48
 
                            suite.addTest(test)
49
 
            else:
50
 
                suite.addTests(module_suite)
51
 
            return suite
52
 
 
53
 
        # We don't use the dirs variable, so ignore the warning
54
 
        # pylint: disable-msg=W0612
55
 
        for root, dirs, files in os.walk("tests"):
56
 
            for file in files:
57
 
                path = os.path.join(root, file)
58
 
                if file.endswith(".py") and file.startswith("test_"):
59
 
                    module_suite = self._load_unittest(path)
60
 
                    if pattern:
61
 
                        for inner_suite in module_suite._tests:
62
 
                            for test in inner_suite._tests:
63
 
                                if pattern.match(test.id()):
64
 
                                    suite.addTest(test)
65
 
                    else:
66
 
                        suite.addTests(module_suite)
67
 
        return suite
68
 
 
69
 
    def run(self, testpath, test_pattern=None, loops=None):
70
 
        """run the tests. """
71
 
        # install the glib2reactor before any import of the reactor to avoid
72
 
        # using the default SelectReactor and be able to run the dbus tests
73
 
        from twisted.internet import glib2reactor
74
 
        glib2reactor.install()
75
 
        from twisted.internet import reactor
76
 
        from twisted.trial.reporter import TreeReporter
77
 
        from twisted.trial.runner import TrialRunner
78
 
 
79
 
        from contrib.dbus_util import DBusRunner
80
 
        dbus_runner = DBusRunner()
81
 
        dbus_runner.startDBus()
82
 
 
83
 
        workingDirectory = os.path.join(os.getcwd(), "_trial_temp", "tmp")
84
 
        runner = TrialRunner(reporterFactory=TreeReporter, realTimeErrors=True,
85
 
                            workingDirectory=workingDirectory)
86
 
 
87
 
        # setup a custom XDG_CACHE_HOME and create the logs directory
88
 
        xdg_cache = os.path.join(os.getcwd(), "_trial_temp", "xdg_cache")
89
 
        os.environ["XDG_CACHE_HOME"] = xdg_cache
90
 
        # setup the ROOTDIR env var
91
 
        os.environ['ROOTDIR'] = os.getcwd()
92
 
        if not os.path.exists(xdg_cache):
93
 
            os.makedirs(xdg_cache)
94
 
        success = 0
95
 
        try:
96
 
            suite = self._collect_tests(testpath, test_pattern)
97
 
            if loops:
98
 
                old_suite = suite
99
 
                suite = unittest.TestSuite()
100
 
                for x in xrange(loops):
101
 
                    suite.addTest(old_suite)
102
 
            result = runner.run(suite)
103
 
            success = result.wasSuccessful()
104
 
        finally:
105
 
            dbus_runner.stopDBus()
106
 
        if not success:
107
 
            sys.exit(1)
108
 
        else:
109
 
            sys.exit(0)
110
 
 
111
 
 
112
 
if __name__ == '__main__':
113
 
    from optparse import OptionParser
114
 
    usage = '%prog [options] path'
115
 
    parser = OptionParser(usage=usage)
116
 
    parser.add_option("-t", "--test", dest="test",
117
 
                  help = "run specific tests, e.g: className.methodName")
118
 
    parser.add_option("-l", "--loop", dest="loops", type="int", default=1,
119
 
                      help = "loop selected tests LOOPS number of times", 
120
 
                      metavar="LOOPS")
121
 
 
122
 
    (options, args) = parser.parse_args()
123
 
    if args:
124
 
        testpath = args[0]
125
 
        if not os.path.exists(testpath):
126
 
            print "the path to test does not exists!"
127
 
            sys.exit()
128
 
    else:
129
 
        testpath = None
130
 
    TestRunner().run(testpath, options.test, options.loops)