~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by 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
23
23
import sys
24
24
 
25
25
import bzrlib
 
26
from bzrlib import (
 
27
    errors,
 
28
    osutils,
 
29
    win32utils,
 
30
    )
26
31
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
27
 
import bzrlib.osutils as osutils
28
32
from bzrlib.tests import (
29
33
        StringIOWrapper,
30
34
        TestCase, 
35
39
 
36
40
class TestOSUtils(TestCaseInTempDir):
37
41
 
 
42
    def test_contains_whitespace(self):
 
43
        self.failUnless(osutils.contains_whitespace(u' '))
 
44
        self.failUnless(osutils.contains_whitespace(u'hello there'))
 
45
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
 
46
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
 
47
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
 
48
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
49
 
 
50
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
 
51
        # is whitespace, but we do not.
 
52
        self.failIf(osutils.contains_whitespace(u''))
 
53
        self.failIf(osutils.contains_whitespace(u'hellothere'))
 
54
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
55
 
38
56
    def test_fancy_rename(self):
39
57
        # This should work everywhere
40
58
        def rename(a, b):
78
96
        self.assertEqual(type(result), str)
79
97
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
80
98
 
 
99
    def test_is_inside(self):
 
100
        is_inside = osutils.is_inside
 
101
        self.assertTrue(is_inside('src', 'src/foo.c'))
 
102
        self.assertFalse(is_inside('src', 'srccontrol'))
 
103
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
 
104
        self.assertTrue(is_inside('foo.c', 'foo.c'))
 
105
        self.assertFalse(is_inside('foo.c', ''))
 
106
        self.assertTrue(is_inside('', 'foo.c'))
81
107
 
82
108
    def test_rmtree(self):
83
109
        # Check to remove tree with read-only files/dirs
131
157
            finally:
132
158
                os.remove('socket')
133
159
 
 
160
    def test_kind_marker(self):
 
161
        self.assertEqual(osutils.kind_marker('file'), '')
 
162
        self.assertEqual(osutils.kind_marker('directory'), '/')
 
163
        self.assertEqual(osutils.kind_marker('symlink'), '@')
 
164
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
 
165
 
134
166
    def test_get_umask(self):
135
167
        if sys.platform == 'win32':
136
168
            # umask always returns '0', no way to set it
186
218
        self.assertFormatedDelta('1 second in the future', -1)
187
219
        self.assertFormatedDelta('2 seconds in the future', -2)
188
220
 
 
221
    def test_dereference_path(self):
 
222
        if not osutils.has_symlinks():
 
223
            raise TestSkipped('Symlinks are not supported on this platform')
 
224
        cwd = osutils.realpath('.')
 
225
        os.mkdir('bar')
 
226
        bar_path = osutils.pathjoin(cwd, 'bar')
 
227
        # Using './' to avoid bug #1213894 (first path component not
 
228
        # dereferenced) in Python 2.4.1 and earlier
 
229
        self.assertEqual(bar_path, osutils.realpath('./bar'))
 
230
        os.symlink('bar', 'foo')
 
231
        self.assertEqual(bar_path, osutils.realpath('./foo'))
 
232
        
 
233
        # Does not dereference terminal symlinks
 
234
        foo_path = osutils.pathjoin(cwd, 'foo')
 
235
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
 
236
 
 
237
        # Dereferences parent symlinks
 
238
        os.mkdir('bar/baz')
 
239
        baz_path = osutils.pathjoin(bar_path, 'baz')
 
240
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
 
241
 
 
242
        # Dereferences parent symlinks that are the first path element
 
243
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
 
244
 
 
245
        # Dereferences parent symlinks in absolute paths
 
246
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
 
247
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
 
248
 
 
249
 
 
250
    def test_changing_access(self):
 
251
        f = file('file', 'w')
 
252
        f.write('monkey')
 
253
        f.close()
 
254
 
 
255
        # Make a file readonly
 
256
        osutils.make_readonly('file')
 
257
        mode = osutils.lstat('file').st_mode
 
258
        self.assertEqual(mode, mode & 0777555)
 
259
 
 
260
        # Make a file writable
 
261
        osutils.make_writable('file')
 
262
        mode = osutils.lstat('file').st_mode
 
263
        self.assertEqual(mode, mode | 0200)
 
264
 
 
265
        if osutils.has_symlinks():
 
266
            # should not error when handed a symlink
 
267
            os.symlink('nonexistent', 'dangling')
 
268
            osutils.make_readonly('dangling')
 
269
            osutils.make_writable('dangling')
 
270
 
 
271
 
 
272
    def test_kind_marker(self):
 
273
        self.assertEqual("", osutils.kind_marker("file"))
 
274
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
 
275
        self.assertEqual("@", osutils.kind_marker("symlink"))
 
276
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
277
 
189
278
 
190
279
class TestSafeUnicode(TestCase):
191
280
 
207
296
                          '\xbb\xbb')
