/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_osutils.py

  • Committer: Vincent Ladeuil
  • Date: 2009-11-03 09:32:17 UTC
  • mto: (4784.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4785.
  • Revision ID: v.ladeuil+lp@free.fr-20091103093217-sodpsi6fzb36vk9y
Hooks daughter classes should always call the base constructor

* bzrlib/tests/test_hooks.py:
(TestHookRegistry.test_items_are_reasonable_keys): Reproduce bug
#389648 and ensures we won't regress again.

* bzrlib/version_info_formats/format_rio.py:
(RioVersionInfoBuilderHooks.__init__): Call base constructor.

* bzrlib/info.py:
(InfoHooks.__init__): Call base constructor.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Tests for the osutils wrapper."""
 
18
 
 
19
from cStringIO import StringIO
 
20
import errno
 
21
import os
 
22
import re
 
23
import socket
 
24
import stat
 
25
import sys
 
26
import time
 
27
 
 
28
from bzrlib import (
 
29
    errors,
 
30
    osutils,
 
31
    tests,
 
32
    trace,
 
33
    win32utils,
 
34
    )
 
35
from bzrlib.tests import (
 
36
    file_utils,
 
37
    test__walkdirs_win32,
 
38
    )
 
39
 
 
40
 
 
41
class _UTF8DirReaderFeature(tests.Feature):
 
42
 
 
43
    def _probe(self):
 
44
        try:
 
45
            from bzrlib import _readdir_pyx
 
46
            self.reader = _readdir_pyx.UTF8DirReader
 
47
            return True
 
48
        except ImportError:
 
49
            return False
 
50
 
 
51
    def feature_name(self):
 
52
        return 'bzrlib._readdir_pyx'
 
53
 
 
54
UTF8DirReaderFeature = _UTF8DirReaderFeature()
 
55
 
 
56
 
 
57
def _already_unicode(s):
 
58
    return s
 
59
 
 
60
 
 
61
def _fs_enc_to_unicode(s):
 
62
    return s.decode(osutils._fs_enc)
 
63
 
 
64
 
 
65
def _utf8_to_unicode(s):
 
66
    return s.decode('UTF-8')
 
67
 
 
68
 
 
69
def dir_reader_scenarios():
 
70
    # For each dir reader we define:
 
71
 
 
72
    # - native_to_unicode: a function converting the native_abspath as returned
 
73
    #   by DirReader.read_dir to its unicode representation
 
74
 
 
75
    # UnicodeDirReader is the fallback, it should be tested on all platforms.
 
76
    scenarios = [('unicode',
 
77
                  dict(_dir_reader_class=osutils.UnicodeDirReader,
 
78
                       _native_to_unicode=_already_unicode))]
 
79
    # Some DirReaders are platform specific and even there they may not be
 
80
    # available.
 
81
    if UTF8DirReaderFeature.available():
 
82
        from bzrlib import _readdir_pyx
 
83
        scenarios.append(('utf8',
 
84
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
 
85
                               _native_to_unicode=_utf8_to_unicode)))
 
86
 
 
87
    if test__walkdirs_win32.Win32ReadDirFeature.available():
 
88
        try:
 
89
            from bzrlib import _walkdirs_win32
 
90
            # TODO: check on windows, it may be that we need to use/add
 
91
            # safe_unicode instead of _fs_enc_to_unicode
 
92
            scenarios.append(
 
93
                ('win32',
 
94
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
 
95
                      _native_to_unicode=_fs_enc_to_unicode)))
 
96
        except ImportError:
 
97
            pass
 
98
    return scenarios
 
99
 
 
100
 
 
101
def load_tests(basic_tests, module, loader):
 
102
    suite = loader.suiteClass()
 
103
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
 
104
        basic_tests, tests.condition_isinstance(TestDirReader))
 
105
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
 
106
    suite.addTest(remaining_tests)
 
107
    return suite
 
108
 
 
109
 
 
110
class TestContainsWhitespace(tests.TestCase):
 
111
 
 
112
    def test_contains_whitespace(self):
 
113
        self.failUnless(osutils.contains_whitespace(u' '))
 
114
        self.failUnless(osutils.contains_whitespace(u'hello there'))
 
115
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
 
116
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
 
117
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
 
118
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
119
 
 
120
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
 
121
        # is whitespace, but we do not.
 
122
        self.failIf(osutils.contains_whitespace(u''))
 
123
        self.failIf(osutils.contains_whitespace(u'hellothere'))
 
124
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
125
 
 
126
 
 
127
class TestRename(tests.TestCaseInTempDir):
 
128
 
 
129
    def test_fancy_rename(self):
 
130
        # This should work everywhere
 
131
        def rename(a, b):
 
132
            osutils.fancy_rename(a, b,
 
133
                    rename_func=os.rename,
 
134
                    unlink_func=os.unlink)
 
135
 
 
136
        open('a', 'wb').write('something in a\n')
 
137
        rename('a', 'b')
 
138
        self.failIfExists('a')
 
139
        self.failUnlessExists('b')
 
140
        self.check_file_contents('b', 'something in a\n')
 
141
 
 
142
        open('a', 'wb').write('new something in a\n')
 
143
        rename('b', 'a')
 
144
 
 
145
        self.check_file_contents('a', 'something in a\n')
 
146
 
 
147
    def test_rename(self):
 
148
        # Rename should be semi-atomic on all platforms
 
149
        open('a', 'wb').write('something in a\n')
 
150
        osutils.rename('a', 'b')
 
151
        self.failIfExists('a')
 
152
        self.failUnlessExists('b')
 
153
        self.check_file_contents('b', 'something in a\n')
 
154
 
 
155
        open('a', 'wb').write('new something in a\n')
 
156
        osutils.rename('b', 'a')
 
157
 
 
158
        self.check_file_contents('a', 'something in a\n')
 
159
 
 
160
    # TODO: test fancy_rename using a MemoryTransport
 
161
 
 
162
    def test_rename_change_case(self):
 
163
        # on Windows we should be able to change filename case by rename
 
164
        self.build_tree(['a', 'b/'])
 
165
        osutils.rename('a', 'A')
 
166
        osutils.rename('b', 'B')
 
167
        # we can't use failUnlessExists on case-insensitive filesystem
 
168
        # so try to check shape of the tree
 
169
        shape = sorted(os.listdir('.'))
 
170
        self.assertEquals(['A', 'B'], shape)
 
171
 
 
172
 
 
173
class TestRandChars(tests.TestCase):
 
174
 
 
175
    def test_01_rand_chars_empty(self):
 
176
        result = osutils.rand_chars(0)
 
177
        self.assertEqual(result, '')
 
178
 
 
179
    def test_02_rand_chars_100(self):
 
180
        result = osutils.rand_chars(100)
 
181
        self.assertEqual(len(result), 100)
 
182
        self.assertEqual(type(result), str)
 
183
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
 
184
 
 
185
 
 
186
class TestIsInside(tests.TestCase):
 
187
 
 
188
    def test_is_inside(self):
 
189
        is_inside = osutils.is_inside
 
190
        self.assertTrue(is_inside('src', 'src/foo.c'))
 
191
        self.assertFalse(is_inside('src', 'srccontrol'))
 
192
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
 
193
        self.assertTrue(is_inside('foo.c', 'foo.c'))
 
194
        self.assertFalse(is_inside('foo.c', ''))
 
195
        self.assertTrue(is_inside('', 'foo.c'))
 
196
 
 
197
    def test_is_inside_any(self):
 
198
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
 
199
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
 
200
                         (['src'], SRC_FOO_C),
 
201
                         (['src'], 'src'),
 
202
                         ]:
 
203
            self.assert_(osutils.is_inside_any(dirs, fn))
 
204
        for dirs, fn in [(['src'], 'srccontrol'),
 
205
                         (['src'], 'srccontrol/foo')]:
 
206
            self.assertFalse(osutils.is_inside_any(dirs, fn))
 
207
 
 
208
    def test_is_inside_or_parent_of_any(self):
 
209
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
 
210
                         (['src'], 'src/foo.c'),
 
211
                         (['src/bar.c'], 'src'),
 
212
                         (['src/bar.c', 'bla/foo.c'], 'src'),
 
213
                         (['src'], 'src'),
 
214
                         ]:
 
215
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
 
216
 
 
217
        for dirs, fn in [(['src'], 'srccontrol'),
 
218
                         (['srccontrol/foo.c'], 'src'),
 
219
                         (['src'], 'srccontrol/foo')]:
 
220
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
 
221
 
 
222
 
 
223
class TestRmTree(tests.TestCaseInTempDir):
 
224
 
 
225
    def test_rmtree(self):
 
226
        # Check to remove tree with read-only files/dirs
 
227
        os.mkdir('dir')
 
228
        f = file('dir/file', 'w')
 
229
        f.write('spam')
 
230
        f.close()
 
231
        # would like to also try making the directory readonly, but at the
 
232
        # moment python shutil.rmtree doesn't handle that properly - it would
 
233
        # need to chmod the directory before removing things inside it - deferred
 
234
        # for now -- mbp 20060505
 
235
        # osutils.make_readonly('dir')
 
236
        osutils.make_readonly('dir/file')
 
237
 
 
238
        osutils.rmtree('dir')
 
239
 
 
240
        self.failIfExists('dir/file')
 
241
        self.failIfExists('dir')
 
242
 
 
243
 
 
244
class TestDeleteAny(tests.TestCaseInTempDir):
 
245
 
 
246
    def test_delete_any_readonly(self):
 
247
        # from <https://bugs.launchpad.net/bzr/+bug/218206>
 
248
        self.build_tree(['d/', 'f'])
 
249
        osutils.make_readonly('d')
 
250
        osutils.make_readonly('f')
 
251
 
 
252
        osutils.delete_any('f')
 
253
        osutils.delete_any('d')
 
254
 
 
255
 
 
256
class TestKind(tests.TestCaseInTempDir):
 
257
 
 
258
    def test_file_kind(self):
 
259
        self.build_tree(['file', 'dir/'])
 
260
        self.assertEquals('file', osutils.file_kind('file'))
 
261
        self.assertEquals('directory', osutils.file_kind('dir/'))
 
262
        if osutils.has_symlinks():
 
263
            os.symlink('symlink', 'symlink')
 
264
            self.assertEquals('symlink', osutils.file_kind('symlink'))
 
265
 
 
266
        # TODO: jam 20060529 Test a block device
 
267
        try:
 
268
            os.lstat('/dev/null')
 
269
        except OSError, e:
 
270
            if e.errno not in (errno.ENOENT,):
 
271
                raise
 
272
        else:
 
273
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
 
274
 
 
275
        mkfifo = getattr(os, 'mkfifo', None)
 
276
        if mkfifo:
 
277
            mkfifo('fifo')
 
278
            try:
 
279
                self.assertEquals('fifo', osutils.file_kind('fifo'))
 
280
            finally:
 
281
                os.remove('fifo')
 
282
 
 
283
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
 
284
        if AF_UNIX:
 
285
            s = socket.socket(AF_UNIX)
 
286
            s.bind('socket')
 
287
            try:
 
288
                self.assertEquals('socket', osutils.file_kind('socket'))
 
289
            finally:
 
290
                os.remove('socket')
 
291
 
 
292
    def test_kind_marker(self):
 
293
        self.assertEqual("", osutils.kind_marker("file"))
 
294
        self.assertEqual("/", osutils.kind_marker('directory'))
 
295
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
 
296
        self.assertEqual("@", osutils.kind_marker("symlink"))
 
297
        self.assertEqual("+", osutils.kind_marker("tree-reference"))
 
298
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
299
 
 
300
 
 
301
class TestUmask(tests.TestCaseInTempDir):
 
302
 
 
303
    def test_get_umask(self):
 
304
        if sys.platform == 'win32':
 
305
            # umask always returns '0', no way to set it
 
306
            self.assertEqual(0, osutils.get_umask())
 
307
            return
 
308
 
 
309
        orig_umask = osutils.get_umask()
 
310
        self.addCleanup(os.umask, orig_umask)
 
311
        os.umask(0222)
 
312
        self.assertEqual(0222, osutils.get_umask())
 
313
        os.umask(0022)
 
314
        self.assertEqual(0022, osutils.get_umask())
 
315
        os.umask(0002)
 
316
        self.assertEqual(0002, osutils.get_umask())
 
317
        os.umask(0027)
 
318
        self.assertEqual(0027, osutils.get_umask())
 
319
 
 
320
 
 
321
class TestDateTime(tests.TestCase):
 
322
 
 
323
    def assertFormatedDelta(self, expected, seconds):
 
324
        """Assert osutils.format_delta formats as expected"""
 
325
        actual = osutils.format_delta(seconds)
 
326
        self.assertEqual(expected, actual)
 
327
 
 
328
    def test_format_delta(self):
 
329
        self.assertFormatedDelta('0 seconds ago', 0)
 
330
        self.assertFormatedDelta('1 second ago', 1)
 
331
        self.assertFormatedDelta('10 seconds ago', 10)
 
332
        self.assertFormatedDelta('59 seconds ago', 59)
 
333
        self.assertFormatedDelta('89 seconds ago', 89)
 
334
        self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
 
335
        self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
 
336
        self.assertFormatedDelta('3 minutes, 1 second ago', 181)
 
337
        self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
 
338
        self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
 
339
        self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
 
340
        self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
 
341
        self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
 
342
        self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
 
343
        self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
 
344
        self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
 
345
        self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
 
346
        self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
 
347
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
 
348
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
 
349
        self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
 
350
        self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
 
351
        self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
 
352
 
 
353
        # We handle when time steps the wrong direction because computers
 
354
        # don't have synchronized clocks.
 
355
        self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
 
356
        self.assertFormatedDelta('1 second in the future', -1)
 
357
        self.assertFormatedDelta('2 seconds in the future', -2)
 
358
 
 
359
    def test_format_date(self):
 
360
        self.assertRaises(errors.UnsupportedTimezoneFormat,
 
361
            osutils.format_date, 0, timezone='foo')
 
362
        self.assertIsInstance(osutils.format_date(0), str)
 
363
        self.assertIsInstance(osutils.format_local_date(0), unicode)
 
364
        # Testing for the actual value of the local weekday without
 
365
        # duplicating the code from format_date is difficult.
 
366
        # Instead blackbox.test_locale should check for localized
 
367
        # dates once they do occur in output strings.
 
368
 
 
369
    def test_local_time_offset(self):
 
370
        """Test that local_time_offset() returns a sane value."""
 
371
        offset = osutils.local_time_offset()
 
372
        self.assertTrue(isinstance(offset, int))
 
373
        # Test that the offset is no more than a eighteen hours in
 
374
        # either direction.
 
375
        # Time zone handling is system specific, so it is difficult to
 
376
        # do more specific tests, but a value outside of this range is
 
377
        # probably wrong.
 
378
        eighteen_hours = 18 * 3600
 
379
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
380
 
 
381
    def test_local_time_offset_with_timestamp(self):
 
382
        """Test that local_time_offset() works with a timestamp."""
 
383
        offset = osutils.local_time_offset(1000000000.1234567)
 
384
        self.assertTrue(isinstance(offset, int))
 
385
        eighteen_hours = 18 * 3600
 
386
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
387
 
 
388
 
 
389
class TestLinks(tests.TestCaseInTempDir):
 
390
 
 
391
    def test_dereference_path(self):
 
392
        self.requireFeature(tests.SymlinkFeature)
 
393
        cwd = osutils.realpath('.')
 
394
        os.mkdir('bar')
 
395
        bar_path = osutils.pathjoin(cwd, 'bar')
 
396
        # Using './' to avoid bug #1213894 (first path component not
 
397
        # dereferenced) in Python 2.4.1 and earlier
 
398
        self.assertEqual(bar_path, osutils.realpath('./bar'))
 
399
        os.symlink('bar', 'foo')
 
400
        self.assertEqual(bar_path, osutils.realpath('./foo'))
 
401
 
 
402
        # Does not dereference terminal symlinks
 
403
        foo_path = osutils.pathjoin(cwd, 'foo')
 
404
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
 
405
 
 
406
        # Dereferences parent symlinks
 
407
        os.mkdir('bar/baz')
 
408
        baz_path = osutils.pathjoin(bar_path, 'baz')
 
409
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
 
410
 
 
411
        # Dereferences parent symlinks that are the first path element
 
412
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
 
413
 
 
414
        # Dereferences parent symlinks in absolute paths
 
415
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
 
416
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
 
417
 
 
418
    def test_changing_access(self):
 
419
        f = file('file', 'w')
 
420
        f.write('monkey')
 
421
        f.close()
 
422
 
 
423
        # Make a file readonly
 
424
        osutils.make_readonly('file')
 
425
        mode = os.lstat('file').st_mode
 
426
        self.assertEqual(mode, mode & 0777555)
 
427
 
 
428
        # Make a file writable
 
429
        osutils.make_writable('file')
 
430
        mode = os.lstat('file').st_mode
 
431
        self.assertEqual(mode, mode | 0200)
 
432
 
 
433
        if osutils.has_symlinks():
 
434
            # should not error when handed a symlink
 
435
            os.symlink('nonexistent', 'dangling')
 
436
            osutils.make_readonly('dangling')
 
437
            osutils.make_writable('dangling')
 
438
 
 
439
    def test_host_os_dereferences_symlinks(self):
 
440
        osutils.host_os_dereferences_symlinks()
 
441
 
 
442
 
 
443
class TestCanonicalRelPath(tests.TestCaseInTempDir):
 
444
 
 
445
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
446
 
 
447
    def test_canonical_relpath_simple(self):
 
448
        f = file('MixedCaseName', 'w')
 
449
        f.close()
 
450
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
 
451
        self.failUnlessEqual('work/MixedCaseName', actual)
 
452
 
 
453
    def test_canonical_relpath_missing_tail(self):
 
454
        os.mkdir('MixedCaseParent')
 
455
        actual = osutils.canonical_relpath(self.test_base_dir,
 
456
                                           'mixedcaseparent/nochild')
 
457
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
 
458
 
 
459
 
 
460
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
 
461
 
 
462
    def assertRelpath(self, expected, base, path):
 
463
        actual = osutils._cicp_canonical_relpath(base, path)
 
464
        self.assertEqual(expected, actual)
 
465
 
 
466
    def test_simple(self):
 
467
        self.build_tree(['MixedCaseName'])
 
468
        base = osutils.realpath(self.get_transport('.').local_abspath('.'))
 
469
        self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
 
470
 
 
471
    def test_subdir_missing_tail(self):
 
472
        self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
 
473
        base = osutils.realpath(self.get_transport('.').local_abspath('.'))
 
474
        self.assertRelpath('MixedCaseParent/a_child', base,
 
475
                           'MixedCaseParent/a_child')
 
476
        self.assertRelpath('MixedCaseParent/a_child', base,
 
477
                           'MixedCaseParent/A_Child')
 
478
        self.assertRelpath('MixedCaseParent/not_child', base,
 
479
                           'MixedCaseParent/not_child')
 
480
 
 
481
    def test_at_root_slash(self):
 
482
        # We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
 
483
        # check...
 
484
        if osutils.MIN_ABS_PATHLENGTH > 1:
 
485
            raise tests.TestSkipped('relpath requires %d chars'
 
486
                                    % osutils.MIN_ABS_PATHLENGTH)
 
487
        self.assertRelpath('foo', '/', '/foo')
 
488
 
 
489
    def test_at_root_drive(self):
 
490
        if sys.platform != 'win32':
 
491
            raise tests.TestNotApplicable('we can only test drive-letter relative'
 
492
                                          ' paths on Windows where we have drive'
 
493
                                          ' letters.')
 
494
        # see bug #322807
 
495
        # The specific issue is that when at the root of a drive, 'abspath'
 
496
        # returns "C:/" or just "/". However, the code assumes that abspath
 
497
        # always returns something like "C:/foo" or "/foo" (no trailing slash).
 
498
        self.assertRelpath('foo', 'C:/', 'C:/foo')
 
499
        self.assertRelpath('foo', 'X:/', 'X:/foo')
 
500
        self.assertRelpath('foo', 'X:/', 'X://foo')
 
501
 
 
502
 
 
503
class TestPumpFile(tests.TestCase):
 
504
    """Test pumpfile method."""
 
505
 
 
506
    def setUp(self):
 
507
        tests.TestCase.setUp(self)
 
508
        # create a test datablock
 
509
        self.block_size = 512
 
510
        pattern = '0123456789ABCDEF'
 
511
        self.test_data = pattern * (3 * self.block_size / len(pattern))
 
512
        self.test_data_len = len(self.test_data)
 
513
 
 
514
    def test_bracket_block_size(self):
 
515
        """Read data in blocks with the requested read size bracketing the
 
516
        block size."""
 
517
        # make sure test data is larger than max read size
 
518
        self.assertTrue(self.test_data_len > self.block_size)
 
519
 
 
520
        from_file = file_utils.FakeReadFile(self.test_data)
 
521
        to_file = StringIO()
 
522
 
 
523
        # read (max / 2) bytes and verify read size wasn't affected
 
524
        num_bytes_to_read = self.block_size / 2
 
525
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
526
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
 
527
        self.assertEqual(from_file.get_read_count(), 1)
 
528
 
 
529
        # read (max) bytes and verify read size wasn't affected
 
530
        num_bytes_to_read = self.block_size
 
531
        from_file.reset_read_count()
 
532
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
533
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
 
534
        self.assertEqual(from_file.get_read_count(), 1)
 
535
 
 
536
        # read (max + 1) bytes and verify read size was limited
 
537
        num_bytes_to_read = self.block_size + 1
 
538
        from_file.reset_read_count()
 
539
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
540
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
541
        self.assertEqual(from_file.get_read_count(), 2)
 
542
 
 
543
        # finish reading the rest of the data
 
544
        num_bytes_to_read = self.test_data_len - to_file.tell()
 
545
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
546
 
 
547
        # report error if the data wasn't equal (we only report the size due
 
548
        # to the length of the data)
 
549
        response_data = to_file.getvalue()
 
550
        if response_data != self.test_data:
 
551
            message = "Data not equal.  Expected %d bytes, received %d."
 
552
            self.fail(message % (len(response_data), self.test_data_len))
 
553
 
 
554
    def test_specified_size(self):
 
555
        """Request a transfer larger than the maximum block size and verify
 
556
        that the maximum read doesn't exceed the block_size."""
 
557
        # make sure test data is larger than max read size
 
558
        self.assertTrue(self.test_data_len > self.block_size)
 
559
 
 
560
        # retrieve data in blocks
 
561
        from_file = file_utils.FakeReadFile(self.test_data)
 
562
        to_file = StringIO()
 
563
        osutils.pumpfile(from_file, to_file, self.test_data_len,
 
564
                         self.block_size)
 
565
 
 
566
        # verify read size was equal to the maximum read size
 
567
        self.assertTrue(from_file.get_max_read_size() > 0)
 
568
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
569
        self.assertEqual(from_file.get_read_count(), 3)
 
570
 
 
571
        # report error if the data wasn't equal (we only report the size due
 
572
        # to the length of the data)
 
573
        response_data = to_file.getvalue()
 
574
        if response_data != self.test_data:
 
575
            message = "Data not equal.  Expected %d bytes, received %d."
 
576
            self.fail(message % (len(response_data), self.test_data_len))
 
577
 
 
578
    def test_to_eof(self):
 
579
        """Read to end-of-file and verify that the reads are not larger than
 
580
        the maximum read size."""
 
581
        # make sure test data is larger than max read size
 
582
        self.assertTrue(self.test_data_len > self.block_size)
 
583
 
 
584
        # retrieve data to EOF
 
585
        from_file = file_utils.FakeReadFile(self.test_data)
 
586
        to_file = StringIO()
 
587
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
 
588
 
 
589
        # verify read size was equal to the maximum read size
 
590
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
 
591
        self.assertEqual(from_file.get_read_count(), 4)
 
592
 
 
593
        # report error if the data wasn't equal (we only report the size due
 
594
        # to the length of the data)
 
595
        response_data = to_file.getvalue()
 
596
        if response_data != self.test_data:
 
597
            message = "Data not equal.  Expected %d bytes, received %d."
 
598
            self.fail(message % (len(response_data), self.test_data_len))
 
599
 
 
600
    def test_defaults(self):
 
601
        """Verifies that the default arguments will read to EOF -- this
 
602
        test verifies that any existing usages of pumpfile will not be broken
 
603
        with this new version."""
 
604
        # retrieve data using default (old) pumpfile method
 
605
        from_file = file_utils.FakeReadFile(self.test_data)
 
606
        to_file = StringIO()
 
607
        osutils.pumpfile(from_file, to_file)
 
608
 
 
609
        # report error if the data wasn't equal (we only report the size due
 
610
        # to the length of the data)
 
611
        response_data = to_file.getvalue()
 
612
        if response_data != self.test_data:
 
613
            message = "Data not equal.  Expected %d bytes, received %d."
 
614
            self.fail(message % (len(response_data), self.test_data_len))
 
615
 
 
616
    def test_report_activity(self):
 
617
        activity = []
 
618
        def log_activity(length, direction):
 
619
            activity.append((length, direction))
 
620
        from_file = StringIO(self.test_data)
 
621
        to_file = StringIO()
 
622
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
623
                         report_activity=log_activity, direction='read')
 
