1
# Copyright (C) 2011, 2016 Canonical Ltd
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.
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.
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
17
"""Tests for test feature dependencies."""
22
plugin as _mod_plugin,
31
class TestFeature(tests.TestCase):
33
def test_caching(self):
34
"""Feature._probe is called by the feature at most once."""
35
class InstrumentedFeature(features.Feature):
37
super(InstrumentedFeature, self).__init__()
41
self.calls.append('_probe')
43
feature = InstrumentedFeature()
45
self.assertEqual(['_probe'], feature.calls)
47
self.assertEqual(['_probe'], feature.calls)
49
def test_named_str(self):
50
"""Feature.__str__ should thunk to feature_name()."""
51
class NamedFeature(features.Feature):
52
def feature_name(self):
54
feature = NamedFeature()
55
self.assertEqual('symlinks', str(feature))
57
def test_default_str(self):
58
"""Feature.__str__ should default to __class__.__name__."""
59
class NamedFeature(features.Feature):
61
feature = NamedFeature()
62
self.assertEqual('NamedFeature', str(feature))
65
class TestUnavailableFeature(tests.TestCase):
67
def test_access_feature(self):
68
feature = features.Feature()
69
exception = tests.UnavailableFeature(feature)
70
self.assertIs(feature, exception.args[0])
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')
83
class Test_CompatibilityFeature(tests.TestCase):
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)
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',
98
'UnicodeFilenameFeature',
99
replacement_module='breezy.tests.features')
100
def test_caller(message, category=None, stacklevel=1):
101
# Find ourselves back from the right frame
102
caller = sys._getframe(stacklevel)
103
reported_file = caller.f_globals['__file__']
104
reported_lineno = caller.f_lineno
105
self.assertEqual(__file__, reported_file)
106
# The call we're tracking occurred the line after we grabbed the
108
self.assertEqual(self.lineno + 1, reported_lineno)
109
self.overrideAttr(symbol_versioning, 'warn', test_caller)
110
# Grab the current lineno
111
self.lineno = sys._getframe().f_lineno
112
self.requireFeature(a_feature)
115
class TestModuleAvailableFeature(tests.TestCase):
117
def test_available_module(self):
118
feature = features.ModuleAvailableFeature('breezy.tests')
119
self.assertEqual('breezy.tests', feature.module_name)
120
self.assertEqual('breezy.tests', str(feature))
121
self.assertTrue(feature.available())
122
self.assertIs(tests, feature.module)
124
def test_unavailable_module(self):
125
feature = features.ModuleAvailableFeature(
126
'breezy.no_such_module_exists')
127
self.assertEqual('breezy.no_such_module_exists', str(feature))
128
self.assertFalse(feature.available())
129
self.assertIs(None, feature.module)
132
class TestPluginLoadedFeature(tests.TestCase):
134
def test_available_plugin(self):
135
plugins = _mod_plugin.plugins()
137
self.skipTest('no plugins available to test with')
138
a_plugin_name = next(iter(plugins))
139
feature = features.PluginLoadedFeature(a_plugin_name)
140
self.assertEqual(a_plugin_name, feature.plugin_name)
141
self.assertEqual(a_plugin_name + ' plugin', str(feature))
142
self.assertTrue(feature.available())
144
def test_unavailable_plugin(self):
145
feature = features.PluginLoadedFeature('idontexist')
146
self.assertEqual('idontexist plugin', str(feature))
147
self.assertFalse(feature.available())
148
self.assertIs(None, feature.plugin)
151
class TestUnicodeFilenameFeature(tests.TestCase):
153
def test_probe_passes(self):
154
"""UnicodeFilenameFeature._probe passes."""
155
# We can't test much more than that because the behaviour depends
157
features.UnicodeFilenameFeature._probe()
160
class TestBackslashFilenameFeature(tests.TestCase):
162
def test_probe_passes(self):
163
"""BackslashFilenameFeature._probe passes."""
164
# We can't test much more than that because the behaviour depends
166
features.BackslashFilenameFeature._probe()