~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Martin Pool
  • Date: 2009-11-16 02:26:32 UTC
  • mto: This revision was merged to the branch mainline in revision 4880.
  • Revision ID: mbp@sourcefrog.net-20091116022632-261trs2osy8oupe3
Convert version-info to use TextUIOutputStream

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
from bzrlib.win32utils import glob_expand, get_app_path
33
33
 
34
34
 
35
 
class _BackslashDirSeparatorFeature(tests.Feature):
 
35
# Features
 
36
# --------
 
37
 
 
38
class _NeedsGlobExpansionFeature(Feature):
36
39
 
37
40
    def _probe(self):
38
 
        try:
39
 
            os.lstat(os.getcwd() + '\\')
40
 
        except OSError:
41
 
            return False
42
 
        else:
43
 
            return True
 
41
        return sys.platform == 'win32'
44
42
 
45
43
    def feature_name(self):
46
 
        return "Filesystem treats '\\' as a directory separator."
 
44
        return 'Internally performed glob expansion'
47
45
 
48
 
BackslashDirSeparatorFeature = _BackslashDirSeparatorFeature()
 
46
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
49
47
 
50
48
 
51
49
class _RequiredModuleFeature(Feature):
72
70
# Tests
73
71
# -----
74
72
 
 
73
class TestNeedsGlobExpansionFeature(TestCase):
 
74
 
 
75
    def test_available(self):
 
76
        self.assertEqual(sys.platform == 'win32',
 
77
                         NeedsGlobExpansionFeature.available())
 
78
 
 
79
    def test_str(self):
 
80
        self.assertTrue("performed" in str(NeedsGlobExpansionFeature))
 
81
 
 
82
 
75
83
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
76
84
 
77
 
    _test_needs_features = []
 
85
    _test_needs_features = [NeedsGlobExpansionFeature]
78
86
 
79
87
    def test_empty_tree(self):
80
88
        self.build_tree([])
84
92
            [['*'], ['*']],
85
93
            [['a', 'a'], ['a', 'a']]])
86
94
 
87
 
    def build_ascii_tree(self):
 
95
    def test_tree_ascii(self):
 
96
        """Checks the glob expansion and path separation char
 
97
        normalization"""
88
98
        self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
89
99
                         'b', 'b1', 'b2', 'b3',
90
100
                         'c/', 'c/c1', 'c/c2',
91
101
                         'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
92
 
 
93
 
    def build_unicode_tree(self):
94
 
        self.requireFeature(UnicodeFilenameFeature)
95
 
        self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/',
96
 
                         u'\u1235/\u1235'])
97
 
 
98
 
    def test_tree_ascii(self):
99
 
        """Checks the glob expansion and path separation char
100
 
        normalization"""
101
 
        self.build_ascii_tree()