624
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
 
625
                          (36, 'read')], activity)
 
626
 
 
627
        from_file = StringIO(self.test_data)
 
628
        to_file = StringIO()
 
629
        del activity[:]
 
630
        osutils.pumpfile(from_file, to_file, buff_size=500,
 
631
                         report_activity=log_activity, direction='write')
 
632
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
 
633
                          (36, 'write')], activity)
 
634
 
 
635
        # And with a limited amount of data
 
636
        from_file = StringIO(self.test_data)
 
637
        to_file = StringIO()
 
638
        del activity[:]
 
639
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
 
640
                         report_activity=log_activity, direction='read')
 
641
        self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
 
642
 
 
643
 
 
644
 
 
645
class TestPumpStringFile(tests.TestCase):
 
646
 
 
647
    def test_empty(self):
 
648
        output = StringIO()
 
649
        osutils.pump_string_file("", output)
 
650
        self.assertEqual("", output.getvalue())
 
651
 
 
652
    def test_more_than_segment_size(self):
 
653
        output = StringIO()
 
654
        osutils.pump_string_file("123456789", output, 2)
 
655
        self.assertEqual("123456789", output.getvalue())
 
656
 
 
657
    def test_segment_size(self):
 
658
        output = StringIO()
 
659
        osutils.pump_string_file("12", output, 2)
 