208
297
 
209
298
 
 
299
class TestSafeUtf8(TestCase):
 
300
 
 
301
    def test_from_ascii_string(self):
 
302
        f = 'foobar'
 
303
        self.assertEqual('foobar', osutils.safe_utf8(f))
 
304
 
 
305
    def test_from_unicode_string_ascii_contents(self):
 
306
        self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
 
307
 
 
308
    def test_from_unicode_string_unicode_contents(self):
 
309
        self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
 
310
 
 
311
    def test_from_utf8_string(self):
 
312
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
 
313
 
 
314
    def test_bad_utf8_string(self):
 
315
        self.assertRaises(BzrBadParameterNotUnicode,
 
316
                          osutils.safe_utf8, '\xbb\xbb')
 
317
 
 
318
 
 
319
class TestSafeRevisionId(TestCase):
 
320
 
 
321
    def test_from_ascii_string(self):
 
322
        self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
 
323
 
 
324
    def test_from_unicode_string_ascii_contents(self):
 
325
        self.assertEqual('bargam',
 
326
                         osutils.safe_revision_id(u'bargam', warn=False))
 
327
 
 
328
    def test_from_unicode_deprecated(self):
 
329
        self.assertEqual('bargam',
 
330
            self.callDeprecated([osutils._revision_id_warning],
 
331
                                osutils.safe_revision_id, u'bargam'))
 
332
 
 
333
    def test_from_unicode_string_unicode_contents(self):
 
334
        self.assertEqual('bargam\xc2\xae',
 
335
                         osutils.safe_revision_id(u'bargam\xae', warn=False))
 
336
 
 
337
    def test_from_utf8_string(self):
 
338
        self.assertEqual('foo\xc2\xae',
 
339
                         osutils.safe_revision_id('foo\xc2\xae'))
 
340
 
 
341
    def test_none(self):
 
342
        """Currently, None is a valid revision_id"""
 
343
        self.assertEqual(None, osutils.safe_revision_id(None))
 
344
 
 
345
 
 
346
class TestSafeFileId(TestCase):
 
347
 
 
348
    def test_from_ascii_string(self):
 
349
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
 
350
 
 
351
    def test_from_unicode_string_ascii_contents(self):
 
352
        self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
 
353
 
 
354
    def test_from_unicode_deprecated(self):
 
355
        self.assertEqual('bargam',
 
356
            self.callDeprecated([osutils._file_id_warning],
 
357
                                osutils.safe_file_id, u'bargam'))
 
358
 
 
359
    def test_from_unicode_string_unicode_contents(self):
 
360
        self.assertEqual('bargam\xc2\xae',
 
361
                         osutils.safe_file_id(u'bargam\xae', warn=False))
 
362
 
 
363
    def test_from_utf8_string(self):
 
364
        self.assertEqual('foo\xc2\xae',
 
365
                         osutils.safe_file_id('foo\xc2\xae'))
 
366
 
 
367
    def test_none(self):
 
368
        """Currently, None is a valid revision_id"""
 
369
        self.assertEqual(None, osutils.safe_file_id(None))
 
370
 
 
371
 
210
372
class TestWin32Funcs(TestCase):
211
373
    """Test that the _win32 versions of os utilities return appropriate paths."""
212
374
 
213
375
    def test_abspath(self):
214
376
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
215
377
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
 
378
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
 
379
        self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
216
380
 
217
381
    def test_realpath(self):
218
382
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
246
410
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
247
411
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
248
412
 
 
413
    def test_win98_abspath(self):
 
414
        # absolute path
 
415
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
 
416
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
 
417
        # UNC path
 
418
        self.assertEqual('//HOST/path', osutils._win98_abspath(r'\\HOST\path'))
 
419
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
 
420
        # relative path
 
421
        cwd = osutils.getcwd().rstrip('/')
 
422
        drive = osutils._nt_splitdrive(cwd)[0]
 
423
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
 
424
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
 
425
        # unicode path
 
426
        u = u'\u1234'
 
427
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
 
428
 
249
429
 
250
430
class TestWin32FuncsDirs(TestCaseInTempDir):
251
431
    """Test win32 functions that create files."""
