~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Aaron Bentley
  • Date: 2007-08-16 00:54:00 UTC
  • mto: This revision was merged to the branch mainline in revision 2711.
  • Revision ID: aaron.bentley@utoronto.ca-20070816005400-oxxtqiy310wx10h9
Fix typo

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import os
18
17
import sys
19
18
 
20
19
from bzrlib import osutils
21
 
from bzrlib.tests import TestCase, TestCaseInTempDir, TestSkipped, Feature
22
 
from bzrlib.win32utils import glob_expand, get_app_path
23
 
from bzrlib import win32utils
 
20
from bzrlib.tests import TestCase, TestCaseInTempDir, Feature
 
21
from bzrlib.win32utils import glob_expand
24
22
 
25
23
 
26
24
# Features
37
35
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
38
36
 
39
37
 
40
 
class _RequiredModuleFeature(Feature):
41
 
 
42
 
    def __init__(self, mod_name):
43
 
        self.mod_name = mod_name
44
 
        super(_RequiredModuleFeature, self).__init__()
45
 
 
46
 
    def _probe(self):
47
 
        try:
48
 
            __import__(self.mod_name)
49
 
            return True
50
 
        except ImportError:
51
 
            return False
52
 
 
53
 
    def feature_name(self):
54
 
        return self.mod_name
55
 
 
56
 
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
57
 
CtypesFeature = _RequiredModuleFeature('ctypes')
58
 
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
59
 
 
60
 
 
61
38
# Tests
62
39
# -----
63
40
 
64
41
class TestNeedsGlobExpansionFeature(TestCase):
65
 
 
 
42
    
66
43
    def test_available(self):
67
 
        self.assertEqual(sys.platform == 'win32',
 
44
        self.assertEqual(sys.platform == 'win32', 
68
45
                         NeedsGlobExpansionFeature.available())
69
 
 
 
46
        
70
47
    def test_str(self):
71
48
        self.assertTrue("performed" in str(NeedsGlobExpansionFeature))
72
49
 
74
51
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
75
52
 
76
53
    _test_needs_features = [NeedsGlobExpansionFeature]
77
 
 
 
54
   
78
55
    def test_empty_tree(self):
79
56
        self.build_tree([])
80
57
        self._run_testset([
82
59
            [['?'], ['?']],
83
60
            [['*'], ['*']],
84
61
            [['a', 'a'], ['a', 'a']]])
85
 
 
 
62
        
86
63
    def test_tree_ascii(self):
87
64
        """Checks the glob expansion and path separation char
88
65
        normalization"""
89
66
        self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
90
67
                         'b', 'b1', 'b2', 'b3',
91
 
                         'c/', 'c/c1', 'c/c2',
 
68
                         'c/', 'c/c1', 'c/c2', 
92
69
                         'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
93
70
        self._run_testset([
94
71
            # no wildcards
95
72
            [[u'a'], [u'a']],
96
73
            [[u'a', u'a' ], [u'a', u'a']],
97
74
            [[u'A'], [u'A']],
98
 
 
 
75
                
99
76
            [[u'd'], [u'd']],
100
77
            [[u'd/'], [u'd/']],
101
78
            [[u'd\\'], [u'd/']],
102
 
 
 
79
            
103
80
            # wildcards
104
81
            [[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
105
82
            [[u'?'], [u'a', u'b', u'c', u'd']],
107
84
            [[u'a??'], [u'a11', u'a.1']],
108
85
            [[u'b[1-2]'], [u'b1', u'b2']],
109
86
            [[u'A?'], [u'a1', u'a2']],
110
 
 
 
87
               
111
88
            [[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
112
89
            [[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
113
90
            [[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
114
91
            [[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
115
92
            [[u'*/'], [u'c/', u'd/']],
116
93
            [[u'*\\'], [u'c/', u'd/']]])
117
 
 
 
94
        
118
95
    def test_tree_unicode(self):
119
96
        """Checks behaviour with non-ascii filenames"""
120
97
        self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
122
99
            # no wildcards
123
100
            [[u'\u1234'], [u'\u1234']],
124
101
            [[u'\u1235'], [u'\u1235']],
125
 
 
 
102
         
126
103
            [[u'\u1235/'], [u'\u1235/']],
127
104
            [[u'\u1235/\u1235'], [u'\u1235/\u1235']],
128
 
 
 
105
            
129
106
            # wildcards
130
107
            [[u'?'], [u'\u1234', u'\u1235']],
131
108
            [[u'*'], [u'\u1234', u'\u1234\u1234', u'\u1235']],
132
109
            [[u'\u1234*'], [u'\u1234', u'\u1234\u1234']],
133
 
 
 
110
            
134
111
            [[u'\u1235/?'], [u'\u1235/\u1235']],
135
112
            [[u'\u1235/*'], [u'\u1235/\u1235']],
136
113
            [[u'\u1235\\?'], [u'\u1235/\u1235']],
151
128
            result.sort()
152
129
            self.assertEqual(expected, result, 'pattern %s' % pattern)
153
130
 
154
 
 
155
 
class TestAppPaths(TestCase):
156
 
 
157
 
    _test_needs_features = [Win32RegistryFeature]
158
 
 
159
 
    def test_iexplore(self):
160
 
        # typical windows users should have IE installed
161
 
        for a in ('iexplore', 'iexplore.exe'):
162
 
            p = get_app_path(a)
163
 
            d, b = os.path.split(p)
164
 
            self.assertEquals('iexplore.exe', b.lower())
165
 
            self.assertNotEquals('', d)
166
 
 
167
 
    def test_not_existing(self):
168
 
        p = get_app_path('not-existing')
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