~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
19
from cStringIO import StringIO
20
20
import errno
21
21
import os
22
 
import re
23
22
import socket
24
23
import stat
25
24
import sys
26
25
import time
27
26
 
 
27
import bzrlib
28
28
from bzrlib import (
29
29
    errors,
30
30
    osutils,
31
31
    tests,
32
32
    win32utils,
33
33
    )
 
34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
 
35
from bzrlib.osutils import (
 
36
        is_inside_any,
 
37
        is_inside_or_parent_of_any,
 
38
        pathjoin,
 
39
        pumpfile,
 
40
        pump_string_file,
 
41
        )
34
42
from bzrlib.tests import (
35
 
    file_utils,
36
 
    test__walkdirs_win32,
 
43
        adapt_tests,
 
44
        Feature,
 
45
        probe_unicode_in_user_encoding,
 
46
        split_suite_by_re,
 
47
        StringIOWrapper,
 
48
        SymlinkFeature,
 
49
        TestCase,
 
50
        TestCaseInTempDir,
 
51
        TestScenarioApplier,
 
52
        TestSkipped,
 
53
        )
 
54
from bzrlib.tests.file_utils import (
 
55
    FakeReadFile,
37
56
    )
38
 
 
39
 
 
40
 
class _UTF8DirReaderFeature(tests.Feature):
 
57
from bzrlib.tests.test__walkdirs_win32 import WalkdirsWin32Feature
 
58
 
 
59
 
 
60
def load_tests(standard_tests, module, loader):
 
61
    """Parameterize readdir tests."""
 
62
    to_adapt, result = split_suite_by_re(standard_tests, "readdir")
 
63
    adapter = TestScenarioApplier()
 
64
    from bzrlib import _readdir_py
 
65
    adapter.scenarios = [('python', {'read_dir': _readdir_py.read_dir})]
 
66
    if ReadDirFeature.available():
 
67
        adapter.scenarios.append(('pyrex',
 
68
            {'read_dir': ReadDirFeature.read_dir}))
 
69
    adapt_tests(to_adapt, adapter, result)
 
70
    return result
 
71
 
 
72
 
 
73
class _ReadDirFeature(Feature):
41
74
 
42
75
    def _probe(self):
43
76
        try:
44
77
            from bzrlib import _readdir_pyx
45
 
            self.reader = _readdir_pyx.UTF8DirReader
 
78
            self.read_dir = _readdir_pyx.read_dir
46
79
            return True
47
80
        except ImportError:
48
81
            return False
50
83
    def feature_name(self):
51
84
        return 'bzrlib._readdir_pyx'
52
85
 
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):
 
86
ReadDirFeature = _ReadDirFeature()
 
87
 
 
88
 
 
89
class TestOSUtils(TestCaseInTempDir):
110
90
 
111
91
    def test_contains_whitespace(self):
112
92
        self.failUnless(osutils.contains_whitespace(u' '))
122
102
        self.failIf(osutils.contains_whitespace(u'hellothere'))
123
103
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
124
104
 
125
 
 
126
 
class TestRename(tests.TestCaseInTempDir):
127
 
 
128
105
    def test_fancy_rename(self):
129
106
        # This should work everywhere
130
107
        def rename(a, b):
168
145
        shape = sorted(os.listdir('.'))
169
146
        self.assertEquals(['A', 'B'], shape)
170
147
 
171
 
 
172
 
class TestRandChars(tests.TestCase):
173
 
 
174
148
    def test_01_rand_chars_empty(self):
175
149
        result = osutils.rand_chars(0)
176
150
        self.assertEqual(result, '')
181
155
        self.assertEqual(type(result), str)
182
156
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
183
157
 
184
 
 
185
 
class TestIsInside(tests.TestCase):
186
 
 
187
158
    def test_is_inside(self):
188
159
        is_inside = osutils.is_inside
189
160
        self.assertTrue(is_inside('src', 'src/foo.c'))
194
165
        self.assertTrue(is_inside('', 'foo.c'))
195
166
 
196
167
    def test_is_inside_any(self):
197
 
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
 
168
        SRC_FOO_C = pathjoin('src', 'foo.c')
198
169
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
199
170
                         (['src'], SRC_FOO_C),
200
171
                         (['src'], 'src'),
201
172
                         ]:
202
 
            self.assert_(osutils.is_inside_any(dirs, fn))
 
173
            self.assert_(is_inside_any(dirs, fn))
203
174
        for dirs, fn in [(['src'], 'srccontrol'),
204
175
                         (['src'], 'srccontrol/foo')]:
205
 
            self.assertFalse(osutils.is_inside_any(dirs, fn))
 
176
            self.assertFalse(is_inside_any(dirs, fn))
206
177
 
207
178
    def test_is_inside_or_parent_of_any(self):
208
179
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
211
182
                         (['src/bar.c', 'bla/foo.c'], 'src'),
212
183
                         (['src'], 'src'),
213
184
                         ]:
214
 
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
215
 
 
 
185
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
 
186
            
216
187
        for dirs, fn in [(['src'], 'srccontrol'),
217
188
                         (['srccontrol/foo.c'], 'src'),
218
189
                         (['src'], 'srccontrol/foo')]:
219
 
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
220
 
 
221
 
 
222
 
class TestRmTree(tests.TestCaseInTempDir):
 
190
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
223
191
 
224
192
    def test_rmtree(self):
225
193
        # Check to remove tree with read-only files/dirs
239
207
        self.failIfExists('dir/file')
240
208
        self.failIfExists('dir')
241
209
 
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
210
    def test_file_kind(self):
258
211
        self.build_tree(['file', 'dir/'])
259
212
        self.assertEquals('file', osutils.file_kind('file'))
261
214
        if osutils.has_symlinks():
262
215
            os.symlink('symlink', 'symlink')
263
216
            self.assertEquals('symlink', osutils.file_kind('symlink'))
264
 
 
 
217
        
265
218
        # TODO: jam 20060529 Test a block device
266
219
        try:
267
220
            os.lstat('/dev/null')
289
242
                os.remove('socket')
290
243
 
291
244
    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):
 
245
        self.assertEqual(osutils.kind_marker('file'), '')
 
246
        self.assertEqual(osutils.kind_marker('directory'), '/')
 
247
        self.assertEqual(osutils.kind_marker('symlink'), '@')
 
248
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
301
249
 
302
250
    def test_get_umask(self):
303
251
        if sys.platform == 'win32':
306
254
            return
307
255
 
308
256
        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):
 
257
        try:
 
258
            os.umask(0222)
 
259
            self.assertEqual(0222, osutils.get_umask())
 