660
        self.assertEqual("12", output.getvalue())
 
661
 
 
662
    def test_segment_size_multiple(self):
 
663
        output = StringIO()
 
664
        osutils.pump_string_file("1234", output, 2)
 
665
        self.assertEqual("1234", output.getvalue())
 
666
 
 
667
 
 
668
class TestRelpath(tests.TestCase):
 
669
 
 
670
    def test_simple_relpath(self):
 
671
        cwd = osutils.getcwd()
 
672
        subdir = cwd + '/subdir'
 
673
        self.assertEqual('subdir', osutils.relpath(cwd, subdir))
 
674
 
 
675
    def test_deep_relpath(self):
 
676
        cwd = osutils.getcwd()
 
677
        subdir = cwd + '/sub/subsubdir'
 
678
        self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
 
679
 
 
680
    def test_not_relative(self):
 
681
        self.assertRaises(errors.PathNotChild,
 
682
                          osutils.relpath, 'C:/path', 'H:/path')
 
683
        self.assertRaises(errors.PathNotChild,
 
684
                          osutils.relpath, 'C:/', 'H:/path')
 
685
 
 
686
 
 
687
class TestSafeUnicode(tests.TestCase):
 
688
 
 
689
    def test_from_ascii_string(self):
 
690
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
 
691
 
 
692
    def test_from_unicode_string_ascii_contents(self):
 
693
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
 
694
 
 
695
    def test_from_unicode_string_unicode_contents(self):
 
696
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
 
697
 
 
698
    def test_from_utf8_string(self):
 
699
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
 
700
 
 
701
    def test_bad_utf8_string(self):
 
702
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
703
                          osutils.safe_unicode,
 
704
                          '\xbb\xbb')
 
705
 
 
706
 
 
707
class TestSafeUtf8(tests.TestCase):
 
708
 
 
709
    def test_from_ascii_string(self):
 
710
        f = 'foobar'
 
711
        self.assertEqual('foobar', osutils.safe_utf8(f))
 
712
 
 
713
    def test_from_unicode_string_ascii_contents(self):
 
714
        self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
 
715
 
 
716
    def test_from_unicode_string_unicode_contents(self):
 
717
        self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
 
718
 
 
719
    def test_from_utf8_string(self):
 
720
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
 
