~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_features.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for test feature dependencies."""
 
18
 
 
19
import sys
 
20
 
 
21
from bzrlib import (
 
22
    symbol_versioning,
 
23
    tests,
 
24
    )
 
25
from bzrlib.tests import (
 
26
    features,
 
27
    )
 
28
 
 
29
 
 
30
class TestFeature(tests.TestCase):
 
31
 
 
32
    def test_caching(self):
 
33
        """Feature._probe is called by the feature at most once."""
 
34
        class InstrumentedFeature(features.Feature):
 
35
            def __init__(self):
 
36
                super(InstrumentedFeature, self).__init__()
 
37
                self.calls = []
 
38
 
 
39
            def _probe(self):
 
40
                self.calls.append('_probe')
 
41
                return False
 
42
        feature = InstrumentedFeature()
 
43
        feature.available()
 
44
        self.assertEqual(['_probe'], feature.calls)
 
45
        feature.available()
 
46
        self.assertEqual(['_probe'], feature.calls)
 
47
 
 
48
    def test_named_str(self):
 
49
        """Feature.__str__ should thunk to feature_name()."""
 
50
        class NamedFeature(features.Feature):
 
51
            def feature_name(self):
 
52
                return 'symlinks'
 
53
        feature = NamedFeature()
 
54
        self.assertEqual('symlinks', str(feature))
 
55
 
 
56
    def test_default_str(self):
 
57
        """Feature.__str__ should default to __class__.__name__."""
 
58
        class NamedFeature(features.Feature):
 
59
            pass
 
60
        feature = NamedFeature()
 
61
        self.assertEqual('NamedFeature', str(feature))
 
62
 
 
63
 
 
64
class TestUnavailableFeature(tests.TestCase):
 
65
 
 
66
    def test_access_feature(self):
 
67
        feature = features.Feature()
 
68
        exception = tests.UnavailableFeature(feature)
 
69
        self.assertIs(feature, exception.args[0])
 
70
 
 
71
 
 
72
# Although this was deprecated a long time ago, please keep it here because
 
73
# it's really just a test fixture for test-feature deprecation.
 
74
simple_thunk_feature = features._CompatabilityThunkFeature(
 
75
    symbol_versioning.deprecated_in((2, 1, 0)),
 
76
    'bzrlib.tests.test_features',
 
77
    'simple_thunk_feature',
 
78
    'UnicodeFilenameFeature',
 
79
    replacement_module='bzrlib.tests.features')
 
80
 
 
81
 
 
82
class Test_CompatibilityFeature(tests.TestCase):
 
83
 
 
84
    def test_does_thunk(self):
 
85
        res = self.callDeprecated(
 
86
            ['bzrlib.tests.test_features.simple_thunk_feature '
 
87
             'was deprecated in version 2.1.0. '
 
88
             'Use bzrlib.tests.features.UnicodeFilenameFeature instead.'],
 
89
            simple_thunk_feature.available)
 
90
        self.assertEqual(features.UnicodeFilenameFeature.available(), res)
 
91
 
 
92
    def test_reports_correct_location(self):
 
93
        a_feature = features._CompatabilityThunkFeature(
 
94
            symbol_versioning.deprecated_in((2, 1, 0)),
 
95
            'bzrlib.tests.test_features',
 
96
            'a_feature',
 
97
            'UnicodeFilenameFeature',
 
98
            replacement_module='bzrlib.tests.features')
 
99
        def test_caller(message, category=None, stacklevel=1):
 
100
            # Find ourselves back from the right frame
 
101
            caller = sys._getframe(stacklevel)
 
102
            reported_file = caller.f_globals['__file__']
 
103
            reported_lineno = caller.f_lineno
 
104
            self.assertEquals(__file__, reported_file)
 
105
            # The call we're tracking occurred the line after we grabbed the
 
106
            # lineno.
 
107
            self.assertEquals(self.lineno + 1, reported_lineno)
 
108
        self.overrideAttr(symbol_versioning, 'warn', test_caller)
 
109
        # Grab the current lineno
 
110
        self.lineno = sys._getframe().f_lineno
 
111
        self.requireFeature(a_feature)
 
112
 
 
113
 
 
114
class TestModuleAvailableFeature(tests.TestCase):
 
115
 
 
116
    def test_available_module(self):
 
117
        feature = features.ModuleAvailableFeature('bzrlib.tests')
 
118
        self.assertEqual('bzrlib.tests', feature.module_name)
 
119
        self.assertEqual('bzrlib.tests', str(feature))
 
120
        self.assertTrue(feature.available())
 
121
        self.assertIs(tests, feature.module)
 
122
 
 
123
    def test_unavailable_module(self):
 
124
        feature = features.ModuleAvailableFeature(
 
125
            'bzrlib.no_such_module_exists')
 
126
        self.assertEqual('bzrlib.no_such_module_exists', str(feature))
 
127
        self.assertFalse(feature.available())
 
128
        self.assertIs(None, feature.module)
 
129
 
 
130
 
 
131
class TestUnicodeFilenameFeature(tests.TestCase):
 
132
 
 
133
    def test_probe_passes(self):
 
134
        """UnicodeFilenameFeature._probe passes."""
 
135
        # We can't test much more than that because the behaviour depends
 
136
        # on the platform.
 
137
        features.UnicodeFilenameFeature._probe()