~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
 
from cStringIO import StringIO
20
19
import errno
21
20
import os
22
21
import socket
23
22
import stat
24
23
import sys
25
 
import time
26
24
 
27
25
import bzrlib
28
26
from bzrlib import (
29
27
    errors,
30
28
    osutils,
31
 
    tests,
32
29
    win32utils,
33
30
    )
34
31
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
 
        )
41
32
from bzrlib.tests import (
42
 
        probe_unicode_in_user_encoding,
43
33
        StringIOWrapper,
44
 
        SymlinkFeature,
45
 
        TestCase,
46
 
        TestCaseInTempDir,
 
34
        TestCase, 
 
35
        TestCaseInTempDir, 
47
36
        TestSkipped,
48
37
        )
49
 
from bzrlib.tests.file_utils import (
50
 
    FakeReadFile,
51
 
    )
52
 
from bzrlib.tests.test__walkdirs_win32 import WalkdirsWin32Feature
53
38
 
54
39
 
55
40
class TestOSUtils(TestCaseInTempDir):
101
86
 
102
87
    # TODO: test fancy_rename using a MemoryTransport
103
88
 
104
 
    def test_rename_change_case(self):
105
 
        # on Windows we should be able to change filename case by rename
106
 
        self.build_tree(['a', 'b/'])
107
 
        osutils.rename('a', 'A')
108
 
        osutils.rename('b', 'B')
109
 
        # we can't use failUnlessExists on case-insensitive filesystem
110
 
        # so try to check shape of the tree
111
 
        shape = sorted(os.listdir('.'))
112
 
        self.assertEquals(['A', 'B'], shape)
113
 
 
114
89
    def test_01_rand_chars_empty(self):
115
90
        result = osutils.rand_chars(0)
116
91
        self.assertEqual(result, '')
130
105
        self.assertFalse(is_inside('foo.c', ''))
131
106
        self.assertTrue(is_inside('', 'foo.c'))
132
107
 
133
 
    def test_is_inside_any(self):
134
 
        SRC_FOO_C = pathjoin('src', 'foo.c')
135
 
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
136
 
                         (['src'], SRC_FOO_C),
137
 
                         (['src'], 'src'),
138
 
                         ]:
139
 
            self.assert_(is_inside_any(dirs, fn))
140
 
        for dirs, fn in [(['src'], 'srccontrol'),
141
 
                         (['src'], 'srccontrol/foo')]:
142
 
            self.assertFalse(is_inside_any(dirs, fn))
143
 
 
144
 
    def test_is_inside_or_parent_of_any(self):
145
 
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
146
 
                         (['src'], 'src/foo.c'),
147
 
                         (['src/bar.c'], 'src'),
148
 
                         (['src/bar.c', 'bla/foo.c'], 'src'),
149
 
                         (['src'], 'src'),
150
 
                         ]:
151
 
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
152
 
            
153
 
        for dirs, fn in [(['src'], 'srccontrol'),
154
 
                         (['srccontrol/foo.c'], 'src'),
155
 
                         (['src'], 'srccontrol/foo')]:
156
 
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
157
 
 
158
108
    def test_rmtree(self):
159
109
        # Check to remove tree with read-only files/dirs
160
110
        os.mkdir('dir')
268
218
        self.assertFormatedDelta('1 second in the future', -1)
269
219
        self.assertFormatedDelta('2 seconds in the future', -2)
270
220
 
271
 
    def test_format_date(self):
272
 
        self.assertRaises(errors.UnsupportedTimezoneFormat,
273
 
            osutils.format_date, 0, timezone='foo')
274
 
 
275
221
    def test_dereference_path(self):
276
 
        self.requireFeature(SymlinkFeature)
 
222
        if not osutils.has_symlinks():
 
223
            raise TestSkipped('Symlinks are not supported on this platform')
277
224
        cwd = osutils.realpath('.')
278
225
        os.mkdir('bar')
279
226
        bar_path = osutils.pathjoin(cwd, 'bar')
