~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""OS Lock implementation tests for bzr.

These test the conformance of all the lock variations to the expected API.
"""

from copy import deepcopy

from bzrlib import (
    lock,
    tests,
    )


class TestCaseWithLock(tests.TestCaseWithTransport):

    write_lock = None
    read_lock = None


class LockTestProviderAdapter(object):
    """A tool to generate a suite testing multiple lock formats at once.

    This is done by copying the test once for each lock and injecting the
    read_lock and write_lock classes.
    They are also given a new test id.
    """

    def __init__(self, lock_classes):
        self._lock_classes = lock_classes

    def _clone_test(self, test, write_lock, read_lock, variation):
        """Clone test for adaption."""
        new_test = deepcopy(test)
        new_test.write_lock = write_lock
        new_test.read_lock = read_lock
        def make_new_test_id():
            new_id = "%s(%s)" % (test.id(), variation)
            return lambda: new_id
        new_test.id = make_new_test_id()
        return new_test

    def adapt(self, test):
        result = tests.TestSuite()
        for name, write_lock, read_lock in self._lock_classes:
            new_test = self._clone_test(test, write_lock, read_lock, name)
            result.addTest(new_test)
        return result


def test_suite():
    result = tests.TestSuite()
    test_lock_implementations = [
        'bzrlib.tests.per_lock.test_lock',
        'bzrlib.tests.per_lock.test_temporary_write_lock',
        ]
    adapter = LockTestProviderAdapter(lock._lock_classes)
    loader = tests.TestLoader()
    tests.adapt_modules(test_lock_implementations, adapter, loader, result)
    return result