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' to optionally run tests.
20
from bzrlib import tests
21
from bzrlib.symbol_versioning import deprecated_in
24
apport = tests.ModuleAvailableFeature('apport')
25
paramiko = tests.ModuleAvailableFeature('paramiko')
26
pycurl = tests.ModuleAvailableFeature('pycurl')
27
subunit = tests.ModuleAvailableFeature('subunit')
30
class _PosixPermissionsFeature(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 breezy 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,
145
# Import the new feature and use it as a replacement for the
147
self._feature = pyutils.get_named_object(
148
self._replacement_module, self._replacement_name)
152
return self._feature._probe()
155
class ModuleAvailableFeature(Feature):
156
"""This is a feature than describes a module we want to be available.
158
Declare the name of the module in __init__(), and then after probing, the
159
module will be available as 'self.module'.
161
:ivar module: The module if it is available, else None.
164
def __init__(self, module_name):
165
super(ModuleAvailableFeature, self).__init__()
166
self.module_name = module_name
170
module = sys.modules.get(self.module_name, sentinel)
171
if module is sentinel:
173
self._module = __import__(self.module_name, {}, {}, [''])
178
self._module = module
187
def feature_name(self):
188
return self.module_name
191
class PluginLoadedFeature(Feature):
192
"""Check whether a plugin with specific name is loaded.
194
This is different from ModuleAvailableFeature, because
195
plugins can be available but explicitly disabled
196
(e.g. through BRZ_DISABLE_PLUGINS=blah).
198
:ivar plugin_name: The name of the plugin
201
def __init__(self, plugin_name):
202
super(PluginLoadedFeature, self).__init__()
203
self.plugin_name = plugin_name
207
return self.plugin_name in breezy.global_state.plugins
212
return breezy.global_state.plugins.get(self.plugin_name)
214
def feature_name(self):
215
return '%s plugin' % self.plugin_name
218
class _HTTPSServerFeature(Feature):
219
"""Some tests want an https Server, check if one is available.
221
Right now, the only way this is available is under python2.6 which provides
232
def feature_name(self):
236
HTTPSServerFeature = _HTTPSServerFeature()
239
class _ByteStringNamedFilesystem(Feature):
240
"""Is the filesystem based on bytes?"""
243
if os.name == "posix":
247
ByteStringNamedFilesystem = _ByteStringNamedFilesystem()
250
class _UTF8Filesystem(Feature):
251
"""Is the filesystem UTF-8?"""
254
if osutils._fs_enc.upper() in ('UTF-8', 'UTF8'):
258
UTF8Filesystem = _UTF8Filesystem()
261
class _BreakinFeature(Feature):
262
"""Does this platform support the breakin feature?"""
265
from breezy import breakin
266
if breakin.determine_signal() is None:
268
if sys.platform == 'win32':
269
# Windows doesn't have os.kill, and we catch the SIGBREAK signal.
270
# We trigger SIGBREAK via a Console api so we need ctypes to
271
# access the function
278
def feature_name(self):
279
return "SIGQUIT or SIGBREAK w/ctypes on win32"
282
BreakinFeature = _BreakinFeature()
285
class _CaseInsCasePresFilenameFeature(Feature):
286
"""Is the file-system case insensitive, but case-preserving?"""
289
fileno, name = tempfile.mkstemp(prefix='MixedCase')
291
# first check truly case-preserving for created files, then check
292
# case insensitive when opening existing files.
293
name = osutils.normpath(name)
294
base, rel = osutils.split(name)
295
found_rel = osutils.canonical_relpath(base, name)
296
return (found_rel == rel
297
and os.path.isfile(name.upper())
298
and os.path.isfile(name.lower()))
303
def feature_name(self):
304
return "case-insensitive case-preserving filesystem"
306
CaseInsCasePresFilenameFeature = _CaseInsCasePresFilenameFeature()
309
class _CaseInsensitiveFilesystemFeature(Feature):
310
"""Check if underlying filesystem is case-insensitive but *not* case
313
# Note that on Windows, Cygwin, MacOS etc, the file-systems are far
314
# more likely to be case preserving, so this case is rare.
317
if CaseInsCasePresFilenameFeature.available():
320
from breezy import tests
322
if tests.TestCaseWithMemoryTransport.TEST_ROOT is None:
323
root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
324
tests.TestCaseWithMemoryTransport.TEST_ROOT = root
326
root = tests.TestCaseWithMemoryTransport.TEST_ROOT
327
tdir = osutils.mkdtemp(prefix='case-sensitive-probe-', suffix='',
329
name_a = osutils.pathjoin(tdir, 'a')
330
name_A = osutils.pathjoin(tdir, 'A')
332
result = osutils.isdir(name_A)
333
tests._rmtree_temp_dir(tdir)
336
def feature_name(self):
337
return 'case-insensitive filesystem'
339
CaseInsensitiveFilesystemFeature = _CaseInsensitiveFilesystemFeature()
342
class _CaseSensitiveFilesystemFeature(Feature):
345
if CaseInsCasePresFilenameFeature.available():
347
elif CaseInsensitiveFilesystemFeature.available():
352
def feature_name(self):
353
return 'case-sensitive filesystem'
355
# new coding style is for feature instances to be lowercase
356
case_sensitive_filesystem_feature = _CaseSensitiveFilesystemFeature()
359
class _NotRunningAsRoot(Feature):
364
except AttributeError:
365
# If there is no uid, chances are there is no root either
369
def feature_name(self):
370
return 'Not running as root'
373
not_running_as_root = _NotRunningAsRoot()
375
apport = ModuleAvailableFeature('apport')
376
gpgme = ModuleAvailableFeature('gpgme')
377
lzma = ModuleAvailableFeature('lzma')
378
meliae = ModuleAvailableFeature('meliae.scanner')
379
paramiko = ModuleAvailableFeature('paramiko')
380
pywintypes = ModuleAvailableFeature('pywintypes')
381
subunit = ModuleAvailableFeature('subunit')
382
testtools = ModuleAvailableFeature('testtools')
384
compiled_patiencediff_feature = ModuleAvailableFeature(
385
'breezy._patiencediff_c')
386
lsprof_feature = ModuleAvailableFeature('breezy.lsprof')
389
class _BackslashDirSeparatorFeature(Feature):
393
os.lstat(os.getcwd() + '\\')
399
def feature_name(self):
400
return "Filesystem treats '\\' as a directory separator."
402
backslashdir_feature = _BackslashDirSeparatorFeature()
405
class _ChownFeature(Feature):
406
"""os.chown is supported"""
409
return os.name == 'posix' and hasattr(os, 'chown')
411
chown_feature = _ChownFeature()
414
class ExecutableFeature(Feature):
415
"""Feature testing whether an executable of a given name is on the PATH."""
417
def __init__(self, name):
418
super(ExecutableFeature, self).__init__()
424
# This is a property, so accessing path ensures _probe was called
429
self._path = osutils.find_executable_on_path(self.name)
430
return self._path is not None
432
def feature_name(self):
433
return '%s executable' % self.name
436
bash_feature = ExecutableFeature('bash')
437
diff_feature = ExecutableFeature('diff')
438
sed_feature = ExecutableFeature('sed')
439
msgmerge_feature = ExecutableFeature('msgmerge')
442
class _PosixPermissionsFeature(Feature):
34
# create temporary file and check if specified perms are maintained.
446
# Create temporary file and check if specified perms are
37
448
write_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
38
449
f = tempfile.mkstemp(prefix='bzr_perms_chk_')
41
os.chmod(name, write_perms)
452
osutils.chmod_if_possible(name, write_perms)
43
read_perms = os.stat(name).st_mode & 0777
454
read_perms = os.stat(name).st_mode & 0o777
45
456
return (write_perms == read_perms)