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
151
176
os.umask(orig_umask)
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)
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)
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)
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))
154
243
class TestSafeUnicode(TestCase):
275
364
except (IOError, OSError), e:
276
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')
279
380
class TestMacFuncsDirs(TestCaseInTempDir):
280
381
"""Test mac special functions that require directories."""
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'))
450
552
def test_copy_tree_target_exists(self):
451
553
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
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'))
457
559
def test_copy_tree_symlinks(self):
492
594
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
495
class TestTerminalEncoding(TestCase):
496
"""Test the auto-detection of proper terminal encoding."""
499
self._stdout = sys.stdout
500
self._stderr = sys.stderr
501
self._stdin = sys.stdin
502
self._user_encoding = bzrlib.user_encoding
504
self.addCleanup(self._reset)
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'
515
sys.stdout = self._stdout
516
sys.stderr = self._stderr
517
sys.stdin = self._stdin
518
bzrlib.user_encoding = self._user_encoding
520
def test_get_terminal_encoding(self):
521
# first preference is stdout encoding
522
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
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())
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
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)
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)