~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

(gz) Fix test failure on alpha by correcting format string for
 gc_chk_sha1_record (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
42
 
43
43
 
44
 
class _UTF8DirReaderFeature(features.Feature):
 
44
class _UTF8DirReaderFeature(tests.Feature):
45
45
 
46
46
    def _probe(self):
47
47
        try:
54
54
    def feature_name(self):
55
55
        return 'bzrlib._readdir_pyx'
56
56
 
57
 
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
 
57
UTF8DirReaderFeature = _UTF8DirReaderFeature()
58
58
 
59
 
term_ios_feature = features.ModuleAvailableFeature('termios')
 
59
term_ios_feature = tests.ModuleAvailableFeature('termios')
60
60
 
61
61
 
62
62
def _already_unicode(s):
103
103
class TestContainsWhitespace(tests.TestCase):
104
104
 
105
105
    def test_contains_whitespace(self):
106
 
        self.assertTrue(osutils.contains_whitespace(u' '))
107
 
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
108
 
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
109
 
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
110
 
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
111
 
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
 
106
        self.failUnless(osutils.contains_whitespace(u' '))
 
107
        self.failUnless(osutils.contains_whitespace(u'hello there'))
 
108
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
 
109
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
 
110
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
 
111
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
112
112
 
113
113
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
114
114
        # is whitespace, but we do not.
115
 
        self.assertFalse(osutils.contains_whitespace(u''))
116
 
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
117
 
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
 
115
        self.failIf(osutils.contains_whitespace(u''))
 
116
        self.failIf(osutils.contains_whitespace(u'hellothere'))
 
117
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
118
118
 
119
119
 
120
120
class TestRename(tests.TestCaseInTempDir):
134
134
        # This should work everywhere
135
135
        self.create_file('a', 'something in a\n')
136
136
        self._fancy_rename('a', 'b')
137
 
        self.assertPathDoesNotExist('a')
138
 
        self.assertPathExists('b')
 
137
        self.failIfExists('a')
 
138
        self.failUnlessExists('b')
139
139
        self.check_file_contents('b', 'something in a\n')
140
140
 
141
141
        self.create_file('a', 'new something in a\n')
148
148
        self.create_file('target', 'data in target\n')
149
149
        self.assertRaises((IOError, OSError), self._fancy_rename,
150
150
                          'missingsource', 'target')
151
 
        self.assertPathExists('target')
 
151
        self.failUnlessExists('target')
152
152
        self.check_file_contents('target', 'data in target\n')
153
153
 
154
154
    def test_fancy_rename_fails_if_source_and_target_missing(self):
159
159
        # Rename should be semi-atomic on all platforms
160
160
        self.create_file('a', 'something in a\n')
161
161
        osutils.rename('a', 'b')
162
 
        self.assertPathDoesNotExist('a')
163
 
        self.assertPathExists('b')
 
162
        self.failIfExists('a')
 
163
        self.failUnlessExists('b')
164
164
        self.check_file_contents('b', 'something in a\n')
165
165
 
166
166
        self.create_file('a', 'new something in a\n')
231
231
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
232
232
 
233
233
 
234
 
class TestLstat(tests.TestCaseInTempDir):
235
 
 
236
 
    def test_lstat_matches_fstat(self):
237
 
        # On Windows, lstat and fstat don't always agree, primarily in the
238
 
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
239
 
        # custom implementation.
240
 
        if sys.platform == 'win32':
241
 
            # We only have special lstat/fstat if we have the extension.
242
 
            # Without it, we may end up re-reading content when we don't have
243
 
            # to, but otherwise it doesn't effect correctness.
244
 
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
245
 
        f = open('test-file.txt', 'wb')
246
 
        self.addCleanup(f.close)
247
 
        f.write('some content\n')
248
 
        f.flush()
249
 
        self.assertEqualStat(osutils.fstat(f.fileno()),
250
 
                             osutils.lstat('test-file.txt'))
251
 
 
252
 
 
253
234
class TestRmTree(tests.TestCaseInTempDir):
254
235
 
255
236
    def test_rmtree(self):
267
248
 
268
249
        osutils.rmtree('dir')
269
250
 
270
 
        self.assertPathDoesNotExist('dir/file')
271
 
        self.assertPathDoesNotExist('dir')
 
251
        self.failIfExists('dir/file')
 
252
        self.failIfExists('dir')
272
253
 
273
254
 
274
255
class TestDeleteAny(tests.TestCaseInTempDir):
429
410
class TestLinks(tests.TestCaseInTempDir):
430
411
 
431
412
    def test_dereference_path(self):
432
 
        self.requireFeature(features.SymlinkFeature)
 
413
        self.requireFeature(tests.SymlinkFeature)
433
414
        cwd = osutils.realpath('.')
434
415
        os.mkdir('bar')
435
416
        bar_path = osutils.pathjoin(cwd, 'bar')
482
463
 
483
464
class TestCanonicalRelPath(tests.TestCaseInTempDir):
484
465
 
485
 
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
 
466
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
486
467
 
487
468
    def test_canonical_relpath_simple(self):
488
469
        f = file('MixedCaseName', 'w')
489
470
        f.close()
490
471
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
 
        self.assertEqual('work/MixedCaseName', actual)
 
472
        self.failUnlessEqual('work/MixedCaseName', actual)
492
473
 
493
474
    def test_canonical_relpath_missing_tail(self):
494
475
        os.mkdir('MixedCaseParent')
495
476
        actual = osutils.canonical_relpath(self.test_base_dir,
496
477
                                           'mixedcaseparent/nochild')
497
 
        self.assertEqual('work/MixedCaseParent/nochild', actual)
 
478
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
498
479
 
499
480
 
500
481
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
888
869
    """Test win32 functions that create files."""
889
870
 
890
871
    def test_getcwd(self):
891
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
872
        self.requireFeature(tests.UnicodeFilenameFeature)
892
873
        os.mkdir(u'mu-\xb5')
893
874
        os.chdir(u'mu-\xb5')
894
875
        # TODO: jam 20060427 This will probably fail on Mac OSX because
924
905
        b.close()
925
906
 
926
907
        osutils._win32_rename('b', 'a')
927
 
        self.assertPathExists('a')
928
 
        self.assertPathDoesNotExist('b')
 
908
        self.failUnlessExists('a')
 
909
        self.failIfExists('b')
929
910
        self.assertFileEqual('baz\n', 'a')
930
911
 
931
912
    def test_rename_missing_file(self):
984
965
    """Test mac special functions that require directories."""
985
966
 
986
967
    def test_getcwd(self):
987
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
968
        self.requireFeature(tests.UnicodeFilenameFeature)
988
969
        os.mkdir(u'B\xe5gfors')
989
970
        os.chdir(u'B\xe5gfors')
990
971
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
991
972
 
992
973
    def test_getcwd_nonnorm(self):
993
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
974
        self.requireFeature(tests.UnicodeFilenameFeature)
994
975
        # Test that _mac_getcwd() will normalize this path
995
976
        os.mkdir(u'Ba\u030agfors')
996
977
        os.chdir(u'Ba\u030agfors')
1106
1087
        # are not using the filesystem's encoding
1107
1088
 
1108
1089
        # require a bytestring based filesystem
1109
 
        self.requireFeature(features.ByteStringNamedFilesystem)
 
1090
        self.requireFeature(tests.ByteStringNamedFilesystem)
1110
1091
 
1111
1092
        tree = [
1112
1093
            '.bzr',
1207
1188
        self._save_platform_info()
1208
1189
        win32utils.winver = None # Avoid the win32 detection code
1209
1190
        osutils._fs_enc = 'UTF-8'
1210
 
        self.assertDirReaderIs(
1211
 
            UTF8DirReaderFeature.module.UTF8DirReader)
 
1191
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1212
1192
 
1213
1193
    def test_force_walkdirs_utf8_fs_ascii(self):
1214
1194
        self.requireFeature(UTF8DirReaderFeature)
1215
1195
        self._save_platform_info()
1216
1196
        win32utils.winver = None # Avoid the win32 detection code
1217
1197
        osutils._fs_enc = 'US-ASCII'
1218
 
        self.assertDirReaderIs(
1219
 
            UTF8DirReaderFeature.module.UTF8DirReader)
 
1198
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1220
1199
 
1221
1200
    def test_force_walkdirs_utf8_fs_ANSI(self):
1222
1201
        self.requireFeature(UTF8DirReaderFeature)
1223
1202
        self._save_platform_info()
1224
1203
        win32utils.winver = None # Avoid the win32 detection code
1225
1204
        osutils._fs_enc = 'ANSI_X3.4-1968'
1226
 
        self.assertDirReaderIs(
1227
 
            UTF8DirReaderFeature.module.UTF8DirReader)
 
1205
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1228
1206
 
1229
1207
    def test_force_walkdirs_utf8_fs_latin1(self):
1230
1208
        self._save_platform_info()
1248
1226
 
1249
1227
    def test_unicode_walkdirs(self):
1250
1228
        """Walkdirs should always return unicode paths."""
1251
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1229
        self.requireFeature(tests.UnicodeFilenameFeature)
1252
1230
        name0 = u'0file-\xb6'
1253
1231
        name1 = u'1dir-\u062c\u0648'
1254
1232
        name2 = u'2file-\u0633'
1291
1269
 
1292
1270
        The abspath portion might be in unicode or utf-8
1293
1271
        """
1294
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1272
        self.requireFeature(tests.UnicodeFilenameFeature)
1295
1273
        name0 = u'0file-\xb6'
1296
1274
        name1 = u'1dir-\u062c\u0648'
1297
1275
        name2 = u'2file-\u0633'
1352
1330
 
1353
1331
        The abspath portion should be in unicode
1354
1332
        """
1355
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1333
        self.requireFeature(tests.UnicodeFilenameFeature)
1356
1334
        # Use the unicode reader. TODO: split into driver-and-driven unit
1357
1335
        # tests.
1358
1336
        self._save_platform_info()
1399
1377
 
1400
1378
    def test__walkdirs_utf8_win32readdir(self):
1401
1379
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1402
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1380
        self.requireFeature(tests.UnicodeFilenameFeature)
1403
1381
        from bzrlib._walkdirs_win32 import Win32ReadDir
1404
1382
        self._save_platform_info()
1405
1383
        osutils._selected_dir_reader = Win32ReadDir()
1456
1434
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1457
1435
        """make sure our Stat values are valid"""
1458
1436
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1459
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1437
        self.requireFeature(tests.UnicodeFilenameFeature)
1460
1438
        from bzrlib._walkdirs_win32 import Win32ReadDir
1461
1439
        name0u = u'0file-\xb6'
1462
1440
        name0 = name0u.encode('utf8')
1480
1458
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1481
1459
        """make sure our Stat values are valid"""
1482
1460
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1483
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1461
        self.requireFeature(tests.UnicodeFilenameFeature)
1484
1462
        from bzrlib._walkdirs_win32 import Win32ReadDir
1485
1463
        name0u = u'0dir-\u062c\u0648'
1486
1464
        name0 = name0u.encode('utf8')
1586
1564
        self.assertEqual(['c'], os.listdir('target/b'))
1587
1565
 
1588
1566
    def test_copy_tree_symlinks(self):
1589
 
        self.requireFeature(features.SymlinkFeature)
 
1567
        self.requireFeature(tests.SymlinkFeature)
1590
1568
        self.build_tree(['source/'])
1591
1569
        os.symlink('a/generic/path', 'source/lnk')
1592
1570
        osutils.copy_tree('source', 'target')
1617
1595
                          ('d', 'source/b', 'target/b'),
1618
1596
                          ('f', 'source/b/c', 'target/b/c'),
1619
1597
                         ], processed_files)
