~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Aaron Bentley
  • Date: 2007-02-06 14:52:16 UTC
  • mfrom: (2266 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2268.
  • Revision ID: abentley@panoramicfeedback.com-20070206145216-fcpi8o3ufvuzwbp9
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
    )
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
150
175
        finally:
151
176
            os.umask(orig_umask)
152
177
 
 
178
    def assertFormatedDelta(self, expected, seconds):
 
179
        """Assert osutils.format_delta formats as expected"""
 
180
        actual = osutils.format_delta(seconds)
 
181
        self.assertEqual(expected, actual)
 
182
 
 
183
    def test_format_delta(self):
 
184
        self.assertFormatedDelta('0 seconds ago', 0)
 
185
        self.assertFormatedDelta('1 second ago', 1)
 
186
        self.assertFormatedDelta('10 seconds ago', 10)
 
187
        self.assertFormatedDelta('59 seconds ago', 59)
 
188
        self.assertFormatedDelta('89 seconds ago', 89)
 
189
        self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
 
190
        self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
 
191
        self.assertFormatedDelta('3 minutes, 1 second ago', 181)
 
192
        self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
 
193
        self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
 
194
        self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
 
195
        self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
 
196
        self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
 
197
        self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
 
198
        self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
 
199
        self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
 
200
        self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
 
201
        self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
 
202
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
 
203
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
 
204
        self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
 
205
        self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
 
206
        self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
 
207
 
 
208
        # We handle when time steps the wrong direction because computers
 
209
        # don't have synchronized clocks.
 
210
        self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
 
211
        self.assertFormatedDelta('1 second in the future', -1)
 
212
        self.assertFormatedDelta('2 seconds in the future', -2)
 
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
 
153
242
 
154
243
class TestSafeUnicode(TestCase):
155
244
 
275
364
        except (IOError, OSError), e:
276
365
            self.assertEqual(errno.ENOENT, e.errno)
277
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
 
278
379
 
279
380
class TestMacFuncsDirs(TestCaseInTempDir):
280
381
    """Test mac special functions that require directories."""
300
401
        os.chdir(u'Ba\u030agfors')
301
402
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
302
403
 
 
404
 
303
405
class TestSplitLines(TestCase):
304
406
 
305
407
    def test_split_unicode(self):
444
546
    def test_copy_basic_tree(self):
445
547
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
446
548
        osutils.copy_tree('source', 'target')
447
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
549
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
448
550
        self.assertEqual(['c'], os.listdir('target/b'))
449
551
 
450
552
    def test_copy_tree_target_exists(self):
451
553
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
452
554
                         'target/'])
453
555
        osutils.copy_tree('source', 'target')
454
 
        self.assertEqual(['a', 'b'], os.listdir('target'))
 
556
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
455
557
        self.assertEqual(['c'], os.listdir('target/b'))
456
558
 
457
559
    def test_copy_tree_symlinks(self):
492
594
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
493
595
 
494
596
 
495
 
class TestTerminalEncoding(TestCase):
496
 
    """Test the auto-detection of proper terminal encoding."""
497
 
 
498
 
    def setUp(self):
499
 
        self._stdout = sys.stdout
500
 
        self._stderr = sys.stderr
501
 
        self._stdin = sys.stdin
502
 
        self._user_encoding = bzrlib.user_encoding
503
 
 
504
 
        self.addCleanup(self._reset)
505
 
 
506
 
        sys.stdout = StringIOWrapper()
507
 
        sys.stdout.encoding = 'stdout_encoding'
508
 
        sys.stderr = StringIOWrapper()
509
 
        sys.stderr.encoding = 'stderr_encoding'
510
 
        sys.stdin = StringIOWrapper()
511
 
        sys.stdin.encoding = 'stdin_encoding'
512
 
        bzrlib.user_encoding = 'user_encoding'
513
 
 
514
 
    def _reset(self):
515
 
        sys.stdout = self._stdout
516
 
        sys.stderr = self._stderr
517
 
        sys.stdin = self._stdin
518
 
        bzrlib.user_encoding = self._user_encoding
519
 
 
520
 
    def test_get_terminal_encoding(self):
521
 
        # first preference is stdout encoding
522
 
        self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
523
 
 
524
 
        sys.stdout.encoding = None
525
 
        # if sys.stdout is None, fall back to sys.stdin
526
 
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
527
 
 
528
 
        sys.stdin.encoding = None
529
 
        # and in the worst case, use bzrlib.user_encoding
530
 
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
 
597
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
598
# [bialix] 2006/12/26
531
599
 
532
600
 
533
601
class TestSetUnsetEnv(TestCase):
589
657
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
590
658
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
591
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)