~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Alexander Belchenko
  • Date: 2007-01-30 23:05:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2259.
  • Revision ID: bialix@ukr.net-20070130230535-kx1rd478rtigyc3v
standalone installer: win98 support

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
26
26
from bzrlib import (
27
27
    errors,
28
28
    osutils,
29
 
    win32utils,
30
29
    )
31
30
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
32
 
from bzrlib.osutils import (
33
 
        is_inside_any,
34
 
        is_inside_or_parent_of_any,
35
 
        pathjoin,
36
 
        )
37
31
from bzrlib.tests import (
38
 
        probe_unicode_in_user_encoding,
39
32
        StringIOWrapper,
40
 
        SymlinkFeature,
41
 
        TestCase,
42
 
        TestCaseInTempDir,
 
33
        TestCase, 
 
34
        TestCaseInTempDir, 
43
35
        TestSkipped,
44
36
        )
45
37
 
46
38
 
47
39
class TestOSUtils(TestCaseInTempDir):
48
40
 
49
 
    def test_contains_whitespace(self):
50
 
        self.failUnless(osutils.contains_whitespace(u' '))
51
 
        self.failUnless(osutils.contains_whitespace(u'hello there'))
52
 
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
53
 
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
54
 
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
55
 
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
56
 
 
57
 
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
58
 
        # is whitespace, but we do not.
59
 
        self.failIf(osutils.contains_whitespace(u''))
60
 
        self.failIf(osutils.contains_whitespace(u'hellothere'))
61
 
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
62
 
 
63
41
    def test_fancy_rename(self):
64
42
        # This should work everywhere
65
43
        def rename(a, b):
93
71
 
94
72
    # TODO: test fancy_rename using a MemoryTransport
95
73
 
96
 
    def test_rename_change_case(self):
97
 
        # on Windows we should be able to change filename case by rename
98
 
        self.build_tree(['a', 'b/'])
99
 
        osutils.rename('a', 'A')
100
 
        osutils.rename('b', 'B')
101
 
        # we can't use failUnlessExists on case-insensitive filesystem
102
 
        # so try to check shape of the tree
103
 
        shape = sorted(os.listdir('.'))
104
 
        self.assertEquals(['A', 'B'], shape)
105
 
 
106
74
    def test_01_rand_chars_empty(self):
107
75
        result = osutils.rand_chars(0)
108
76
        self.assertEqual(result, '')
122
90
        self.assertFalse(is_inside('foo.c', ''))
123
91
        self.assertTrue(is_inside('', 'foo.c'))
124
92
 
125
 
    def test_is_inside_any(self):
126
 
        SRC_FOO_C = pathjoin('src', 'foo.c')
127
 
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
128
 
                         (['src'], SRC_FOO_C),
129
 
                         (['src'], 'src'),
130
 
                         ]:
131
 
            self.assert_(is_inside_any(dirs, fn))
132
 
        for dirs, fn in [(['src'], 'srccontrol'),
133
 
                         (['src'], 'srccontrol/foo')]:
134
 
            self.assertFalse(is_inside_any(dirs, fn))
135
 
 
136
 
    def test_is_inside_or_parent_of_any(self):
137
 
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
138
 
                         (['src'], 'src/foo.c'),
139
 
                         (['src/bar.c'], 'src'),
140
 
                         (['src/bar.c', 'bla/foo.c'], 'src'),
141
 
                         (['src'], 'src'),
142
 
                         ]:
143
 
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
144
 
            
145
 
        for dirs, fn in [(['src'], 'srccontrol'),
146
 
                         (['srccontrol/foo.c'], 'src'),
147
 
                         (['src'], 'srccontrol/foo')]:
148
 
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
149
 
 
150
93
    def test_rmtree(self):
151
94
        # Check to remove tree with read-only files/dirs
152
95
        os.mkdir('dir')
199
142
            finally:
200
143
                os.remove('socket')
201
144
 
202
 
    def test_kind_marker(self):
203
 
        self.assertEqual(osutils.kind_marker('file'), '')
204
 
        self.assertEqual(osutils.kind_marker('directory'), '/')
205
 
        self.assertEqual(osutils.kind_marker('symlink'), '@')