1620
 
        self.assertPathDoesNotExist('target')
 
1598
        self.failIfExists('target')
1621
1599
        if osutils.has_symlinks():
1622
1600
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1623
1601
 
1669
1647
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1670
1648
        self.assertEqual('foo', old)
1671
1649
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1672
 
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
 
1650
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1673
1651
 
1674
1652
 
1675
1653
class TestSizeShaFile(tests.TestCaseInTempDir):
1859
1837
        return filtered_dirblocks
1860
1838
 
1861
1839
    def test_walk_unicode_tree(self):
1862
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1840
        self.requireFeature(tests.UnicodeFilenameFeature)
1863
1841
        tree, expected_dirblocks = self._get_unicode_tree()
1864
1842
        self.build_tree(tree)
1865
1843
        result = list(osutils._walkdirs_utf8('.'))
1866
1844
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1867
1845
 
1868
1846
    def test_symlink(self):
1869
 
        self.requireFeature(features.SymlinkFeature)
1870
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
1847
        self.requireFeature(tests.SymlinkFeature)
 
1848
        self.requireFeature(tests.UnicodeFilenameFeature)
1871
1849
        target = u'target\N{Euro Sign}'
1872
1850
        link_name = u'l\N{Euro Sign}nk'
1873
1851
        os.symlink(target, link_name)