260
            os.umask(0022)
 
261
            self.assertEqual(0022, osutils.get_umask())
 
262
            os.umask(0002)
 
263
            self.assertEqual(0002, osutils.get_umask())
 
264
            os.umask(0027)
 
265
            self.assertEqual(0027, osutils.get_umask())
 
266
        finally:
 
267
            os.umask(orig_umask)
321
268
 
322
269
    def assertFormatedDelta(self, expected, seconds):
323
270
        """Assert osutils.format_delta formats as expected"""
358
305
    def test_format_date(self):
359
306
        self.assertRaises(errors.UnsupportedTimezoneFormat,
360
307
            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
308
 
390
309
    def test_dereference_path(self):
391
 
        self.requireFeature(tests.SymlinkFeature)
 
310
        self.requireFeature(SymlinkFeature)
392
311
        cwd = osutils.realpath('.')
393
312
        os.mkdir('bar')
394
313
        bar_path = osutils.pathjoin(cwd, 'bar')
397
316
        self.assertEqual(bar_path, osutils.realpath('./bar'))
398
317
        os.symlink('bar', 'foo')
399
318
        self.assertEqual(bar_path, osutils.realpath('./foo'))
400
 
 
 
319
        
401
320
        # Does not dereference terminal symlinks
402
321
        foo_path = osutils.pathjoin(cwd, 'foo')
403
322
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
435
354
            osutils.make_readonly('dangling')
436
355
            osutils.make_writable('dangling')
437
356
 
 
357
    def test_kind_marker(self):
 
358
        self.assertEqual("", osutils.kind_marker("file"))
 
359
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
 
360
        self.assertEqual("@", osutils.kind_marker("symlink"))
 
361
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
362
 
438
363
    def test_host_os_dereferences_symlinks(self):
439
364
        osutils.host_os_dereferences_symlinks()
440
365
 
441
366
 
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 TestPumpFile(tests.TestCase):
 
367
class TestPumpFile(TestCase):
464
368
    """Test pumpfile method."""
465
 
 
466
369
    def setUp(self):
467
 
        tests.TestCase.setUp(self)
468
370
        # create a test datablock
469
371
        self.block_size = 512
470
372
        pattern = '0123456789ABCDEF'
477
379
        # make sure test data is larger than max read size
478
380
        self.assertTrue(self.test_data_len > self.block_size)
479
381
 
480
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
382
        from_file = FakeReadFile(self.test_data)
481
383
        to_file = StringIO()
482
384
 
483
385
        # read (max / 2) bytes and verify read size wasn't affected
484
386
        num_bytes_to_read = self.block_size / 2
485
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
387
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
486
388
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
487
389
        self.assertEqual(from_file.get_read_count(), 1)
488
390
 
489
391
        # read (max) bytes and verify read size wasn't affected
490
392
        num_bytes_to_read = self.block_size
491
393
        from_file.reset_read_count()
492
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
394
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
493
395
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
494
396
        self.assertEqual(from_file.get_read_count(), 1)
495
397
 
496
398
        # read (max + 1) bytes and verify read size was limited
497
399
        num_bytes_to_read = self.block_size + 1
498
400
        from_file.reset_read_count()
499
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
401
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
500
402
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
501
403
        self.assertEqual(from_file.get_read_count(), 2)
502
404
 
503
405
        # finish reading the rest of the data
504
406
        num_bytes_to_read = self.test_data_len - to_file.tell()
505
 
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
 
407
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
506
408
 
507
409
        # report error if the data wasn't equal (we only report the size due
508
410
        # to the length of the data)
518
420
        self.assertTrue(self.test_data_len > self.block_size)
519
421
 
520
422
        # retrieve data in blocks
521
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
423
        from_file = FakeReadFile(self.test_data)
522
424
        to_file = StringIO()
523
 
        osutils.pumpfile(from_file, to_file, self.test_data_len,
524
 
                         self.block_size)
 
425
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
525
426
 
526
427
        # verify read size was equal to the maximum read size
527
428
        self.assertTrue(from_file.get_max_read_size() > 0)
542
443
        self.assertTrue(self.test_data_len > self.block_size)
543
444
 
544
445
        # retrieve data to EOF
545
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
446
        from_file = FakeReadFile(self.test_data)
546
447
        to_file = StringIO()
547
 
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
 
448
        pumpfile(from_file, to_file, -1, self.block_size)
548
449
 
549
450
        # verify read size was equal to the maximum read size
550
451
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
562
463
        test verifies that any existing usages of pumpfile will not be broken
563
464
        with this new version."""
564
465
        # retrieve data using default (old) pumpfile method
565
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
466
        from_file = FakeReadFile(self.test_data)
566
467
        to_file = StringIO()
567
 
        osutils.pumpfile(from_file, to_file)
 
468
        pumpfile(from_file, to_file)
568
469
 
569
470
        # report error if the data wasn't equal (we only report the size due
570
471
        # to the length of the data)
573
474
            message = "Data not equal.  Expected %d bytes, received %d."
574
475
            self.fail(message % (len(response_data), self.test_data_len))
575
476
 
576
 
    def test_report_activity(self):
577
 
        activity = []
578
 
        def log_activity(length, direction):
579
 
            activity.append((length, direction))
580
 
        from_file = StringIO(self.test_data)
581
 
        to_file = StringIO()
582
 
        osutils.pumpfile(from_file, to_file, buff_size=500,
583
 
                         report_activity=log_activity, direction='read')
584
 
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
585
 
                          (36, 'read')], activity)
586
 
 
587
 
        from_file = StringIO(self.test_data)
588
 
        to_file = StringIO()
589
 
        del activity[:]
590
 
        osutils.pumpfile(from_file, to_file, buff_size=500,
591
 
                         report_activity=log_activity, direction='write')
592
 
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
593
 
                          (36, 'write')], activity)
594
 
 
595
 
        # And with a limited amount of data
596
 
        from_file = StringIO(self.test_data)
597
 
        to_file = StringIO()
598
 
        del activity[:]
599
 
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
600
 
                         report_activity=log_activity, direction='read')
601
 
        self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
602
 
 
603
 
 
604
 
 
605
 
class TestPumpStringFile(tests.TestCase):
 
477
 
 
478
class TestPumpStringFile(TestCase):
606
479
 
607
480
    def test_empty(self):
608
481
        output = StringIO()
609
 
        osutils.pump_string_file("", output)
 
482
        pump_string_file("", output)
610
483
        self.assertEqual("", output.getvalue())
611
484
 
612
485
    def test_more_than_segment_size(self):
613
486
        output = StringIO()
614
 
        osutils.pump_string_file("123456789", output, 2)
 
487
        pump_string_file("123456789", output, 2)
615
488
        self.assertEqual("123456789", output.getvalue())
616
489
 
617
490
    def test_segment_size(self):
618
491
        output = StringIO()
619
 
        osutils.pump_string_file("12", output, 2)
 
492
        pump_string_file("12", output, 2)
620
493
        self.assertEqual("12", output.getvalue())
621
494
 
622
495
    def test_segment_size_multiple(self):
623
496
        output = StringIO()
624
 
        osutils.pump_string_file("1234", output, 2)
 
497
        pump_string_file("1234", output, 2)
625
498
        self.assertEqual("1234", output.getvalue())
626
499
 
627
500
 
628
 
class TestRelpath(tests.TestCase):
629
 
 
630
 
    def test_simple_relpath(self):
631
 
        cwd = osutils.getcwd()
632
 
        subdir = cwd + '/subdir'
633
 
        self.assertEqual('subdir', osutils.relpath(cwd, subdir))
634
 
 
635
 
    def test_deep_relpath(self):
636
 
        cwd = osutils.getcwd()
637
 
        subdir = cwd + '/sub/subsubdir'
638
 
        self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
639
 
 
640
 
    def test_not_relative(self):
641
 
        self.assertRaises(errors.PathNotChild,
642
 
                          osutils.relpath, 'C:/path', 'H:/path')
643
 
        self.assertRaises(errors.PathNotChild,
644
 
                          osutils.relpath, 'C:/', 'H:/path')
645
 
 
646
 
 
647
 
class TestSafeUnicode(tests.TestCase):
 
501
class TestSafeUnicode(TestCase):
648
502
 
649
503
    def test_from_ascii_string(self):
650
504
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
659
513
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
660
514
 
661
515
    def test_bad_utf8_string(self):
662
 
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
516
        self.assertRaises(BzrBadParameterNotUnicode,
663
517
                          osutils.safe_unicode,
664
518
                          '\xbb\xbb')
665
519
 
666
520
 
667
 
class TestSafeUtf8(tests.TestCase):
 
521
class TestSafeUtf8(TestCase):
668
522
 
669
523
    def test_from_ascii_string(self):
670
524
        f = 'foobar'
680
534
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
681
535
 
682
536
    def test_bad_utf8_string(self):
683
 
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
537
        self.assertRaises(BzrBadParameterNotUnicode,
684
538
                          osutils.safe_utf8, '\xbb\xbb')
685
539
 
686
540
 
687
 
class TestSafeRevisionId(tests.TestCase):
 
541
class TestSafeRevisionId(TestCase):
688
542
 
689
543
    def test_from_ascii_string(self):
690
544
        # this shouldn't give a warning because it's getting an ascii string
712
566
        self.assertEqual(None, osutils.safe_revision_id(None))
713
567
 
714
568
 
715
 
class TestSafeFileId(tests.TestCase):
 
569
class TestSafeFileId(TestCase):
716
570
 
717
571
    def test_from_ascii_string(self):
718
572
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
738
592
        self.assertEqual(None, osutils.safe_file_id(None))
739
593
 
740
594
 
741
 
class TestWin32Funcs(tests.TestCase):
742
 
    """Test that _win32 versions of os utilities return appropriate paths."""
 
595
class TestWin32Funcs(TestCase):
 
596
    """Test that the _win32 versions of os utilities return appropriate paths."""
743
597
 
744
598
    def test_abspath(self):
745
599
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
752
606
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
753
607
 
754
608
    def test_pathjoin(self):
755
 
        self.assertEqual('path/to/foo',
756
 
                         osutils._win32_pathjoin('path', 'to', 'foo'))
757
 
        self.assertEqual('C:/foo',
758
 
                         osutils._win32_pathjoin('path\\to', 'C:\\foo'))
759
 
        self.assertEqual('C:/foo',
760
 
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
761
 
        self.assertEqual('path/to/foo',
762
 
                         osutils._win32_pathjoin('path/to/', 'foo'))
763
 
        self.assertEqual('/foo',
764
 
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
765
 
        self.assertEqual('/foo',
766
 
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
 
609
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
 
610
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
 
611
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
 
612
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
 
613
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
 
614
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
767
615
 
768
616
    def test_normpath(self):
769
 
        self.assertEqual('path/to/foo',
770
 
                         osutils._win32_normpath(r'path\\from\..\to\.\foo'))
771
 
        self.assertEqual('path/to/foo',
772
 
                         osutils._win32_normpath('path//from/../to/./foo'))
 
617
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
 
618
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
773
619
 
774
620
    def test_getcwd(self):
775
621
        cwd = osutils._win32_getcwd()
804
650
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
805
651
 
806
652
 
807
 
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
 
653
class TestWin32FuncsDirs(TestCaseInTempDir):
808
654
    """Test win32 functions that create files."""
 
655
    
 
656
    def test_getcwd(self):
 
657
        if win32utils.winver == 'Windows 98':
 
658
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
 
659
        # Make sure getcwd can handle unicode filenames
 
660
        try:
 
661
            os.mkdir(u'mu-\xb5')
 
662
        except UnicodeError:
 
663
            raise TestSkipped("Unable to create Unicode filename")
809
664
 
810
 
    def test_getcwd(self):
811
 
        self.requireFeature(tests.UnicodeFilenameFeature)
812
 
        os.mkdir(u'mu-\xb5')
813
665
        os.chdir(u'mu-\xb5')
814
666
        # TODO: jam 20060427 This will probably fail on Mac OSX because
815
667
        #       it will change the normalization of B\xe5gfors
820
672
    def test_minimum_path_selection(self):
821
673
        self.assertEqual(set(),
822
674
            osutils.minimum_path_selection([]))
823
 
        self.assertEqual(set(['a']),
824
 
            osutils.minimum_path_selection(['a']))
825
675
        self.assertEqual(set(['a', 'b']),
826
676
            osutils.minimum_path_selection(['a', 'b']))
827
677
        self.assertEqual(set(['a/', 'b']),
828
678
            osutils.minimum_path_selection(['a/', 'b']))
829
679
        self.assertEqual(set(['a/', 'b']),
830
680
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
831
 
        self.assertEqual(set(['a-b', 'a', 'a0b']),
832
 
            osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
833
681
 
834
682
    def test_mkdtemp(self):
835
683
        tmpdir = osutils._win32_mkdtemp(dir='.')
891
739
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
892
740
 
893
741
 
894
 
class TestParentDirectories(tests.TestCaseInTempDir):
895
 
    """Test osutils.parent_directories()"""
896
 
 
897
 
    def test_parent_directories(self):
898
 
        self.assertEqual([], osutils.parent_directories('a'))
899
 
        self.assertEqual(['a'], osutils.parent_directories('a/b'))
900
 
        self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
901
 
 
902
 
 
903
 
class TestMacFuncsDirs(tests.TestCaseInTempDir):
 
742
class TestMacFuncsDirs(TestCaseInTempDir):
904
743
    """Test mac special functions that require directories."""
905
744
 
906
745
    def test_getcwd(self):
907
 
        self.requireFeature(tests.UnicodeFilenameFeature)
908
 
        os.mkdir(u'B\xe5gfors')
 
746
        # On Mac, this will actually create Ba\u030agfors
 
747
        # but chdir will still work, because it accepts both paths
 
748
        try:
 
749
            os.mkdir(u'B\xe5gfors')
 
750
        except UnicodeError:
 
751
            raise TestSkipped("Unable to create Unicode filename")
 
752
 
909
753
        os.chdir(u'B\xe5gfors')
910
754
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
911
755
 
912
756
    def test_getcwd_nonnorm(self):
913
 
        self.requireFeature(tests.UnicodeFilenameFeature)
914
757
        # Test that _mac_getcwd() will normalize this path
915
 
        os.mkdir(u'Ba\u030agfors')
 
758
        try:
 
759
            os.mkdir(u'Ba\u030agfors')
 
760
        except UnicodeError:
 
761
            raise TestSkipped("Unable to create Unicode filename")
 
762
 
916
763
        os.chdir(u'Ba\u030agfors')
917
764
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
918
765
 
919
766
 
920
 
class TestChunksToLines(tests.TestCase):
921
 
 
922
 
    def test_smoketest(self):
923
 
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
924
 
                         osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
925
 
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
926
 
                         osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
927
 
 
928
 
    def test_osutils_binding(self):
929
 
        from bzrlib.tests import test__chunks_to_lines
930
 
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
931
 
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
932
 
        else:
933
 
            from bzrlib._chunks_to_lines_py import chunks_to_lines
934
 
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
935
 
 
936
 
 
937
 
class TestSplitLines(tests.TestCase):
 
767
class TestSplitLines(TestCase):
938
768
 
939
769
    def test_split_unicode(self):
940
770
        self.assertEqual([u'foo\n', u'bar\xae'],
947
777
                         osutils.split_lines('foo\rbar\n'))
948
778
 
949
779
 
950
 
class TestWalkDirs(tests.TestCaseInTempDir):
951
 
 
952
 
    def assertExpectedBlocks(self, expected, result):
953
 
        self.assertEqual(expected,
954
 
                         [(dirinfo, [line[0:3] for line in block])
955
 
                          for dirinfo, block in result])
956
 
 
 
780
class TestWalkDirs(TestCaseInTempDir):
 
781
 
 
782
    def test_readdir(self):
 
783
        tree = [
 
784
            '.bzr/',
 
785
            '0file',
 
786
            '1dir/',
 
787
            '1dir/0file',
 
788
            '1dir/1dir/',
 
789
            '2file'
 
790
            ]
 
791
        self.build_tree(tree)
 
792
        expected_names = ['.bzr', '0file', '1dir', '2file']
 
793
        # read_dir returns pairs, which form a table with either None in all
 
794
        # the first columns, or a sort key to get best on-disk-read order, 
 
795
        # and the disk path name in utf-8 encoding in the second column.
 
796
        read_result = self.read_dir('.')
 
797
        # The second column is always the names, and every name except "." and
 
798
        # ".." should be present.
 
799
        names = sorted([row[1] for row in read_result])
 
800
        self.assertEqual(expected_names, names)
 
801
        expected_sort_key = None
 
802
        if read_result[0][0] is None:
 
803
            # No sort key returned - all keys must None
 
804
            operator = self.assertEqual
 
805
        else:
 
806
            # A sort key in the first row implies sort keys in the other rows.
 
807
            operator = self.assertNotEqual
 
808
        for row in read_result:
 
809
            operator(None, row[0])
 
810
 
 
811
    def test_compiled_extension_exists(self):
 
812
        self.requireFeature(ReadDirFeature)
 
813
        
957
814
    def test_walkdirs(self):
958
815
        tree = [
959
816
            '.bzr',
991
848
            result.append((dirdetail, dirblock))
992
849
 
993
850
        self.assertTrue(found_bzrdir)
994
 
        self.assertExpectedBlocks(expected_dirblocks, result)
 
851
        self.assertEqual(expected_dirblocks,
 
852
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
995
853
        # you can search a subdir only, with a supplied prefix.
996
854
        result = []
997
855
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
998
856
            result.append(dirblock)
999
 
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1000
 
 
1001
 
    def test_walkdirs_os_error(self):
1002
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1003
 
        # Pyrex readdir didn't raise useful messages if it had an error
1004
 
        # reading the directory
1005
 
        if sys.platform == 'win32':
1006
 
            raise tests.TestNotApplicable(
1007
 
                "readdir IOError not tested on win32")
1008
 
        os.mkdir("test-unreadable")
1009
 
        os.chmod("test-unreadable", 0000)
1010
 
        # must chmod it back so that it can be removed
1011
 
        self.addCleanup(os.chmod, "test-unreadable", 0700)
1012
 
        # The error is not raised until the generator is actually evaluated.
1013
 
        # (It would be ok if it happened earlier but at the moment it
1014
 
        # doesn't.)
1015
 
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
1016
 
        self.assertEquals('./test-unreadable', e.filename)
1017
 
        self.assertEquals(errno.EACCES, e.errno)
1018
 
        # Ensure the message contains the file name
1019
 
        self.assertContainsRe(str(e), "\./test-unreadable")
 
857
        self.assertEqual(expected_dirblocks[1:],
 
858
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1020
859
 
1021
860
    def test__walkdirs_utf8(self):
1022
861
        tree = [
1055
894
            result.append((dirdetail, dirblock))
1056
895
 
1057
896
        self.assertTrue(found_bzrdir)
1058
 
        self.assertExpectedBlocks(expected_dirblocks, result)
1059
 
 
 
897
        self.assertEqual(expected_dirblocks,
 
898
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1060
899
        # you can search a subdir only, with a supplied prefix.
1061
900
        result = []
1062
901
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1063
902
            result.append(dirblock)
1064
 
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
903
        self.assertEqual(expected_dirblocks[1:],
 
904
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1065
905
 
1066
906
    def _filter_out_stat(self, result):
1067
907
        """Filter out the stat value from the walkdirs result"""
1072
912
                new_dirblock.append((info[0], info[1], info[2], info[4]))
1073
913
            dirblock[:] = new_dirblock
1074
914
 
 
915
    def test__walkdirs_utf8_selection(self):
 
916
        # Just trigger the function once, to make sure it has selected a real
 
917
        # implementation.
 
918
        list(osutils._walkdirs_utf8('.'))
 
919
        if WalkdirsWin32Feature.available():
 
920
            # If the compiled form is available, make sure it is used
 
921
            from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
 
922
            self.assertIs(_walkdirs_utf8_win32_find_file,
 
923
                          osutils._real_walkdirs_utf8)
 
924
        elif sys.platform == 'win32':
 
925
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
 
926
                          osutils._real_walkdirs_utf8)
 
927
        elif osutils._fs_enc.upper() in ('UTF-8', 'ASCII', 'ANSI_X3.4-1968'): # ascii
 
928
            self.assertIs(osutils._walkdirs_fs_utf8,
 
929
                          osutils._real_walkdirs_utf8)
 
930
        else:
 
931
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
 
932
                          osutils._real_walkdirs_utf8)
 
933
 
1075
934
    def _save_platform_info(self):
1076
935
        cur_winver = win32utils.winver
1077
936
        cur_fs_enc = osutils._fs_enc
1078
 
        cur_dir_reader = osutils._selected_dir_reader
 
937
        cur_real_walkdirs_utf8 = osutils._real_walkdirs_utf8
1079
938
        def restore():
1080
939
            win32utils.winver = cur_winver
1081
940
            osutils._fs_enc = cur_fs_enc
1082
 
            osutils._selected_dir_reader = cur_dir_reader
 
941
            osutils._real_walkdirs_utf8 = cur_real_walkdirs_utf8
1083
942
        self.addCleanup(restore)
1084
943
 
1085
 
    def assertDirReaderIs(self, expected):
 
944
    def assertWalkdirsUtf8Is(self, expected):
1086
945
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1087
946
        # Force it to redetect
1088
 
        osutils._selected_dir_reader = None
 
947
        osutils._real_walkdirs_utf8 = None
1089
948
        # Nothing to list, but should still trigger the selection logic
1090
949
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
1091
 
        self.assertIsInstance(osutils._selected_dir_reader, expected)
 
950
        self.assertIs(expected, osutils._real_walkdirs_utf8)
1092
951
 
1093
952
    def test_force_walkdirs_utf8_fs_utf8(self):
1094
 
        self.requireFeature(UTF8DirReaderFeature)
1095
953
        self._save_platform_info()
1096
954
        win32utils.winver = None # Avoid the win32 detection code
1097
955
        osutils._fs_enc = 'UTF-8'
1098
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
956
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1099
957
 
1100
958
    def test_force_walkdirs_utf8_fs_ascii(self):
1101
 
        self.requireFeature(UTF8DirReaderFeature)
1102
959
        self._save_platform_info()
1103
960
        win32utils.winver = None # Avoid the win32 detection code
1104
961
        osutils._fs_enc = 'US-ASCII'
1105
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
962
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1106
963
 
1107
964
    def test_force_walkdirs_utf8_fs_ANSI(self):
1108
 
        self.requireFeature(UTF8DirReaderFeature)
1109
965
        self._save_platform_info()
1110
966
        win32utils.winver = None # Avoid the win32 detection code
1111
967
        osutils._fs_enc = 'ANSI_X3.4-1968'
1112
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
968
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1113
969
 
1114
970
    def test_force_walkdirs_utf8_fs_latin1(self):
1115
971
        self._save_platform_info()
1116
972
        win32utils.winver = None # Avoid the win32 detection code
1117
973
        osutils._fs_enc = 'latin1'
1118
 
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
974
        self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
1119
975
 
1120
976
    def test_force_walkdirs_utf8_nt(self):
1121
 
        # Disabled because the thunk of the whole walkdirs api is disabled.
1122
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
977
        self.requireFeature(WalkdirsWin32Feature)
1123
978
        self._save_platform_info()
1124
979
        win32utils.winver = 'Windows NT'
1125
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
1126
 
        self.assertDirReaderIs(Win32ReadDir)
 
980
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
 
981
        self.assertWalkdirsUtf8Is(_walkdirs_utf8_win32_find_file)
1127
982
 
1128
 
    def test_force_walkdirs_utf8_98(self):
1129
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
983
    def test_force_walkdirs_utf8_nt(self):
 
984
        self.requireFeature(WalkdirsWin32Feature)
1130
985
        self._save_platform_info()
1131
986
        win32utils.winver = 'Windows 98'
1132
 
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
987
        self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
1133
988
 
1134
989
    def test_unicode_walkdirs(self):
1135
990
        """Walkdirs should always return unicode paths."""
1136
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1137
991
        name0 = u'0file-\xb6'
1138
992
        name1 = u'1dir-\u062c\u0648'
1139
993
        name2 = u'2file-\u0633'
1144
998
            name1 + '/' + name1 + '/',
1145
999
            name2,
1146
1000
            ]
1147
 
        self.build_tree(tree)
 
1001
        try:
 
1002
            self.build_tree(tree)
 
1003
        except UnicodeError:
 
1004
            raise TestSkipped('Could not represent Unicode chars'
 
1005
                              ' in current encoding.')
1148
1006
        expected_dirblocks = [
1149
1007
                ((u'', u'.'),
1150
1008
                 [(name0, name0, 'file', './' + name0),
1176
1034
 
1177
1035
        The abspath portion might be in unicode or utf-8
1178
1036
        """
1179
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1180
1037
        name0 = u'0file-\xb6'
1181
1038
        name1 = u'1dir-\u062c\u0648'
1182
1039
        name2 = u'2file-\u0633'
1187
1044
            name1 + '/' + name1 + '/',
1188
1045
            name2,
1189
1046
            ]