721
 
 
722
    def test_bad_utf8_string(self):
 
723
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
724
                          osutils.safe_utf8, '\xbb\xbb')
 
725
 
 
726
 
 
727
class TestSafeRevisionId(tests.TestCase):
 
728
 
 
729
    def test_from_ascii_string(self):
 
730
        # this shouldn't give a warning because it's getting an ascii string
 
731
        self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
 
732
 
 
733
    def test_from_unicode_string_ascii_contents(self):
 
734
        self.assertEqual('bargam',
 
735
                         osutils.safe_revision_id(u'bargam', warn=False))
 
736
 
 
737
    def test_from_unicode_deprecated(self):
 
738
        self.assertEqual('bargam',
 
739
            self.callDeprecated([osutils._revision_id_warning],
 
740
                                osutils.safe_revision_id, u'bargam'))
 
741
 
 
742
    def test_from_unicode_string_unicode_contents(self):
 
743
        self.assertEqual('bargam\xc2\xae',
 
744
                         osutils.safe_revision_id(u'bargam\xae', warn=False))
 
745
 
 
746
    def test_from_utf8_string(self):
 
747
        self.assertEqual('foo\xc2\xae',
 
748
                         osutils.safe_revision_id('foo\xc2\xae'))
 
749
 
 
750
    def test_none(self):
 
751
        """Currently, None is a valid revision_id"""
 
752
        self.assertEqual(None, osutils.safe_revision_id(None))
 
753
 
 
754
 
 
755
class TestSafeFileId(tests.TestCase):
 
756
 
 
757
    def test_from_ascii_string(self):
 
758
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
 
759
 
 
760
    def test_from_unicode_string_ascii_contents(self):
 
761
        self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
 
762
 
 
763
    def test_from_unicode_deprecated(self):
 
764
        self.assertEqual('bargam',
 
765
            self.callDeprecated([osutils._file_id_warning],
 
766
                                osutils.safe_file_id, u'bargam'))
 
767
 
 
768
    def test_from_unicode_string_unicode_contents(self):
 
769
        self.assertEqual('bargam\xc2\xae',
 
770
                         osutils.safe_file_id(u'bargam\xae', warn=False))
 
771
 
 
772
    def test_from_utf8_string(self):
 
773
        self.assertEqual('foo\xc2\xae',
 
774
                         osutils.safe_file_id('foo\xc2\xae'))
 
775
 
 
776
    def test_none(self):
 
777
        """Currently, None is a valid revision_id"""
 
778
        self.assertEqual(None, osutils.safe_file_id(None))
 
779
 
 
780
 
 
781
class TestWin32Funcs(tests.TestCase):
 
782
    """Test that _win32 versions of os utilities return appropriate paths."""
 
783
 
 
784
    def test_abspath(self):
 
785
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
 
786
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
 
787
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
 
788
        self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
 
789
 
 
790
    def test_realpath(self):
 
791
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
 
792
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
 
793
 
 
794
    def test_pathjoin(self):
 
795
        self.assertEqual('path/to/foo',
 
796
                         osutils._win32_pathjoin('path', 'to', 'foo'))
 
797
        self.assertEqual('C:/foo',
 
798
                         osutils._win32_pathjoin('path\\to', 'C:\\foo'))
 
