/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

[merge] bzr.dev 2240

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
    )
26
30
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
27
 
import bzrlib.osutils as osutils
28
31
from bzrlib.tests import (
29
32
        StringIOWrapper,
30
33
        TestCase, 
78
81
        self.assertEqual(type(result), str)
79
82
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
80
83
 
 
84
    def test_is_inside(self):
 
85
        is_inside = osutils.is_inside
 
86
        self.assertTrue(is_inside('src', 'src/foo.c'))
 
87
        self.assertFalse(is_inside('src', 'srccontrol'))
 
88
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
 
89
        self.assertTrue(is_inside('foo.c', 'foo.c'))
 
90
        self.assertFalse(is_inside('foo.c', ''))
 
91
        self.assertTrue(is_inside('', 'foo.c'))
81
92
 
82
93
    def test_rmtree(self):
83
94
        # Check to remove tree with read-only files/dirs
186
197
        self.assertFormatedDelta('1 second in the future', -1)
187
198
        self.assertFormatedDelta('2 seconds in the future', -2)
188
199
 
 
200
    def test_dereference_path(self):
 
201
        if not osutils.has_symlinks():
 
202
            raise TestSkipped('Symlinks are not supported on this platform')
 
203
        cwd = osutils.realpath('.')
 
204
        os.mkdir('bar')
 
205
        bar_path = osutils.pathjoin(cwd, 'bar')
 
206
        # Using './' to avoid bug #1213894 (first path component not
 
207
        # dereferenced) in Python 2.4.1 and earlier
 
208
        self.assertEqual(bar_path, osutils.realpath('./bar'))
 
209
        os.symlink('bar', 'foo')
 
210
        self.assertEqual(bar_path, osutils.realpath('./foo'))
 
211
        
 
212
        # Does not dereference terminal symlinks
 
213
        foo_path = osutils.pathjoin(cwd, 'foo')
 
214
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
 
215
 
 
216
        # Dereferences parent symlinks
 
217
        os.mkdir('bar/baz')
 
218
        baz_path = osutils.pathjoin(bar_path, 'baz')
 
219
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
 
220
 
 
221
        # Dereferences parent symlinks that are the first path element
 
222
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
 
223
 
 
224
        # Dereferences parent symlinks in absolute paths
 
225
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
 
226
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
 
227
 
189
228
 
190
229
class TestSafeUnicode(TestCase):
191
230
 
311
350
        except (IOError, OSError), e:
312
351
            self.assertEqual(errno.ENOENT, e.errno)
313
352
 
 
353
    def test_splitpath(self):
 
354
        def check(expected, path):
 
355
            self.assertEqual(expected, osutils.splitpath(path))
 
356
 
 
357
        check(['a'], 'a')
 
358
        check(['a', 'b'], 'a/b')
 
359
        check(['a', 'b'], 'a/./b')
 
360
        check(['a', '.b'], 'a/.b')
 
361
        check(['a', '.b'], 'a\\.b')
 
362
 
 
363
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
 
364
 
314
365
 
315
366
class TestMacFuncsDirs(TestCaseInTempDir):
316
367
    """Test mac special functions that require directories."""
336
387
        os.chdir(u'Ba\u030agfors')
337
388
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
338
389
 
 
390
 
339
391
class TestSplitLines(TestCase):
340
392
 
341
393
    def test_split_unicode(self):
480
532
    def test_copy_basic_tree(self):
481
533
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
482
534
        osutils.copy_tree('source', 'target')
483
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
535
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
484
536
        self.assertEqual(['c'], os.listdir('target/b'))
485
537
 
486
538
    def test_copy_tree_target_exists(self):
487
539
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
488
540
                         'target/'])
489
541
        osutils.copy_tree('source', 'target')
490
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
542
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
491
543
        self.assertEqual(['c'], os.listdir('target/b'))
492
544
 
493
545
    def test_copy_tree_symlinks(self):
528
580
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
529
581
 
530
582
 
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())
 
583
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
584
# [bialix] 2006/12/26
567
585
 
568
586
 
569
587
class TestSetUnsetEnv(TestCase):
625
643
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
626
644
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
627
645
 
 
646
 
 
647
class TestLocalTimeOffset(TestCase):
 
648
 
 
649
    def test_local_time_offset(self):
 
650
        """Test that local_time_offset() returns a sane value."""
 
651
        offset = osutils.local_time_offset()
 
652
        self.assertTrue(isinstance(offset, int))
 
653
        # Test that the offset is no more than a eighteen hours in
 
654
        # either direction.
 
655
        # Time zone handling is system specific, so it is difficult to
 
656
        # do more specific tests, but a value outside of this range is
 
657
        # probably wrong.
 
658
        eighteen_hours = 18 * 3600
 
659
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
660
 
 
661
    def test_local_time_offset_with_timestamp(self):
 
662
        """Test that local_time_offset() works with a timestamp."""
 
663
        offset = osutils.local_time_offset(1000000000.1234567)
 
664
        self.assertTrue(isinstance(offset, int))
 
665
        eighteen_hours = 18 * 3600
 
666
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)