206
 
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
207
 
 
208
145
    def test_get_umask(self):
209
146
        if sys.platform == 'win32':
210
147
            # umask always returns '0', no way to set it
260
197
        self.assertFormatedDelta('1 second in the future', -1)
261
198
        self.assertFormatedDelta('2 seconds in the future', -2)
262
199
 
263
 
    def test_format_date(self):
264
 
        self.assertRaises(errors.UnsupportedTimezoneFormat,
265
 
            osutils.format_date, 0, timezone='foo')
266
 
 
267
200
    def test_dereference_path(self):
268
 
        self.requireFeature(SymlinkFeature)
 
201
        if not osutils.has_symlinks():
 
202
            raise TestSkipped('Symlinks are not supported on this platform')
269
203
        cwd = osutils.realpath('.')
270
204
        os.mkdir('bar')
271
205
        bar_path = osutils.pathjoin(cwd, 'bar')
291
225
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
292
226
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
293
227
 
294
 
    def test_changing_access(self):
295
 
        f = file('file', 'w')
296
 
        f.write('monkey')
297
 
        f.close()
298
 
 
299
 
        # Make a file readonly
300
 
        osutils.make_readonly('file')
301
 
        mode = os.lstat('file').st_mode
302
 
        self.assertEqual(mode, mode & 0777555)
303
 
 
304
 
        # Make a file writable
305
 
        osutils.make_writable('file')
306
 
        mode = os.lstat('file').st_mode
307
 
        self.assertEqual(mode, mode | 0200)
308
 
 
309
 
        if osutils.has_symlinks():
310
 
            # should not error when handed a symlink
311
 
            os.symlink('nonexistent', 'dangling')
312
 
            osutils.make_readonly('dangling')
313
 
            osutils.make_writable('dangling')
314
 
 
315
 
    def test_kind_marker(self):
316
 
        self.assertEqual("", osutils.kind_marker("file"))
317
 
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
318
 
        self.assertEqual("@", osutils.kind_marker("symlink"))
319
 
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
320
 
 
321
228
 
322
229
class TestSafeUnicode(TestCase):
323
230
 
339
246
                          '\xbb\xbb')
340
247
 
341
248
 
342
 
class TestSafeUtf8(TestCase):
343
 
 
344
 
    def test_from_ascii_string(self):
345
 
        f = 'foobar'
346
 
        self.assertEqual('foobar', osutils.safe_utf8(f))
347
 
 
348
 
    def test_from_unicode_string_ascii_contents(self):
349
 
        self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
350
 
 
351
 
    def test_from_unicode_string_unicode_contents(self):
352
 
        self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
353
 
 
354
 
    def test_from_utf8_string(self):
355
 
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
356
 
 
357
 
    def test_bad_utf8_string(self):
358
 
        self.assertRaises(BzrBadParameterNotUnicode,
359
 
                          osutils.safe_utf8, '\xbb\xbb')
360
 
 
361
 
 
362
 
class TestSafeRevisionId(TestCase):
363
 
 
364
 
    def test_from_ascii_string(self):
365
 
        # this shouldn't give a warning because it's getting an ascii string
366
 
        self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
367
 
 
368
 
    def test_from_unicode_string_ascii_contents(self):
369
 
        self.assertEqual('bargam',
370
 
                         osutils.safe_revision_id(u'bargam', warn=False))
371
 
 
372
 
    def test_from_unicode_deprecated(self):
373
 
        self.assertEqual('bargam',
374
 
            self.callDeprecated([osutils._revision_id_warning],
375
 
                                osutils.safe_revision_id, u'bargam'))
376
 
 
377
 
    def test_from_unicode_string_unicode_contents(self):
378
 
        self.assertEqual('bargam\xc2\xae',
379
 
                         osutils.safe_revision_id(u'bargam\xae', warn=False))
380
 
 
381
 
    def test_from_utf8_string(self):
382
 
        self.assertEqual('foo\xc2\xae',
383
 
                         osutils.safe_revision_id('foo\xc2\xae'))
384
 
 
385
 
    def test_none(self):
386
 
        """Currently, None is a valid revision_id"""