1190
 
        self.build_tree(tree)
 
1047
        try:
 
1048
            self.build_tree(tree)
 
1049
        except UnicodeError:
 
1050
            raise TestSkipped('Could not represent Unicode chars'
 
1051
                              ' in current encoding.')
1191
1052
        name0 = name0.encode('utf8')
1192
1053
        name1 = name1.encode('utf8')
1193
1054
        name2 = name2.encode('utf8')
1232
1093
            result.append((dirdetail, new_dirblock))
1233
1094
        self.assertEqual(expected_dirblocks, result)
1234
1095
 
1235
 
    def test__walkdirs_utf8_with_unicode_fs(self):
1236
 
        """UnicodeDirReader should be a safe fallback everywhere
 
1096
    def test_unicode__walkdirs_unicode_to_utf8(self):
 
1097
        """walkdirs_unicode_to_utf8 should be a safe fallback everywhere
1237
1098
 
1238
1099
        The abspath portion should be in unicode
1239
1100
        """
1240
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1241
 
        # Use the unicode reader. TODO: split into driver-and-driven unit
1242
 
        # tests.
1243
 
        self._save_platform_info()
1244
 
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
1245
1101
        name0u = u'0file-\xb6'
1246
1102
        name1u = u'1dir-\u062c\u0648'
1247
1103
        name2u = u'2file-\u0633'
1252
1108
            name1u + '/' + name1u + '/',
1253
1109
            name2u,
1254
1110
            ]
1255
 
        self.build_tree(tree)
 
1111
        try:
 
1112
            self.build_tree(tree)
 
1113
        except UnicodeError:
 
1114
            raise TestSkipped('Could not represent Unicode chars'
 
1115
                              ' in current encoding.')
1256
1116
        name0 = name0u.encode('utf8')
1257
1117
        name1 = name1u.encode('utf8')
1258
1118
        name2 = name2u.encode('utf8')
1278
1138
                 ]
1279
1139
                ),
1280
1140
            ]
1281
 
        result = list(osutils._walkdirs_utf8('.'))
 
1141
        result = list(osutils._walkdirs_unicode_to_utf8('.'))
1282
1142
        self._filter_out_stat(result)
1283
1143
        self.assertEqual(expected_dirblocks, result)
1284
1144
 
1285
 
    def test__walkdirs_utf8_win32readdir(self):
1286
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1145
    def test__walkdirs_utf_win32_find_file(self):
 
1146
        self.requireFeature(WalkdirsWin32Feature)
1287
1147
        self.requireFeature(tests.UnicodeFilenameFeature)
1288
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
1289
 
        self._save_platform_info()
1290
 
        osutils._selected_dir_reader = Win32ReadDir()
 
1148
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1291
1149
        name0u = u'0file-\xb6'
1292
1150
        name1u = u'1dir-\u062c\u0648'
1293
1151
        name2u = u'2file-\u0633'
1324
1182
                 ]
1325
1183
                ),
1326
1184
            ]
1327
 
        result = list(osutils._walkdirs_utf8(u'.'))
 
1185
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1328
1186
        self._filter_out_stat(result)
1329
1187
        self.assertEqual(expected_dirblocks, result)
1330
1188
 
1340
1198
 
1341
1199
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1342
1200
        """make sure our Stat values are valid"""
1343
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1201
        self.requireFeature(WalkdirsWin32Feature)
1344
1202
        self.requireFeature(tests.UnicodeFilenameFeature)
1345
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1203
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1346
1204
        name0u = u'0file-\xb6'
1347
1205
        name0 = name0u.encode('utf8')
1348
1206
        self.build_tree([name0u])
1355
1213
        finally:
1356
1214
            f.close()
1357
1215
 
1358
 
        result = Win32ReadDir().read_dir('', u'.')
1359
 
        entry = result[0]
 
1216
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
 
1217
        entry = result[0][1][0]
1360
1218
        self.assertEqual((name0, name0, 'file'), entry[:3])
1361
1219
        self.assertEqual(u'./' + name0u, entry[4])
1362
1220
        self.assertStatIsCorrect(entry[4], entry[3])
1364
1222
 
1365
1223
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1366
1224
        """make sure our Stat values are valid"""
1367
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1225
        self.requireFeature(WalkdirsWin32Feature)
1368
1226
        self.requireFeature(tests.UnicodeFilenameFeature)
1369
 
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1227
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1370
1228
        name0u = u'0dir-\u062c\u0648'
1371
1229
        name0 = name0u.encode('utf8')
1372
1230
        self.build_tree([name0u + '/'])
1373
1231
 
1374
 
        result = Win32ReadDir().read_dir('', u'.')
1375
 
        entry = result[0]
 
1232
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
 
1233
        entry = result[0][1][0]
1376
1234
        self.assertEqual((name0, name0, 'directory'), entry[:3])
1377
1235
        self.assertEqual(u'./' + name0u, entry[4])
1378
1236
        self.assertStatIsCorrect(entry[4], entry[3])
1455
1313
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1456
1314
 
1457
1315
 
1458
 
class TestCopyTree(tests.TestCaseInTempDir):
1459
 
 
 
1316
class TestCopyTree(TestCaseInTempDir):
 
1317
    
1460
1318
    def test_copy_basic_tree(self):
1461
1319
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1462
1320
        osutils.copy_tree('source', 'target')
1471
1329
        self.assertEqual(['c'], os.listdir('target/b'))
1472
1330
 
1473
1331
    def test_copy_tree_symlinks(self):
1474
 
        self.requireFeature(tests.SymlinkFeature)
 
1332
        self.requireFeature(SymlinkFeature)
1475
1333
        self.build_tree(['source/'])
1476
1334
        os.symlink('a/generic/path', 'source/lnk')
1477
1335
        osutils.copy_tree('source', 'target')
1507
1365
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1508
1366
 
1509
1367
 
1510
 
class TestSetUnsetEnv(tests.TestCase):
 
1368
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
1369
# [bialix] 2006/12/26
 
1370
 
 
1371
 
 
1372
class TestSetUnsetEnv(TestCase):
1511
1373
    """Test updating the environment"""
1512
1374
 
1513
1375
    def setUp(self):
1537
1399
 
1538
1400
    def test_unicode(self):
1539
1401
        """Environment can only contain plain strings
