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 __future__ import absolute_import
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):
34
class Feature(object):
35
"""An operating system Feature."""
38
self._available = None
41
"""Is the feature available?
43
:return: True if the feature is available.
45
if self._available is None:
46
self._available = self._probe()
47
return self._available
50
"""Implement this method in concrete features.
52
:return: True if the feature is available.
54
raise NotImplementedError
57
if getattr(self, 'feature_name', None):
58
return self.feature_name()
59
return self.__class__.__name__
62
class _SymlinkFeature(Feature):
65
return osutils.has_symlinks()
67
def feature_name(self):
70
SymlinkFeature = _SymlinkFeature()
73
class _HardlinkFeature(Feature):
76
return osutils.has_hardlinks()
78
def feature_name(self):
81
HardlinkFeature = _HardlinkFeature()
84
class _OsFifoFeature(Feature):
87
return getattr(os, 'mkfifo', None)
89
def feature_name(self):
90
return 'filesystem fifos'
92
OsFifoFeature = _OsFifoFeature()
95
class _UnicodeFilenameFeature(Feature):
96
"""Does the filesystem support Unicode filenames?"""
100
# Check for character combinations unlikely to be covered by any
101
# single non-unicode encoding. We use the characters
102
# - greek small letter alpha (U+03B1) and
103
# - braille pattern dots-123456 (U+283F).
104
os.stat(u'\u03b1\u283f')
105
except UnicodeEncodeError:
107
except (IOError, OSError):
108
# The filesystem allows the Unicode filename but the file doesn't
112
# The filesystem allows the Unicode filename and the file exists,
116
UnicodeFilenameFeature = _UnicodeFilenameFeature()
119
class _CompatabilityThunkFeature(Feature):
120
"""This feature is just a thunk to another feature.
122
It issues a deprecation warning if it is accessed, to let you know that you
123
should really use a different feature.
126
def __init__(self, dep_version, module, name,
127
replacement_name, replacement_module=None):
128
super(_CompatabilityThunkFeature, self).__init__()
129
self._module = module
130
if replacement_module is None:
131
replacement_module = module
132
self._replacement_module = replacement_module
134
self._replacement_name = replacement_name
135
self._dep_version = dep_version
139
if self._feature is None:
140
from breezy import pyutils
141
depr_msg = self._dep_version % ('%s.%s'
142
% (self._module, self._name))
143
use_msg = ' Use %s.%s instead.' % (self._replacement_module,
144
self._replacement_name)
145
symbol_versioning.warn(depr_msg + use_msg, DeprecationWarning,
147
# Import the new feature and use it as a replacement for the
149
self._feature = pyutils.get_named_object(
150
self._replacement_module, self._replacement_name)
154
return self._feature._probe()
157
class ModuleAvailableFeature(Feature):
158
"""This is a feature than describes a module we want to be available.
160
Declare the name of the module in __init__(), and then after probing, the
161
module will be available as 'self.module'.
163
:ivar module: The module if it is available, else None.
166
def __init__(self, module_name):
167
super(ModuleAvailableFeature, self).__init__()
168
self.module_name = module_name
172
module = sys.modules.get(self.module_name, sentinel)
173
if module is sentinel:
175
self._module = __import__(self.module_name, {}, {}, [''])
180
self._module = module
189
def feature_name(self):
190
return self.module_name
193
class PluginLoadedFeature(Feature):
194
"""Check whether a plugin with specific name is loaded.
196
This is different from ModuleAvailableFeature, because
197
plugins can be available but explicitly disabled
198
(e.g. through BRZ_DISABLE_PLUGINS=blah).
200
:ivar plugin_name: The name of the plugin
203
def __init__(self, plugin_name):
204
super(PluginLoadedFeature, self).__init__()
205
self.plugin_name = plugin_name
209
return self.plugin_name in breezy.global_state.plugins
214
return breezy.global_state.plugins.get(self.plugin_name)
216
def feature_name(self):
217
return '%s plugin' % self.plugin_name
220
class _HTTPSServerFeature(Feature):
221
"""Some tests want an https Server, check if one is available.
223
Right now, the only way this is available is under python2.6 which provides
234
def feature_name(self):
238
HTTPSServerFeature = _HTTPSServerFeature()
241
class _ByteStringNamedFilesystem(Feature):
242
"""Is the filesystem based on bytes?"""
245
if os.name == "posix":
249
ByteStringNamedFilesystem = _ByteStringNamedFilesystem()
252
class _UTF8Filesystem(Feature):
253
"""Is the filesystem UTF-8?"""
256
if osutils._fs_enc.upper() in ('UTF-8', 'UTF8'):
260
UTF8Filesystem = _UTF8Filesystem()
263
class _BreakinFeature(Feature):
264
"""Does this platform support the breakin feature?"""
267
from breezy import breakin
268
if breakin.determine_signal() is None:
270
if sys.platform == 'win32':
271
# Windows doesn't have os.kill, and we catch the SIGBREAK signal.
272
# We trigger SIGBREAK via a Console api so we need ctypes to
273
# access the function
280
def feature_name(self):
281
return "SIGQUIT or SIGBREAK w/ctypes on win32"
284
BreakinFeature = _BreakinFeature()
287
class _CaseInsCasePresFilenameFeature(Feature):
288
"""Is the file-system case insensitive, but case-preserving?"""
291
fileno, name = tempfile.mkstemp(prefix='MixedCase')
293
# first check truly case-preserving for created files, then check
294
# case insensitive when opening existing files.
295
name = osutils.normpath(name)
296
base, rel = osutils.split(name)
297
found_rel = osutils.canonical_relpath(base, name)
298
return (found_rel == rel
299
and os.path.isfile(name.upper())
300
and os.path.isfile(name.lower()))
305
def feature_name(self):
306
return "case-insensitive case-preserving filesystem"
308
CaseInsCasePresFilenameFeature = _CaseInsCasePresFilenameFeature()
311
class _CaseInsensitiveFilesystemFeature(Feature):
312
"""Check if underlying filesystem is case-insensitive but *not* case
315
# Note that on Windows, Cygwin, MacOS etc, the file-systems are far
316
# more likely to be case preserving, so this case is rare.
319
if CaseInsCasePresFilenameFeature.available():
322
from breezy import tests
324
if tests.TestCaseWithMemoryTransport.TEST_ROOT is None:
325
root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
326
tests.TestCaseWithMemoryTransport.TEST_ROOT = root
328
root = tests.TestCaseWithMemoryTransport.TEST_ROOT
329
tdir = osutils.mkdtemp(prefix='case-sensitive-probe-', suffix='',
331
name_a = osutils.pathjoin(tdir, 'a')
332
name_A = osutils.pathjoin(tdir, 'A')
334
result = osutils.isdir(name_A)
335
tests._rmtree_temp_dir(tdir)
338
def feature_name(self):
339
return 'case-insensitive filesystem'
341
CaseInsensitiveFilesystemFeature = _CaseInsensitiveFilesystemFeature()
344
class _CaseSensitiveFilesystemFeature(Feature):
347
if CaseInsCasePresFilenameFeature.available():
349
elif CaseInsensitiveFilesystemFeature.available():
354
def feature_name(self):
355
return 'case-sensitive filesystem'
357
# new coding style is for feature instances to be lowercase
358
case_sensitive_filesystem_feature = _CaseSensitiveFilesystemFeature()
361
class _NotRunningAsRoot(Feature):
366
except AttributeError:
367
# If there is no uid, chances are there is no root either
371
def feature_name(self):
372
return 'Not running as root'
375
not_running_as_root = _NotRunningAsRoot()
377
apport = ModuleAvailableFeature('apport.report')
378
gpg = ModuleAvailableFeature('gpg')
379
lzma = ModuleAvailableFeature('lzma')
380
meliae = ModuleAvailableFeature('meliae.scanner')
381
paramiko = ModuleAvailableFeature('paramiko')
382
pywintypes = ModuleAvailableFeature('pywintypes')
383
subunit = ModuleAvailableFeature('subunit')
384
testtools = ModuleAvailableFeature('testtools')
386
compiled_patiencediff_feature = ModuleAvailableFeature(
387
'breezy._patiencediff_c')
388
lsprof_feature = ModuleAvailableFeature('breezy.lsprof')
391
class _BackslashDirSeparatorFeature(Feature):
395
os.lstat(os.getcwd() + '\\')
401
def feature_name(self):
402
return "Filesystem treats '\\' as a directory separator."
404
backslashdir_feature = _BackslashDirSeparatorFeature()
407
class _ChownFeature(Feature):
408
"""os.chown is supported"""
411
return os.name == 'posix' and hasattr(os, 'chown')
413
chown_feature = _ChownFeature()
416
class ExecutableFeature(Feature):
417
"""Feature testing whether an executable of a given name is on the PATH."""
419
def __init__(self, name):
420
super(ExecutableFeature, self).__init__()
426
# This is a property, so accessing path ensures _probe was called
431
self._path = osutils.find_executable_on_path(self.name)
432
return self._path is not None
434
def feature_name(self):
435
return '%s executable' % self.name
438
bash_feature = ExecutableFeature('bash')
439
diff_feature = ExecutableFeature('diff')
440
sed_feature = ExecutableFeature('sed')
441
msgmerge_feature = ExecutableFeature('msgmerge')
444
class _PosixPermissionsFeature(Feature):
34
# create temporary file and check if specified perms are maintained.
448
# Create temporary file and check if specified perms are
37
450
write_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
38
451
f = tempfile.mkstemp(prefix='bzr_perms_chk_')
41
os.chmod(name, write_perms)
454
osutils.chmod_if_possible(name, write_perms)
43
read_perms = os.stat(name).st_mode & 0777
456
read_perms = os.stat(name).st_mode & 0o777
45
458
return (write_perms == read_perms)