14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""A collection of commonly used 'Features' which bzrlib uses to skip tests."""
17
"""A collection of commonly used 'Features' to optionally run tests.
22
25
from bzrlib import (
28
class _NotRunningAsRoot(tests.Feature):
32
class Feature(object):
33
"""An operating system Feature."""
36
self._available = None
39
"""Is the feature available?
41
:return: True if the feature is available.
43
if self._available is None:
44
self._available = self._probe()
45
return self._available
48
"""Implement this method in concrete features.
50
:return: True if the feature is available.
52
raise NotImplementedError
55
if getattr(self, 'feature_name', None):
56
return self.feature_name()
57
return self.__class__.__name__
60
class _SymlinkFeature(Feature):
63
return osutils.has_symlinks()
65
def feature_name(self):
68
SymlinkFeature = _SymlinkFeature()
71
class _HardlinkFeature(Feature):
74
return osutils.has_hardlinks()
76
def feature_name(self):
79
HardlinkFeature = _HardlinkFeature()
82
class _OsFifoFeature(Feature):
85
return getattr(os, 'mkfifo', None)
87
def feature_name(self):
88
return 'filesystem fifos'
90
OsFifoFeature = _OsFifoFeature()
93
class _UnicodeFilenameFeature(Feature):
94
"""Does the filesystem support Unicode filenames?"""
98
# Check for character combinations unlikely to be covered by any
99
# single non-unicode encoding. We use the characters
100
# - greek small letter alpha (U+03B1) and
101
# - braille pattern dots-123456 (U+283F).
102
os.stat(u'\u03b1\u283f')
103
except UnicodeEncodeError:
105
except (IOError, OSError):
106
# The filesystem allows the Unicode filename but the file doesn't
110
# The filesystem allows the Unicode filename and the file exists,
114
UnicodeFilenameFeature = _UnicodeFilenameFeature()
117
class _CompatabilityThunkFeature(Feature):
118
"""This feature is just a thunk to another feature.
120
It issues a deprecation warning if it is accessed, to let you know that you
121
should really use a different feature.
124
def __init__(self, dep_version, module, name,
125
replacement_name, replacement_module=None):
126
super(_CompatabilityThunkFeature, self).__init__()
127
self._module = module
128
if replacement_module is None:
129
replacement_module = module
130
self._replacement_module = replacement_module
132
self._replacement_name = replacement_name
133
self._dep_version = dep_version
137
if self._feature is None:
138
from bzrlib import pyutils
139
depr_msg = self._dep_version % ('%s.%s'
140
% (self._module, self._name))
141
use_msg = ' Use %s.%s instead.' % (self._replacement_module,
142
self._replacement_name)
143
symbol_versioning.warn(depr_msg + use_msg, DeprecationWarning)
144
# Import the new feature and use it as a replacement for the
146
self._feature = pyutils.get_named_object(
147
self._replacement_module, self._replacement_name)
151
return self._feature._probe()
154
class ModuleAvailableFeature(Feature):
155
"""This is a feature than describes a module we want to be available.
157
Declare the name of the module in __init__(), and then after probing, the
158
module will be available as 'self.module'.
160
:ivar module: The module if it is available, else None.
163
def __init__(self, module_name):
164
super(ModuleAvailableFeature, self).__init__()
165
self.module_name = module_name
169
self._module = __import__(self.module_name, {}, {}, [''])
180
def feature_name(self):
181
return self.module_name
184
class _HTTPSServerFeature(Feature):
185
"""Some tests want an https Server, check if one is available.
187
Right now, the only way this is available is under python2.6 which provides
198
def feature_name(self):
202
HTTPSServerFeature = _HTTPSServerFeature()
205
class _UnicodeFilename(Feature):
206
"""Does the filesystem support Unicode filenames?"""
211
except UnicodeEncodeError:
213
except (IOError, OSError):
214
# The filesystem allows the Unicode filename but the file doesn't
218
# The filesystem allows the Unicode filename and the file exists,
222
UnicodeFilename = _UnicodeFilename()
225
class _ByteStringNamedFilesystem(Feature):
226
"""Is the filesystem based on bytes?"""
229
if os.name == "posix":
233
ByteStringNamedFilesystem = _ByteStringNamedFilesystem()
236
class _UTF8Filesystem(Feature):
237
"""Is the filesystem UTF-8?"""
240
if osutils._fs_enc.upper() in ('UTF-8', 'UTF8'):
244
UTF8Filesystem = _UTF8Filesystem()
247
class _BreakinFeature(Feature):
248
"""Does this platform support the breakin feature?"""
251
from bzrlib import breakin
252
if breakin.determine_signal() is None:
254
if sys.platform == 'win32':
255
# Windows doesn't have os.kill, and we catch the SIGBREAK signal.
256
# We trigger SIGBREAK via a Console api so we need ctypes to
257
# access the function
264
def feature_name(self):
265
return "SIGQUIT or SIGBREAK w/ctypes on win32"
268
BreakinFeature = _BreakinFeature()
271
class _CaseInsCasePresFilenameFeature(Feature):
272
"""Is the file-system case insensitive, but case-preserving?"""
275
fileno, name = tempfile.mkstemp(prefix='MixedCase')
277
# first check truly case-preserving for created files, then check
278
# case insensitive when opening existing files.
279
name = osutils.normpath(name)
280
base, rel = osutils.split(name)
281
found_rel = osutils.canonical_relpath(base, name)
282
return (found_rel == rel
283
and os.path.isfile(name.upper())
284
and os.path.isfile(name.lower()))
289
def feature_name(self):
290
return "case-insensitive case-preserving filesystem"
292
CaseInsCasePresFilenameFeature = _CaseInsCasePresFilenameFeature()
295
class _CaseInsensitiveFilesystemFeature(Feature):
296
"""Check if underlying filesystem is case-insensitive but *not* case
299
# Note that on Windows, Cygwin, MacOS etc, the file-systems are far
300
# more likely to be case preserving, so this case is rare.
303
if CaseInsCasePresFilenameFeature.available():
306
if tests.TestCaseWithMemoryTransport.TEST_ROOT is None:
307
root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
308
tests.TestCaseWithMemoryTransport.TEST_ROOT = root
310
root = tests.TestCaseWithMemoryTransport.TEST_ROOT
311
tdir = osutils.mkdtemp(prefix='case-sensitive-probe-', suffix='',
313
name_a = osutils.pathjoin(tdir, 'a')
314
name_A = osutils.pathjoin(tdir, 'A')
316
result = osutils.isdir(name_A)
317
tests._rmtree_temp_dir(tdir)
320
def feature_name(self):
321
return 'case-insensitive filesystem'
323
CaseInsensitiveFilesystemFeature = _CaseInsensitiveFilesystemFeature()
326
class _CaseSensitiveFilesystemFeature(Feature):
329
if CaseInsCasePresFilenameFeature.available():
331
elif CaseInsensitiveFilesystemFeature.available():
336
def feature_name(self):
337
return 'case-sensitive filesystem'
339
# new coding style is for feature instances to be lowercase
340
case_sensitive_filesystem_feature = _CaseSensitiveFilesystemFeature()
343
class _NotRunningAsRoot(Feature):