~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Vincent Ladeuil
  • Date: 2011-02-10 12:37:27 UTC
  • mto: This revision was merged to the branch mainline in revision 5661.
  • Revision ID: v.ladeuil+lp@free.fr-20110210123727-8e0pu4wtlt6fj7nf
thread is already a python module, avoid confusion and use cethread instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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):
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):
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):
1614
1595
                          ('d', 'source/b', 'target/b'),
1615
1596
                          ('f', 'source/b/c', 'target/b/c'),
1616
1597
                         ], processed_files)
1617
 
        self.assertPathDoesNotExist('target')
 
1598
        self.failIfExists('target')
1618
1599
        if osutils.has_symlinks():
1619
1600
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1620
1601
 
1666
1647
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1667
1648
        self.assertEqual('foo', old)
1668
1649
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1669
 
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
 
1650
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1670
1651
 
1671
1652
 
1672
1653
class TestSizeShaFile(tests.TestCaseInTempDir):
1897
1878
        os.symlink(self.target, self.link)
1898
1879
 
1899
1880
    def test_os_readlink_link_encoding(self):
1900
 
        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))
1901
1885
 
1902
1886
    def test_os_readlink_link_decoding(self):
1903
1887
        self.assertEquals(self.target.encode(osutils._fs_enc),
2052
2036
        # Whatever the result is, if we don't raise an exception, it's ok.
2053
2037
        osutils.terminal_width()
2054
2038
 
2055
 
 
2056
2039
class TestCreationOps(tests.TestCaseInTempDir):
2057
2040
    _test_needs_features = [features.chown_feature]
2058
2041
 
2088
2071
        self.assertEquals(self.uid, s.st_uid)
2089
2072
        self.assertEquals(self.gid, s.st_gid)
2090
2073
 
2091
 
 
2092
2074
class TestGetuserUnicode(tests.TestCase):
2093
2075
 
2094
2076
    def test_ascii_user(self):
2109
2091
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2110
2092
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2111
2093
 
2112
 
    def test_no_username_bug_660174(self):
2113
 
        self.requireFeature(features.win32_feature)
2114
 
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2115
 
            self.overrideEnv(name, None)
2116
 
        self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2117
 
 
2118
 
 
2119
2094
class TestBackupNames(tests.TestCase):
2120
2095
 
2121
2096
    def setUp(self):