1540
 
 
 
1402
        
1541
1403
        So Unicode strings must be encoded.
1542
1404
        """
1543
 
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
1405
        uni_val, env_val = probe_unicode_in_user_encoding()
1544
1406
        if uni_val is None:
1545
 
            raise tests.TestSkipped(
1546
 
                'Cannot find a unicode character that works in encoding %s'
1547
 
                % (osutils.get_user_encoding(),))
 
1407
            raise TestSkipped('Cannot find a unicode character that works in'
 
1408
                              ' encoding %s' % (bzrlib.user_encoding,))
1548
1409
 
1549
1410
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1550
1411
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1558
1419
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1559
1420
 
1560
1421
 
1561
 
class TestSizeShaFile(tests.TestCaseInTempDir):
1562
 
 
1563
 
    def test_sha_empty(self):
1564
 
        self.build_tree_contents([('foo', '')])
1565
 
        expected_sha = osutils.sha_string('')
1566
 
        f = open('foo')
1567
 
        self.addCleanup(f.close)
1568
 
        size, sha = osutils.size_sha_file(f)
1569
 
        self.assertEqual(0, size)
1570
 
        self.assertEqual(expected_sha, sha)
1571
 
 
1572
 
    def test_sha_mixed_endings(self):
1573
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1574
 
        self.build_tree_contents([('foo', text)])
1575
 
        expected_sha = osutils.sha_string(text)
1576
 
        f = open('foo')
1577
 
        self.addCleanup(f.close)
1578
 
        size, sha = osutils.size_sha_file(f)
1579
 
        self.assertEqual(38, size)
1580
 
        self.assertEqual(expected_sha, sha)
1581
 
 
1582
 
 
1583
 
class TestShaFileByName(tests.TestCaseInTempDir):
1584
 
 
1585
 
    def test_sha_empty(self):
1586
 
        self.build_tree_contents([('foo', '')])
1587
 
        expected_sha = osutils.sha_string('')
1588
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1589
 
 
1590
 
    def test_sha_mixed_endings(self):
1591
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1592
 
        self.build_tree_contents([('foo', text)])
1593
 
        expected_sha = osutils.sha_string(text)
1594
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1595
 
 
1596
 
 
1597
 
class TestResourceLoading(tests.TestCaseInTempDir):
 
1422
class TestLocalTimeOffset(TestCase):
 
1423
 
 
1424
    def test_local_time_offset(self):
 
1425
        """Test that local_time_offset() returns a sane value."""
 
1426
        offset = osutils.local_time_offset()
 
1427
        self.assertTrue(isinstance(offset, int))
 
1428
        # Test that the offset is no more than a eighteen hours in
 
1429
        # either direction.
 
1430
        # Time zone handling is system specific, so it is difficult to
 
1431
        # do more specific tests, but a value outside of this range is
 
1432
        # probably wrong.
 
1433
        eighteen_hours = 18 * 3600
 
1434
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1435
 
 
1436
    def test_local_time_offset_with_timestamp(self):
 
1437
        """Test that local_time_offset() works with a timestamp."""
 
1438
        offset = osutils.local_time_offset(1000000000.1234567)
 
1439
        self.assertTrue(isinstance(offset, int))
 
1440
        eighteen_hours = 18 * 3600
 
1441
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1442
 
 
1443
 
 
1444
class TestShaFileByName(TestCaseInTempDir):
 
1445
 
 
1446
    def test_sha_empty(self):
 
1447
        self.build_tree_contents([('foo', '')])
 
1448
        expected_sha = osutils.sha_string('')
 
1449
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1450
 
 
1451
    def test_sha_mixed_endings(self):
 
1452
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1453
        self.build_tree_contents([('foo', text)])
 
1454
        expected_sha = osutils.sha_string(text)
 
1455
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
 
1456
 
 
1457
 
 
1458
_debug_text = \
 
1459
r'''# Copyright (C) 2005, 2006 Canonical Ltd
 
1460
#
 
1461
# This program is free software; you can redistribute it and/or modify
 
1462
# it under the terms of the GNU General Public License as published by
 
1463
# the Free Software Foundation; either version 2 of the License, or
 
1464
# (at your option) any later version.
 
1465
#
 
1466
# This program is distributed in the hope that it will be useful,
 
1467
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
1468
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
1469
# GNU General Public License for more details.
 
1470
#
 
1471
# You should have received a copy of the GNU General Public License
 
1472
# along with this program; if not, write to the Free Software
 
1473
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
1474
 
 
1475
 
 
1476
# NOTE: If update these, please also update the help for global-options in
 
1477
#       bzrlib/help_topics/__init__.py
 
1478
 
 
1479
debug_flags = set()
 
1480
"""Set of flags that enable different debug behaviour.
 