252
432
    
253
433
    def test_getcwd(self):
 
434
        if win32utils.winver == 'Windows 98':
 
435
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
254
436
        # Make sure getcwd can handle unicode filenames
255
437
        try:
256
438
            os.mkdir(u'mu-\xb5')
311
493
        except (IOError, OSError), e:
312
494
            self.assertEqual(errno.ENOENT, e.errno)
313
495
 
 
496
    def test_splitpath(self):
 
497
        def check(expected, path):
 
498
            self.assertEqual(expected, osutils.splitpath(path))
 
499
 
 
500
        check(['a'], 'a')
 
501
        check(['a', 'b'], 'a/b')
 
502
        check(['a', 'b'], 'a/./b')
 
503
        check(['a', '.b'], 'a/.b')
 
504
        check(['a', '.b'], 'a\\.b')
 
505
 
 
506
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
 
507
 
314
508
 
315
509
class TestMacFuncsDirs(TestCaseInTempDir):
316
510
    """Test mac special functions that require directories."""
336
530
        os.chdir(u'Ba\u030agfors')
337
531
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
338
532
 
 
533
 
339
534
class TestSplitLines(TestCase):
340
535
 
341
536
    def test_split_unicode(self):
397
592
        self.assertEqual(expected_dirblocks[1:],
398
593
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
399
594
 
 
595
    def test__walkdirs_utf8(self):
 
596
        tree = [
 
597
            '.bzr',
 
598
            '0file',
 
599
            '1dir/',
 
600
            '1dir/0file',
 
601
            '1dir/1dir/',
 
602
            '2file'
 
603
            ]
 
604
        self.build_tree(tree)
 
605
        expected_dirblocks = [
 
606
                (('', '.'),
 
607
                 [('0file', '0file', 'file'),
 
608
                  ('1dir', '1dir', 'directory'),
 
609
                  ('2file', '2file', 'file'),
 
610
                 ]
 
611
                ),
 
612
                (('1dir', './1dir'),
 
613
                 [('1dir/0file', '0file', 'file'),
 
614
                  ('1dir/1dir', '1dir', 'directory'),
 
615
                 ]
 
616
                ),
 
617
                (('1dir/1dir', './1dir/1dir'),
 
618
                 [
 
619
                 ]
 
620
                ),
 
621
            ]
 
622
        result = []
 
623
        found_bzrdir = False
 
624
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
 
625
            if len(dirblock) and dirblock[0][1] == '.bzr':
 
626
                # this tests the filtering of selected paths
 
627
                found_bzrdir = True
 
628
                del dirblock[0]
 
629
            result.append((dirdetail, dirblock))
 
630
 
 
631
        self.assertTrue(found_bzrdir)
 
632
        self.assertEqual(expected_dirblocks,
 
633
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
 
634
        # you can search a subdir only, with a supplied prefix.
 
635
        result = []
 
636
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
 
637
            result.append(dirblock)
 
638
        self.assertEqual(expected_dirblocks[1:],
 
639
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
 
640
 
 
641
    def _filter_out_stat(self, result):
 
642
        """Filter out the stat value from the walkdirs result"""
 
643
        for dirdetail, dirblock in result:
 
644
            new_dirblock = []
 
645
            for info in dirblock:
 
646
                # Ignore info[3] which is the stat
 
647
                new_dirblock.append((info[0], info[1], info[2], info[4]))
 
648
            dirblock[:] = new_dirblock
 
649
 
 
650
    def test_unicode_walkdirs(self):
 
651
        """Walkdirs should always return unicode paths."""
 
652
        name0 = u'0file-\xb6'
 
653
        name1 = u'1dir-\u062c\u0648'
 
654
        name2 = u'2file-\u0633'
 
655
        tree = [
 
656
            name0,
 
657
            name1 + '/',
 
658
            name1 + '/' + name0,
 
659
            name1 + '/' + name1 + '/',
 
660
            name2,
 
661
            ]
 
662
        try:
 
663
            self.build_tree(tree)
 
664
        except UnicodeError:
 
665
            raise TestSkipped('Could not represent Unicode chars'
 
666
                              ' in current encoding.')
 
667
        expected_dirblocks = [
 
668
                ((u'', u'.'),
 
669
                 [(name0, name0, 'file', './' + name0),
 
670
                  (name1, name1, 'directory', './' + name1),
 
671
                  (name2, name2, 'file', './' + name2),
 
672
                 ]
 
673
                ),
 
674
                ((name1, './' + name1),
 
675
                 [(name1 + '/' + name0, name0, 'file', './' + name1
 
676
                                                        + '/' + name0),
 
677
                  (name1 + '/' + name1, name1, 'directory', './' + name1
 
678
                                                            + '/' + name1),
 
679
                 ]
 
680
                ),
 
681
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
 
682
                 [
 
683
                 ]
 
684
                ),
 
685
            ]
 
686
        result = list(osutils.walkdirs('.'))
 
687
        self._filter_out_stat(result)
 
688
        self.assertEqual(expected_dirblocks, result)
 
689
        result = list(osutils.walkdirs(u'./'+name1, name1))
 
690
        self._filter_out_stat(result)
 
691
        self.assertEqual(expected_dirblocks[1:], result)
 
692
 
 
693
    def test_unicode__walkdirs_utf8(self):
 
694
        """Walkdirs_utf8 should always return utf8 paths.
 
695
 
 
696
        The abspath portion might be in unicode or utf-8
 
697
        """
 
698
        name0 = u'0file-\xb6'
 
699
        name1 = u'1dir-\u062c\u0648'
 
700
        name2 = u'2file-\u0633'
 
701
        tree = [
 
702
            name0,
 
703
            name1 + '/',
 
704
            name1 + '/' + name0,
 
705
            name1 + '/' + name1 + '/',
 
706
            name2,
 
707
            ]
 
708
        try:
 
709
            self.build_tree(tree)
 
710
        except UnicodeError:
 
711
            raise TestSkipped('Could not represent Unicode chars'
 
712
                              ' in current encoding.')
 
713
        name0 = name0.encode('utf8')
 
714
        name1 = name1.encode('utf8')
 
715
        name2 = name2.encode('utf8')
 
716
 
 
717
        expected_dirblocks = [
 
718
                (('', '.'),
 
719
                 [(name0, name0, 'file', './' + name0),
 
720
                  (name1, name1, 'directory', './' + name1),
 
721
                  (name2, name2, 'file', './' + name2),
 
722
                 ]
 
723
                ),
 
724
                ((name1, './' + name1),
 
725
                 [(name1 + '/' + name0, name0, 'file', './' + name1
 
726
                                                        + '/' + name0),
 
727
                  (name1 + '/' + name1, name1, 'directory', './' + name1
 
728
                                                            + '/' + name1),
 
729
                 ]
 
730
                ),
 
731
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
 
732
                 [
 
733
                 ]
 
734
                ),
 
735
            ]
 
736
        result = []
 
737
        # For ease in testing, if walkdirs_utf8 returns Unicode, assert that
 
738
        # all abspaths are Unicode, and encode them back into utf8.
 
739
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
 
740
            self.assertIsInstance(dirdetail[0], str)
 
741
            if isinstance(dirdetail[1], unicode):
 
742
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
 
743
                dirblock = [list(info) for info in dirblock]
 
744
                for info in dirblock:
 
745
                    self.assertIsInstance(info[4], unicode)
 
746
                    info[4] = info[4].encode('utf8')
 
747
            new_dirblock = []
 
748
            for info in dirblock:
 
749
                self.assertIsInstance(info[0], str)
 
750
                self.assertIsInstance(info[1], str)
 
751
                self.assertIsInstance(info[4], str)
 
752
                # Remove the stat information
 
753
                new_dirblock.append((info[0], info[1], info[2], info[4]))
 
754
            result.append((dirdetail, new_dirblock))
 
755
        self.assertEqual(expected_dirblocks, result)
 
756
 
 
757
    def test_unicode__walkdirs_unicode_to_utf8(self):
 
758
        """walkdirs_unicode_to_utf8 should be a safe fallback everywhere
 
759
 
 
760
        The abspath portion should be in unicode
 
761
        """
 
762
        name0u = u'0file-\xb6'
 
763
        name1u = u'1dir-\u062c\u0648'
 
764
        name2u = u'2file-\u0633'
 
765
        tree = [
 
766
            name0u,
 
767
            name1u + '/',
 
768
            name1u + '/' + name0u,
 
769
            name1u + '/' + name1u + '/',
 
770
            name2u,
 
771
            ]
 
772
        try:
 
773
            self.build_tree(tree)
 
774
        except UnicodeError:
 
775
            raise TestSkipped('Could not represent Unicode chars'
 
776
                              ' in current encoding.')
 
777
        name0 = name0u.encode('utf8')
 
778
        name1 = name1u.encode('utf8')
 
779
        name2 = name2u.encode('utf8')
 
780
 
 
781
        # All of the abspaths should be in unicode, all of the relative paths
 
782
        # should be in utf8
 
783
        expected_dirblocks = [
 
784
                (('', '.'),
 
785
                 [(name0, name0, 'file', './' + name0u),
 
786
                  (name1, name1, 'directory', './' + name1u),
 
787
                  (name2, name2, 'file', './' + name2u),
 
788
                 ]
 
789
                ),
 
790
                ((name1, './' + name1u),
 
791
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
 
792
                                                        + '/' + name0u),
 
793
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
 
794
                                                            + '/' + name1u),
 
795
                 ]
 
