212
197
self.assertFormatedDelta('1 second in the future', -1)
213
198
self.assertFormatedDelta('2 seconds in the future', -2)
215
def test_dereference_path(self):
216
if not osutils.has_symlinks():
217
raise TestSkipped('Symlinks are not supported on this platform')
218
cwd = osutils.realpath('.')
220
bar_path = osutils.pathjoin(cwd, 'bar')
221
# Using './' to avoid bug #1213894 (first path component not
222
# dereferenced) in Python 2.4.1 and earlier
223
self.assertEqual(bar_path, osutils.realpath('./bar'))
224
os.symlink('bar', 'foo')
225
self.assertEqual(bar_path, osutils.realpath('./foo'))
227
# Does not dereference terminal symlinks
228
foo_path = osutils.pathjoin(cwd, 'foo')
229
self.assertEqual(foo_path, osutils.dereference_path('./foo'))
231
# Dereferences parent symlinks
233
baz_path = osutils.pathjoin(bar_path, 'baz')
234
self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
236
# Dereferences parent symlinks that are the first path element
237
self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
239
# Dereferences parent symlinks in absolute paths
240
foo_baz_path = osutils.pathjoin(foo_path, 'baz')
241
self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
244
201
class TestSafeUnicode(TestCase):
264
class TestSafeUtf8(TestCase):
266
def test_from_ascii_string(self):
268
self.assertEqual('foobar', osutils.safe_utf8(f))
270
def test_from_unicode_string_ascii_contents(self):
271
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
273
def test_from_unicode_string_unicode_contents(self):
274
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
276
def test_from_utf8_string(self):
277
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
279
def test_bad_utf8_string(self):
280
self.assertRaises(BzrBadParameterNotUnicode,
281
osutils.safe_utf8, '\xbb\xbb')
284
class TestSafeRevisionId(TestCase):
286
def test_from_ascii_string(self):
287
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
289
def test_from_unicode_string_ascii_contents(self):
290
self.assertEqual('bargam',
291
osutils.safe_revision_id(u'bargam', warn=False))
293
def test_from_unicode_deprecated(self):
294
self.assertEqual('bargam',
295
self.callDeprecated([osutils._revision_id_warning],
296
osutils.safe_revision_id, u'bargam'))
298
def test_from_unicode_string_unicode_contents(self):
299
self.assertEqual('bargam\xc2\xae',
300
osutils.safe_revision_id(u'bargam\xae', warn=False))
302
def test_from_utf8_string(self):
303
self.assertEqual('foo\xc2\xae',
304
osutils.safe_revision_id('foo\xc2\xae'))
307
"""Currently, None is a valid revision_id"""
308
self.assertEqual(None, osutils.safe_revision_id(None))
311
class TestSafeFileId(TestCase):
313
def test_from_ascii_string(self):
314
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
316
def test_from_unicode_string_ascii_contents(self):
317
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
319
def test_from_unicode_deprecated(self):
320
self.assertEqual('bargam',
321
self.callDeprecated([osutils._file_id_warning],
322
osutils.safe_file_id, u'bargam'))
324
def test_from_unicode_string_unicode_contents(self):
325
self.assertEqual('bargam\xc2\xae',
326
osutils.safe_file_id(u'bargam\xae', warn=False))
328
def test_from_utf8_string(self):
329
self.assertEqual('foo\xc2\xae',
330
osutils.safe_file_id('foo\xc2\xae'))
333
"""Currently, None is a valid revision_id"""
334
self.assertEqual(None, osutils.safe_file_id(None))
337
221
class TestWin32Funcs(TestCase):
338
222
"""Test that the _win32 versions of os utilities return appropriate paths."""
340
224
def test_abspath(self):
341
225
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
342
226
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
343
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
344
self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
346
228
def test_realpath(self):
347
229
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
557
421
self.assertEqual(expected_dirblocks[1:],
558
422
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
560
def test__walkdirs_utf8(self):
569
self.build_tree(tree)
570
expected_dirblocks = [
572
[('0file', '0file', 'file'),
573
('1dir', '1dir', 'directory'),
574
('2file', '2file', 'file'),
578
[('1dir/0file', '0file', 'file'),
579
('1dir/1dir', '1dir', 'directory'),
582
(('1dir/1dir', './1dir/1dir'),
589
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
590
if len(dirblock) and dirblock[0][1] == '.bzr':
591
# this tests the filtering of selected paths
594
result.append((dirdetail, dirblock))
596
self.assertTrue(found_bzrdir)
597
self.assertEqual(expected_dirblocks,
598
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
599
# you can search a subdir only, with a supplied prefix.
601
for dirblock in osutils.walkdirs('./1dir', '1dir'):
602
result.append(dirblock)
603
self.assertEqual(expected_dirblocks[1:],
604
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
606
def _filter_out_stat(self, result):
607
"""Filter out the stat value from the walkdirs result"""
608
for dirdetail, dirblock in result:
610
for info in dirblock:
611
# Ignore info[3] which is the stat
612
new_dirblock.append((info[0], info[1], info[2], info[4]))
613
dirblock[:] = new_dirblock
615
def test_unicode_walkdirs(self):
616
"""Walkdirs should always return unicode paths."""
617
name0 = u'0file-\xb6'
618
name1 = u'1dir-\u062c\u0648'
619
name2 = u'2file-\u0633'
624
name1 + '/' + name1 + '/',
628
self.build_tree(tree)
630
raise TestSkipped('Could not represent Unicode chars'
631
' in current encoding.')
632
expected_dirblocks = [
634
[(name0, name0, 'file', './' + name0),
635
(name1, name1, 'directory', './' + name1),
636
(name2, name2, 'file', './' + name2),
639
((name1, './' + name1),
640
[(name1 + '/' + name0, name0, 'file', './' + name1
642
(name1 + '/' + name1, name1, 'directory', './' + name1
646
((name1 + '/' + name1, './' + name1 + '/' + name1),
651
result = list(osutils.walkdirs('.'))
652
self._filter_out_stat(result)
653
self.assertEqual(expected_dirblocks, result)
654
result = list(osutils.walkdirs(u'./'+name1, name1))
655
self._filter_out_stat(result)
656
self.assertEqual(expected_dirblocks[1:], result)
658
def test_unicode__walkdirs_utf8(self):
659
"""Walkdirs_utf8 should always return utf8 paths.
661
The abspath portion might be in unicode or utf-8
663
name0 = u'0file-\xb6'
664
name1 = u'1dir-\u062c\u0648'
665
name2 = u'2file-\u0633'
670
name1 + '/' + name1 + '/',
674
self.build_tree(tree)
676
raise TestSkipped('Could not represent Unicode chars'
677
' in current encoding.')
678
name0 = name0.encode('utf8')
679
name1 = name1.encode('utf8')
680
name2 = name2.encode('utf8')
682
expected_dirblocks = [
684
[(name0, name0, 'file', './' + name0),
685
(name1, name1, 'directory', './' + name1),
686
(name2, name2, 'file', './' + name2),
689
((name1, './' + name1),
690
[(name1 + '/' + name0, name0, 'file', './' + name1
692
(name1 + '/' + name1, name1, 'directory', './' + name1
696
((name1 + '/' + name1, './' + name1 + '/' + name1),
702
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
703
# all abspaths are Unicode, and encode them back into utf8.
704
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
705
self.assertIsInstance(dirdetail[0], str)
706
if isinstance(dirdetail[1], unicode):
707
dirdetail[1] = dirdetail[1].encode('utf8')
708
for info in dirblock:
709
self.assertIsInstance(info[4], unicode)
710
info[4] = info[4].encode('utf8')
712
for info in dirblock:
713
self.assertIsInstance(info[0], str)
714
self.assertIsInstance(info[1], str)
715
self.assertIsInstance(info[4], str)
716
# Remove the stat information
717
new_dirblock.append((info[0], info[1], info[2], info[4]))
718
result.append((dirdetail, new_dirblock))
719
self.assertEqual(expected_dirblocks, result)
721
def test_unicode__walkdirs_unicode_to_utf8(self):
722
"""walkdirs_unicode_to_utf8 should be a safe fallback everywhere
724
The abspath portion should be in unicode
726
name0u = u'0file-\xb6'
727
name1u = u'1dir-\u062c\u0648'
728
name2u = u'2file-\u0633'
732
name1u + '/' + name0u,
733
name1u + '/' + name1u + '/',
737
self.build_tree(tree)
739
raise TestSkipped('Could not represent Unicode chars'
740
' in current encoding.')
741
name0 = name0u.encode('utf8')
742
name1 = name1u.encode('utf8')
743
name2 = name2u.encode('utf8')
745
# All of the abspaths should be in unicode, all of the relative paths
747
expected_dirblocks = [
749
[(name0, name0, 'file', './' + name0u),
750
(name1, name1, 'directory', './' + name1u),
751
(name2, name2, 'file', './' + name2u),
754
((name1, './' + name1u),
755
[(name1 + '/' + name0, name0, 'file', './' + name1u
757
(name1 + '/' + name1, name1, 'directory', './' + name1u
761
((name1 + '/' + name1, './' + name1u + '/' + name1u),
766
result = list(osutils._walkdirs_unicode_to_utf8('.'))
767
self._filter_out_stat(result)
768
self.assertEqual(expected_dirblocks, result)
770
424
def assertPathCompare(self, path_less, path_greater):
771
425
"""check that path_less and path_greater compare correctly."""
772
426
self.assertEqual(0, osutils.compare_paths_prefix_order(
898
552
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
901
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
902
# [bialix] 2006/12/26
555
class TestTerminalEncoding(TestCase):
556
"""Test the auto-detection of proper terminal encoding."""
559
self._stdout = sys.stdout
560
self._stderr = sys.stderr
561
self._stdin = sys.stdin
562
self._user_encoding = bzrlib.user_encoding
564
self.addCleanup(self._reset)
566
sys.stdout = StringIOWrapper()
567
sys.stdout.encoding = 'stdout_encoding'
568
sys.stderr = StringIOWrapper()
569
sys.stderr.encoding = 'stderr_encoding'
570
sys.stdin = StringIOWrapper()
571
sys.stdin.encoding = 'stdin_encoding'
572
bzrlib.user_encoding = 'user_encoding'
575
sys.stdout = self._stdout
576
sys.stderr = self._stderr
577
sys.stdin = self._stdin
578
bzrlib.user_encoding = self._user_encoding
580
def test_get_terminal_encoding(self):
581
# first preference is stdout encoding
582
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
584
sys.stdout.encoding = None
585
# if sys.stdout is None, fall back to sys.stdin
586
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
588
sys.stdin.encoding = None
589
# and in the worst case, use bzrlib.user_encoding
590
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
905
593
class TestSetUnsetEnv(TestCase):