102
102
        self._run_testset([
103
103
            # no wildcards
104
104
            [[u'a'], [u'a']],
105
105
            [[u'a', u'a' ], [u'a', u'a']],
 
106
            [[u'A'], [u'A']],
106
107
 
107
108
            [[u'd'], [u'd']],
108
109
            [[u'd/'], [u'd/']],
 
110
            [[u'd\\'], [u'd/']],
109
111
 
110
112
            # wildcards
111
113
            [[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
113
115
            [[u'a?'], [u'a1', u'a2']],
114
116
            [[u'a??'], [u'a11', u'a.1']],
115
117
            [[u'b[1-2]'], [u'b1', u'b2']],
 
118
            [[u'A?'], [u'a1', u'a2']],
116
119
 
117
120
            [[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
118
 
            [[u'?/*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
119
 
            [[u'*/*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
120
 
            [[u'*/'], [u'c/', u'd/']],
121
 
            ])
122
 
 
123
 
    def test_backslash_globbing(self):
124
 
        self.requireFeature(BackslashDirSeparatorFeature)
125
 
        self.build_ascii_tree()
126
 
        self._run_testset([
127
 
            [[u'd\\'], [u'd/']],
128
121
            [[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
129
122
            [[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
130
123
            [[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
131
 
            [[u'*\\'], [u'c/', u'd/']],
132
 
            ])
133
 
 
134
 
    def test_case_insensitive_globbing(self):
135
 
        self.requireFeature(tests.CaseInsCasePresFilenameFeature)
136
 
        self.build_ascii_tree()
137
 
        self._run_testset([
138
 
            [[u'A'], [u'A']],
139
 
            [[u'A?'], [u'a1', u'a2']],
140
 
            ])
 
124
            [[u'*/'], [u'c/', u'd/']],
 
125
            [[u'*\\'], [u'c/', u'd/']]])
141
126
 
142
127
    def test_tree_unicode(self):
143
128
        """Checks behaviour with non-ascii filenames"""
144
 
        self.build_unicode_tree()
 
129
        self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
145
130
        self._run_testset([
146
131
            # no wildcards
147
132
            [[u'\u1234'], [u'\u1234']],
157
142
 
158
143
            [[u'\u1235/?'], [u'\u1235/\u1235']],
159
144
            [[u'\u1235/*'], [u'\u1235/\u1235']],
 
145
            [[u'\u1235\\?'], [u'\u1235/\u1235']],
 
146
            [[u'\u1235\\*'], [u'\u1235/\u1235']],
160
147
            [[u'?/'], [u'\u1235/']],
161
148
            [[u'*/'], [u'\u1235/']],
 
149
            [[u'?\\'], [u'\u1235/']],
 
150
            [[u'*\\'], [u'\u1235/']],
162
151
            [[u'?/?'], [u'\u1235/\u1235']],
163
152
            [[u'*/*'], [u'\u1235/\u1235']],
164
 
            ])
165
 
 
166
 
    def test_unicode_backslashes(self):
167
 
        self.requireFeature(BackslashDirSeparatorFeature)
168
 
        self.build_unicode_tree()
169
 
        self._run_testset([
170
 
            # no wildcards
171
 
            [[u'\u1235\\'], [u'\u1235/']],
172
 
            [[u'\u1235\\\u1235'], [u'\u1235/\u1235']],
173
 
            [[u'\u1235\\?'], [u'\u1235/\u1235']],
174
 
            [[u'\u1235\\*'], [u'\u1235/\u1235']],
175
 
            [[u'?\\'], [u'\u1235/']],
176
 
            [[u'*\\'], [u'\u1235/']],
177
153
            [[u'?\\?'], [u'\u1235/\u1235']],
178
 
            [[u'*\\*'], [u'\u1235/\u1235']],
179
 
            ])
 
154
            [[u'*\\*'], [u'\u1235/\u1235']]])
180
155
 
181
156
    def _run_testset(self, testset):
182
157
        for pattern, expected in testset:
266
241
        super(TestLocationsPywin32, self).setUp()
267
242
        # We perform the exact same tests after disabling the use of ctypes.
268
243
        # This causes the implementation to fall back to pywin32.
269
 
        self.overrideAttr(win32utils, 'has_ctypes', False)
270
 
        # FIXME: this should be done by parametrization -- vila 100123
 
244
        self.old_ctypes = win32utils.has_ctypes
 
245
        win32utils.has_ctypes = False
 
246
        self.addCleanup(self.restoreCtypes)
 
247
 
 
248
    def restoreCtypes(self):
 
249
        win32utils.has_ctypes = self.old_ctypes
271
250
 
272
251
 
273
252
class TestSetHidden(TestCaseInTempDir):
348
327
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
349
328
 
350
329
    def assertCommandLine(self, expected, line):
351
 
        # Strictly speaking we should respect parameter order versus glob
352
 
        # expansions, but it's not really worth the effort here
353
 
        self.assertEqual(expected,
354
 
                         sorted(win32utils._command_line_to_argv(line)))
 
330
        self.assertEqual(expected, win32utils._command_line_to_argv(line))
355
331
 
356
332
    def test_glob_paths(self):
357
333
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
369
345
        self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
370
346
 
371
347
    def test_slashes_changed(self):
372
 
        # Quoting doesn't change the supplied args
373
 
        self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
374
 
        # Expands the glob, but nothing matches, swaps slashes
 
348
        self.assertCommandLine([u'a/*.c'], '"a\\*.c"')
 
349
        # Expands the glob, but nothing matches
375
350
        self.assertCommandLine([u'a/*.c'], 'a\\*.c')
376
 
        self.assertCommandLine([u'a/?.c'], 'a\\?.c')
377
 
        # No glob, doesn't touch slashes
378
 
        self.assertCommandLine([u'a\\foo.c'], 'a\\foo.c')
 
351
        self.assertCommandLine([u'a/foo.c'], 'a\\foo.c')
379
352
 
380
353
    def test_no_single_quote_supported(self):
381
354
        self.assertCommandLine(["add", "let's-do-it.txt"],
382
355
            "add let's-do-it.txt")
383
 
 
384
 
    def test_case_insensitive_globs(self):
385
 
        self.requireFeature(tests.CaseInsCasePresFilenameFeature)
386
 
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
387
 
        self.assertCommandLine([u'A/b.c'], 'A/B*')
388
 
 
389
 
    def test_backslashes(self):
390
 
        self.requireFeature(BackslashDirSeparatorFeature)
391
 
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
392
 
        self.assertCommandLine([u'a/b.c'], 'a\\b*')