122
90
self.assertFalse(is_inside('foo.c', ''))
123
91
self.assertTrue(is_inside('', 'foo.c'))
125
def test_is_inside_any(self):
126
SRC_FOO_C = pathjoin('src', 'foo.c')
127
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
128
(['src'], SRC_FOO_C),
131
self.assert_(is_inside_any(dirs, fn))
132
for dirs, fn in [(['src'], 'srccontrol'),
133
(['src'], 'srccontrol/foo')]:
134
self.assertFalse(is_inside_any(dirs, fn))
136
def test_is_inside_or_parent_of_any(self):
137
for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
138
(['src'], 'src/foo.c'),
139
(['src/bar.c'], 'src'),
140
(['src/bar.c', 'bla/foo.c'], 'src'),
143
self.assert_(is_inside_or_parent_of_any(dirs, fn))
145
for dirs, fn in [(['src'], 'srccontrol'),
146
(['srccontrol/foo.c'], 'src'),
147
(['src'], 'srccontrol/foo')]:
148
self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
150
93
def test_rmtree(self):
151
94
# Check to remove tree with read-only files/dirs
342
class TestSafeUtf8(TestCase):
344
def test_from_ascii_string(self):
346
self.assertEqual('foobar', osutils.safe_utf8(f))
348
def test_from_unicode_string_ascii_contents(self):
349
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
351
def test_from_unicode_string_unicode_contents(self):
352
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
354
def test_from_utf8_string(self):
355
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
357
def test_bad_utf8_string(self):
358
self.assertRaises(BzrBadParameterNotUnicode,
359
osutils.safe_utf8, '\xbb\xbb')
362
class TestSafeRevisionId(TestCase):
364
def test_from_ascii_string(self):
365
# this shouldn't give a warning because it's getting an ascii string
366
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
368
def test_from_unicode_string_ascii_contents(self):
369
self.assertEqual('bargam',
370
osutils.safe_revision_id(u'bargam', warn=False))
372
def test_from_unicode_deprecated(self):
373
self.assertEqual('bargam',
374
self.callDeprecated([osutils._revision_id_warning],
375
osutils.safe_revision_id, u'bargam'))
377
def test_from_unicode_string_unicode_contents(self):
378
self.assertEqual('bargam\xc2\xae',
379
osutils.safe_revision_id(u'bargam\xae', warn=False))
381
def test_from_utf8_string(self):
382
self.assertEqual('foo\xc2\xae',
383
osutils.safe_revision_id('foo\xc2\xae'))
386
"""Currently, None is a valid revision_id"""
387
self.assertEqual(None, osutils.safe_revision_id(None))
390
class TestSafeFileId(TestCase):
392
def test_from_ascii_string(self):
393
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
395
def test_from_unicode_string_ascii_contents(self):
396
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
398
def test_from_unicode_deprecated(self):
399
self.assertEqual('bargam',
400
self.callDeprecated([osutils._file_id_warning],
401
osutils.safe_file_id, u'bargam'))
403
def test_from_unicode_string_unicode_contents(self):
404
self.assertEqual('bargam\xc2\xae',
405
osutils.safe_file_id(u'bargam\xae', warn=False))
407
def test_from_utf8_string(self):
408
self.assertEqual('foo\xc2\xae',
409
osutils.safe_file_id('foo\xc2\xae'))
412
"""Currently, None is a valid revision_id"""
413
self.assertEqual(None, osutils.safe_file_id(None))
416
249
class TestWin32Funcs(TestCase):
417
250
"""Test that the _win32 versions of os utilities return appropriate paths."""
419
252
def test_abspath(self):
420
253
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
421
254
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
422
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
423
self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
425
256
def test_realpath(self):
426
257
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
646
449
self.assertEqual(expected_dirblocks[1:],
647
450
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
649
def test__walkdirs_utf8(self):
658
self.build_tree(tree)
659
expected_dirblocks = [
661
[('0file', '0file', 'file'),
662
('1dir', '1dir', 'directory'),
663
('2file', '2file', 'file'),
667
[('1dir/0file', '0file', 'file'),
668
('1dir/1dir', '1dir', 'directory'),
671
(('1dir/1dir', './1dir/1dir'),
678
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
679
if len(dirblock) and dirblock[0][1] == '.bzr':
680
# this tests the filtering of selected paths
683
result.append((dirdetail, dirblock))
685
self.assertTrue(found_bzrdir)
686
self.assertEqual(expected_dirblocks,
687
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
688
# you can search a subdir only, with a supplied prefix.
690
for dirblock in osutils.walkdirs('./1dir', '1dir'):
691
result.append(dirblock)
692
self.assertEqual(expected_dirblocks[1:],
693
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
695
def _filter_out_stat(self, result):
696
"""Filter out the stat value from the walkdirs result"""
697
for dirdetail, dirblock in result:
699
for info in dirblock:
700
# Ignore info[3] which is the stat
701
new_dirblock.append((info[0], info[1], info[2], info[4]))
702
dirblock[:] = new_dirblock
704
def test_unicode_walkdirs(self):
705
"""Walkdirs should always return unicode paths."""
706
name0 = u'0file-\xb6'
707
name1 = u'1dir-\u062c\u0648'
708
name2 = u'2file-\u0633'
713
name1 + '/' + name1 + '/',
717
self.build_tree(tree)
719
raise TestSkipped('Could not represent Unicode chars'
720
' in current encoding.')
721
expected_dirblocks = [
723
[(name0, name0, 'file', './' + name0),
724
(name1, name1, 'directory', './' + name1),
725
(name2, name2, 'file', './' + name2),
728
((name1, './' + name1),
729
[(name1 + '/' + name0, name0, 'file', './' + name1
731
(name1 + '/' + name1, name1, 'directory', './' + name1
735
((name1 + '/' + name1, './' + name1 + '/' + name1),
740
result = list(osutils.walkdirs('.'))
741
self._filter_out_stat(result)
742
self.assertEqual(expected_dirblocks, result)
743
result = list(osutils.walkdirs(u'./'+name1, name1))
744
self._filter_out_stat(result)
745
self.assertEqual(expected_dirblocks[1:], result)
747
def test_unicode__walkdirs_utf8(self):
748
"""Walkdirs_utf8 should always return utf8 paths.
750
The abspath portion might be in unicode or utf-8
752
name0 = u'0file-\xb6'
753
name1 = u'1dir-\u062c\u0648'
754
name2 = u'2file-\u0633'
759
name1 + '/' + name1 + '/',
763
self.build_tree(tree)
765
raise TestSkipped('Could not represent Unicode chars'
766
' in current encoding.')
767
name0 = name0.encode('utf8')
768
name1 = name1.encode('utf8')
769
name2 = name2.encode('utf8')
771
expected_dirblocks = [
773
[(name0, name0, 'file', './' + name0),
774
(name1, name1, 'directory', './' + name1),
775
(name2, name2, 'file', './' + name2),
778
((name1, './' + name1),
779
[(name1 + '/' + name0, name0, 'file', './' + name1
781
(name1 + '/' + name1, name1, 'directory', './' + name1
785
((name1 + '/' + name1, './' + name1 + '/' + name1),
791
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
792
# all abspaths are Unicode, and encode them back into utf8.
793
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
794
self.assertIsInstance(dirdetail[0], str)
795
if isinstance(dirdetail[1], unicode):
796
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
797
dirblock = [list(info) for info in dirblock]
798
for info in dirblock:
799
self.assertIsInstance(info[4], unicode)
800
info[4] = info[4].encode('utf8')
802
for info in dirblock:
803
self.assertIsInstance(info[0], str)
804
self.assertIsInstance(info[1], str)
805
self.assertIsInstance(info[4], str)
806
# Remove the stat information
807
new_dirblock.append((info[0], info[1], info[2], info[4]))
808
result.append((dirdetail, new_dirblock))
809
self.assertEqual(expected_dirblocks, result)
811
def test_unicode__walkdirs_unicode_to_utf8(self):
812
"""walkdirs_unicode_to_utf8 should be a safe fallback everywhere
814
The abspath portion should be in unicode
816
name0u = u'0file-\xb6'
817
name1u = u'1dir-\u062c\u0648'
818
name2u = u'2file-\u0633'
822
name1u + '/' + name0u,
823
name1u + '/' + name1u + '/',
827
self.build_tree(tree)
829
raise TestSkipped('Could not represent Unicode chars'
830
' in current encoding.')
831
name0 = name0u.encode('utf8')
832
name1 = name1u.encode('utf8')
833
name2 = name2u.encode('utf8')
835
# All of the abspaths should be in unicode, all of the relative paths
837
expected_dirblocks = [
839
[(name0, name0, 'file', './' + name0u),
840
(name1, name1, 'directory', './' + name1u),
841
(name2, name2, 'file', './' + name2u),
844
((name1, './' + name1u),
845
[(name1 + '/' + name0, name0, 'file', './' + name1u
847
(name1 + '/' + name1, name1, 'directory', './' + name1u
851
((name1 + '/' + name1, './' + name1u + '/' + name1u),
856
result = list(osutils._walkdirs_unicode_to_utf8('.'))
857
self._filter_out_stat(result)
858
self.assertEqual(expected_dirblocks, result)
860
452
def assertPathCompare(self, path_less, path_greater):
861
453
"""check that path_less and path_greater compare correctly."""
862
454
self.assertEqual(0, osutils.compare_paths_prefix_order(
1061
664
self.assertTrue(isinstance(offset, int))
1062
665
eighteen_hours = 18 * 3600
1063
666
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1066
class TestShaFileByName(TestCaseInTempDir):
1068
def test_sha_empty(self):
1069
self.build_tree_contents([('foo', '')])
1070
expected_sha = osutils.sha_string('')
1071
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1073
def test_sha_mixed_endings(self):
1074
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1075
self.build_tree_contents([('foo', text)])
1076
expected_sha = osutils.sha_string(text)
1077
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1081
r'''# Copyright (C) 2005, 2006 Canonical Ltd
1083
# This program is free software; you can redistribute it and/or modify
1084
# it under the terms of the GNU General Public License as published by
1085
# the Free Software Foundation; either version 2 of the License, or
1086
# (at your option) any later version.
1088
# This program is distributed in the hope that it will be useful,
1089
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1090
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1091
# GNU General Public License for more details.
1093
# You should have received a copy of the GNU General Public License
1094
# along with this program; if not, write to the Free Software
1095
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1098
# NOTE: If update these, please also update the help for global-options in
1099
# bzrlib/help_topics/__init__.py
1102
"""Set of flags that enable different debug behaviour.
1104
These are set with eg ``-Dlock`` on the bzr command line.
1108
* auth - show authentication sections used
1109
* error - show stack traces for all top level exceptions
1110
* evil - capture call sites that do expensive or badly-scaling operations.
1111
* fetch - trace history copying between repositories
1112
* hashcache - log every time a working file is read to determine its hash
1113
* hooks - trace hook execution
1114
* hpss - trace smart protocol requests and responses
1115
* http - trace http connections, requests and responses
1116
* index - trace major index operations
1117
* knit - trace knit operations
1118
* lock - trace when lockdir locks are taken or released
1119
* merge - emit information for debugging merges
1120
* pack - emit information about pack operations
1126
class TestResourceLoading(TestCaseInTempDir):
1128
def test_resource_string(self):
1129
# test resource in bzrlib
1130
text = osutils.resource_string('bzrlib', 'debug.py')
1131
self.assertEquals(_debug_text, text)
1132
# test resource under bzrlib
1133
text = osutils.resource_string('bzrlib.ui', 'text.py')
1134
self.assertContainsRe(text, "class TextUIFactory")
1135
# test unsupported package
1136
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1138
# test unknown resource
1139
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')