/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: John Arbash Meinel
  • Date: 2009-12-15 17:20:40 UTC
  • mto: (4634.108.4 2.0)
  • mto: This revision was merged to the branch mainline in revision 4928.
  • Revision ID: john@arbash-meinel.com-20091215172040-46v20oub01qly6e0
Remove a spurious double line.

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