/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
"""Tests for win32utils."""
 
18
 
17
19
import os
18
20
import sys
19
21
 
29
31
    TestSkipped,
30
32
    UnicodeFilenameFeature,
31
33
    )
 
34
from bzrlib.tests.features import backslashdir_feature
32
35
from bzrlib.win32utils import glob_expand, get_app_path
33
36
 
34
37
 
35
 
class _BackslashDirSeparatorFeature(tests.Feature):
36
 
 
37
 
    def _probe(self):
38
 
        try:
39
 
            os.lstat(os.getcwd() + '\\')
40
 
        except OSError:
41
 
            return False
42
 
        else:
43
 
            return True
44
 
 
45
 
    def feature_name(self):
46
 
        return "Filesystem treats '\\' as a directory separator."
47
 
 
48
 
BackslashDirSeparatorFeature = _BackslashDirSeparatorFeature()
49
 
 
50
 
 
51
38
class _RequiredModuleFeature(Feature):
52
39
 
53
40
    def __init__(self, mod_name):
67
54
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
68
55
CtypesFeature = _RequiredModuleFeature('ctypes')
69
56
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
 
57
Win32ApiFeature = _RequiredModuleFeature('win32api') 
70
58
 
71
59
 
72
60
# Tests
121
109
            ])
122
110
 
123
111
    def test_backslash_globbing(self):
124
 
        self.requireFeature(BackslashDirSeparatorFeature)
 
112
        self.requireFeature(backslashdir_feature)
125
113
        self.build_ascii_tree()
126
114
        self._run_testset([
127
115
            [[u'd\\'], [u'd/']],
164
152
            ])
165
153
 
166
154
    def test_unicode_backslashes(self):
167
 
        self.requireFeature(BackslashDirSeparatorFeature)
 
155
        self.requireFeature(backslashdir_feature)
168
156
        self.build_unicode_tree()
169
157
        self._run_testset([
170
158
            # no wildcards
202
190
        # typical windows users should have wordpad in the system
203
191
        # but there is problem: its path has the format REG_EXPAND_SZ
204
192
        # so naive attempt to get the path is not working
 
193
        self.requireFeature(Win32ApiFeature)
205
194
        for a in ('wordpad', 'wordpad.exe'):
206
195
            p = get_app_path(a)
207
196
            d, b = os.path.split(p)
287
276
        win32utils.set_file_attr_hidden(path)
288
277
 
289
278
 
290
 
 
291
 
 
292
279
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
293
280
 
294
 
    def assertCommandLine(self, expected, line, single_quotes_allowed=False):
 
281
    def assertCommandLine(self, expected, line, argv=None,
 
282
            single_quotes_allowed=False):
295
283
        # Strictly speaking we should respect parameter order versus glob
296
284
        # expansions, but it's not really worth the effort here
297
 
        argv = win32utils._command_line_to_argv(line,
 
285
        if argv is None:
 
286
            argv = [line]
 
287
        argv = win32utils._command_line_to_argv(line, argv,
298
288
                single_quotes_allowed=single_quotes_allowed)
299
289
        self.assertEqual(expected, sorted(argv))
300
290
 
328
318
 
329
319
    def test_single_quote_support(self):
330
320
        self.assertCommandLine(["add", "let's-do-it.txt"],
331
 
            "add let's-do-it.txt")
332
 
        self.assertCommandLine(["add", "lets do it.txt"],
333
 
            "add 'lets do it.txt'", single_quotes_allowed=True)
 
321
            "add let's-do-it.txt",
 
322
            ["add", "let's-do-it.txt"])
 
323
        self.expectFailure("Using single quotes breaks trimming from argv",
 
324
            self.assertCommandLine, ["add", "lets do it.txt"],
 
325
            "add 'lets do it.txt'", ["add", "'lets", "do", "it.txt'"],
 
326
            single_quotes_allowed=True)
334
327
 
335
328
    def test_case_insensitive_globs(self):
336
329
        self.requireFeature(tests.CaseInsCasePresFilenameFeature)
338
331
        self.assertCommandLine([u'A/b.c'], 'A/B*')
339
332
 
340
333
    def test_backslashes(self):
341
 
        self.requireFeature(BackslashDirSeparatorFeature)
 
334
        self.requireFeature(backslashdir_feature)
342
335
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
343
336
        self.assertCommandLine([u'a/b.c'], 'a\\b*')
 
337
 
 
338
    def test_with_pdb(self):
 
339
        """Check stripping Python arguments before bzr script per lp:587868"""
 
340
        self.assertCommandLine([u"rocks"], "-m pdb rocks", ["rocks"])
 
341
        self.build_tree(['d/', 'd/f1', 'd/f2'])
 
342
        self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
 
343
        self.assertCommandLine([u"add", u"d/f1", u"d/f2"], "-m pdb add d/*",
 
344
            ["add", u"d/*"])