/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

merge trunk and resolve conflicts to cleanup submission

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