186
218
self.assertFormatedDelta('1 second in the future', -1)
187
219
self.assertFormatedDelta('2 seconds in the future', -2)
221
def test_dereference_path(self):
222
if not osutils.has_symlinks():
223
raise TestSkipped('Symlinks are not supported on this platform')
224
cwd = osutils.realpath('.')
226
bar_path = osutils.pathjoin(cwd, 'bar')
227
# Using './' to avoid bug #1213894 (first path component not
228
# dereferenced) in Python 2.4.1 and earlier
229
self.assertEqual(bar_path, osutils.realpath('./bar'))
230
os.symlink('bar', 'foo')
231
self.assertEqual(bar_path, osutils.realpath('./foo'))
233
# Does not dereference terminal symlinks
234
foo_path = osutils.pathjoin(cwd, 'foo')
235
self.assertEqual(foo_path, osutils.dereference_path('./foo'))
237
# Dereferences parent symlinks
239
baz_path = osutils.pathjoin(bar_path, 'baz')
240
self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
242
# Dereferences parent symlinks that are the first path element
243
self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
245
# Dereferences parent symlinks in absolute paths
246
foo_baz_path = osutils.pathjoin(foo_path, 'baz')
247
self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
250
def test_changing_access(self):
251
f = file('file', 'w')
255
# Make a file readonly
256
osutils.make_readonly('file')
257
mode = osutils.lstat('file').st_mode
258
self.assertEqual(mode, mode & 0777555)
260
# Make a file writable
261
osutils.make_writable('file')
262
mode = osutils.lstat('file').st_mode
263
self.assertEqual(mode, mode | 0200)
265
if osutils.has_symlinks():
266
# should not error when handed a symlink
267
os.symlink('nonexistent', 'dangling')
268
osutils.make_readonly('dangling')
269
osutils.make_writable('dangling')
272
def test_kind_marker(self):
273
self.assertEqual("", osutils.kind_marker("file"))
274
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
275
self.assertEqual("@", osutils.kind_marker("symlink"))
276
self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
190
279
class TestSafeUnicode(TestCase):
299
class TestSafeUtf8(TestCase):
301
def test_from_ascii_string(self):
303
self.assertEqual('foobar', osutils.safe_utf8(f))
305
def test_from_unicode_string_ascii_contents(self):
306
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
308
def test_from_unicode_string_unicode_contents(self):
309
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
311
def test_from_utf8_string(self):
312
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
314
def test_bad_utf8_string(self):
315
self.assertRaises(BzrBadParameterNotUnicode,
316
osutils.safe_utf8, '\xbb\xbb')
319
class TestSafeRevisionId(TestCase):
321
def test_from_ascii_string(self):
322
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
324
def test_from_unicode_string_ascii_contents(self):
325
self.assertEqual('bargam',
326
osutils.safe_revision_id(u'bargam', warn=False))
328
def test_from_unicode_deprecated(self):
329
self.assertEqual('bargam',
330
self.callDeprecated([osutils._revision_id_warning],
331
osutils.safe_revision_id, u'bargam'))
333
def test_from_unicode_string_unicode_contents(self):
334
self.assertEqual('bargam\xc2\xae',
335
osutils.safe_revision_id(u'bargam\xae', warn=False))
337
def test_from_utf8_string(self):
338
self.assertEqual('foo\xc2\xae',
339
osutils.safe_revision_id('foo\xc2\xae'))
342
"""Currently, None is a valid revision_id"""
343
self.assertEqual(None, osutils.safe_revision_id(None))
346
class TestSafeFileId(TestCase):
348
def test_from_ascii_string(self):
349
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
351
def test_from_unicode_string_ascii_contents(self):
352
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
354
def test_from_unicode_deprecated(self):
355
self.assertEqual('bargam',
356
self.callDeprecated([osutils._file_id_warning],
357
osutils.safe_file_id, u'bargam'))
359
def test_from_unicode_string_unicode_contents(self):
360
self.assertEqual('bargam\xc2\xae',
361
osutils.safe_file_id(u'bargam\xae', warn=False))
363
def test_from_utf8_string(self):
364
self.assertEqual('foo\xc2\xae',
365
osutils.safe_file_id('foo\xc2\xae'))
368
"""Currently, None is a valid revision_id"""
369
self.assertEqual(None, osutils.safe_file_id(None))
210
372
class TestWin32Funcs(TestCase):
211
373
"""Test that the _win32 versions of os utilities return appropriate paths."""
213
375
def test_abspath(self):
214
376
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
215
377
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
378
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
379
self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
217
381
def test_realpath(self):
218
382
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
397
592
self.assertEqual(expected_dirblocks[1:],
398
593
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
595
def test__walkdirs_utf8(self):
604
self.build_tree(tree)
605
expected_dirblocks = [
607
[('0file', '0file', 'file'),
608
('1dir', '1dir', 'directory'),
609
('2file', '2file', 'file'),
613
[('1dir/0file', '0file', 'file'),
614
('1dir/1dir', '1dir', 'directory'),
617
(('1dir/1dir', './1dir/1dir'),
624
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
625
if len(dirblock) and dirblock[0][1] == '.bzr':
626
# this tests the filtering of selected paths
629
result.append((dirdetail, dirblock))
631
self.assertTrue(found_bzrdir)
632
self.assertEqual(expected_dirblocks,
633
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
634
# you can search a subdir only, with a supplied prefix.
636
for dirblock in osutils.walkdirs('./1dir', '1dir'):
637
result.append(dirblock)
638
self.assertEqual(expected_dirblocks[1:],
639
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
641
def _filter_out_stat(self, result):
642
"""Filter out the stat value from the walkdirs result"""
643
for dirdetail, dirblock in result:
645
for info in dirblock:
646
# Ignore info[3] which is the stat
647
new_dirblock.append((info[0], info[1], info[2], info[4]))
648
dirblock[:] = new_dirblock
650
def test_unicode_walkdirs(self):
651
"""Walkdirs should always return unicode paths."""
652
name0 = u'0file-\xb6'
653
name1 = u'1dir-\u062c\u0648'
654
name2 = u'2file-\u0633'
659
name1 + '/' + name1 + '/',
663
self.build_tree(tree)
665
raise TestSkipped('Could not represent Unicode chars'
666
' in current encoding.')
667
expected_dirblocks = [
669
[(name0, name0, 'file', './' + name0),
670
(name1, name1, 'directory', './' + name1),
671
(name2, name2, 'file', './' + name2),
674
((name1, './' + name1),
675
[(name1 + '/' + name0, name0, 'file', './' + name1
677
(name1 + '/' + name1, name1, 'directory', './' + name1
681
((name1 + '/' + name1, './' + name1 + '/' + name1),
686
result = list(osutils.walkdirs('.'))
687
self._filter_out_stat(result)
688
self.assertEqual(expected_dirblocks, result)
689
result = list(osutils.walkdirs(u'./'+name1, name1))
690
self._filter_out_stat(result)
691
self.assertEqual(expected_dirblocks[1:], result)
693
def test_unicode__walkdirs_utf8(self):
694
"""Walkdirs_utf8 should always return utf8 paths.
696
The abspath portion might be in unicode or utf-8
698
name0 = u'0file-\xb6'
699
name1 = u'1dir-\u062c\u0648'
700
name2 = u'2file-\u0633'
705
name1 + '/' + name1 + '/',
709
self.build_tree(tree)
711
raise TestSkipped('Could not represent Unicode chars'
712
' in current encoding.')
713
name0 = name0.encode('utf8')
714
name1 = name1.encode('utf8')
715
name2 = name2.encode('utf8')
717
expected_dirblocks = [
719
[(name0, name0, 'file', './' + name0),
720
(name1, name1, 'directory', './' + name1),
721
(name2, name2, 'file', './' + name2),
724
((name1, './' + name1),
725
[(name1 + '/' + name0, name0, 'file', './' + name1
727
(name1 + '/' + name1, name1, 'directory', './' + name1
731
((name1 + '/' + name1, './' + name1 + '/' + name1),
737
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
738
# all abspaths are Unicode, and encode them back into utf8.
739
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
740
self.assertIsInstance(dirdetail[0], str)
741
if isinstance(dirdetail[1], unicode):
742
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
743
dirblock = [list(info) for info in dirblock]
744
for info in dirblock:
745
self.assertIsInstance(info[4], unicode)
746
info[4] = info[4].encode('utf8')
748
for info in dirblock:
749
self.assertIsInstance(info[0], str)
750
self.assertIsInstance(info[1], str)
751
self.assertIsInstance(info[4], str)
752
# Remove the stat information
753
new_dirblock.append((info[0], info[1], info[2], info[4]))
754
result.append((dirdetail, new_dirblock))
755
self.assertEqual(expected_dirblocks, result)
757
def test_unicode__walkdirs_unicode_to_utf8(self):
758
"""walkdirs_unicode_to_utf8 should be a safe fallback everywhere
760
The abspath portion should be in unicode
762
name0u = u'0file-\xb6'
763
name1u = u'1dir-\u062c\u0648'
764
name2u = u'2file-\u0633'
768
name1u + '/' + name0u,
769
name1u + '/' + name1u + '/',
773
self.build_tree(tree)
775
raise TestSkipped('Could not represent Unicode chars'
776
' in current encoding.')
777
name0 = name0u.encode('utf8')
778
name1 = name1u.encode('utf8')
779
name2 = name2u.encode('utf8')
781
# All of the abspaths should be in unicode, all of the relative paths
783
expected_dirblocks = [
785
[(name0, name0, 'file', './' + name0u),
786
(name1, name1, 'directory', './' + name1u),
787
(name2, name2, 'file', './' + name2u),
790
((name1, './' + name1u),
791
[(name1 + '/' + name0, name0, 'file', './' + name1u
793
(name1 + '/' + name1, name1, 'directory', './' + name1u
797
((name1 + '/' + name1, './' + name1u + '/' + name1u),
802
result = list(osutils._walkdirs_unicode_to_utf8('.'))
803
self._filter_out_stat(result)
804
self.assertEqual(expected_dirblocks, result)
400
806
def assertPathCompare(self, path_less, path_greater):
401
807
"""check that path_less and path_greater compare correctly."""
402
808
self.assertEqual(0, osutils.compare_paths_prefix_order(
528
934
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
531
class TestTerminalEncoding(TestCase):
532
"""Test the auto-detection of proper terminal encoding."""
535
self._stdout = sys.stdout
536
self._stderr = sys.stderr
537
self._stdin = sys.stdin
538
self._user_encoding = bzrlib.user_encoding
540
self.addCleanup(self._reset)
542
sys.stdout = StringIOWrapper()
543
sys.stdout.encoding = 'stdout_encoding'
544
sys.stderr = StringIOWrapper()
545
sys.stderr.encoding = 'stderr_encoding'
546
sys.stdin = StringIOWrapper()
547
sys.stdin.encoding = 'stdin_encoding'
548
bzrlib.user_encoding = 'user_encoding'
551
sys.stdout = self._stdout
552
sys.stderr = self._stderr
553
sys.stdin = self._stdin
554
bzrlib.user_encoding = self._user_encoding
556
def test_get_terminal_encoding(self):
557
# first preference is stdout encoding
558
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
560
sys.stdout.encoding = None
561
# if sys.stdout is None, fall back to sys.stdin
562
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
564
sys.stdin.encoding = None
565
# and in the worst case, use bzrlib.user_encoding
566
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
937
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
938
# [bialix] 2006/12/26
569
941
class TestSetUnsetEnv(TestCase):