796
                ),
 
797
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
 
798
                 [
 
799
                 ]
 
800
                ),
 
801
            ]
 
802
        result = list(osutils._walkdirs_unicode_to_utf8('.'))
 
803
        self._filter_out_stat(result)
 
804
        self.assertEqual(expected_dirblocks, result)
 
805
 
400
806
    def assertPathCompare(self, path_less, path_greater):
401
807
        """check that path_less and path_greater compare correctly."""
402
808
        self.assertEqual(0, osutils.compare_paths_prefix_order(
480
886
    def test_copy_basic_tree(self):
481
887
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
482
888
        osutils.copy_tree('source', 'target')
483
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
889
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
484
890
        self.assertEqual(['c'], os.listdir('target/b'))
485
891
 
486
892
    def test_copy_tree_target_exists(self):
487
893
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
488
894
                         'target/'])
489
895
        osutils.copy_tree('source', 'target')
490
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
896
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
491
897
        self.assertEqual(['c'], os.listdir('target/b'))
492
898
 
493
899
    def test_copy_tree_symlinks(self):
528
934
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
529
935
 
530
936
 
531
 
class TestTerminalEncoding(TestCase):
532
 
    """Test the auto-detection of proper terminal encoding."""
533
 
 
534
 
    def setUp(self):