299
246
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
300
247
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
301
248
 
302
 
    def test_changing_access(self):
303
 
        f = file('file', 'w')
304
 
        f.write('monkey')
305
 
        f.close()
306
 
 
307
 
        # Make a file readonly
308
 
        osutils.make_readonly('file')
309
 
        mode = os.lstat('file').st_mode
310
 
        self.assertEqual(mode, mode & 0777555)
311
 
 
312
 
        # Make a file writable
313
 
        osutils.make_writable('file')
314
 
        mode = os.lstat('file').st_mode
315
 
        self.assertEqual(mode, mode | 0200)
316
 
 
317
 
        if osutils.has_symlinks():
318
 
            # should not error when handed a symlink
319
 
            os.symlink('nonexistent', 'dangling')
320
 
            osutils.make_readonly('dangling')
321
 
            osutils.make_writable('dangling')
322
249
 
323
250
    def test_kind_marker(self):
324
251
        self.assertEqual("", osutils.kind_marker("file"))
326
253
        self.assertEqual("@", osutils.kind_marker("symlink"))
327
254
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
328
255
 
329
 
    def test_host_os_dereferences_symlinks(self):
330
 
        osutils.host_os_dereferences_symlinks()
331
 
 
332
 
 
333
 
class TestPumpFile(TestCase):
334
 
    """Test pumpfile method."""
335
 
    def setUp(self):
336
 
        # create a test datablock
337
 
        self.block_size = 512
338
 
        pattern = '0123456789ABCDEF'
339
 
        self.test_data = pattern * (3 * self.block_size / len(pattern))
340
 
        self.test_data_len = len(self.test_data)
341
 
 
342
 
    def test_bracket_block_size(self):
343
 
        """Read data in blocks with the requested read size bracketing the
344
 
        block size."""
345
 
        # make sure test data is larger than max read size
346
 
        self.assertTrue(self.test_data_len > self.block_size)
347
 
 
348
 
        from_file = FakeReadFile(self.test_data)
349
 
        to_file = StringIO()
350
 
 
351
 
        # read (max / 2) bytes and verify read size wasn't affected
352
 
        num_bytes_to_read = self.block_size / 2
353
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
354
 
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
355
 
        self.assertEqual(from_file.get_read_count(), 1)
356
 
 
357
 
        # read (max) bytes and verify read size wasn't affected
358
 
        num_bytes_to_read = self.block_size
359
 
        from_file.reset_read_count()
360
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
361
 
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
362
 
        self.assertEqual(from_file.get_read_count(), 1)
363
 
 
364
 
        # read (max + 1) bytes and verify read size was limited
365
 
        num_bytes_to_read = self.block_size + 1
366
 
        from_file.reset_read_count()
367
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
368
 
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
369
 
        self.assertEqual(from_file.get_read_count(), 2)
370
 
 
371
 
        # finish reading the rest of the data
372
 
        num_bytes_to_read = self.test_data_len - to_file.tell()
373
 
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
374
 
 
375
 
        # report error if the data wasn't equal (we only report the size due
376
 
        # to the length of the data)
377
 
        response_data = to_file.getvalue()
378
 
        if response_data != self.test_data:
379
 
            message = "Data not equal.  Expected %d bytes, received %d."
380
 
            self.fail(message % (len(response_data), self.test_data_len))
381
 
 
382
 
    def test_specified_size(self):
383
 
        """Request a transfer larger than the maximum block size and verify
384
 
        that the maximum read doesn't exceed the block_size."""
385
 
        # make sure test data is larger than max read size
386
 
        self.assertTrue(self.test_data_len > self.block_size)
387
 
 
388
 
        # retrieve data in blocks
389
 
        from_file = FakeReadFile(self.test_data)
390
 
        to_file = StringIO()
391
 
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
392
 
 
393
 
        # verify read size was equal to the maximum read size
394
 
        self.assertTrue(from_file.get_max_read_size() > 0)
395
 
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
396
 
        self.assertEqual(from_file.get_read_count(), 3)