1481
 
 
1482
These are set with eg ``-Dlock`` on the bzr command line.
 
1483
 
 
1484
Options include:
 
1485
 
 
1486
 * auth - show authentication sections used
 
1487
 * error - show stack traces for all top level exceptions
 
1488
 * evil - capture call sites that do expensive or badly-scaling operations.
 
1489
 * fetch - trace history copying between repositories
 
1490
 * graph - trace graph traversal information
 
1491
 * hashcache - log every time a working file is read to determine its hash
 
1492
 * hooks - trace hook execution
 
1493
 * hpss - trace smart protocol requests and responses
 
1494
 * http - trace http connections, requests and responses
 
1495
 * index - trace major index operations
 
1496
 * knit - trace knit operations
 
1497
 * lock - trace when lockdir locks are taken or released
 
1498
 * merge - emit information for debugging merges
 
1499
 * pack - emit information about pack operations
 
1500
 
 
1501
"""
 
1502
'''
 
1503
 
 
1504
 
 
1505
class TestResourceLoading(TestCaseInTempDir):
1598
1506
 
1599
1507
    def test_resource_string(self):
1600
1508
        # test resource in bzrlib
1601
1509
        text = osutils.resource_string('bzrlib', 'debug.py')
1602
 
        self.assertContainsRe(text, "debug_flags = set()")
 
1510
        self.assertEquals(_debug_text, text)
1603
1511
        # test resource under bzrlib
1604
1512
        text = osutils.resource_string('bzrlib.ui', 'text.py')
1605
1513
        self.assertContainsRe(text, "class TextUIFactory")
1608
1516
            'yyy.xx')
1609
1517
        # test unknown resource
1610
1518
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1611
 
 
1612
 
 
1613
 
class TestReCompile(tests.TestCase):
1614
 
 
1615
 
    def test_re_compile_checked(self):
1616
 
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1617
 
        self.assertTrue(r.match('aaaa'))
1618
 
        self.assertTrue(r.match('aAaA'))
1619
 
 
1620
 
    def test_re_compile_checked_error(self):
1621
 
        # like https://bugs.launchpad.net/bzr/+bug/251352
1622
 
        err = self.assertRaises(
1623
 
            errors.BzrCommandError,
1624
 
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1625
 
        self.assertEqual(
1626
 
            "Invalid regular expression in test case: '*': "
1627
 
            "nothing to repeat",
1628
 
            str(err))
1629
 
 
1630
 
 
1631
 
class TestDirReader(tests.TestCaseInTempDir):
1632
 
 
1633
 
    # Set by load_tests
1634
 
    _dir_reader_class = None
1635
 
    _native_to_unicode = None
1636
 
 
1637
 
    def setUp(self):
1638
 
        tests.TestCaseInTempDir.setUp(self)
1639
 
 
1640
 
        # Save platform specific info and reset it
1641
 
        cur_dir_reader = osutils._selected_dir_reader
1642
 
 
1643
 
        def restore():
1644
 
            osutils._selected_dir_reader = cur_dir_reader
1645
 
        self.addCleanup(restore)
1646
 
 
1647
 
        osutils._selected_dir_reader = self._dir_reader_class()
1648
 
 
1649
 
    def _get_ascii_tree(self):
1650
 
        tree = [
1651
 
            '0file',
1652
 
            '1dir/',
1653
 
            '1dir/0file',
1654
 
            '1dir/1dir/',
1655
 
            '2file'
1656
 
            ]
1657
 
        expected_dirblocks = [
1658
 
                (('', '.'),
1659
 
                 [('0file', '0file', 'file'),
1660
 
                  ('1dir', '1dir', 'directory'),
1661
 
                  ('2file', '2file', 'file'),
1662
 
                 ]
1663
 
                ),
1664
 
                (('1dir', './1dir'),
1665
 
                 [('1dir/0file', '0file', 'file'),
1666
 
                  ('1dir/1dir', '1dir', 'directory'),
1667
 
                 ]
1668
 
                ),
1669
 
                (('1dir/1dir', './1dir/1dir'),
1670
 
                 [
1671
 
                 ]
1672
 
                ),
1673
 
            ]
1674
 
        return tree, expected_dirblocks
1675
 
 
1676
 
    def test_walk_cur_dir(self):
1677
 
        tree, expected_dirblocks = self._get_ascii_tree()
1678
 
        self.build_tree(tree)
1679
 
        result = list(osutils._walkdirs_utf8('.'))
1680
 
        # Filter out stat and abspath
1681
 
        self.assertEqual(expected_dirblocks,
1682
 
                         [(dirinfo, [line[0:3] for line in block])
1683
 
                          for dirinfo, block in result])
1684
 
 
1685
 
    def test_walk_sub_dir(self):
1686
 
        tree, expected_dirblocks = self._get_ascii_tree()
1687
 
        self.build_tree(tree)
1688
 
        # you can search a subdir only, with a supplied prefix.
1689
 
        result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1690
 
        # Filter out stat and abspath
1691
 
        self.assertEqual(expected_dirblocks[1:],
1692
 
                         [(dirinfo, [line[0:3] for line in block])
1693
 
                          for dirinfo, block in result])
1694
 
 
1695
 
    def _get_unicode_tree(self):
1696
 
        name0u = u'0file-\xb6'
1697
 
        name1u = u'1dir-\u062c\u0648'
1698
 
        name2u = u'2file-\u0633'
1699
 
        tree = [
1700
 
            name0u,
1701
 
            name1u + '/',
1702
 
            name1u + '/' + name0u,
1703
 
            name1u + '/' + name1u + '/',
1704
 
            name2u,
1705
 
            ]
1706
 
        name0 = name0u.encode('UTF-8')
1707
 
        name1 = name1u.encode('UTF-8')
1708
 
        name2 = name2u.encode('UTF-8')
1709
 
        expected_dirblocks = [
1710
 
                (('', '.'),
1711
 
                 [(name0, name0, 'file', './' + name0u),
1712
 
                  (name1, name1, 'directory', './' + name1u),
1713
 
                  (name2, name2, 'file', './' + name2u),
1714
 
                 ]
1715
 
                ),
1716
 
                ((name1, './' + name1u),
1717
 
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1718
 
                                                        + '/' + name0u),
1719
 
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1720
 
                                                            + '/' + name1u),
1721
 
                 ]
1722
 
                ),
1723
 
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1724
 
                 [
1725
 
                 ]
1726
 
                ),
1727
 
            ]
1728
 
        return tree, expected_dirblocks
1729
 
 
1730
 
    def _filter_out(self, raw_dirblocks):
1731
 
        """Filter out a walkdirs_utf8 result.