799
        self.assertEqual('C:/foo',
 
800
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
 
801
        self.assertEqual('path/to/foo',
 
802
                         osutils._win32_pathjoin('path/to/', 'foo'))
 
803
        self.assertEqual('/foo',
 
804
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
 
805
        self.assertEqual('/foo',
 
806
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
 
807
 
 
808
    def test_normpath(self):
 
809
        self.assertEqual('path/to/foo',
 
810
                         osutils._win32_normpath(r'path\\from\..\to\.\foo'))
 
811
        self.assertEqual('path/to/foo',
 
812
                         osutils._win32_normpath('path//from/../to/./foo'))
 
813
 
 
814
    def test_getcwd(self):
 
815
        cwd = osutils._win32_getcwd()
 
816
        os_cwd = os.getcwdu()
 
817
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
 
818
        # win32 is inconsistent whether it returns lower or upper case
 
819
        # and even if it was consistent the user might type the other
 
820
        # so we force it to uppercase
 
821
        # running python.exe under cmd.exe return capital C:\\
 
822
        # running win32 python inside a cygwin shell returns lowercase
 
823
        self.assertEqual(os_cwd[0].upper(), cwd[0])
 
824
 
 
825
    def test_fixdrive(self):
 
826
        self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
 
827
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
 
828
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
 
829
 
 
830
    def test_win98_abspath(self):
 
831
        # absolute path
 
832
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
 
833
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
 
834
        # UNC path
 
835
        self.assertEqual('//HOST/path', osutils._win98_abspath(r'\\HOST\path'))
 
836
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
 
837
        # relative path
 
838
        cwd = osutils.getcwd().rstrip('/')
 
839
        drive = osutils._nt_splitdrive(cwd)[0]
 
840
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
 
841
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
 
842
        # unicode path
 
843
        u = u'\u1234'
 
844
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
 
845
 
 
846
 
 
847
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
 
848
    """Test win32 functions that create files."""
 
849
 
 
850
    def test_getcwd(self):
 
851
        self.requireFeature(tests.UnicodeFilenameFeature)
 
852
        os.mkdir(u'mu-\xb5')
 
853
        os.chdir(u'mu-\xb5')
 
854
        # TODO: jam 20060427 This will probably fail on Mac OSX because
 
855
        #       it will change the normalization of B\xe5gfors
 
856
        #       Consider using a different unicode character, or make
 
857
        #       osutils.getcwd() renormalize the path.
 
858
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
 
859
 
 
860
    def test_minimum_path_selection(self):
 
861
        self.assertEqual(set(),
 
862
            osutils.minimum_path_selection([]))
 
863
        self.assertEqual(set(['a']),
 
864
            osutils.minimum_path_selection(['a']))
 
865
        self.assertEqual(set(['a', 'b']),
 
866
            osutils.minimum_path_selection(['a', 'b']))
 
867
        self.assertEqual(set(['a/', 'b']),
 
868
            osutils.minimum_path_selection(['a/', 'b']))
 
869
        self.assertEqual(set(['a/', 'b']),
 
870
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
 
871
        self.assertEqual(set(['a-b', 'a', 'a0b']),
 
872
            osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
 
873
 
 
874
    def test_mkdtemp(self):
 
875
        tmpdir = osutils._win32_mkdtemp(dir='.')
 
876
        self.assertFalse('\\' in tmpdir)
 
877
 
 
878
    def test_rename(self):
 
879
        a = open('a', 'wb')
 
880
        a.write('foo\n')
 
881
        a.close()
 
882
        b = open('b', 'wb')
 
883
        b.write('baz\n')
 
884
        b.close()
 
885
 
 
886
        osutils._win32_rename('b', 'a')
 
887
        self.failUnlessExists('a')
 
888
        self.failIfExists('b')
 
889
        self.assertFileEqual('baz\n', 'a')
 
890
 
 
891
    def test_rename_missing_file(self):
 
892
        a = open('a', 'wb')
 
893
        a.write('foo\n')
 
894
        a.close()
 
895
 
 
896
        try:
 
897
            osutils._win32_rename('b', 'a')
 
898
        except (IOError, OSError), e:
 
899
            self.assertEqual(errno.ENOENT, e.errno)
 
900
        self.assertFileEqual('foo\n', 'a')
 
901
 
 
902
    def test_rename_missing_dir(self):
 
903
        os.mkdir('a')
 
904
        try:
 
905
            osutils._win32_rename('b', 'a')
 
906
        except (IOError, OSError), e:
 
907
            self.assertEqual(errno.ENOENT, e.errno)
 
908
 
 
909
    def test_rename_current_dir(self):
 
910
        os.mkdir('a')
 
911
        os.chdir('a')
 
912
        # You can't rename the working directory
 
913
        # doing rename non-existant . usually
 
914
        # just raises ENOENT, since non-existant
 
915
        # doesn't exist.
 
916
        try:
 
917
            osutils._win32_rename('b', '.')
 
918
        except (IOError, OSError), e:
 
919
            self.assertEqual(errno.ENOENT, e.errno)
 
920
 
 
921
    def test_splitpath(self):
 
922
        def check(expected, path):
 
923
            self.assertEqual(expected, osutils.splitpath(path))
 
924
 
 
925
        check(['a'], 'a')
 
926
        check(['a', 'b'], 'a/b')
 
927
        check(['a', 'b'], 'a/./b')
 
928
        check(['a', '.b'], 'a/.b')
 
929
        check(['a', '.b'], 'a\\.b')
 
930
 
 
931
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
 
932
 
 
933
 
 
934
class TestParentDirectories(tests.TestCaseInTempDir):
 
935
    """Test osutils.parent_directories()"""
 
936
 
 
937
    def test_parent_directories(self):
 
938
        self.assertEqual([], osutils.parent_directories('a'))
 
939
        self.assertEqual(['a'], osutils.parent_directories('a/b'))
 
940
        self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
 
941
 
 
942
 
 
943
class TestMacFuncsDirs(tests.TestCaseInTempDir):
 
944
    """Test mac special functions that require directories."""
 
945
 
 
946
    def test_getcwd(self):
 
947
        self.requireFeature(tests.UnicodeFilenameFeature)
 
948
        os.mkdir(u'B\xe5gfors')
 
949
        os.chdir(u'B\xe5gfors')
 
950
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
 
951
 
 
952
    def test_getcwd_nonnorm(self):
 
953
        self.requireFeature(tests.UnicodeFilenameFeature)
 
954
        # Test that _mac_getcwd() will normalize this path
 
955
        os.mkdir(u'Ba\u030agfors')
 
956
        os.chdir(u'Ba\u030agfors')
 
957
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
 
958
 
 
959
 
 
960
class TestChunksToLines(tests.TestCase):
 
961
 
 
962
    def test_smoketest(self):
 
963
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
 
964
                         osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
 
965
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
 
966
                         osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
 
967
 
 
968
    def test_osutils_binding(self):
 
969
        from bzrlib.tests import test__chunks_to_lines
 
970
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
 
971
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
 
972
        else:
 
973
            from bzrlib._chunks_to_lines_py import chunks_to_lines
 
974
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
 
975
 
 
976
 
 
977
class TestSplitLines(tests.TestCase):
 
978
 
 
979
    def test_split_unicode(self):
 
980
        self.assertEqual([u'foo\n', u'bar\xae'],
 
981
                         osutils.split_lines(u'foo\nbar\xae'))
 
982
        self.assertEqual([u'foo\n', u'bar\xae\n'],
 
983
                         osutils.split_lines(u'foo\nbar\xae\n'))
 
984
 
 
985
    def test_split_with_carriage_returns(self):
 
986
        self.assertEqual(['foo\rbar\n'],
 
987
                         osutils.split_lines('foo\rbar\n'))
 
988
 
 
989
 
 
990
class TestWalkDirs(tests.TestCaseInTempDir):
 
991
 
 
992
    def assertExpectedBlocks(self, expected, result):
 
993
        self.assertEqual(expected,
 
994
                         [(dirinfo, [line[0:3] for line in block])
 
995
                          for dirinfo, block in result])
 
996
 
 
997
    def test_walkdirs(self):
 
998
        tree = [
 
999
            '.bzr',
 
1000
            '0file',
 
1001
            '1dir/',
 
1002
            '1dir/0file',
 
1003
            '1dir/1dir/',
 
1004
            '2file'
 
1005
            ]
 
1006
        self.build_tree(tree)
 
1007
        expected_dirblocks = [
 
1008
                (('', '.'),
 
1009
                 [('0file', '0file', 'file'),
 
1010
                  ('1dir', '1dir', 'directory'),
 
1011
                  ('2file', '2file', 'file'),
 
1012
                 ]
 
1013
                ),
 
1014
                (('1dir', './1dir'),
 
1015
                 [('1dir/0file', '0file', 'file'),
 
1016
                  ('1dir/1dir', '1dir', 'directory'),
 
1017
                 ]
 
1018
                ),
 
1019
                (('1dir/1dir', './1dir/1dir'),
 
1020
                 [
 
1021
                 ]
 
1022
                ),
 
1023
            ]
 
1024
        result = []
 
1025
        found_bzrdir = False
 
1026
        for dirdetail, dirblock in osutils.walkdirs('.'):
 
1027
            if len(dirblock) and dirblock[0][1] == '.bzr':
 
1028
                # this tests the filtering of selected paths
 
1029
                found_bzrdir = True
 
1030
                del dirblock[0]
 
1031
            result.append((dirdetail, dirblock))
 
1032
 
 
1033
        self.assertTrue(found_bzrdir)
 
1034
        self.assertExpectedBlocks(expected_dirblocks, result)
 
1035
        # you can search a subdir only, with a supplied prefix.
 
1036
        result = []
 
1037
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
 
1038
            result.append(dirblock)
 
1039
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
1040
 
 
1041
    def test_walkdirs_os_error(self):
 
1042
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1043
        # Pyrex readdir didn't raise useful messages if it had an error
 
1044
        # reading the directory
 
1045
        if sys.platform == 'win32':
 
1046
            raise tests.TestNotApplicable(
 
1047
                "readdir IOError not tested on win32")
 
1048
        os.mkdir("test-unreadable")
 
1049
        os.chmod("test-unreadable", 0000)
 
1050
        # must chmod it back so that it can be removed
 
1051
        self.addCleanup(os.chmod, "test-unreadable", 0700)
 
1052
        # The error is not raised until the generator is actually evaluated.
 
1053
        # (It would be ok if it happened earlier but at the moment it
 
1054
        # doesn't.)
 
1055
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
 
1056
        self.assertEquals('./test-unreadable', e.filename)
 
1057
        self.assertEquals(errno.EACCES, e.errno)
 
1058
        # Ensure the message contains the file name
 
1059
        self.assertContainsRe(str(e), "\./test-unreadable")
 
1060
 
 
1061
    def test__walkdirs_utf8(self):
 
1062
        tree = [
 
1063
            '.bzr',
 
1064
            '0file',
 
1065
            '1dir/',
 
1066
            '1dir/0file',
 
1067
            '1dir/1dir/',
 
1068
            '2file'
 
1069
            ]
 
1070
        self.build_tree(tree)
 
1071
        expected_dirblocks = [
 
1072
                (('', '.'),
 
1073
                 [('0file', '0file', 'file'),
 
1074
                  ('1dir', '1dir', 'directory'),
 
1075
                  ('2file', '2file', 'file'),
 
1076
                 ]
 
1077
                ),
 
1078
                (('1dir', './1dir'),
 
1079
                 [('1dir/0file', '0file', 'file'),
 
1080
                  ('1dir/1dir', '1dir', 'directory'),
 
1081
                 ]
 
1082
                ),
 
1083
                (('1dir/1dir', './1dir/1dir'),
 
1084
                 [
 
1085
                 ]
 
1086
                ),
 
1087
            ]
 
1088
        result = []
 
1089
        found_bzrdir = False
 
1090
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
 
1091
            if len(dirblock) and dirblock[0][1] == '.bzr':
 
1092
                # this tests the filtering of selected paths
 
1093
                found_bzrdir = True
 
1094
                del dirblock[0]
 
1095
            result.append((dirdetail, dirblock))
 
1096
 
 
1097
        self.assertTrue(found_bzrdir)
 
1098
        self.assertExpectedBlocks(expected_dirblocks, result)
 
1099
 
 
1100
        # you can search a subdir only, with a supplied prefix.
 
1101
        result = []
 
1102
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
 
1103
            result.append(dirblock)
 
1104
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
1105
 
 
1106
    def _filter_out_stat(self, result):
 
1107
        """Filter out the stat value from the walkdirs result"""
 
1108
        for dirdetail, dirblock in result:
 
1109
            new_dirblock = []
 
1110
            for info in dirblock:
 
1111
                # Ignore info[3] which is the stat
 
1112
                new_dirblock.append((info[0], info[1], info[2], info[4]))
 
1113
            dirblock[:] = new_dirblock
 
1114
 
 
1115
    def _save_platform_info(self):
 
1116
        cur_winver = win32utils.winver
 
1117
        cur_fs_enc = osutils._fs_enc
 
1118
        cur_dir_reader = osutils._selected_dir_reader
 
1119
        def restore():
 
1120
            win32utils.winver = cur_winver
 
1121
            osutils._fs_enc = cur_fs_enc
 
1122
            osutils._selected_dir_reader = cur_dir_reader
 
1123
        self.addCleanup(restore)
 
1124
 
 
1125
    def assertDirReaderIs(self, expected):
 
1126
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
 
1127
        # Force it to redetect
 
1128
        osutils._selected_dir_reader = None
 
1129
        # Nothing to list, but should still trigger the selection logic
 
1130
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
 
1131
        self.assertIsInstance(osutils._selected_dir_reader, expected)
 
1132
 
 
1133
    def test_force_walkdirs_utf8_fs_utf8(self):
 
1134
        self.requireFeature(UTF8DirReaderFeature)
 
1135
        self._save_platform_info()
 
1136
        win32utils.winver = None # Avoid the win32 detection code
 
1137
        osutils._fs_enc = 'UTF-8'
 
1138
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1139
 
 
1140
    def test_force_walkdirs_utf8_fs_ascii(self):
 
1141
        self.requireFeature(UTF8DirReaderFeature)
 
1142
        self._save_platform_info()
 
1143
        win32utils.winver = None # Avoid the win32 detection code
 
1144
        osutils._fs_enc = 'US-ASCII'
 
1145
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1146
 
 
1147
    def test_force_walkdirs_utf8_fs_ANSI(self):
 
1148
        self.requireFeature(UTF8DirReaderFeature)
 
1149
        self._save_platform_info()
 
1150
        win32utils.winver = None # Avoid the win32 detection code
 
1151
        osutils._fs_enc = 'ANSI_X3.4-1968'
 
1152
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1153
 
 
1154
    def test_force_walkdirs_utf8_fs_latin1(self):
 
1155
        self._save_platform_info()
 
1156
        win32utils.winver = None # Avoid the win32 detection code
 
1157
        osutils._fs_enc = 'latin1'
 
1158
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
1159
 
 
1160
    def test_force_walkdirs_utf8_nt(self):
 
1161
        # Disabled because the thunk of the whole walkdirs api is disabled.
 
1162
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1163
        self._save_platform_info()
 
1164
        win32utils.winver = 'Windows NT'
 
1165
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1166
        self.assertDirReaderIs(Win32ReadDir)
 
1167
 
 
1168
    def test_force_walkdirs_utf8_98(self):
 
1169
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1170
        self._save_platform_info()
 
1171
        win32utils.winver = 'Windows 98'
 
1172
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
1173
 
 
1174
    def test_unicode_walkdirs(self):
 
1175
        """Walkdirs should always return unicode paths."""
 
1176
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1177
        name0 = u'0file-\xb6'
 
1178
        name1 = u'1dir-\u062c\u0648'
 
1179
        name2 = u'2file-\u0633'
 
1180
        tree = [
 
1181
            name0,
 
1182
            name1 + '/',
 
1183
            name1 + '/' + name0,
 
1184
            name1 + '/' + name1 + '/',
 
1185
            name2,
 
1186
            ]
 
1187
        self.build_tree(tree)
 
1188
        expected_dirblocks = [
 
1189
                ((u'', u'.'),
 
1190
                 [(name0, name0, 'file', './' + name0),
 
1191
                  (name1, name1, 'directory', './' + name1),
 
1192
                  (name2, name2, 'file', './' + name2),
 
1193
                 ]
 
1194
                ),
 
1195
                ((name1, './' + name1),
 
1196
                 [(name1 + '/' + name0, name0, 'file', './' + name1
 
1197
                                                        + '/' + name0),
 
1198
                  (name1 + '/' + name1, name1, 'directory', './' + name1
 
1199
                                                            + '/' + name1),
 
1200
                 ]
 
1201
                ),
 
1202
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
 
1203
                 [
 
1204
                 ]
 
1205
                ),
 
1206
            ]
 
1207
        result = list(osutils.walkdirs('.'))
 
1208
        self._filter_out_stat(result)
 
1209
        self.assertEqual(expected_dirblocks, result)
 
1210
        result = list(osutils.walkdirs(u'./'+name1, name1))
 
1211
        self._filter_out_stat(result)
 
1212
        self.assertEqual(expected_dirblocks[1:], result)
 
1213
 
 
1214
    def test_unicode__walkdirs_utf8(self):
 
1215
        """Walkdirs_utf8 should always return utf8 paths.
 
1216
 
 
1217
        The abspath portion might be in unicode or utf-8
 
1218
        """
 
1219
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1220
        name0 = u'0file-\xb6'
 
1221
        name1 = u'1dir-\u062c\u0648'
 
1222
        name2 = u'2file-\u0633'
 
1223
        tree = [
 
1224
            name0,
 
1225
            name1 + '/',
 
1226
            name1 + '/' + name0,
 
1227
            name1 + '/' + name1 + '/',
 
1228
            name2,
 
1229
            ]
 
1230
        self.build_tree(tree)
 
1231
        name0 = name0.encode('utf8')
 
1232
        name1 = name1.encode('utf8')
 
1233
        name2 = name2.encode('utf8')
 
1234
 
 
1235
        expected_dirblocks = [
 
1236
                (('', '.'),
 
1237
                 [(name0, name0, 'file', './' + name0),
 
1238
                  (name1, name1, 'directory', './' + name1),
 
1239
                  (name2, name2, 'file', './' + name2),
 
1240
                 ]
 
1241
                ),
 
1242
                ((name1, './' + name1),
 
1243
                 [(name1 + '/' + name0, name0, 'file', './' + name1
 
1244
                                                        + '/' + name0),
 
1245
                  (name1 + '/' + name1, name1, 'directory', './' + name1
 
1246
                                                            + '/' + name1),
 
1247
                 ]
 
1248
                ),
 
1249
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
 
1250
                 [
 
1251
                 ]
 
1252
                ),
 
1253
            ]
 
1254
        result = []
 
1255
        # For ease in testing, if walkdirs_utf8 returns Unicode, assert that
 
1256
        # all abspaths are Unicode, and encode them back into utf8.
 
1257
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
 
1258
            self.assertIsInstance(dirdetail[0], str)
 
1259
            if isinstance(dirdetail[1], unicode):
 
1260
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
 
1261
                dirblock = [list(info) for info in dirblock]
 
1262
                for info in dirblock:
 
1263
                    self.assertIsInstance(info[4], unicode)
 
1264
                    info[4] = info[4].encode('utf8')
 
1265
            new_dirblock = []
 
1266
            for info in dirblock:
 
1267
                self.assertIsInstance(info[0], str)
 
1268
                self.assertIsInstance(info[1], str)
 
1269
                self.assertIsInstance(info[4], str)
 
1270
                # Remove the stat information
 
1271
                new_dirblock.append((info[0], info[1], info[2], info[4]))
 
1272
            result.append((dirdetail, new_dirblock))
 
1273
        self.assertEqual(expected_dirblocks, result)
 
1274
 
 
1275
    def test__walkdirs_utf8_with_unicode_fs(self):
 
1276
        """UnicodeDirReader should be a safe fallback everywhere
 
1277
 
 
1278
        The abspath portion should be in unicode
 
1279
        """
 
1280
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1281
        # Use the unicode reader. TODO: split into driver-and-driven unit
 
1282
        # tests.
 
1283
        self._save_platform_info()
 
1284
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
 
1285
        name0u = u'0file-\xb6'
 
1286
        name1u = u'1dir-\u062c\u0648'
 
1287
        name2u = u'2file-\u0633'
 
1288
        tree = [
 
1289
            name0u,
 
1290
            name1u + '/',
 
1291
            name1u + '/' + name0u,
 
1292
            name1u + '/' + name1u + '/',
 
1293
            name2u,
 
1294
            ]
 
1295
        self.build_tree(tree)
 
1296
        name0 = name0u.encode('utf8')
 
1297
        name1 = name1u.encode('utf8')
 
1298
        name2 = name2u.encode('utf8')
 
1299
 
 
1300
        # All of the abspaths should be in unicode, all of the relative paths
 
1301
        # should be in utf8
 
1302
        expected_dirblocks = [
 
1303
                (('', '.'),
 
1304
                 [(name0, name0, 'file', './' + name0u),
 
1305
                  (name1, name1, 'directory', './' + name1u),
 
1306
                  (name2, name2, 'file', './' + name2u),
 
1307
                 ]
 
1308
                ),
 
1309
                ((name1, './' + name1u),
 
1310
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
 
1311
                                                        + '/' + name0u),
 
1312
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
 
1313
                                                            + '/' + name1u),
 
1314
                 ]
 
1315
                ),
 
1316
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
 
1317
                 [
 
1318
                 ]
 
1319
                ),
 