397
 
 
398
 
        # report error if the data wasn't equal (we only report the size due
399
 
        # to the length of the data)
400
 
        response_data = to_file.getvalue()
401
 
        if response_data != self.test_data:
402
 
            message = "Data not equal.  Expected %d bytes, received %d."
403
 
            self.fail(message % (len(response_data), self.test_data_len))
404
 
 
405
 
    def test_to_eof(self):
406
 
        """Read to end-of-file and verify that the reads are not larger than
407
 
        the maximum read size."""
408
 
        # make sure test data is larger than max read size
409
 
        self.assertTrue(self.test_data_len > self.block_size)
410
 
 
411
 
        # retrieve data to EOF
412
 
        from_file = FakeReadFile(self.test_data)
413
 
        to_file = StringIO()
414
 
        pumpfile(from_file, to_file, -1, self.block_size)
415
 
 
416
 
        # verify read size was equal to the maximum read size
417
 
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
418
 
        self.assertEqual(from_file.get_read_count(), 4)
419
 
 
420
 
        # report error if the data wasn't equal (we only report the size due
421
 
        # to the length of the data)
422
 
        response_data = to_file.getvalue()
423
 
        if response_data != self.test_data:
424
 
            message = "Data not equal.  Expected %d bytes, received %d."
425
 
            self.fail(message % (len(response_data), self.test_data_len))
426
 
 
427
 
    def test_defaults(self):
428
 
        """Verifies that the default arguments will read to EOF -- this
429
 
        test verifies that any existing usages of pumpfile will not be broken
430
 
        with this new version."""
431
 
        # retrieve data using default (old) pumpfile method
432
 
        from_file = FakeReadFile(self.test_data)
433
 
        to_file = StringIO()
434
 
        pumpfile(from_file, to_file)
435
 
 
436
 
        # report error if the data wasn't equal (we only report the size due
437
 
        # to the length of the data)
438
 
        response_data = to_file.getvalue()
439
 
        if response_data != self.test_data:
440
 
            message = "Data not equal.  Expected %d bytes, received %d."
441
 
            self.fail(message % (len(response_data), self.test_data_len))
442
256
 
443
257
class TestSafeUnicode(TestCase):
444
258
 
483
297
class TestSafeRevisionId(TestCase):
484
298
 
485
299
    def test_from_ascii_string(self):
486
 
        # this shouldn't give a warning because it's getting an ascii string
487
300
        self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
488
301
 
489
302
    def test_from_unicode_string_ascii_contents(self):
611
424
        #       osutils.getcwd() renormalize the path.
612
425
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
613
426
 
614
 
    def test_minimum_path_selection(self):
615
 
        self.assertEqual(set(),
616
 
            osutils.minimum_path_selection([]))
617
 
        self.assertEqual(set(['a', 'b']),
618
 
            osutils.minimum_path_selection(['a', 'b']))
619
 
        self.assertEqual(set(['a/', 'b']),
620
 
            osutils.minimum_path_selection(['a/', 'b']))
621
 
        self.assertEqual(set(['a/', 'b']),
622
 
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
623
 
 
624
427
    def test_mkdtemp(self):
625
428
        tmpdir = osutils._win32_mkdtemp(dir='.')
626
429
        self.assertFalse('\\' in tmpdir)
822
625
                new_dirblock.append((info[0], info[1], info[2], info[4]))
823
626
            dirblock[:] = new_dirblock
824
627
 
825
 
    def test__walkdirs_utf8_selection(self):
826
 
        # Just trigger the function once, to make sure it has selected a real
827
 
        # implementation.
828
 
        list(osutils._walkdirs_utf8('.'))
829
 
        if WalkdirsWin32Feature.available():
830
 
            # If the compiled form is available, make sure it is used
831
 
            from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
832
 
            self.assertIs(_walkdirs_utf8_win32_find_file,
833
 
                          osutils._real_walkdirs_utf8)
834
 
        elif sys.platform == 'win32':
835
 
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
836
 
                          osutils._real_walkdirs_utf8)
