/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_features.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011, 2016 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 .. import (
 
22
    plugin as _mod_plugin,
 
23
    symbol_versioning,
 
24
    tests,
 
25
    )
 
26
from . import (
 
27
    features,
 
28
    )
 
29
 
 
30
 
 
31
class TestFeature(tests.TestCase):
 
32
 
 
33
    def test_caching(self):
 
34
        """Feature._probe is called by the feature at most once."""
 
35
        class InstrumentedFeature(features.Feature):
 
36
            def __init__(self):
 
37
                super(InstrumentedFeature, self).__init__()
 
38
                self.calls = []
 
39
 
 
40
            def _probe(self):
 
41
                self.calls.append('_probe')
 
42
                return False
 
43
        feature = InstrumentedFeature()
 
44
        feature.available()
 
45
        self.assertEqual(['_probe'], feature.calls)
 
46
        feature.available()
 
47
        self.assertEqual(['_probe'], feature.calls)
 
48
 
 
49
    def test_named_str(self):
 
50
        """Feature.__str__ should thunk to feature_name()."""
 
51
        class NamedFeature(features.Feature):
 
52
            def feature_name(self):
 
53
                return 'symlinks'
 
54
        feature = NamedFeature()
 
55
        self.assertEqual('symlinks', str(feature))
 
56
 
 
57
    def test_default_str(self):
 
58
        """Feature.__str__ should default to __class__.__name__."""
 
59
        class NamedFeature(features.Feature):
 
60
            pass
 
61
        feature = NamedFeature()
 
62
        self.assertEqual('NamedFeature', str(feature))
 
63
 
 
64
 
 
65
class TestUnavailableFeature(tests.TestCase):
 
66
 
 
67
    def test_access_feature(self):
 
68
        feature = features.Feature()
 
69
        exception = tests.UnavailableFeature(feature)
 
70
        self.assertIs(feature, exception.args[0])
 
71
 
 
72
 
 
73
# Although this was deprecated a long time ago, please keep it here because
 
74
# it's really just a test fixture for test-feature deprecation.
 
75
simple_thunk_feature = features._CompatabilityThunkFeature(
 
76
    symbol_versioning.deprecated_in((2, 1, 0)),
 
77
    'breezy.tests.test_features',
 
78
    'simple_thunk_feature',
 
79
    'UnicodeFilenameFeature',
 
80
    replacement_module='breezy.tests.features')
 
81
 
 
82
 
 
83
class Test_CompatibilityFeature(tests.TestCase):
 
84
 
 
85
    def test_does_thunk(self):
 
86
        res = self.callDeprecated(
 
87
            ['breezy.tests.test_features.simple_thunk_feature '
 
88
             'was deprecated in version 2.1.0. '
 
89
             'Use breezy.tests.features.UnicodeFilenameFeature instead.'],
 
90
            simple_thunk_feature.available)
 
91
        self.assertEqual(features.UnicodeFilenameFeature.available(), res)
 
92
 
 
93
    def test_reports_correct_location(self):
 
94
        a_feature = features._CompatabilityThunkFeature(
 
95
            symbol_versioning.deprecated_in((2, 1, 0)),
 
96
            'breezy.tests.test_features',
 
97
            'a_feature',
 
98
            'UnicodeFilenameFeature',
 
99
            replacement_module='breezy.tests.features')
 
100
 
 
101
        def test_caller(message, category=None, stacklevel=1):
 
102
            # Find ourselves back from the right frame
 
103
            caller = sys._getframe(stacklevel)
 
104
            reported_file = caller.f_globals['__file__']
 
105
            reported_lineno = caller.f_lineno
 
106
            self.assertEqual(__file__, reported_file)
 
107
            # The call we're tracking occurred the line after we grabbed the
 
108
            # lineno.
 
109
            self.assertEqual(self.lineno + 1, reported_lineno)
 
110
        self.overrideAttr(symbol_versioning, 'warn', test_caller)
 
111
        # Grab the current lineno
 
112
        self.lineno = sys._getframe().f_lineno
 
113
        self.requireFeature(a_feature)
 
114
 
 
115
 
 
116
class TestModuleAvailableFeature(tests.TestCase):
 
117
 
 
118
    def test_available_module(self):
 
119
        feature = features.ModuleAvailableFeature('breezy.tests')
 
120
        self.assertEqual('breezy.tests', feature.module_name)
 
121
        self.assertEqual('breezy.tests', str(feature))
 
122
        self.assertTrue(feature.available())
 
123
        self.assertIs(tests, feature.module)
 
124
 
 
125
    def test_unavailable_module(self):
 
126
        feature = features.ModuleAvailableFeature(
 
127
            'breezy.no_such_module_exists')
 
128
        self.assertEqual('breezy.no_such_module_exists', str(feature))
 
129
        self.assertFalse(feature.available())
 
130
        self.assertIs(None, feature.module)
 
131
 
 
132
 
 
133
class TestPluginLoadedFeature(tests.TestCase):
 
134
 
 
135
    def test_available_plugin(self):
 
136
        plugins = _mod_plugin.plugins()
 
137
        if not plugins:
 
138
            self.skipTest('no plugins available to test with')
 
139
        a_plugin_name = next(iter(plugins))
 
140
        feature = features.PluginLoadedFeature(a_plugin_name)
 
141
        self.assertEqual(a_plugin_name, feature.plugin_name)
 
142
        self.assertEqual(a_plugin_name + ' plugin', str(feature))
 
143
        self.assertTrue(feature.available())
 
144
 
 
145
    def test_unavailable_plugin(self):
 
146
        feature = features.PluginLoadedFeature('idontexist')
 
147
        self.assertEqual('idontexist plugin', str(feature))
 
148
        self.assertFalse(feature.available())
 
149
        self.assertIs(None, feature.plugin)
 
150
 
 
151
 
 
152
class TestUnicodeFilenameFeature(tests.TestCase):
 
153
 
 
154
    def test_probe_passes(self):
 
155
        """UnicodeFilenameFeature._probe passes."""
 
156
        # We can't test much more than that because the behaviour depends
 
157
        # on the platform.
 
158
        features.UnicodeFilenameFeature._probe()
 
159
 
 
160
 
 
161
class TestBackslashFilenameFeature(tests.TestCase):
 
162
 
 
163
    def test_probe_passes(self):
 
164
        """BackslashFilenameFeature._probe passes."""
 
165
        # We can't test much more than that because the behaviour depends
 
166
        # on the platform.
 
167
        features.BackslashFilenameFeature._probe()