1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
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
36
39
class TestOSUtils(TestCaseInTempDir):
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'))
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'))
38
55
def test_fancy_rename(self):
39
56
# This should work everywhere
78
95
self.assertEqual(type(result), str)
79
96
self.assertContainsRe(result, r'^[a-z0-9]{100}$')
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'))
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)
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('.')
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'))
226
# Does not dereference terminal symlinks
227
foo_path = osutils.pathjoin(cwd, 'foo')
228
self.assertEqual(foo_path, osutils.dereference_path('./foo'))
230
# Dereferences parent symlinks
232
baz_path = osutils.pathjoin(bar_path, 'baz')
233
self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
235
# Dereferences parent symlinks that are the first path element
236
self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
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))
190
243
class TestSafeUnicode(TestCase):
311
364
except (IOError, OSError), e:
312
365
self.assertEqual(errno.ENOENT, e.errno)
367
def test_splitpath(self):
368
def check(expected, path):
369
self.assertEqual(expected, osutils.splitpath(path))
372
check(['a', 'b'], 'a/b')
373
check(['a', 'b'], 'a/./b')
374
check(['a', '.b'], 'a/.b')
375
check(['a', '.b'], 'a\\.b')
377
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
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')
339
405
class TestSplitLines(TestCase):
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'))
486
552
def test_copy_tree_target_exists(self):
487
553
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
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'))
493
559
def test_copy_tree_symlinks(self):
528
594
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
531
class TestTerminalEncoding(TestCase):
532
"""Test the auto-detection of proper terminal encoding."""
535
self._stdout = sys.stdout
536
self._stderr = sys.stderr
537
self._stdin = sys.stdin
538
self._user_encoding = bzrlib.user_encoding
540
self.addCleanup(self._reset)
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'
551
sys.stdout = self._stdout
552
sys.stderr = self._stderr
553
sys.stdin = self._stdin
554
bzrlib.user_encoding = self._user_encoding
556
def test_get_terminal_encoding(self):
557
# first preference is stdout encoding
558
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
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())
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
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)
661
class TestLocalTimeOffset(TestCase):
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
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
672
eighteen_hours = 18 * 3600
673
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
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)