535
 
        self._stdout = sys.stdout
536
 
        self._stderr = sys.stderr
537
 
        self._stdin = sys.stdin
538
 
        self._user_encoding = bzrlib.user_encoding
539
 
 
540
 
        self.addCleanup(self._reset)
541
 
 
542
 
        sys.stdout = StringIOWrapper()
543
 
        sys.stdout.encoding = 'stdout_encoding'
544
 
        sys.stderr = StringIOWrapper()
545
 
        sys.stderr.encoding = 'stderr_encoding'
546
 
        sys.stdin = StringIOWrapper()
547
 
        sys.stdin.encoding = 'stdin_encoding'
548
 
        bzrlib.user_encoding = 'user_encoding'
549
 
 
550
 
    def _reset(self):
551
 
        sys.stdout = self._stdout
552
 
        sys.stderr = self._stderr
553
 
        sys.stdin = self._stdin
554
 
        bzrlib.user_encoding = self._user_encoding
555
 
 
556
 
    def test_get_terminal_encoding(self):
557
 
        # first preference is stdout encoding
558
 
        self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
559
 
 
560
 
        sys.stdout.encoding = None
561
 
        # if sys.stdout is None, fall back to sys.stdin
562
 
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
563
 
 
564
 
        sys.stdin.encoding = None
565
 
        # and in the worst case, use bzrlib.user_encoding
566
 
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
 
937
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
938
# [bialix] 2006/12/26
567
939
 
568
940
 
569
941
class TestSetUnsetEnv(TestCase):
625
997
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
626
998
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
627
999
 
 
1000
 
 
1001
class TestLocalTimeOffset(TestCase):
 
1002
 
 
1003
    def test_local_time_offset(self):
 
1004
        """Test that local_time_offset() returns a sane value."""
 
1005
        offset = osutils.local_time_offset()
 
1006
        self.assertTrue(isinstance(offset, int))
 
1007
        # Test that the offset is no more than a eighteen hours in
 
1008
        # either direction.
 
1009
        # Time zone handling is system specific, so it is difficult to
 
1010
        # do more specific tests, but a value outside of this range is
 
1011
        # probably wrong.
 
1012
        eighteen_hours = 18 * 3600
 
1013
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1014
 
 
1015
    def test_local_time_offset_with_timestamp(self):
 
1016
        """Test that local_time_offset() works with a timestamp."""
 
1017
        offset = osutils.local_time_offset(1000000000.1234567)
 
1018
        self.assertTrue(isinstance(offset, int))
 
1019
        eighteen_hours = 18 * 3600
 
1020
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)