837
 
        elif osutils._fs_enc.upper() in ('UTF-8', 'ASCII', 'ANSI_X3.4-1968'): # ascii
838
 
            self.assertIs(osutils._walkdirs_fs_utf8,
839
 
                          osutils._real_walkdirs_utf8)
840
 
        else:
841
 
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
842
 
                          osutils._real_walkdirs_utf8)
843
 
 
844
 
    def _save_platform_info(self):
845
 
        cur_winver = win32utils.winver
846
 
        cur_fs_enc = osutils._fs_enc
847
 
        cur_real_walkdirs_utf8 = osutils._real_walkdirs_utf8
848
 
        def restore():
849
 
            win32utils.winver = cur_winver
850
 
            osutils._fs_enc = cur_fs_enc
851
 
            osutils._real_walkdirs_utf8 = cur_real_walkdirs_utf8
852
 
        self.addCleanup(restore)
853
 
 
854
 
    def assertWalkdirsUtf8Is(self, expected):
855
 
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
856
 
        # Force it to redetect
857
 
        osutils._real_walkdirs_utf8 = None
858
 
        # Nothing to list, but should still trigger the selection logic
859
 
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
860
 
        self.assertIs(expected, osutils._real_walkdirs_utf8)
861
 
 
862
 
    def test_force_walkdirs_utf8_fs_utf8(self):
863
 
        self._save_platform_info()
864
 
        win32utils.winver = None # Avoid the win32 detection code
865
 
        osutils._fs_enc = 'UTF-8'
866
 
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
867
 
 
868
 
    def test_force_walkdirs_utf8_fs_ascii(self):
869
 
        self._save_platform_info()
870
 
        win32utils.winver = None # Avoid the win32 detection code
871
 
        osutils._fs_enc = 'US-ASCII'
872
 
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
873
 
 
874
 
    def test_force_walkdirs_utf8_fs_ANSI(self):
875
 
        self._save_platform_info()
876
 
        win32utils.winver = None # Avoid the win32 detection code
877
 
        osutils._fs_enc = 'ANSI_X3.4-1968'
878
 
        self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
879
 
 
880
 
    def test_force_walkdirs_utf8_fs_latin1(self):
881
 
        self._save_platform_info()
882
 
        win32utils.winver = None # Avoid the win32 detection code
883
 
        osutils._fs_enc = 'latin1'
884
 
        self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
885
 
 
886
 
    def test_force_walkdirs_utf8_nt(self):
887
 
        self.requireFeature(WalkdirsWin32Feature)
888
 
        self._save_platform_info()
889
 
        win32utils.winver = 'Windows NT'
890
 
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
891
 
        self.assertWalkdirsUtf8Is(_walkdirs_utf8_win32_find_file)
892
 
 
893
 
    def test_force_walkdirs_utf8_nt(self):
894
 
        self.requireFeature(WalkdirsWin32Feature)
895
 
        self._save_platform_info()
896
 
        win32utils.winver = 'Windows 98'
897
 
        self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
898
 
 
899
628
    def test_unicode_walkdirs(self):
900
629
        """Walkdirs should always return unicode paths."""
901
630
        name0 = u'0file-\xb6'
1052
781
        self._filter_out_stat(result)
1053
782
        self.assertEqual(expected_dirblocks, result)
1054
783
 
1055
 
    def test__walkdirs_utf_win32_find_file(self):
1056
 
        self.requireFeature(WalkdirsWin32Feature)
1057
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1058
 
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1059
 
        name0u = u'0file-\xb6'
1060
 
        name1u = u'1dir-\u062c\u0648'
1061
 
        name2u = u'2file-\u0633'
1062
 
        tree = [
1063
 
            name0u,
1064
 
            name1u + '/',
1065
 
            name1u + '/' + name0u,
1066
 
            name1u + '/' + name1u + '/',
1067
 
            name2u,
1068
 
            ]
1069
 
        self.build_tree(tree)
1070
 
        name0 = name0u.encode('utf8')
