~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

[merge] bzr.dev 2255

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, 
35
38
 
36
39
class TestOSUtils(TestCaseInTempDir):
37
40
 
 
41
    def test_contains_whitespace(self):
 
42
        self.failUnless(osutils.contains_whitespace(u' '))
 
43
        self.failUnless(osutils.contains_whitespace(u'hello there'))
 
44
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
 
45
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
 
46
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
 
47
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
48
 
 
49
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
 
50
        # is whitespace, but we do not.
 
51
        self.failIf(osutils.contains_whitespace(u''))
 
52
        self.failIf(osutils.contains_whitespace(u'hellothere'))
 
53
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
54
 
38
55
    def test_fancy_rename(self):
39
56
        # This should work everywhere
40
57
        def rename(a, b):
78
95
        self.assertEqual(type(result), str)
79
96
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
80
97
 
 
98
    def test_is_inside(self):
 
99
        is_inside = osutils.is_inside
 
100
        self.assertTrue(is_inside('src', 'src/foo.c'))
 
101
        self.assertFalse(is_inside('src', 'srccontrol'))
 
102
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
 
103
        self.assertTrue(is_inside('foo.c', 'foo.c'))
 
104
        self.assertFalse(is_inside('foo.c', ''))
 
105
        self.assertTrue(is_inside('', 'foo.c'))
81
106
 
82
107
    def test_rmtree(self):
83
108
        # Check to remove tree with read-only files/dirs
186
211
        self.assertFormatedDelta('1 second in the future', -1)
187
212
        self.assertFormatedDelta('2 seconds in the future', -2)
188
213
 
 
214
    def test_dereference_path(self):
 
215
        if not osutils.has_symlinks():
 
216
            raise TestSkipped('Symlinks are not supported on this platform')
 
217
        cwd = osutils.realpath('.')
 
218
        os.mkdir('bar')
 
219
        bar_path = osutils.pathjoin(cwd, 'bar')
 
220
        # Using './' to avoid bug #1213894 (first path component not
 
221
        # dereferenced) in Python 2.4.1 and earlier
 
222
        self.assertEqual(bar_path, osutils.realpath('./bar'))
 
223
        os.symlink('bar', 'foo')
 
224
        self.assertEqual(bar_path, osutils.realpath('./foo'))
 
225
        
 
226
        # Does not dereference terminal symlinks
 
227
        foo_path = osutils.pathjoin(cwd, 'foo')
 
228
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
 
229
 
 
230
        # Dereferences parent symlinks
 
231
        os.mkdir('bar/baz')
 
232
        baz_path = osutils.pathjoin(bar_path, 'baz')
 
233
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
 
234
 
 
235
        # Dereferences parent symlinks that are the first path element
 
236
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
 
237
 
 
238
        # Dereferences parent symlinks in absolute paths
 
239
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
 
240
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
 
241
 
189
242
 
190
243
class TestSafeUnicode(TestCase):
191
244
 
311
364
        except (IOError, OSError), e:
312
365
            self.assertEqual(errno.ENOENT, e.errno)
313
366
 
 
367
    def test_splitpath(self):
 
368
        def check(expected, path):
 
369
            self.assertEqual(expected, osutils.splitpath(path))
 
370
 
 
371
        check(['a'], 'a')
 
372
        check(['a', 'b'], 'a/b')
 
373
        check(['a', 'b'], 'a/./b')
 
374
        check(['a', '.b'], 'a/.b')
 
375
        check(['a', '.b'], 'a\\.b')
 
376
 
 
377
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
 
378
 
314
379
 
315
380
class TestMacFuncsDirs(TestCaseInTempDir):
316
381
    """Test mac special functions that require directories."""
336
401
        os.chdir(u'Ba\u030agfors')
337
402
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
338
403
 
 
404
 
339
405
class TestSplitLines(TestCase):
340
406
 
341
407
    def test_split_unicode(self):
480
546
    def test_copy_basic_tree(self):
481
547
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
482
548
        osutils.copy_tree('source', 'target')
483
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
549
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
484
550
        self.assertEqual(['c'], os.listdir('target/b'))
485
551
 
486
552
    def test_copy_tree_target_exists(self):
487
553
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
488
554
                         'target/'])
489
555
        osutils.copy_tree('source', 'target')
490
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
556
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
491
557
        self.assertEqual(['c'], os.listdir('target/b'))
492
558
 
493
559
    def test_copy_tree_symlinks(self):
528
594
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
529
595
 
530
596
 
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())
 
597
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
598
# [bialix] 2006/12/26
567
599
 
568
600
 
569
601
class TestSetUnsetEnv(TestCase):
625
657
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
626
658
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
627
659
 
 
660
 
 
661
class TestLocalTimeOffset(TestCase):
 
662
 
 
663
    def test_local_time_offset(self):
 
664
        """Test that local_time_offset() returns a sane value."""
 
665
        offset = osutils.local_time_offset()
 
666
        self.assertTrue(isinstance(offset, int))
 
667
        # Test that the offset is no more than a eighteen hours in
 
668
        # either direction.
 
669
        # Time zone handling is system specific, so it is difficult to
 
670
        # do more specific tests, but a value outside of this range is
 
671
        # probably wrong.
 
672
        eighteen_hours = 18 * 3600
 
673
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
674
 
 
675
    def test_local_time_offset_with_timestamp(self):
 
676
        """Test that local_time_offset() works with a timestamp."""
 
677
        offset = osutils.local_time_offset(1000000000.1234567)
 
678
        self.assertTrue(isinstance(offset, int))
 
679
        eighteen_hours = 18 * 3600
 
680
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)