1891
1869
    But prior python versions failed to properly encode the passed unicode
1892
1870
    string.
1893
1871
    """
1894
 
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
 
1872
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1895
1873
 
1896
1874
    def setUp(self):
1897
1875
        super(tests.TestCaseInTempDir, self).setUp()
1900
1878
        os.symlink(self.target, self.link)
1901
1879
 
1902
1880
    def test_os_readlink_link_encoding(self):
1903
 
        self.assertEquals(self.target,  os.readlink(self.link))
 
1881
        if sys.version_info < (2, 6):
 
1882
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
 
1883
        else:
 
1884
            self.assertEquals(self.target,  os.readlink(self.link))
1904
1885
 
1905
1886
    def test_os_readlink_link_decoding(self):
1906
1887
        self.assertEquals(self.target.encode(osutils._fs_enc),
2055
2036
        # Whatever the result is, if we don't raise an exception, it's ok.
2056
2037
        osutils.terminal_width()
2057
2038
 
2058
 
 
2059
2039
class TestCreationOps(tests.TestCaseInTempDir):
2060
2040
    _test_needs_features = [features.chown_feature]
2061
2041
 
2091
2071
        self.assertEquals(self.uid, s.st_uid)
2092
2072
        self.assertEquals(self.gid, s.st_gid)
2093
2073
 
2094
 
 
2095
2074
class TestGetuserUnicode(tests.TestCase):
2096
2075
 
2097
2076
    def test_ascii_user(self):
2112
2091
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2113
2092
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2114
2093
 
2115
 
    def test_no_username_bug_660174(self):
2116
 
        self.requireFeature(features.win32_feature)
2117
 
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2118
 
            self.overrideEnv(name, None)
2119
 
        self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2120
 
 
2121
 
 
2122
2094
class TestBackupNames(tests.TestCase):
2123
2095
 
2124
2096
    def setUp(self):