1071
 
        name1 = name1u.encode('utf8')
1072
 
        name2 = name2u.encode('utf8')
1073
 
 
1074
 
        # All of the abspaths should be in unicode, all of the relative paths
1075
 
        # should be in utf8
1076
 
        expected_dirblocks = [
1077
 
                (('', '.'),
1078
 
                 [(name0, name0, 'file', './' + name0u),
1079
 
                  (name1, name1, 'directory', './' + name1u),
1080
 
                  (name2, name2, 'file', './' + name2u),
1081
 
                 ]
1082
 
                ),
1083
 
                ((name1, './' + name1u),
1084
 
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1085
 
                                                        + '/' + name0u),
1086
 
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1087
 
                                                            + '/' + name1u),
1088
 
                 ]
1089
 
                ),
1090
 
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1091
 
                 [
1092
 
                 ]
1093
 
                ),
1094
 
            ]
1095
 
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1096
 
        self._filter_out_stat(result)
1097
 
        self.assertEqual(expected_dirblocks, result)
1098
 
 
1099
 
    def assertStatIsCorrect(self, path, win32stat):
1100
 
        os_stat = os.stat(path)
1101
 
        self.assertEqual(os_stat.st_size, win32stat.st_size)
1102
 
        self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
1103
 
        self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
1104
 
        self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
1105
 
        self.assertEqual(os_stat.st_dev, win32stat.st_dev)
1106
 
        self.assertEqual(os_stat.st_ino, win32stat.st_ino)
1107
 
        self.assertEqual(os_stat.st_mode, win32stat.st_mode)
1108
 
 
1109
 
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1110
 
        """make sure our Stat values are valid"""
1111
 
        self.requireFeature(WalkdirsWin32Feature)
1112
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1113
 
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1114
 
        name0u = u'0file-\xb6'
1115
 
        name0 = name0u.encode('utf8')
1116
 
        self.build_tree([name0u])
1117
 
        # I hate to sleep() here, but I'm trying to make the ctime different
1118
 
        # from the mtime
1119
 
        time.sleep(2)
1120
 
        f = open(name0u, 'ab')
1121
 
        try:
1122
 
            f.write('just a small update')
1123
 
        finally:
1124
 
            f.close()
1125
 
 
1126
 
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1127
 
        entry = result[0][1][0]
1128
 
        self.assertEqual((name0, name0, 'file'), entry[:3])
1129
 
        self.assertEqual(u'./' + name0u, entry[4])
1130
 
        self.assertStatIsCorrect(entry[4], entry[3])
1131
 
        self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
1132
 
 
1133
 
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1134
 
        """make sure our Stat values are valid"""
1135
 
        self.requireFeature(WalkdirsWin32Feature)
1136
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1137
 
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1138
 
        name0u = u'0dir-\u062c\u0648'
1139
 
        name0 = name0u.encode('utf8')
1140
 
        self.build_tree([name0u + '/'])
1141
 
 
1142
 
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1143
 
        entry = result[0][1][0]
1144
 
        self.assertEqual((name0, name0, 'directory'), entry[:3])
1145
 
        self.assertEqual(u'./' + name0u, entry[4])
1146
 
        self.assertStatIsCorrect(entry[4], entry[3])
1147
 
 
1148
784
    def assertPathCompare(self, path_less, path_greater):
1149
785
        """check that path_less and path_greater compare correctly."""
