~bzr-pqm/bzr/bzr.dev

6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2011, 2016 Canonical Ltd
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
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
6325.3.1 by Vincent Ladeuil
Give meaningful deprecation warnings for deprecated test features
17
"""Tests for test feature dependencies."""
18
19
import sys
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
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
5967.12.3 by Martin Pool
Unify duplicated UnicodeFilename and _PosixPermissionsFeature
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.
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
74
simple_thunk_feature = features._CompatabilityThunkFeature(
75
    symbol_versioning.deprecated_in((2, 1, 0)),
76
    'bzrlib.tests.test_features',
77
    'simple_thunk_feature',
5967.12.3 by Martin Pool
Unify duplicated UnicodeFilename and _PosixPermissionsFeature
78
    'UnicodeFilenameFeature',
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
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. '
5967.12.3 by Martin Pool
Unify duplicated UnicodeFilename and _PosixPermissionsFeature
88
             'Use bzrlib.tests.features.UnicodeFilenameFeature instead.'],
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
89
            simple_thunk_feature.available)
5967.12.3 by Martin Pool
Unify duplicated UnicodeFilename and _PosixPermissionsFeature
90
        self.assertEqual(features.UnicodeFilenameFeature.available(), res)
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
91
6325.3.1 by Vincent Ladeuil
Give meaningful deprecation warnings for deprecated test features
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
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
104
            self.assertEqual(__file__, reported_file)
6325.3.1 by Vincent Ladeuil
Give meaningful deprecation warnings for deprecated test features
105
            # The call we're tracking occurred the line after we grabbed the
106
            # lineno.
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
107
            self.assertEqual(self.lineno + 1, reported_lineno)
6325.3.1 by Vincent Ladeuil
Give meaningful deprecation warnings for deprecated test features
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
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
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()