277
class TestSafeUtf8(TestCase):
279
def test_from_ascii_string(self):
281
self.assertEqual('foobar', osutils.safe_utf8(f))
283
def test_from_unicode_string_ascii_contents(self):
284
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
286
def test_from_unicode_string_unicode_contents(self):
287
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
289
def test_from_utf8_string(self):
290
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
292
def test_bad_utf8_string(self):
293
self.assertRaises(BzrBadParameterNotUnicode,
294
osutils.safe_utf8, '\xbb\xbb')
297
class TestSafeRevisionId(TestCase):
299
def test_from_ascii_string(self):
300
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
302
def test_from_unicode_string_ascii_contents(self):
303
self.assertEqual('bargam',
304
osutils.safe_revision_id(u'bargam', warn=False))
306
def test_from_unicode_deprecated(self):
307
self.assertEqual('bargam',
308
self.callDeprecated([osutils._revision_id_warning],
309
osutils.safe_revision_id, u'bargam'))
311
def test_from_unicode_string_unicode_contents(self):
312
self.assertEqual('bargam\xc2\xae',
313
osutils.safe_revision_id(u'bargam\xae', warn=False))
315
def test_from_utf8_string(self):
316
self.assertEqual('foo\xc2\xae',
317
osutils.safe_revision_id('foo\xc2\xae'))
320
"""Currently, None is a valid revision_id"""
321
self.assertEqual(None, osutils.safe_revision_id(None))
324
class TestSafeFileId(TestCase):
326
def test_from_ascii_string(self):
327
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
329
def test_from_unicode_string_ascii_contents(self):
330
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
332
def test_from_unicode_deprecated(self):
333
self.assertEqual('bargam',
334
self.callDeprecated([osutils._file_id_warning],
335
osutils.safe_file_id, u'bargam'))
337
def test_from_unicode_string_unicode_contents(self):
338
self.assertEqual('bargam\xc2\xae',
339
osutils.safe_file_id(u'bargam\xae', warn=False))
341
def test_from_utf8_string(self):
342
self.assertEqual('foo\xc2\xae',
343
osutils.safe_file_id('foo\xc2\xae'))
346
"""Currently, None is a valid revision_id"""
347
self.assertEqual(None, osutils.safe_file_id(None))
249
350
class TestWin32Funcs(TestCase):
250
351
"""Test that the _win32 versions of os utilities return appropriate paths."""
252
353
def test_abspath(self):
253
354
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
254
355
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
356
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
357
self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
256
359
def test_realpath(self):
257
360
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
449
570
self.assertEqual(expected_dirblocks[1:],
450
571
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
573
def test__walkdirs_utf8(self):
582
self.build_tree(tree)
583
expected_dirblocks = [
585
[('0file', '0file', 'file'),
586
('1dir', '1dir', 'directory'),
587
('2file', '2file', 'file'),
591
[('1dir/0file', '0file', 'file'),
592
('1dir/1dir', '1dir', 'directory'),
595
(('1dir/1dir', './1dir/1dir'),
602
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
603
if len(dirblock) and dirblock[0][1] == '.bzr':
604
# this tests the filtering of selected paths
607
result.append((dirdetail, dirblock))
609
self.assertTrue(found_bzrdir)
610
self.assertEqual(expected_dirblocks,
611
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
612
# you can search a subdir only, with a supplied prefix.
614
for dirblock in osutils.walkdirs('./1dir', '1dir'):
615
result.append(dirblock)
616
self.assertEqual(expected_dirblocks[1:],
617
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
619
def _filter_out_stat(self, result):
620
"""Filter out the stat value from the walkdirs result"""
621
for dirdetail, dirblock in result:
623
for info in dirblock:
624
# Ignore info[3] which is the stat
625
new_dirblock.append((info[0], info[1], info[2], info[4]))
626
dirblock[:] = new_dirblock
628
def test_unicode_walkdirs(self):
629
"""Walkdirs should always return unicode paths."""
630
name0 = u'0file-\xb6'
631
name1 = u'1dir-\u062c\u0648'
632
name2 = u'2file-\u0633'
637
name1 + '/' + name1 + '/',
641
self.build_tree(tree)
643
raise TestSkipped('Could not represent Unicode chars'
644
' in current encoding.')
645
expected_dirblocks = [
647
[(name0, name0, 'file', './' + name0),
648
(name1, name1, 'directory', './' + name1),
649
(name2, name2, 'file', './' + name2),
652
((name1, './' + name1),
653
[(name1 + '/' + name0, name0, 'file', './' + name1
655
(name1 + '/' + name1, name1, 'directory', './' + name1
659
((name1 + '/' + name1, './' + name1 + '/' + name1),
664
result = list(osutils.walkdirs('.'))
665
self._filter_out_stat(result)
666
self.assertEqual(expected_dirblocks, result)
667
result = list(osutils.walkdirs(u'./'+name1, name1))
668
self._filter_out_stat(result)
669
self.assertEqual(expected_dirblocks[1:], result)
671
def test_unicode__walkdirs_utf8(self):
672
"""Walkdirs_utf8 should always return utf8 paths.
674
The abspath portion might be in unicode or utf-8
676
name0 = u'0file-\xb6'
677
name1 = u'1dir-\u062c\u0648'
678
name2 = u'2file-\u0633'
683
name1 + '/' + name1 + '/',
687
self.build_tree(tree)
689
raise TestSkipped('Could not represent Unicode chars'
690
' in current encoding.')
691
name0 = name0.encode('utf8')
692
name1 = name1.encode('utf8')
693
name2 = name2.encode('utf8')
695
expected_dirblocks = [
697
[(name0, name0, 'file', './' + name0),
698
(name1, name1, 'directory', './' + name1),
699
(name2, name2, 'file', './' + name2),
702
((name1, './' + name1),
703
[(name1 + '/' + name0, name0, 'file', './' + name1
705
(name1 + '/' + name1, name1, 'directory', './' + name1
709
((name1 + '/' + name1, './' + name1 + '/' + name1),
715
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
716
# all abspaths are Unicode, and encode them back into utf8.
717
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
718
self.assertIsInstance(dirdetail[0], str)
719
if isinstance(dirdetail[1], unicode):
720
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
721
dirblock = [list(info) for info in dirblock]
722
for info in dirblock:
723
self.assertIsInstance(info[4], unicode)
724
info[4] = info[4].encode('utf8')
726
for info in dirblock:
727
self.assertIsInstance(info[0], str)
728
self.assertIsInstance(info[1], str)
729
self.assertIsInstance(info[4], str)
730
# Remove the stat information
731
new_dirblock.append((info[0], info[1], info[2], info[4]))
732
result.append((dirdetail, new_dirblock))
733
self.assertEqual(expected_dirblocks, result)
735
def test_unicode__walkdirs_unicode_to_utf8(self):
736
"""walkdirs_unicode_to_utf8 should be a safe fallback everywhere
738
The abspath portion should be in unicode
740
name0u = u'0file-\xb6'
741
name1u = u'1dir-\u062c\u0648'
742
name2u = u'2file-\u0633'
746
name1u + '/' + name0u,
747
name1u + '/' + name1u + '/',
751
self.build_tree(tree)
753
raise TestSkipped('Could not represent Unicode chars'
754
' in current encoding.')
755
name0 = name0u.encode('utf8')
756
name1 = name1u.encode('utf8')
757
name2 = name2u.encode('utf8')
759
# All of the abspaths should be in unicode, all of the relative paths
761
expected_dirblocks = [
763
[(name0, name0, 'file', './' + name0u),
764
(name1, name1, 'directory', './' + name1u),
765
(name2, name2, 'file', './' + name2u),
768
((name1, './' + name1u),
769
[(name1 + '/' + name0, name0, 'file', './' + name1u
771
(name1 + '/' + name1, name1, 'directory', './' + name1u
775
((name1 + '/' + name1, './' + name1u + '/' + name1u),
780
result = list(osutils._walkdirs_unicode_to_utf8('.'))
781
self._filter_out_stat(result)
782
self.assertEqual(expected_dirblocks, result)
452
784
def assertPathCompare(self, path_less, path_greater):
453
785
"""check that path_less and path_greater compare correctly."""
454
786
self.assertEqual(0, osutils.compare_paths_prefix_order(
580
912
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
583
class TestTerminalEncoding(TestCase):
584
"""Test the auto-detection of proper terminal encoding."""
587
self._stdout = sys.stdout
588
self._stderr = sys.stderr
589
self._stdin = sys.stdin
590
self._user_encoding = bzrlib.user_encoding
592
self.addCleanup(self._reset)
594
sys.stdout = StringIOWrapper()
595
sys.stdout.encoding = 'stdout_encoding'
596
sys.stderr = StringIOWrapper()
597
sys.stderr.encoding = 'stderr_encoding'
598
sys.stdin = StringIOWrapper()
599
sys.stdin.encoding = 'stdin_encoding'
600
bzrlib.user_encoding = 'user_encoding'
603
sys.stdout = self._stdout
604
sys.stderr = self._stderr
605
sys.stdin = self._stdin
606
bzrlib.user_encoding = self._user_encoding
608
def test_get_terminal_encoding(self):
609
# first preference is stdout encoding
610
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
612
sys.stdout.encoding = None
613
# if sys.stdout is None, fall back to sys.stdin
614
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
616
sys.stdin.encoding = None
617
# and in the worst case, use bzrlib.user_encoding
618
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
915
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
916
# [bialix] 2006/12/26
621
919
class TestSetUnsetEnv(TestCase):