~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Andrew Bennetts
  • Date: 2008-10-01 05:40:45 UTC
  • mfrom: (3753 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20081001054045-z50qc0d3p9qsc5im
Merge from bzr.dev; resolve osutils.py conflict by reverting my sha import hackery.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import sys
19
19
 
20
20
from bzrlib import osutils
21
 
from bzrlib.tests import TestCase, TestCaseInTempDir, Feature
 
21
from bzrlib.tests import TestCase, TestCaseInTempDir, TestSkipped, Feature
22
22
from bzrlib.win32utils import glob_expand, get_app_path
 
23
from bzrlib import win32utils
23
24
 
24
25
 
25
26
# Features
36
37
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
37
38
 
38
39
 
39
 
class _Win32RegistryFeature(Feature):
 
40
class _RequiredModuleFeature(Feature):
 
41
 
 
42
    def __init__(self, mod_name):
 
43
        self.mod_name = mod_name
 
44
        super(_RequiredModuleFeature, self).__init__()
40
45
 
41
46
    def _probe(self):
42
47
        try:
43
 
            import _winreg
 
48
            __import__(self.mod_name)
44
49
            return True
45
50
        except ImportError:
46
51
            return False
47
52
 
48
53
    def feature_name(self):
49
 
        return '_winreg'
 
54
        return self.mod_name
50
55
 
51
 
Win32RegistryFeature = _Win32RegistryFeature()
 
56
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
 
57
CtypesFeature = _RequiredModuleFeature('ctypes')
 
58
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
52
59
 
53
60
 
54
61
# Tests
160
167
    def test_not_existing(self):
161
168
        p = get_app_path('not-existing')
162
169
        self.assertEquals('not-existing', p)
 
170
 
 
171
 
 
172
class TestLocationsCtypes(TestCase):
 
173
 
 
174
    _test_needs_features = [CtypesFeature]
 
175
 
 
176
    def assertPathsEqual(self, p1, p2):
 
177
        # TODO: The env var values in particular might return the "short"
 
178
        # version (ie, "C:\DOCUME~1\...").  Its even possible the returned
 
179
        # values will differ only by case - handle these situations as we
 
180
        # come across them.
 
181
        self.assertEquals(p1, p2)
 
182
 
 
183
    def test_appdata_not_using_environment(self):
 
184
        # Test that we aren't falling back to the environment
 
185
        first = win32utils.get_appdata_location()
 
186
        self._captureVar("APPDATA", None)
 
187
        self.assertPathsEqual(first, win32utils.get_appdata_location())
 
188
 
 
189
    def test_appdata_matches_environment(self):
 
190
        # Typically the APPDATA environment variable will match
 
191
        # get_appdata_location
 
192
        # XXX - See bug 262874, which asserts the correct encoding is 'mbcs',
 
193
        encoding = osutils.get_user_encoding()
 
194
        env_val = os.environ.get("APPDATA", None)
 
195
        if not env_val:
 
196
            raise TestSkipped("No APPDATA environment variable exists")
 
197
        self.assertPathsEqual(win32utils.get_appdata_location(),
 
198
                              env_val.decode(encoding))
 
199
 
 
200
    def test_local_appdata_not_using_environment(self):
 
201
        # Test that we aren't falling back to the environment
 
202
        first = win32utils.get_local_appdata_location()
 
203
        self._captureVar("LOCALAPPDATA", None)
 
204
        self.assertPathsEqual(first, win32utils.get_local_appdata_location())
 
205
 
 
206
    def test_local_appdata_matches_environment(self):
 
207
        # LOCALAPPDATA typically only exists on Vista, so we only attempt to
 
208
        # compare when it exists.
 
209
        lad = win32utils.get_local_appdata_location()
 
210
        env = os.environ.get("LOCALAPPDATA")
 
211
        if env:
 
212
            # XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
 
213
            encoding = osutils.get_user_encoding()
 
214
            self.assertPathsEqual(lad, env.decode(encoding))
 
215
 
 
216
 
 
217
class TestLocationsPywin32(TestLocationsCtypes):
 
218
 
 
219
    _test_needs_features = [Win32comShellFeature]
 
220
 
 
221
    def setUp(self):
 
222
        super(TestLocationsPywin32, self).setUp()
 
223
        # We perform the exact same tests after disabling the use of ctypes.
 
224
        # This causes the implementation to fall back to pywin32.
 
225
        self.old_ctypes = win32utils.has_ctypes
 
226
        win32utils.has_ctypes = False
 
227
        self.addCleanup(self.restoreCtypes)
 
228
 
 
229
    def restoreCtypes(self):
 
230
        win32utils.has_ctypes = self.old_ctypes