557
557
self.assertEqual(expected_dirblocks[1:],
558
558
[(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)
560
770
def assertPathCompare(self, path_less, path_greater):
561
771
"""check that path_less and path_greater compare correctly."""
562
772
self.assertEqual(0, osutils.compare_paths_prefix_order(