~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/features.py

  • Committer: Aaron Bentley
  • Date: 2006-09-29 18:58:43 UTC
  • mto: This revision was merged to the branch mainline in revision 2127.
  • Revision ID: abentley@panoramicfeedback.com-20060929185843-6841128d849c46a2
Get page generation working

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
"""A collection of commonly used 'Features' which bzrlib uses to skip tests."""
18
 
 
19
 
import os
20
 
import stat
21
 
 
22
 
from bzrlib import (
23
 
    osutils,
24
 
    tests,
25
 
    )
26
 
 
27
 
 
28
 
class _NotRunningAsRoot(tests.Feature):
29
 
 
30
 
    def _probe(self):
31
 
        try:
32
 
            uid = os.getuid()
33
 
        except AttributeError:
34
 
            # If there is no uid, chances are there is no root either
35
 
            return True
36
 
        return uid != 0
37
 
 
38
 
    def feature_name(self):
39
 
        return 'Not running as root'
40
 
 
41
 
 
42
 
not_running_as_root = _NotRunningAsRoot()
43
 
 
44
 
apport = tests.ModuleAvailableFeature('apport')
45
 
lzma = tests.ModuleAvailableFeature('lzma')
46
 
meliae = tests.ModuleAvailableFeature('meliae')
47
 
paramiko = tests.ModuleAvailableFeature('paramiko')
48
 
pycurl = tests.ModuleAvailableFeature('pycurl')
49
 
pywintypes = tests.ModuleAvailableFeature('pywintypes')
50
 
sphinx = tests.ModuleAvailableFeature('sphinx')
51
 
subunit = tests.ModuleAvailableFeature('subunit')
52
 
 
53
 
 
54
 
class _BackslashDirSeparatorFeature(tests.Feature):
55
 
 
56
 
    def _probe(self):
57
 
        try:
58
 
            os.lstat(os.getcwd() + '\\')
59
 
        except OSError:
60
 
            return False
61
 
        else:
62
 
            return True
63
 
 
64
 
    def feature_name(self):
65
 
        return "Filesystem treats '\\' as a directory separator."
66
 
 
67
 
backslashdir_feature = _BackslashDirSeparatorFeature()
68
 
 
69
 
 
70
 
class _PosixPermissionsFeature(tests.Feature):
71
 
 
72
 
    def _probe(self):
73
 
        def has_perms():
74
 
            # create temporary file and check if specified perms are maintained.
75
 
            import tempfile
76
 
 
77
 
            write_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
78
 
            f = tempfile.mkstemp(prefix='bzr_perms_chk_')
79
 
            fd, name = f
80
 
            os.close(fd)
81
 
            os.chmod(name, write_perms)
82
 
 
83
 
            read_perms = os.stat(name).st_mode & 0777
84
 
            os.unlink(name)
85
 
            return (write_perms == read_perms)
86
 
 
87
 
        return (os.name == 'posix') and has_perms()
88
 
 
89
 
    def feature_name(self):
90
 
        return 'POSIX permissions support'
91
 
 
92
 
 
93
 
posix_permissions_feature = _PosixPermissionsFeature()
94
 
 
95
 
 
96
 
class _ChownFeature(tests.Feature):
97
 
    """os.chown is supported"""
98
 
 
99
 
    def _probe(self):
100
 
        return os.name == 'posix' and hasattr(os, 'chown')
101
 
 
102
 
chown_feature = _ChownFeature()
103
 
 
104
 
 
105
 
class ExecutableFeature(tests.Feature):
106
 
    """Feature testing whether an executable of a given name is on the PATH."""
107
 
 
108
 
    def __init__(self, name):
109
 
        super(ExecutableFeature, self).__init__()
110
 
        self.name = name
111
 
        self._path = None
112
 
 
113
 
    @property
114
 
    def path(self):
115
 
        # This is a property, so accessing path ensures _probe was called
116
 
        self.available()
117
 
        return self._path
118
 
 
119
 
    def _probe(self):
120
 
        self._path = osutils.find_executable_on_path(self.name)
121
 
        return self._path is not None
122
 
 
123
 
    def feature_name(self):
124
 
        return '%s executable' % self.name
125
 
 
126
 
 
127
 
bash_feature = ExecutableFeature('bash')
128
 
sed_feature = ExecutableFeature('sed')
129
 
diff_feature = ExecutableFeature('diff')