1320
            ]
 
1321
        result = list(osutils._walkdirs_utf8('.'))
 
1322
        self._filter_out_stat(result)
 
1323
        self.assertEqual(expected_dirblocks, result)
 
1324
 
 
1325
    def test__walkdirs_utf8_win32readdir(self):
 
1326
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1327
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1328
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1329
        self._save_platform_info()
 
1330
        osutils._selected_dir_reader = Win32ReadDir()
 
1331
        name0u = u'0file-\xb6'
 
1332
        name1u = u'1dir-\u062c\u0648'
 
1333
        name2u = u'2file-\u0633'
 
1334
        tree = [
 
1335
            name0u,
 
1336
            name1u + '/',
 
1337
            name1u + '/' + name0u,
 
1338
            name1u + '/' + name1u + '/',
 
1339
            name2u,
 
1340
            ]
 
1341
        self.build_tree(tree)
 
1342
        name0 = name0u.encode('utf8')
 
1343
        name1 = name1u.encode('utf8')
 
1344
        name2 = name2u.encode('utf8')
 
1345
 
 
1346
        # All of the abspaths should be in unicode, all of the relative paths
 
1347
        # should be in utf8
 
1348
        expected_dirblocks = [
 
1349
                (('', '.'),
 
1350
                 [(name0, name0, 'file', './' + name0u),
 
1351
                  (name1, name1, 'directory', './' + name1u),
 
1352
                  (name2, name2, 'file', './' + name2u),
 
1353
                 ]
 
1354
                ),
 
1355
                ((name1, './' + name1u),
 
1356
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
 
1357
                                                        + '/' + name0u),
 
1358
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
 
1359
                                                            + '/' + name1u),
 
1360
                 ]
 
1361
                ),
 
1362
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
 
1363
                 [
 
1364
                 ]
 
1365
                ),
 
1366
            ]
 
1367
        result = list(osutils._walkdirs_utf8(u'.'))
 
1368
        self._filter_out_stat(result)
 
1369
        self.assertEqual(expected_dirblocks, result)
 
1370
 
 
1371
    def assertStatIsCorrect(self, path, win32stat):
 
1372
        os_stat = os.stat(path)
 
1373
        self.assertEqual(os_stat.st_size, win32stat.st_size)
 
1374
        self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
 
1375
        self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
 
1376
        self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
 
1377
        self.assertEqual(os_stat.st_dev, win32stat.st_dev)
 
1378
        self.assertEqual(os_stat.st_ino, win32stat.st_ino)
 
1379
        self.assertEqual(os_stat.st_mode, win32stat.st_mode)
 
1380
 
 
1381
    def test__walkdirs_utf_win32_find_file_stat_file(self):
 
1382
        """make sure our Stat values are valid"""
 
1383
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1384
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1385
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1386
        name0u = u'0file-\xb6'
 
1387
        name0 = name0u.encode('utf8')
 
1388
        self.build_tree([name0u])
 
1389
        # I hate to sleep() here, but I'm trying to make the ctime different
 
1390
        # from the mtime
 
1391
        time.sleep(2)
 
1392
        f = open(name0u, 'ab')
 
1393
        try:
 
1394
            f.write('just a small update')
 
1395
        finally:
 
1396
            f.close()
 
1397
 
 
1398
        result = Win32ReadDir().read_dir('', u'.')
 
1399
        entry = result[0]
 
1400
        self.assertEqual((name0, name0, 'file'), entry[:3])
 
1401
        self.assertEqual(u'./' + name0u, entry[4])
 
1402
        self.assertStatIsCorrect(entry[4], entry[3])
 
1403
        self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
 
1404
 
 
1405
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
 
1406
        """make sure our Stat values are valid"""
 
1407
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1408
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1409
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1410
        name0u = u'0dir-\u062c\u0648'
 
1411
        name0 = name0u.encode('utf8')
 
1412
        self.build_tree([name0u + '/'])
 
1413
 
 
1414
        result = Win32ReadDir().read_dir('', u'.')
 
1415
        entry = result[0]
 
1416
        self.assertEqual((name0, name0, 'directory'), entry[:3])
 
1417
        self.assertEqual(u'./' + name0u, entry[4])
 