387
 
        self.assertEqual(None, osutils.safe_revision_id(None))
388
 
 
389
 
 
390
 
class TestSafeFileId(TestCase):
391
 
 
392
 
    def test_from_ascii_string(self):
393
 
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
394
 
 
395
 
    def test_from_unicode_string_ascii_contents(self):
396
 
        self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
397
 
 
398
 
    def test_from_unicode_deprecated(self):
399
 
        self.assertEqual('bargam',
400
 
            self.callDeprecated([osutils._file_id_warning],
401
 
                                osutils.safe_file_id, u'bargam'))
402
 
 
403
 
    def test_from_unicode_string_unicode_contents(self):
404
 
        self.assertEqual('bargam\xc2\xae',
405
 
                         osutils.safe_file_id(u'bargam\xae', warn=False))
406
 
 
407
 
    def test_from_utf8_string(self):
408
 
        self.assertEqual('foo\xc2\xae',
409
 
                         osutils.safe_file_id('foo\xc2\xae'))
410
 
 
411
 
    def test_none(self):
412
 
        """Currently, None is a valid revision_id"""
413
 
        self.assertEqual(None, osutils.safe_file_id(None))
414
 
 
415
 
 
416
249
class TestWin32Funcs(TestCase):
417
250
    """Test that the _win32 versions of os utilities return appropriate paths."""
418
251
 
419
252
    def test_abspath(self):
420
253
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
421
254
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
422
 
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
423
 
        self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
424
255
 
425
256
    def test_realpath(self):
426
257
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
454
285
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
455
286
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
456
287
 
457
 
    def test_win98_abspath(self):
458
 
        # absolute path
459
 
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
460
 
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
461
 
        # UNC path
462
 
        self.assertEqual('//HOST/path', osutils._win98_abspath(r'\\HOST\path'))
463
 
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
464
 
        # relative path
465
 
        cwd = osutils.getcwd().rstrip('/')
466
 
        drive = osutils._nt_splitdrive(cwd)[0]
467
 
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
468
 
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
469
 
        # unicode path
470
 
        u = u'\u1234'
471
 
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
472
 
 
473
288
 
474
289
class TestWin32FuncsDirs(TestCaseInTempDir):
475
290
    """Test win32 functions that create files."""
476
291
    
477
292
    def test_getcwd(self):
478
 
        if win32utils.winver == 'Windows 98':
479
 
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
480
293
        # Make sure getcwd can handle unicode filenames
481
294
        try:
482
295
            os.mkdir(u'mu-\xb5')
490
303
        #       osutils.getcwd() renormalize the path.
491
304
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
492
305
 
493
 
    def test_minimum_path_selection(self):
494
 
        self.assertEqual(set(),
495
 
            osutils.minimum_path_selection([]))
496
 
        self.assertEqual(set(['a', 'b']),
497
 
            osutils.minimum_path_selection(['a', 'b']))
498
 
        self.assertEqual(set(['a/', 'b']),
499
 
            osutils.minimum_path_selection(['a/', 'b']))
