~ubuntuone-control-tower/ubuntuone-dev-tools/stable-0-2

« back to all changes in this revision

Viewing changes to ubuntuone/devtools/testcase.py

  • Committer: Tarmac
  • Author(s): Manuel de la Pena
  • Date: 2011-01-04 21:03:41 UTC
  • mfrom: (19.1.12 skip_test)
  • Revision ID: tarmac-20110104210341-8y2877edyu3cpjlu
Adds a number of decorators that allows to skip tests if certain conditions are met. Usage can be seen in the test_decorators.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import contextlib
24
24
import os
25
25
import shutil
26
 
 
27
 
import dbus
28
 
import dbus.service
29
 
 
30
 
from dbus.mainloop.glib import DBusGMainLoop
 
26
import sys
 
27
 
 
28
from functools import wraps
31
29
 
32
30
from twisted.internet import defer
33
31
from twisted.python import failure
34
 
from twisted.trial.unittest import TestCase
 
32
from twisted.trial.unittest import TestCase, SkipTest
35
33
 
36
34
# DBusRunner for DBusTestCase using tests
37
35
from ubuntuone.devtools.services.dbus import DBusRunner
38
36
 
39
37
 
 
38
# pylint: disable=F0401,C0103
 
39
try:
 
40
    import dbus
 
41
except ImportError:
 
42
    dbus = None
 
43
 
 
44
try:
 
45
    import dbus.service as service
 
46
except ImportError:
 
47
    service = None
 
48
 
 
49
try:
 
50
    from dbus.mainloop.glib import DBusGMainLoop
 
51
except ImportError:
 
52
    DBusGMainLoop = None
 
53
 
 
54
 
 
55
# pylint: enable=F0401,C0103
40
56
@contextlib.contextmanager
41
57
def environ(env_var, new_value):
42
58
    """context manager to replace/add an environ value"""
49
65
        os.environ[env_var] = old_value
50
66
 
51
67
 
 
68
def _id(obj):
 
69
    """Return the obj calling the funct."""
 
70
    return obj
 
71
 
 
72
 
 
73
# pylint: disable=C0103
 
74
def skipTest(reason):
 
75
    """Unconditionally skip a test."""
 
76
    def decorator(test_item):
 
77
        """Decorate the test so that it is skipped."""
 
78
        if not (isinstance(test_item, type) and\
 
79
            issubclass(test_item, TestCase)):
 
80
 
 
81
            @wraps(test_item)
 
82
            def skip_wrapper(*args, **kwargs):
 
83
                """Skip a test method raising an exception."""
 
84
                raise SkipTest(reason)
 
85
            test_item = skip_wrapper
 
86
 
 
87
        # tell twisted.trial.unittest to skip the test, pylint will complain
 
88
        # since it thinks we are redefining a name out of the scope
 
89
        # pylint: disable=W0621,W0612
 
90
        test_item.skip = reason
 
91
        # pylint: enable=W0621,W0612
 
92
        # because the item was skipped, we will make sure that no
 
93
        # services are started for it
 
94
        if hasattr(test_item, "required_services"):
 
95
            # pylint: disable=W0612
 
96
            test_item.required_services = lambda *args, **kwargs: []
 
97
            # pylint: enable=W0612
 
98
        return test_item
 
99
    return decorator
 
100
 
 
101
 
 
102
def skipIf(condition, reason):
 
103
    """Skip a test if the condition is true."""
 
104
    if condition:
 
105
        return skipTest(reason)
 
106
    return _id
 
107
 
 
108
 
 
109
def skipIfOS(current_os, reason):
 
110
    """Skip test for a particular os or lists of them."""
 
111
    if os:
 
112
        if sys.platform in current_os or sys.platform == current_os:
 
113
            return skipTest(reason)
 
114
        return _id
 
115
    return _id
 
116
 
 
117
 
 
118
def skipIfNotOS(current_os, reason):
 
119
    """Skip test we are not in a particular os."""
 
120
    if os:
 
121
        if sys.platform not in current_os or\
 
122
            sys.platform != current_os:
 
123
            return skipTest(reason)
 
124
        return _id
 
125
    return _id
 
126
 
 
127
 
 
128
# pylint: enable=C0103
 
129
 
 
130
 
52
131
class FakeDBusInterface(object):
53
132
    """A fake DBusInterface..."""
54
133
 
118
197
        os.makedirs(path)
119
198
 
120
199
 
 
200
@skipIf(dbus is None or service is None or DBusGMainLoop is None,
 
201
    "The test requires dbus.")
121
202
class DBusTestCase(BaseTestCase):
122
203
    """Test the DBus event handling."""
123
204
 
131
212
        """Setup the infrastructure fo the test (dbus service)."""
132
213
        # Class 'BaseTestCase' has no 'setUp' member
133
214
        # pylint: disable=E1101
 
215
        # dbus modules will be imported by the decorator
 
216
        # pylint: disable=E0602
134
217
        BaseTestCase.setUp(self)
135
218
        self.loop = DBusGMainLoop(set_as_default=True)
136
219
        self.bus = dbus.bus.BusConnection(mainloop=self.loop)
137
220
        # monkeypatch busName.__del__ to avoid errors on gc
138
221
        # we take care of releasing the name in shutdown
139
 
        dbus.service.BusName.__del__ = lambda _: None
 
222
        service.BusName.__del__ = lambda _: None
140
223
        self.bus.set_exit_on_disconnect(False)
141
224
        self.signal_receivers = set()
142
225
 
161
244
 
162
245
    def cleanup_signal_receivers(self, signal_receivers):
163
246
        """Cleanup self.signal_receivers and returns a deferred."""
 
247
        # dbus modules will be imported by the decorator
 
248
        # pylint: disable=E0602
164
249
        deferreds = []
165
250
        for match in signal_receivers:
166
251
            d = defer.Deferred()