1732
 
 
1733
 
        stat field is removed, all native paths are converted to unicode
1734
 
        """
1735
 
        filtered_dirblocks = []
1736
 
        for dirinfo, block in raw_dirblocks:
1737
 
            dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1738
 
            details = []
1739
 
            for line in block:
1740
 
                details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1741
 
            filtered_dirblocks.append((dirinfo, details))
1742
 
        return filtered_dirblocks
1743
 
 
1744
 
    def test_walk_unicode_tree(self):
1745
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1746
 
        tree, expected_dirblocks = self._get_unicode_tree()
1747
 
        self.build_tree(tree)
1748
 
        result = list(osutils._walkdirs_utf8('.'))
1749
 
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1750
 
 
1751
 
    def test_symlink(self):
1752
 
        self.requireFeature(tests.SymlinkFeature)
1753
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1754
 
        target = u'target\N{Euro Sign}'
1755
 
        link_name = u'l\N{Euro Sign}nk'
1756
 
        os.symlink(target, link_name)
1757
 
        target_utf8 = target.encode('UTF-8')
1758
 
        link_name_utf8 = link_name.encode('UTF-8')
1759
 
        expected_dirblocks = [
1760
 
                (('', '.'),
1761
 
                 [(link_name_utf8, link_name_utf8,
1762
 
                   'symlink', './' + link_name),],
1763
 
                 )]
1764
 
        result = list(osutils._walkdirs_utf8('.'))
1765
 
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1766
 
 
1767
 
 
1768
 
class TestReadLink(tests.TestCaseInTempDir):
1769
 
    """Exposes os.readlink() problems and the osutils solution.