500
 
        self.assertEqual(set(['a/', 'b']),
501
 
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
502
 
 
503
306
    def test_mkdtemp(self):
504
307
        tmpdir = osutils._win32_mkdtemp(dir='.')
505
308
        self.assertFalse('\\' in tmpdir)
646
449
        self.assertEqual(expected_dirblocks[1:],
647
450
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
648
451
 
649
 
    def test__walkdirs_utf8(self):
650
 
        tree = [
651
 
            '.bzr',
652
 
            '0file',
653
 
            '1dir/',
654
 
            '1dir/0file',
655
 
            '1dir/1dir/',
656
 
            '2file'
657
 
            ]
658
 
        self.build_tree(tree)
659
 
        expected_dirblocks = [
660
 
                (('', '.'),
661
 
                 [('0file', '0file', 'file'),
662
 
                  ('1dir', '1dir', 'directory'),
663
 
                  ('2file', '2file', 'file'),
664
 
                 ]
665
 
                ),
666
 
                (('1dir', './1dir'),
667
 
                 [('1dir/0file', '0file', 'file'),
668
 
                  ('1dir/1dir', '1dir', 'directory'),
669
 
                 ]
670
 
                ),
671
 
                (('1dir/1dir', './1dir/1dir'),
672
 
                 [
673
 
                 ]
674
 
                ),
675
 
            ]
676
 
        result = []
677
 
        found_bzrdir = False
678
 
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
679
 
            if len(dirblock) and dirblock[0][1] == '.bzr':
680
 
                # this tests the filtering of selected paths
681
 
                found_bzrdir = True
682
 
                del dirblock[0]
683
 
            result.append((dirdetail, dirblock))
684
 
 
685
 
        self.assertTrue(found_bzrdir)
686
 
        self.assertEqual(expected_dirblocks,
687
 
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
688
 
        # you can search a subdir only, with a supplied prefix.
689
 
        result = []
690
 
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
691
 
            result.append(dirblock)
692
 
        self.assertEqual(expected_dirblocks[1:],
693
 
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
694
 
 
695
 
    def _filter_out_stat(self, result):
696
 
        """Filter out the stat value from the walkdirs result"""
697
 
        for dirdetail, dirblock in result:
698
 
            new_dirblock = []
699
 
            for info in dirblock:
700
 
                # Ignore info[3] which is the stat
701
 
                new_dirblock.append((info[0], info[1], info[2], info[4]))
702
 
            dirblock[:] = new_dirblock
703
 
 
704
 
    def test_unicode_walkdirs(self):
705
 
        """Walkdirs should always return unicode paths."""
706
 
        name0 = u'0file-\xb6'
707
 
        name1 = u'1dir-\u062c\u0648'
708
 
        name2 = u'2file-\u0633'
709
 
        tree = [
710
 
            name0,
711
 
            name1 + '/',
712
 
            name1 + '/' + name0,
713
 
            name1 + '/' + name1 + '/',
714
 
            name2,
715
 
            ]
716
 
        try:
717
 
            self.build_tree(tree)
718
 
        except UnicodeError:
719
 
            raise TestSkipped('Could not represent Unicode chars'
720
 
                              ' in current encoding.')
721
 
        expected_dirblocks = [
722
 
                ((u'', u'.'),
723
 
                 [(name0, name0, 'file', './' + name0),
724
 
                  (name1, name1, 'directory', './' + name1),
725
 
                  (name2, name2, 'file', './' + name2),
726
 
                 ]
727
 
                ),
728
 
                ((name1, './' + name1),
729
 
                 [(name1 + '/' + name0, name0, 'file', './' + name1
730
 
                                                        + '/' + name0),
731
 
                  (name1 + '/' + name1, name1, 'directory', './' + name1
732
 
                                                            + '/' + name1),
733
 
                 ]
734
 
                ),
735
 
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
736
 
                 [
737
 
                 ]
738
 
                ),
739
 
            ]
740
 
        result = list(osutils.walkdirs('.'))
741
 
        self._filter_out_stat(result)
742
 
        self.assertEqual(expected_dirblocks, result)
743
 
        result = list(osutils.walkdirs(u'./'+name1, name1))
744
 
        self._filter_out_stat(result)
745
 
        self.assertEqual(expected_dirblocks[1:], result)
746
 
 
747
 
    def test_unicode__walkdirs_utf8(self):
748
 
        """Walkdirs_utf8 should always return utf8 paths.
749
 
 
750
 
        The abspath portion might be in unicode or utf-8
751
 
        """
752
 
        name0 = u'0file-\xb6'
753
 
        name1 = u'1dir-\u062c\u0648'
754
 
        name2 = u'2file-\u0633'
755
 
        tree = [
756
 
            name0,
757
 
            name1 + '/',
758
 
            name1 + '/' + name0,
759
 
            name1 + '/' + name1 + '/',
760
 
            name2,
761
 
            ]
762
 
        try:
763
 
            self.build_tree(tree)
764
 
        except UnicodeError:
765
 
            raise TestSkipped('Could not represent Unicode chars'
766
 
                              ' in current encoding.')
767
 
        name0 = name0.encode('utf8')
768
 
        name1 = name1.encode('utf8')
769
 
        name2 = name2.encode('utf8')
770
 
 
771
 
        expected_dirblocks = [
772
 
                (('', '.'),
773
 
                 [(name0, name0, 'file', './' + name0),
774
 
                  (name1, name1, 'directory', './' + name1),
775
 
                  (name2, name2, 'file', './' + name2),
776
 
                 ]
777
 
                ),
778
 
                ((name1, './' + name1),
779
 
                 [(name1 + '/' + name0, name0, 'file', './' + name1
780
 
                                                        + '/' + name0),
781
 
                  (name1 + '/' + name1, name1, 'directory', './' + name1
782
 
                                                            + '/' + name1),
783
 
                 ]
784
 
                ),
785
 
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
786
 
                 [
787
 
                 ]
788
 
                ),
789
 
            ]
790
 
        result = []
791
 
        # For ease in testing, if walkdirs_utf8 returns Unicode, assert that
792
 
        # all abspaths are Unicode, and encode them back into utf8.
793
 
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
794
 
            self.assertIsInstance(dirdetail[0], str)
795
 
            if isinstance(dirdetail[1], unicode):
796
 
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
797
 
                dirblock = [list(info) for info in dirblock]
798
 
                for info in dirblock:
799
 
                    self.assertIsInstance(info[4], unicode)
800
 
                    info[4] = info[4].encode('utf8')
801
 
            new_dirblock = []
802
 
            for info in dirblock:
803
 
                self.assertIsInstance(info[0], str)
804
 
                self.assertIsInstance(info[1], str)
805
 
                self.assertIsInstance(info[4], str)
806
 
                # Remove the stat information
807
 
                new_dirblock.append((info[0], info[1], info[2], info[4]))
808
 
            result.append((dirdetail, new_dirblock))
809
 
        self.assertEqual(expected_dirblocks, result)
810
 
 
811
 
    def test_unicode__walkdirs_unicode_to_utf8(self):
812
 
        """walkdirs_unicode_to_utf8 should be a safe fallback everywhere
813
 
 
814
 
        The abspath portion should be in unicode
815
 
        """
816
 
        name0u = u'0file-\xb6'
817
 
        name1u = u'1dir-\u062c\u0648'
818
 
        name2u = u'2file-\u0633'
819
 
        tree = [
820
 
            name0u,
821
 
            name1u + '/',
822
 
            name1u + '/' + name0u,
823
 
            name1u + '/' + name1u + '/',
824
 
            name2u,
825
 
            ]
826
 
        try:
827
 
            self.build_tree(tree)
828
 
        except UnicodeError:
829
 
            raise TestSkipped('Could not represent Unicode chars'
830
 
                              ' in current encoding.')
831
 
        name0 = name0u.encode('utf8')
832
 
        name1 = name1u.encode('utf8')
833
 
        name2 = name2u.encode('utf8')
834
 
 
835
 
        # All of the abspaths should be in unicode, all of the relative paths
836
 
        # should be in utf8
837
 
        expected_dirblocks = [
838
 
                (('', '.'),
839
 
                 [(name0, name0, 'file', './' + name0u),
840
 
                  (name1, name1, 'directory', './' + name1u),
841
 
                  (name2, name2, 'file', './' + name2u),
842
 
                 ]
843
 
                ),
844
 
                ((name1, './' + name1u),
845
 
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
846
 
                                                        + '/' + name0u),
847
 
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
848
 
                                                            + '/' + name1u),
849
 
                 ]
850
 
                ),
