~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Mark Hammond
  • Date: 2008-08-23 03:58:23 UTC
  • mto: This revision was merged to the branch mainline in revision 3728.
  • Revision ID: mhammond@skippinet.com.au-20080823035823-1anntol029n0il6r
Add win32utils.get_local_appdata_location() so bzr and plugins can 
differentiate between the 'local' and 'roaming' locations offered by
windows.  Includes support for falling back to pywin32 if ctypes isn't
available and introduces new tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import os
18
18
import sys
19
19
 
20
 
from bzrlib import osutils
 
20
from bzrlib import osutils, get_user_encoding
21
21
from bzrlib.tests import TestCase, TestCaseInTempDir, Feature
22
22
from bzrlib.win32utils import glob_expand, get_app_path
 
23
import bzrlib.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
class TestLocationsCtypes(TestCase):
 
172
 
 
173
    _test_needs_features = [CtypesFeature]
 
174
 
 
175
    def test_appdata_not_using_environment(self):
 
176
        # Test that we aren't falling back to the environment
 
177
        first = bzrlib.win32utils.get_appdata_location()
 
178
        old = os.environ.get("APPDATA")
 
179
        if old is not None:
 
180
            del os.environ["APPDATA"]
 
181
        try:
 
182
            self.assertEquals(first, bzrlib.win32utils.get_appdata_location())
 
183
        finally:
 
184
            if old is not None:
 
185
                os.environ["APPDATA"] = old
 
186
 
 
187
    def test_appdata_matches_environment(self):
 
188
        # Typically the APPDATA environment variable will match
 
189
        # get_appdata_location
 
190
        self.assertEquals(bzrlib.win32utils.get_appdata_location(),
 
191
                          os.environ["APPDATA"].decode(get_user_encoding()))
 
192
 
 
193
    def test_local_appdata_not_using_environment(self):
 
194
        # Test that we aren't falling back to the environment
 
195
        first = bzrlib.win32utils.get_local_appdata_location()
 
196
        old = os.environ.get("LOCALAPPDATA")
 
197
        if old is not None:
 
198
            del os.environ["LOCALAPPDATA"]
 
199
        try:
 
200
            self.assertEquals(first,
 
201
                              bzrlib.win32utils.get_local_appdata_location())
 
202
        finally:
 
203
            if old is not None:
 
204
                os.environ["LOCALAPPDATA"] = old
 
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 = bzrlib.win32utils.get_local_appdata_location()
 
210
        env = os.environ.get("LOCALAPPDATA")
 
211
        if env:
 
212
            self.assertEquals(lad, env)
 
213
 
 
214
class TestLocationsPywin32(TestCase):
 
215
 
 
216
    _test_needs_features = [Win32comShellFeature]
 
217
 
 
218
    def setUp(self):
 
219
        self.old_ctypes = bzrlib.win32utils.has_ctypes
 
220
        bzrlib.win32utils.has_ctypes = False
 
221
        self.addCleanup(self.restoreCtypes)
 
222
 
 
223
    def restoreCtypes(self):
 
224
        bzrlib.win32utils.has_ctypes = self.old_ctypes
 
225
 
 
226
    def test_appdata_not_using_environment(self):
 
227
        # Test that we aren't falling back to the environment
 
228
        first = bzrlib.win32utils.get_appdata_location()
 
229
        old = os.environ.get("APPDATA")
 
230
        if old is not None:
 
231
            del os.environ["APPDATA"]
 
232
        try:
 
233
            self.assertEquals(first, bzrlib.win32utils.get_appdata_location())
 
234
        finally:
 
235
            if old is not None:
 
236
                os.environ["APPDATA"] = old
 
237
 
 
238
    def test_appdata_matches_environment(self):
 
239
        # Typically the APPDATA environment variable will match
 
240
        # get_appdata_location
 
241
        self.assertEquals(bzrlib.win32utils.get_appdata_location(),
 
242
                          os.environ["APPDATA"].decode(get_user_encoding()))
 
243
 
 
244
    def test_local_appdata_not_using_environment(self):
 
245
        # Test that we aren't falling back to the environment
 
246
        first = bzrlib.win32utils.get_local_appdata_location()
 
247
        old = os.environ.get("LOCALAPPDATA")
 
248
        if old is not None:
 
249
            del os.environ["LOCALAPPDATA"]
 
250
        try:
 
251
            self.assertEquals(first,
 
252
                              bzrlib.win32utils.get_local_appdata_location())
 
253
        finally:
 
254
            if old is not None:
 
255
                os.environ["LOCALAPPDATA"] = old
 
256
 
 
257
    def test_local_appdata_matches_environment(self):
 
258
        # LOCALAPPDATA typically only exists on Vista.
 
259
        lad = bzrlib.win32utils.get_local_appdata_location()
 
260
        env = os.environ.get("LOCALAPPDATA")
 
261
        if env:
 
262
            self.assertEquals(lad, env)