~bzr-pqm/bzr/bzr.dev

5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
4584.3.21 by Martin Pool
Start adding tests for apport
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
5241.2.1 by Robert Collins
Merge up from 2.0/2.1:
17
"""A collection of commonly used 'Features' which bzrlib uses to skip tests."""
18
5036.3.8 by Parth Malwankar
closed review comments from vila
19
import os
20
import stat
4584.3.21 by Martin Pool
Start adding tests for apport
21
5321.2.1 by Vincent Ladeuil
Fix style issues, including vertical spaces, lines too long and multi lines imports.
22
from bzrlib import (
23
    osutils,
24
    tests,
25
    )
4913.2.19 by John Arbash Meinel
Compatibly rename ApportFeature to features.apport.
26
27
4797.70.1 by Vincent Ladeuil
Skip chmodbits dependent tests when running as root
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()
5448.5.9 by Vincent Ladeuil
Make the test depends on a feature so it's skipped if meliae is not installed
43
4913.2.19 by John Arbash Meinel
Compatibly rename ApportFeature to features.apport.
44
apport = tests.ModuleAvailableFeature('apport')
5448.5.9 by Vincent Ladeuil
Make the test depends on a feature so it's skipped if meliae is not installed
45
meliae = tests.ModuleAvailableFeature('meliae')
4913.2.18 by John Arbash Meinel
Add a _CompatibilityThunkFeature.
46
paramiko = tests.ModuleAvailableFeature('paramiko')
4913.2.11 by John Arbash Meinel
Convert a bunch more features over to ModuleAvailableFeature
47
pycurl = tests.ModuleAvailableFeature('pycurl')
5200.4.5 by Martin
Move pywintypes ModuleAvailableFeature to bzrlib.tests.features
48
pywintypes = tests.ModuleAvailableFeature('pywintypes')
5448.5.9 by Vincent Ladeuil
Make the test depends on a feature so it's skipped if meliae is not installed
49
sphinx = tests.ModuleAvailableFeature('sphinx')
4913.2.18 by John Arbash Meinel
Add a _CompatibilityThunkFeature.
50
subunit = tests.ModuleAvailableFeature('subunit')
5036.3.8 by Parth Malwankar
closed review comments from vila
51
5094.3.1 by Martin Pool
``.bazaar``, ``.bazaar/bazaar.conf`` and ``.bzr.log`` inherit user and group ownership from the containing directory. This allow bzr to work better with sudo.
52
5241.2.1 by Robert Collins
Merge up from 2.0/2.1:
53
class _BackslashDirSeparatorFeature(tests.Feature):
54
55
    def _probe(self):
56
        try:
57
            os.lstat(os.getcwd() + '\\')
58
        except OSError:
59
            return False
60
        else:
61
            return True
62
63
    def feature_name(self):
64
        return "Filesystem treats '\\' as a directory separator."
65
66
backslashdir_feature = _BackslashDirSeparatorFeature()
67
68
5036.3.8 by Parth Malwankar
closed review comments from vila
69
class _PosixPermissionsFeature(tests.Feature):
70
71
    def _probe(self):
72
        def has_perms():
73
            # create temporary file and check if specified perms are maintained.
74
            import tempfile
75
76
            write_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
77
            f = tempfile.mkstemp(prefix='bzr_perms_chk_')
78
            fd, name = f
79
            os.close(fd)
80
            os.chmod(name, write_perms)
81
82
            read_perms = os.stat(name).st_mode & 0777
83
            os.unlink(name)
84
            return (write_perms == read_perms)
85
86
        return (os.name == 'posix') and has_perms()
87
88
    def feature_name(self):
89
        return 'POSIX permissions support'
90
5094.3.1 by Martin Pool
``.bazaar``, ``.bazaar/bazaar.conf`` and ``.bzr.log`` inherit user and group ownership from the containing directory. This allow bzr to work better with sudo.
91
92
posix_permissions_feature = _PosixPermissionsFeature()
93
94
5051.4.10 by Parth Malwankar
moved ChownFeature to tests/features.py
95
class _ChownFeature(tests.Feature):
96
    """os.chown is supported"""
97
98
    def _probe(self):
99
        return os.name == 'posix' and hasattr(os, 'chown')
100
5051.4.11 by Parth Malwankar
closed Martins review comments.
101
chown_feature = _ChownFeature()
5051.4.10 by Parth Malwankar
moved ChownFeature to tests/features.py
102
5147.5.20 by Martin von Gagern
Move ExecutableFeature class from bash_completion plugin to bzrlib tests.
103
104
class ExecutableFeature(tests.Feature):
105
    """Feature testing whether an executable of a given name is on the PATH."""
106
107
    def __init__(self, name):
108
        super(ExecutableFeature, self).__init__()
109
        self.name = name
5147.5.23 by Martin von Gagern
Have ExecutableFeature implement and use _probe.
110
        self._path = None
5147.5.20 by Martin von Gagern
Move ExecutableFeature class from bash_completion plugin to bzrlib tests.
111
112
    @property
113
    def path(self):
5147.5.23 by Martin von Gagern
Have ExecutableFeature implement and use _probe.
114
        # This is a property, so accessing path ensures _probe was called
115
        self.available()
116
        return self._path
5147.5.20 by Martin von Gagern
Move ExecutableFeature class from bash_completion plugin to bzrlib tests.
117
5147.5.23 by Martin von Gagern
Have ExecutableFeature implement and use _probe.
118
    def _probe(self):
5321.1.82 by Gordon Tyler
Changed ExecutableFeature to use osutils.find_executable_on_path.
119
        self._path = osutils.find_executable_on_path(self.name)
120
        return self._path is not None
5147.5.20 by Martin von Gagern
Move ExecutableFeature class from bash_completion plugin to bzrlib tests.
121
122
    def feature_name(self):
123
        return '%s executable' % self.name
5147.5.24 by Martin von Gagern
Move ExecutableFeature instances to tests.features module.
124
125
126
bash_feature = ExecutableFeature('bash')
127
sed_feature = ExecutableFeature('sed')
5349.1.5 by Matthäus G. Chajdas
Fix issues raised by Vincent Ladeuil.
128
diff_feature = ExecutableFeature('diff')