851
 
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
852
 
                 [
853
 
                 ]
854
 
                ),
855
 
            ]
856
 
        result = list(osutils._walkdirs_unicode_to_utf8('.'))
857
 
        self._filter_out_stat(result)
858
 
        self.assertEqual(expected_dirblocks, result)
859
 
 
860
452
    def assertPathCompare(self, path_less, path_greater):
861
453
        """check that path_less and path_greater compare correctly."""
862
454
        self.assertEqual(0, osutils.compare_paths_prefix_order(
951
543
        self.assertEqual(['c'], os.listdir('target/b'))
952
544
 
953
545
    def test_copy_tree_symlinks(self):
954
 
        self.requireFeature(SymlinkFeature)
 
546
        if not osutils.has_symlinks():
 
547
            return
955
548
        self.build_tree(['source/'])
956
549
        os.symlink('a/generic/path', 'source/lnk')
957
550
        osutils.copy_tree('source', 'target')
1024
617
        
1025
618
        So Unicode strings must be encoded.
1026
619
        """
1027
 
        uni_val, env_val = probe_unicode_in_user_encoding()
1028
 
        if uni_val is None:
 
620
        # Try a few different characters, to see if we can get
 
621
        # one that will be valid in the user_encoding
 
622
        possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
 
623
        for uni_val in possible_vals:
 
624
            try:
 
625
                env_val = uni_val.encode(bzrlib.user_encoding)
 
626
            except UnicodeEncodeError:
 
627
                # Try a different character
 
628
                pass
 
629
            else:
 
630
                break
 
631
        else:
1029
632
            raise TestSkipped('Cannot find a unicode character that works in'
1030
633
                              ' encoding %s' % (bzrlib.user_encoding,))
1031
634
 
1061
664
        self.assertTrue(isinstance(offset, int))
1062
665
        eighteen_hours = 18 * 3600
1063
666
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1064
 
 
1065
 
 
1066
 
class TestShaFileByName(TestCaseInTempDir):
1067
 
 
1068
 
    def test_sha_empty(self):
1069
 
        self.build_tree_contents([('foo', '')])
1070
 
        expected_sha = osutils.sha_string('')
1071
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1072
 
 
1073
 
    def test_sha_mixed_endings(self):
1074
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1075
 
        self.build_tree_contents([('foo', text)])
1076
 
        expected_sha = osutils.sha_string(text)
1077
 
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1078
 
 
1079
 
 
1080
 
_debug_text = \
1081
 
r'''# Copyright (C) 2005, 2006 Canonical Ltd
1082
 
#
1083
 
# This program is free software; you can redistribute it and/or modify
1084
 
# it under the terms of the GNU General Public License as published by
1085
 
# the Free Software Foundation; either version 2 of the License, or
1086
 
# (at your option) any later version.
1087
 
#
1088
 
# This program is distributed in the hope that it will be useful,
1089
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1090
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1091
 
# GNU General Public License for more details.
1092
 
#
1093
 
# You should have received a copy of the GNU General Public License
1094
 
# along with this program; if not, write to the Free Software
1095
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1096
 
 
1097
 
 
1098
 
# NOTE: If update these, please also update the help for global-options in
1099
 
#       bzrlib/help_topics/__init__.py
1100
 
 
1101
 
debug_flags = set()
1102
 
"""Set of flags that enable different debug behaviour.
1103
 
 
1104
 
These are set with eg ``-Dlock`` on the bzr command line.
1105
 
 
1106
 
Options include:
1107
 
 
1108
 
 * auth - show authentication sections used
1109
 
 * error - show stack traces for all top level exceptions
1110
 
 * evil - capture call sites that do expensive or badly-scaling operations.
1111
 
 * fetch - trace history copying between repositories
1112
 
 * hashcache - log every time a working file is read to determine its hash
1113
 
 * hooks - trace hook execution
1114
 
 * hpss - trace smart protocol requests and responses
1115
 
 * http - trace http connections, requests and responses
1116
 
 * index - trace major index operations
1117
 
 * knit - trace knit operations
1118
 
 * lock - trace when lockdir locks are taken or released
1119
 
 * merge - emit information for debugging merges
1120
 
 * pack - emit information about pack operations
1121
 
 
1122
 
"""
1123
 
'''
1124
 
 
1125
 
 
1126
 
class TestResourceLoading(TestCaseInTempDir):
1127
 
 
1128
 
    def test_resource_string(self):
1129
 
        # test resource in bzrlib
1130
 
        text = osutils.resource_string('bzrlib', 'debug.py')
1131
 
        self.assertEquals(_debug_text, text)
1132
 
        # test resource under bzrlib
1133
 
        text = osutils.resource_string('bzrlib.ui', 'text.py')
1134
 
        self.assertContainsRe(text, "class TextUIFactory")
1135
 
        # test unsupported package
1136
 
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1137
 
            'yyy.xx')
1138
 
        # test unknown resource
1139
 
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')