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')
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
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)
116
class TestModuleAvailableFeature(tests.TestCase):
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)
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)
133
class TestPluginLoadedFeature(tests.TestCase):
135
def test_available_plugin(self):
136
plugins = _mod_plugin.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())
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)
152
class TestUnicodeFilenameFeature(tests.TestCase):
154
def test_probe_passes(self):
155
"""UnicodeFilenameFeature._probe passes."""
156
# We can't test much more than that because the behaviour depends
158
features.UnicodeFilenameFeature._probe()
161
class TestBackslashFilenameFeature(tests.TestCase):
163
def test_probe_passes(self):
164
"""BackslashFilenameFeature._probe passes."""
165
# We can't test much more than that because the behaviour depends
167
features.BackslashFilenameFeature._probe()