1770
 
 
1771
 
    The only guarantee offered by os.readlink(), starting with 2.6, is that a
1772
 
    unicode string will be returned if a unicode string is passed.
1773
 
 
1774
 
    But prior python versions failed to properly encode the passed unicode
1775
 
    string.
1776
 
    """
1777
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1778
 
 
1779
 
    def setUp(self):
1780
 
        super(tests.TestCaseInTempDir, self).setUp()
1781
 
        self.link = u'l\N{Euro Sign}ink'
1782
 
        self.target = u'targe\N{Euro Sign}t'
1783
 
        os.symlink(self.target, self.link)
1784
 
 
1785
 
    def test_os_readlink_link_encoding(self):
1786
 
        if sys.version_info < (2, 6):
1787
 
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1788
 
        else:
1789
 
            self.assertEquals(self.target,  os.readlink(self.link))
1790
 
 
1791
 
    def test_os_readlink_link_decoding(self):
1792
 
        self.assertEquals(self.target.encode(osutils._fs_enc),
1793
 
                          os.readlink(self.link.encode(osutils._fs_enc)))
1794
 
 
1795
 
 
1796
 
class TestConcurrency(tests.TestCase):
1797
 
 
1798
 
    def test_local_concurrency(self):
1799
 
        concurrency = osutils.local_concurrency()
1800
 
        self.assertIsInstance(concurrency, int)