1150
786
        self.assertEqual(0, osutils.compare_paths_prefix_order(
1239
875
        self.assertEqual(['c'], os.listdir('target/b'))
1240
876
 
1241
877
    def test_copy_tree_symlinks(self):
1242
 
        self.requireFeature(SymlinkFeature)
 
878
        if not osutils.has_symlinks():
 
879
            return
1243
880
        self.build_tree(['source/'])
1244
881
        os.symlink('a/generic/path', 'source/lnk')
1245
882
        osutils.copy_tree('source', 'target')
1312
949
        
1313
950
        So Unicode strings must be encoded.
1314
951
        """
1315
 
        uni_val, env_val = probe_unicode_in_user_encoding()
1316
 
        if uni_val is None:
 
952
        # Try a few different characters, to see if we can get
 
953
        # one that will be valid in the user_encoding
 
954
        possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
 
955
        for uni_val in possible_vals:
 
956
            try:
 
957
                env_val = uni_val.encode(bzrlib.user_encoding)
 
958
            except UnicodeEncodeError:
 
959
                # Try a different character
 
960
                pass
 
961
            else:
 
962
                break
 
963
        else:
1317
964
            raise TestSkipped('Cannot find a unicode character that works in'
1318
965
                              ' encoding %s' % (bzrlib.user_encoding,))
1319
966
 
1349
996
        self.assertTrue(isinstance(offset, int))
1350
997
        eighteen_hours = 18 * 3600
1351
998
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1352
 
 
1353
 
 
1354
 
class TestShaFileByName(TestCaseInTempDir):
1355
 
 
1356
 
    def test_sha_empty(self):
1357
 
        self.build_tree_contents([('foo', '')])
1358
 
        expected_sha = osutils.sha_string('')
1359
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1360
 
 
1361
 
    def test_sha_mixed_endings(self):
1362
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1363
 
        self.build_tree_contents([('foo', text)])
1364
 
        expected_sha = osutils.sha_string(text)
1365
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1366
 
 
1367
 
 
1368
 
_debug_text = \
1369
 
r'''# Copyright (C) 2005, 2006 Canonical Ltd
1370
 
#
1371
 
# This program is free software; you can redistribute it and/or modify
1372
 
# it under the terms of the GNU General Public License as published by
1373
 
# the Free Software Foundation; either version 2 of the License, or
1374
 
# (at your option) any later version.
1375
 
#
1376
 
# This program is distributed in the hope that it will be useful,
1377
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1378
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1379
 
# GNU General Public License for more details.
1380
 
#
1381
 
# You should have received a copy of the GNU General Public License
1382
 
# along with this program; if not, write to the Free Software
1383
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1384
 
 
1385
 
 
1386
 
# NOTE: If update these, please also update the help for global-options in
1387
 
#       bzrlib/help_topics/__init__.py
1388
 
 
1389
 
debug_flags = set()
1390
 
"""Set of flags that enable different debug behaviour.
1391
 
 
1392
 
These are set with eg ``-Dlock`` on the bzr command line.
1393
 
 
1394
 
Options include:
1395
 
 
1396
 
 * auth - show authentication sections used
1397
 
 * error - show stack traces for all top level exceptions
1398
 
 * evil - capture call sites that do expensive or badly-scaling operations.
1399
 
 * fetch - trace history copying between repositories
1400
 
 * graph - trace graph traversal information
1401
 
 * hashcache - log every time a working file is read to determine its hash
1402
 
 * hooks - trace hook execution
1403
 
 * hpss - trace smart protocol requests and responses
1404
 
 * http - trace http connections, requests and responses
1405
 
 * index - trace major index operations
1406
 
 * knit - trace knit operations
1407
 
 * lock - trace when lockdir locks are taken or released
1408
 
 * merge - emit information for debugging merges
1409
 
 * pack - emit information about pack operations
1410
 
 
1411
 
"""
1412
 
'''
1413
 
 
1414
 
 
1415
 
class TestResourceLoading(TestCaseInTempDir):
1416
 
 
1417
 
    def test_resource_string(self):
1418
 
        # test resource in bzrlib
1419
 
        text = osutils.resource_string('bzrlib', 'debug.py')
1420
 
        self.assertEquals(_debug_text, text)
1421
 
        # test resource under bzrlib
1422
 
        text = osutils.resource_string('bzrlib.ui', 'text.py')
1423
 
        self.assertContainsRe(text, "class TextUIFactory")
1424
 
        # test unsupported package
1425
 
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1426
 
            'yyy.xx')
1427
 
        # test unknown resource
1428
 
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')