1418
        self.assertStatIsCorrect(entry[4], entry[3])
 
1419
 
 
1420
    def assertPathCompare(self, path_less, path_greater):
 
1421
        """check that path_less and path_greater compare correctly."""
 
1422
        self.assertEqual(0, osutils.compare_paths_prefix_order(
 
1423
            path_less, path_less))
 
1424
        self.assertEqual(0, osutils.compare_paths_prefix_order(
 
1425
            path_greater, path_greater))
 
1426
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
 
1427
            path_less, path_greater))
 
1428
        self.assertEqual(1, osutils.compare_paths_prefix_order(
 
1429
            path_greater, path_less))
 
1430
 
 
1431
    def test_compare_paths_prefix_order(self):
 
1432
        # root before all else
 
1433
        self.assertPathCompare("/", "/a")
 
1434
        # alpha within a dir
 
1435
        self.assertPathCompare("/a", "/b")
 
1436
        self.assertPathCompare("/b", "/z")
 
1437
        # high dirs before lower.
 
1438
        self.assertPathCompare("/z", "/a/a")
 
1439
        # except if the deeper dir should be output first
 
1440
        self.assertPathCompare("/a/b/c", "/d/g")
 
1441
        # lexical betwen dirs of the same height
 
1442
        self.assertPathCompare("/a/z", "/z/z")
 
1443
        self.assertPathCompare("/a/c/z", "/a/d/e")
 
1444
 
 
1445
        # this should also be consistent for no leading / paths
 
1446
        # root before all else
 
1447
        self.assertPathCompare("", "a")
 
1448
        # alpha within a dir
 
1449
        self.assertPathCompare("a", "b")
 
1450
        self.assertPathCompare("b", "z")
 
1451
        # high dirs before lower.
 
1452
        self.assertPathCompare("z", "a/a")
 
1453
        # except if the deeper dir should be output first
 
1454
        self.assertPathCompare("a/b/c", "d/g")
 
1455
        # lexical betwen dirs of the same height
 
1456
        self.assertPathCompare("a/z", "z/z")
 
1457
        self.assertPathCompare("a/c/z", "a/d/e")
 
1458
 
 
1459
    def test_path_prefix_sorting(self):
 
1460
        """Doing a sort on path prefix should match our sample data."""
 
1461
        original_paths = [
 
1462
            'a',
 
1463
            'a/b',
 
1464
            'a/b/c',
 
1465
            'b',
 
1466
            'b/c',
 
1467
            'd',
 
1468
            'd/e',
 
1469
            'd/e/f',
 
1470
            'd/f',
 
1471
            'd/g',
 
1472
            'g',
 
1473
            ]
 
1474
 
 
1475
        dir_sorted_paths = [
 
1476
            'a',
 
1477
            'b',
 
1478
            'd',
 
1479
            'g',
 
1480
            'a/b',
 
1481
            'a/b/c',
 
1482
            'b/c',
 
1483
            'd/e',
 
1484
            'd/f',
 
1485
            'd/g',
 
1486
            'd/e/f',
 
1487
            ]
 
1488
 
 
1489
        self.assertEqual(
 
1490
            dir_sorted_paths,
 
1491
            sorted(original_paths, key=osutils.path_prefix_key))
 
1492
        # using the comparison routine shoudl work too:
 
1493
        self.assertEqual(
 
1494
            dir_sorted_paths,
 
1495
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
 
1496
 
 
1497
 
 
1498
class TestCopyTree(tests.TestCaseInTempDir):
 
1499
 
 
1500
    def test_copy_basic_tree(self):
 
1501
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
1502
        osutils.copy_tree('source', 'target')
 
1503
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
 
1504
        self.assertEqual(['c'], os.listdir('target/b'))
 
1505
 
 
1506
    def test_copy_tree_target_exists(self):
 
1507
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
 
1508
                         'target/'])
 
1509
        osutils.copy_tree('source', 'target')
 
1510
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
 
1511
        self.assertEqual(['c'], os.listdir('target/b'))
 
1512
 
 
1513
    def test_copy_tree_symlinks(self):
 
1514
        self.requireFeature(tests.SymlinkFeature)
 
1515
        self.build_tree(['source/'])
 
1516
        os.symlink('a/generic/path', 'source/lnk')
 
1517
        osutils.copy_tree('source', 'target')
 
1518
        self.assertEqual(['lnk'], os.listdir('target'))
 
1519
        self.assertEqual('a/generic/path', os.readlink('target/lnk'))
 
1520
 
 
1521
    def test_copy_tree_handlers(self):
 
1522
        processed_files = []
 
1523
        processed_links = []
 
1524
        def file_handler(from_path, to_path):
 
1525
            processed_files.append(('f', from_path, to_path))
 
1526
        def dir_handler(from_path, to_path):
 
1527
            processed_files.append(('d', from_path, to_path))
 
1528
        def link_handler(from_path, to_path):
 
1529
            processed_links.append((from_path, to_path))
 
1530
        handlers = {'file':file_handler,
 
1531
                    'directory':dir_handler,
 
1532
                    'symlink':link_handler,
 
1533
                   }
 
1534
 
 
1535
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
1536
        if osutils.has_symlinks():
 
1537
            os.symlink('a/generic/path', 'source/lnk')
 
1538
        osutils.copy_tree('source', 'target', handlers=handlers)
 
1539
 
 
1540
        self.assertEqual([('d', 'source', 'target'),
 
1541
                          ('f', 'source/a', 'target/a'),
 
1542
                          ('d', 'source/b', 'target/b'),
 
1543
                          ('f', 'source/b/c', 'target/b/c'),
 
1544
                         ], processed_files)
 
1545
        self.failIfExists('target')
 
1546
        if osutils.has_symlinks():
 
1547
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
 
1548
 
 
1549
 
 
1550
class TestSetUnsetEnv(tests.TestCase):
 
1551
    """Test updating the environment"""
 
1552
 
 
1553
    def setUp(self):
 
1554
        super(TestSetUnsetEnv, self).setUp()
 
1555
 
 
1556
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
 
1557
                         'Environment was not cleaned up properly.'
 
1558
                         ' Variable BZR_TEST_ENV_VAR should not exist.')
 
1559
        def cleanup():
 
1560
            if 'BZR_TEST_ENV_VAR' in os.environ:
 
1561
                del os.environ['BZR_TEST_ENV_VAR']
 
1562
 
 
1563
        self.addCleanup(cleanup)
 
1564
 
 
1565
    def test_set(self):
 
1566
        """Test that we can set an env variable"""
 
1567
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
 
1568
        self.assertEqual(None, old)
 
1569
        self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
 
1570
 
 
1571
    def test_double_set(self):
 
1572
        """Test that we get the old value out"""
 
1573
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
 
1574
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
 
1575
        self.assertEqual('foo', old)
 
1576
        self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
 
1577
 
 
1578
    def test_unicode(self):
 
1579
        """Environment can only contain plain strings
 
1580
 
 
1581
        So Unicode strings must be encoded.
 
1582
        """
 
1583
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
1584
        if uni_val is None:
 
1585
            raise tests.TestSkipped(
 
1586
                'Cannot find a unicode character that works in encoding %s'
 
1587
                % (osutils.get_user_encoding(),))
 
1588
 
 
1589
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
 
1590
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
 
1591
 
 
1592
    def test_unset(self):
 
1593
        """Test that passing None will remove the env var"""
 
1594
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
 
1595
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
 
1596
        self.assertEqual('foo', old)
 
1597
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
 
1598
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
 
1599
 
 
1600
 
 
1601
class TestSizeShaFile(tests.TestCaseInTempDir):
 
1602
 
 
1603
    def test_sha_empty(self):
 
1604
        self.build_tree_contents([('foo', '')])
 
1605
        expected_sha = osutils.sha_string('')
 
1606
        f = open('foo')
 
1607
        self.addCleanup(f.close)
 
1608
        size, sha = osutils.size_sha_file(f)
 
1609
        self.assertEqual(0, size)
 
1610
        self.assertEqual(expected_sha, sha)
 
1611
 
 
1612
    def test_sha_mixed_endings(self):
 
1613
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1614
        self.build_tree_contents([('foo', text)])
 
1615
        expected_sha = osutils.sha_string(text)
 
1616
        f = open('foo')
 
1617
        self.addCleanup(f.close)
 
1618
        size, sha = osutils.size_sha_file(f)
 
1619
        self.assertEqual(38, size)
 
1620
        self.assertEqual(expected_sha, sha)
 
1621
 
 
1622
 
 
1623
class TestShaFileByName(tests.TestCaseInTempDir):
 
1624
 
 
1625
    def test_sha_empty(self):
 
1626
        self.build_tree_contents([('foo', '')])
 
1627
        expected_sha = osutils.sha_string('')
 
1628
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1629
 
 
1630
    def test_sha_mixed_endings(self):
 
1631
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1632
        self.build_tree_contents([('foo', text)])
 
1633
        expected_sha = osutils.sha_string(text)
 
1634
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1635
 
 
1636
 
 
1637
class TestResourceLoading(tests.TestCaseInTempDir):
 
1638
 
 
1639
    def test_resource_string(self):
 
1640
        # test resource in bzrlib
 
1641
        text = osutils.resource_string('bzrlib', 'debug.py')
 
1642
        self.assertContainsRe(text, "debug_flags = set()")
 
1643
        # test resource under bzrlib
 
1644
        text = osutils.resource_string('bzrlib.ui', 'text.py')
 
1645
        self.assertContainsRe(text, "class TextUIFactory")
 
1646
        # test unsupported package
 
1647
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
 
1648
            'yyy.xx')
 
1649
        # test unknown resource
 
1650
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
 
1651
 
 
1652
 
 
1653
class TestReCompile(tests.TestCase):
 
1654
 
 
1655
    def test_re_compile_checked(self):
 
1656
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1657
        self.assertTrue(r.match('aaaa'))
 
1658
        self.assertTrue(r.match('aAaA'))
 
1659
 
 
1660
    def test_re_compile_checked_error(self):
 
1661
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1662
        err = self.assertRaises(
 
1663
            errors.BzrCommandError,
 
1664
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1665
        self.assertEqual(
 
1666
            "Invalid regular expression in test case: '*': "
 
1667
            "nothing to repeat",
 
1668
            str(err))
 
1669
 
 
1670
 
 
1671
class TestDirReader(tests.TestCaseInTempDir):
 
1672
 
 
1673
    # Set by load_tests
 
1674
    _dir_reader_class = None
 
1675
    _native_to_unicode = None
 
1676
 
 
1677
    def setUp(self):
 
1678
        tests.TestCaseInTempDir.setUp(self)
 
1679
 
 
1680
        # Save platform specific info and reset it
 
1681
        cur_dir_reader = osutils._selected_dir_reader
 
1682
 
 
1683
        def restore():
 
1684
            osutils._selected_dir_reader = cur_dir_reader
 
1685
        self.addCleanup(restore)
 
1686
 
 
1687
        osutils._selected_dir_reader = self._dir_reader_class()
 
1688
 
 
1689
    def _get_ascii_tree(self):
 
1690
        tree = [
 
1691
            '0file',
 
1692
            '1dir/',
 
1693
            '1dir/0file',
 
1694
            '1dir/1dir/',
 
1695
            '2file'
 
1696
            ]
 
1697
        expected_dirblocks = [
 
1698
                (('', '.'),
 
1699
                 [('0file', '0file', 'file'),
 
1700
                  ('1dir', '1dir', 'directory'),
 
1701
                  ('2file', '2file', 'file'),
 
1702
                 ]
 
1703
                ),
 
1704
                (('1dir', './1dir'),
 
1705
                 [('1dir/0file', '0file', 'file'),
 
1706
                  ('1dir/1dir', '1dir', 'directory'),
 
1707
                 ]
 
1708
                ),
 
1709
                (('1dir/1dir', './1dir/1dir'),
 
1710
                 [
 
1711
                 ]
 
1712
                ),
 
1713
            ]
 
1714
        return tree, expected_dirblocks
 
1715
 
 
1716
    def test_walk_cur_dir(self):
 
1717
        tree, expected_dirblocks = self._get_ascii_tree()
 
1718
        self.build_tree(tree)
 
1719
        result = list(osutils._walkdirs_utf8('.'))
 
1720
        # Filter out stat and abspath
 
1721
        self.assertEqual(expected_dirblocks,
 
1722
                         [(dirinfo, [line[0:3] for line in block])
 
1723
                          for dirinfo, block in result])
 
1724
 
 
1725
    def test_walk_sub_dir(self):
 
1726
        tree, expected_dirblocks = self._get_ascii_tree()
 
1727
        self.build_tree(tree)
 
1728
        # you can search a subdir only, with a supplied prefix.
 
1729
        result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
 
1730
        # Filter out stat and abspath
 
1731
        self.assertEqual(expected_dirblocks[1:],
 
1732
                         [(dirinfo, [line[0:3] for line in block])
 
1733
                          for dirinfo, block in result])
 
1734
 
 
1735
    def _get_unicode_tree(self):
 
1736
        name0u = u'0file-\xb6'
 
1737
        name1u = u'1dir-\u062c\u0648'
 
1738
        name2u = u'2file-\u0633'
 
1739
        tree = [
 
1740
            name0u,
 
1741
            name1u + '/',
 
1742
            name1u + '/' + name0u,
 
1743
            name1u + '/' + name1u + '/',
 
1744
            name2u,
 
1745
            ]
 
1746
        name0 = name0u.encode('UTF-8')
 
1747
        name1 = name1u.encode('UTF-8')
 
1748
        name2 = name2u.encode('UTF-8')
 
1749
        expected_dirblocks = [
 
1750
                (('', '.'),
 
1751
                 [(name0, name0, 'file', './' + name0u),
 
1752
                  (name1, name1, 'directory', './' + name1u),
 
1753
                  (name2, name2, 'file', './' + name2u),
 
1754
                 ]
 
1755
                ),
 
1756
                ((name1, './' + name1u),
 
1757
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
 
1758
                                                        + '/' + name0u),
 
1759
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
 
1760
                                                            + '/' + name1u),
 
1761
                 ]
 
1762
                ),
 
1763
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
 
1764
                 [
 
1765
                 ]
 
1766
                ),
 
1767
            ]
 
1768
        return tree, expected_dirblocks
 
1769
 
 
1770
    def _filter_out(self, raw_dirblocks):
 
1771
        """Filter out a walkdirs_utf8 result.
 
1772
 
 
1773
        stat field is removed, all native paths are converted to unicode
 
1774
        """
 
1775
        filtered_dirblocks = []
 
1776
        for dirinfo, block in raw_dirblocks:
 
1777
            dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
 
1778
            details = []
 
1779
            for line in block:
 
1780
                details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
 
1781
            filtered_dirblocks.append((dirinfo, details))
 
1782
        return filtered_dirblocks
 
1783
 
 
1784
    def test_walk_unicode_tree(self):
 
1785
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1786
        tree, expected_dirblocks = self._get_unicode_tree()
 
1787
        self.build_tree(tree)
 
1788
        result = list(osutils._walkdirs_utf8('.'))
 
1789
        self.assertEqual(expected_dirblocks, self._filter_out(result))
 
1790
 
 
1791
    def test_symlink(self):
 
1792
        self.requireFeature(tests.SymlinkFeature)
 
1793
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1794
        target = u'target\N{Euro Sign}'
 
1795
        link_name = u'l\N{Euro Sign}nk'
 
1796
        os.symlink(target, link_name)
 
1797
        target_utf8 = target.encode('UTF-8')
 
1798
        link_name_utf8 = link_name.encode('UTF-8')
 
1799
        expected_dirblocks = [
 
1800
                (('', '.'),
 
1801
                 [(link_name_utf8, link_name_utf8,
 
1802
                   'symlink', './' + link_name),],
 
1803
                 )]
 
1804
        result = list(osutils._walkdirs_utf8('.'))
 
1805
        self.assertEqual(expected_dirblocks, self._filter_out(result))
 
1806
 
 
1807
 
 
1808
class TestReadLink(tests.TestCaseInTempDir):
 
1809
    """Exposes os.readlink() problems and the osutils solution.
 
1810
 
 
1811
    The only guarantee offered by os.readlink(), starting with 2.6, is that a
 
1812
    unicode string will be returned if a unicode string is passed.
 
1813
 
 
1814
    But prior python versions failed to properly encode the passed unicode
 
1815
    string.
 
1816
    """
 
1817
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
 
1818
 
 
1819
    def setUp(self):
 
1820
        super(tests.TestCaseInTempDir, self).setUp()
 
1821
        self.link = u'l\N{Euro Sign}ink'
 
1822
        self.target = u'targe\N{Euro Sign}t'
 
1823
        os.symlink(self.target, self.link)
 
1824
 
 
1825
    def test_os_readlink_link_encoding(self):
 
1826
        if sys.version_info < (2, 6):
 
1827
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
 
1828
        else:
 
1829
            self.assertEquals(self.target,  os.readlink(self.link))
 
1830
 
 
1831
    def test_os_readlink_link_decoding(self):
 
1832
        self.assertEquals(self.target.encode(osutils._fs_enc),
 
1833
                          os.readlink(self.link.encode(osutils._fs_enc)))
 
1834
 
 
1835
 
 
1836
class TestConcurrency(tests.TestCase):
 
1837
 
 
1838
    def test_local_concurrency(self):
 
1839
        concurrency = osutils.local_concurrency()
 
1840
        self.assertIsInstance(concurrency, int)
 
1841
 
 
1842
 
 
1843
class TestFailedToLoadExtension(tests.TestCase):
 
1844
 
 
1845
    def _try_loading(self):
 
1846
        try:
 
1847
            import bzrlib._fictional_extension_py
 
1848
        except ImportError, e:
 
1849
            osutils.failed_to_load_extension(e)
 
1850
            return True
 
1851
 
 
1852
    def setUp(self):
 
1853
        super(TestFailedToLoadExtension, self).setUp()
 
1854
        self.saved_failures = osutils._extension_load_failures[:]
 
1855
        del osutils._extension_load_failures[:]
 
1856
        self.addCleanup(self.restore_failures)
 
1857
 
 
1858
    def restore_failures(self):
 
1859
        osutils._extension_load_failures = self.saved_failures
 
1860
 
 
1861
    def test_failure_to_load(self):
 
1862
        self._try_loading()
 
1863
        self.assertLength(1, osutils._extension_load_failures)
 
1864
        self.assertEquals(osutils._extension_load_failures[0],
 
1865
            "No module named _fictional_extension_py")
 
1866
 
 
1867
    def test_report_extension_load_failures_no_warning(self):
 
1868
        self.assertTrue(self._try_loading())
 
1869
        warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
 
1870
        # it used to give a Python warning; it no longer does
 
1871
        self.assertLength(0, warnings)
 
1872
 
 
1873
    def test_report_extension_load_failures_message(self):
 
1874
        log = StringIO()
 
1875
        trace.push_log_file(log)
 
1876
        self.assertTrue(self._try_loading())
 
1877
        osutils.report_extension_load_failures()
 
1878
        self.assertContainsRe(
 
1879
            log.getvalue(),
 
1880
            r"bzr: warning: some compiled extensions could not be loaded; "
 
1881
            "see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
 
1882
            )