/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3959.1.4 by Martin Pool
test_resource_string shouldn't depend on the precise source file contents
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
16
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
17
"""Tests for the osutils wrapper."""
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
18
3504.4.12 by John Arbash Meinel
A couple small cleanups, make test_osutils more correct
19
from cStringIO import StringIO
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
20
import errno
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
21
import os
4183.6.4 by Martin Pool
Separate out re_compile_checked
22
import re
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
23
import socket
24
import stat
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
25
import sys
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
26
import time
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
27
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
28
from bzrlib import (
29
    errors,
30
    osutils,
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
31
    tests,
4695.4.1 by Martin Pool
Give a shorter/cleaner message for missing extensions
32
    trace,
2279.4.1 by Alexander Belchenko
Reimplementation of ntpath.abspath in Python for Windows98: unicode safe, UNC path safe
33
    win32utils,
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
34
    )
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
35
from bzrlib.tests import (
36
    file_utils,
37
    test__walkdirs_win32,
38
    )
4297.1.1 by Vincent Ladeuil
Trivial cleanups.
39
40
4241.14.3 by Vincent Ladeuil
Cleanup imports.
41
class _UTF8DirReaderFeature(tests.Feature):
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
42
43
    def _probe(self):
44
        try:
45
            from bzrlib import _readdir_pyx
3696.3.5 by Robert Collins
Streamline _walkdirs_utf8 for utf8 file systems, reducing time to traverse a mozilla tree from 1s to .6 seconds. (Robert Collins)
46
            self.reader = _readdir_pyx.UTF8DirReader
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
47
            return True
48
        except ImportError:
49
            return False
50
51
    def feature_name(self):
1739.2.13 by Robert Collins
Fix typo in ReadDirFeature.
52
        return 'bzrlib._readdir_pyx'
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
53
3696.3.5 by Robert Collins
Streamline _walkdirs_utf8 for utf8 file systems, reducing time to traverse a mozilla tree from 1s to .6 seconds. (Robert Collins)
54
UTF8DirReaderFeature = _UTF8DirReaderFeature()
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
55
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
56
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
57
def _already_unicode(s):
58
    return s
59
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
60
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
61
def _fs_enc_to_unicode(s):
62
    return s.decode(osutils._fs_enc)
63
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
64
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
65
def _utf8_to_unicode(s):
66
    return s.decode('UTF-8')
67
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
68
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
69
def dir_reader_scenarios():
70
    # For each dir reader we define:
71
72
    # - native_to_unicode: a function converting the native_abspath as returned
73
    #   by DirReader.read_dir to its unicode representation
74
75
    # UnicodeDirReader is the fallback, it should be tested on all platforms.
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
76
    scenarios = [('unicode',
77
                  dict(_dir_reader_class=osutils.UnicodeDirReader,
78
                       _native_to_unicode=_already_unicode))]
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
79
    # Some DirReaders are platform specific and even there they may not be
80
    # available.
81
    if UTF8DirReaderFeature.available():
82
        from bzrlib import _readdir_pyx
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
83
        scenarios.append(('utf8',
84
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
85
                               _native_to_unicode=_utf8_to_unicode)))
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
86
87
    if test__walkdirs_win32.Win32ReadDirFeature.available():
88
        try:
89
            from bzrlib import _walkdirs_win32
90
            # TODO: check on windows, it may be that we need to use/add
91
            # safe_unicode instead of _fs_enc_to_unicode
4241.14.24 by Vincent Ladeuil
Fixed as per John's review.
92
            scenarios.append(
93
                ('win32',
94
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
95
                      _native_to_unicode=_fs_enc_to_unicode)))
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
96
        except ImportError:
97
            pass
98
    return scenarios
99
100
101
def load_tests(basic_tests, module, loader):
102
    suite = loader.suiteClass()
103
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
104
        basic_tests, tests.condition_isinstance(TestDirReader))
105
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
106
    suite.addTest(remaining_tests)
107
    return suite
108
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
109
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
110
class TestContainsWhitespace(tests.TestCase):
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
111
2249.2.1 by John Arbash Meinel
(John Arbash Meinel) hard-code the whitespace chars to avoid problems in some locales.
112
    def test_contains_whitespace(self):
113
        self.failUnless(osutils.contains_whitespace(u' '))
114
        self.failUnless(osutils.contains_whitespace(u'hello there'))
115
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
116
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
117
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
118
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
119
120
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
121
        # is whitespace, but we do not.
122
        self.failIf(osutils.contains_whitespace(u''))
123
        self.failIf(osutils.contains_whitespace(u'hellothere'))
124
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
125
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
126
127
class TestRename(tests.TestCaseInTempDir):
128
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
129
    def create_file(self, filename, content):
130
        f = open(filename, 'wb')
131
        try:
132
            f.write(content)
133
        finally:
134
            f.close()
135
4789.17.2 by John Arbash Meinel
Also handle the case when source *and* target does not exist.
136
    def _fancy_rename(self, a, b):
137
        osutils.fancy_rename(a, b, rename_func=os.rename,
138
                             unlink_func=os.unlink)
139
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
140
    def test_fancy_rename(self):
141
        # This should work everywhere
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
142
        self.create_file('a', 'something in a\n')
4789.17.2 by John Arbash Meinel
Also handle the case when source *and* target does not exist.
143
        self._fancy_rename('a', 'b')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
144
        self.failIfExists('a')
145
        self.failUnlessExists('b')
146
        self.check_file_contents('b', 'something in a\n')
147
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
148
        self.create_file('a', 'new something in a\n')
4789.17.2 by John Arbash Meinel
Also handle the case when source *and* target does not exist.
149
        self._fancy_rename('b', 'a')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
150
151
        self.check_file_contents('a', 'something in a\n')
152
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
153
    def test_fancy_rename_fails_source_missing(self):
154
        # An exception should be raised, and the target should be left in place
155
        self.create_file('target', 'data in target\n')
4789.17.2 by John Arbash Meinel
Also handle the case when source *and* target does not exist.
156
        self.assertRaises((IOError, OSError), self._fancy_rename,
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
157
                          'missingsource', 'target')
158
        self.failUnlessExists('target')
159
        self.check_file_contents('target', 'data in target\n')
160
4789.17.2 by John Arbash Meinel
Also handle the case when source *and* target does not exist.
161
    def test_fancy_rename_fails_if_source_and_target_missing(self):
162
        self.assertRaises((IOError, OSError), self._fancy_rename,
163
                          'missingsource', 'missingtarget')
164
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
165
    def test_rename(self):
166
        # Rename should be semi-atomic on all platforms
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
167
        self.create_file('a', 'something in a\n')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
168
        osutils.rename('a', 'b')
169
        self.failIfExists('a')
170
        self.failUnlessExists('b')
171
        self.check_file_contents('b', 'something in a\n')
172
4789.17.1 by John Arbash Meinel
Change fancy_rename slightly.
173
        self.create_file('a', 'new something in a\n')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
174
        osutils.rename('b', 'a')
175
176
        self.check_file_contents('a', 'something in a\n')
177
178
    # TODO: test fancy_rename using a MemoryTransport
179
2978.8.2 by Alexander Belchenko
teach fancy_rename to handle change case renames in possible case-insensitive filesystem
180
    def test_rename_change_case(self):
181
        # on Windows we should be able to change filename case by rename
2978.8.1 by Alexander Belchenko
Rename on Windows is able to change filename case. (#77740)
182
        self.build_tree(['a', 'b/'])
183
        osutils.rename('a', 'A')
184
        osutils.rename('b', 'B')
2978.8.2 by Alexander Belchenko
teach fancy_rename to handle change case renames in possible case-insensitive filesystem
185
        # we can't use failUnlessExists on case-insensitive filesystem
186
        # so try to check shape of the tree
2978.8.1 by Alexander Belchenko
Rename on Windows is able to change filename case. (#77740)
187
        shape = sorted(os.listdir('.'))
188
        self.assertEquals(['A', 'B'], shape)
189
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
190
191
class TestRandChars(tests.TestCase):
192
1553.5.5 by Martin Pool
New utility routine rand_chars
193
    def test_01_rand_chars_empty(self):
194
        result = osutils.rand_chars(0)
195
        self.assertEqual(result, '')
196
197
    def test_02_rand_chars_100(self):
198
        result = osutils.rand_chars(100)
199
        self.assertEqual(len(result), 100)
200
        self.assertEqual(type(result), str)
201
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
202
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
203
204
class TestIsInside(tests.TestCase):
205
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
206
    def test_is_inside(self):
207
        is_inside = osutils.is_inside
208
        self.assertTrue(is_inside('src', 'src/foo.c'))
209
        self.assertFalse(is_inside('src', 'srccontrol'))
210
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
211
        self.assertTrue(is_inside('foo.c', 'foo.c'))
212
        self.assertFalse(is_inside('foo.c', ''))
213
        self.assertTrue(is_inside('', 'foo.c'))
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
214
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
215
    def test_is_inside_any(self):
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
216
        SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
217
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
218
                         (['src'], SRC_FOO_C),
219
                         (['src'], 'src'),
220
                         ]:
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
221
            self.assert_(osutils.is_inside_any(dirs, fn))
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
222
        for dirs, fn in [(['src'], 'srccontrol'),
223
                         (['src'], 'srccontrol/foo')]:
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
224
            self.assertFalse(osutils.is_inside_any(dirs, fn))
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
225
226
    def test_is_inside_or_parent_of_any(self):
227
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
228
                         (['src'], 'src/foo.c'),
229
                         (['src/bar.c'], 'src'),
230
                         (['src/bar.c', 'bla/foo.c'], 'src'),
231
                         (['src'], 'src'),
232
                         ]:
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
233
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
234
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
235
        for dirs, fn in [(['src'], 'srccontrol'),
236
                         (['srccontrol/foo.c'], 'src'),
237
                         (['src'], 'srccontrol/foo')]:
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
238
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
239
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
240
241
class TestRmTree(tests.TestCaseInTempDir):
242
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
243
    def test_rmtree(self):
244
        # Check to remove tree with read-only files/dirs
245
        os.mkdir('dir')
246
        f = file('dir/file', 'w')
247
        f.write('spam')
248
        f.close()
249
        # would like to also try making the directory readonly, but at the
250
        # moment python shutil.rmtree doesn't handle that properly - it would
251
        # need to chmod the directory before removing things inside it - deferred
252
        # for now -- mbp 20060505
253
        # osutils.make_readonly('dir')
254
        osutils.make_readonly('dir/file')
255
256
        osutils.rmtree('dir')
257
258
        self.failIfExists('dir/file')
259
        self.failIfExists('dir')
260
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
261
4490.1.2 by Martin Pool
test and news for forcing readonly deletion
262
class TestDeleteAny(tests.TestCaseInTempDir):
263
264
    def test_delete_any_readonly(self):
265
        # from <https://bugs.launchpad.net/bzr/+bug/218206>
266
        self.build_tree(['d/', 'f'])
267
        osutils.make_readonly('d')
268
        osutils.make_readonly('f')
269
270
        osutils.delete_any('f')
271
        osutils.delete_any('d')
272
273
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
274
class TestKind(tests.TestCaseInTempDir):
275
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
276
    def test_file_kind(self):
277
        self.build_tree(['file', 'dir/'])
278
        self.assertEquals('file', osutils.file_kind('file'))
279
        self.assertEquals('directory', osutils.file_kind('dir/'))
280
        if osutils.has_symlinks():
281
            os.symlink('symlink', 'symlink')
282
            self.assertEquals('symlink', osutils.file_kind('symlink'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
283
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
284
        # TODO: jam 20060529 Test a block device
285
        try:
286
            os.lstat('/dev/null')
287
        except OSError, e:
288
            if e.errno not in (errno.ENOENT,):
289
                raise
290
        else:
291
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
292
293
        mkfifo = getattr(os, 'mkfifo', None)
294
        if mkfifo:
295
            mkfifo('fifo')
296
            try:
297
                self.assertEquals('fifo', osutils.file_kind('fifo'))
298
            finally:
299
                os.remove('fifo')
300
301
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
302
        if AF_UNIX:
303
            s = socket.socket(AF_UNIX)
304
            s.bind('socket')
305
            try:
306
                self.assertEquals('socket', osutils.file_kind('socket'))
307
            finally:
308
                os.remove('socket')
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
309
1551.10.27 by Aaron Bentley
Add a kind marker for subtrees
310
    def test_kind_marker(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
311
        self.assertEqual("", osutils.kind_marker("file"))
312
        self.assertEqual("/", osutils.kind_marker('directory'))
313
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
314
        self.assertEqual("@", osutils.kind_marker("symlink"))
315
        self.assertEqual("+", osutils.kind_marker("tree-reference"))
316
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
317
318
319
class TestUmask(tests.TestCaseInTempDir):
1551.10.27 by Aaron Bentley
Add a kind marker for subtrees
320
1755.3.7 by John Arbash Meinel
Clean up and write tests for permissions. Now we use fstat which should be cheap, and lets us check the permissions and the file size
321
    def test_get_umask(self):
322
        if sys.platform == 'win32':
323
            # umask always returns '0', no way to set it
324
            self.assertEqual(0, osutils.get_umask())
325
            return
326
327
        orig_umask = osutils.get_umask()
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
328
        self.addCleanup(os.umask, orig_umask)
329
        os.umask(0222)
330
        self.assertEqual(0222, osutils.get_umask())
331
        os.umask(0022)
332
        self.assertEqual(0022, osutils.get_umask())
333
        os.umask(0002)
334
        self.assertEqual(0002, osutils.get_umask())
335
        os.umask(0027)
336
        self.assertEqual(0027, osutils.get_umask())
1755.3.7 by John Arbash Meinel
Clean up and write tests for permissions. Now we use fstat which should be cheap, and lets us check the permissions and the file size
337
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
338
339
class TestDateTime(tests.TestCase):
340
1957.1.15 by John Arbash Meinel
Review feedback from Robert
341
    def assertFormatedDelta(self, expected, seconds):
342
        """Assert osutils.format_delta formats as expected"""
343
        actual = osutils.format_delta(seconds)
344
        self.assertEqual(expected, actual)
345
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
346
    def test_format_delta(self):
1957.1.15 by John Arbash Meinel
Review feedback from Robert
347
        self.assertFormatedDelta('0 seconds ago', 0)
348
        self.assertFormatedDelta('1 second ago', 1)
349
        self.assertFormatedDelta('10 seconds ago', 10)
350
        self.assertFormatedDelta('59 seconds ago', 59)
351
        self.assertFormatedDelta('89 seconds ago', 89)
352
        self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
353
        self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
354
        self.assertFormatedDelta('3 minutes, 1 second ago', 181)
355
        self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
356
        self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
357
        self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
358
        self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
359
        self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
360
        self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
361
        self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
362
        self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
363
        self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
364
        self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
365
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
366
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
367
        self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
368
        self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
369
        self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
370
371
        # We handle when time steps the wrong direction because computers
372
        # don't have synchronized clocks.
1957.1.15 by John Arbash Meinel
Review feedback from Robert
373
        self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
374
        self.assertFormatedDelta('1 second in the future', -1)
375
        self.assertFormatedDelta('2 seconds in the future', -2)
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
376
3144.1.1 by Lukáš Lalinský
Fixed error reporting of unsupported timezone format.
377
    def test_format_date(self):
378
        self.assertRaises(errors.UnsupportedTimezoneFormat,
379
            osutils.format_date, 0, timezone='foo')
3526.5.4 by Martin von Gagern
Use separate function format_local_date for local weekday formats in unicode.
380
        self.assertIsInstance(osutils.format_date(0), str)
381
        self.assertIsInstance(osutils.format_local_date(0), unicode)
382
        # Testing for the actual value of the local weekday without
3526.5.2 by Martin von Gagern
Check output type of format_date
383
        # duplicating the code from format_date is difficult.
384
        # Instead blackbox.test_locale should check for localized
385
        # dates once they do occur in output strings.
3144.1.1 by Lukáš Lalinský
Fixed error reporting of unsupported timezone format.
386
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
387
    def test_local_time_offset(self):
388
        """Test that local_time_offset() returns a sane value."""
389
        offset = osutils.local_time_offset()
390
        self.assertTrue(isinstance(offset, int))
391
        # Test that the offset is no more than a eighteen hours in
392
        # either direction.
393
        # Time zone handling is system specific, so it is difficult to
394
        # do more specific tests, but a value outside of this range is
395
        # probably wrong.
396
        eighteen_hours = 18 * 3600
397
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
398
399
    def test_local_time_offset_with_timestamp(self):
400
        """Test that local_time_offset() works with a timestamp."""
401
        offset = osutils.local_time_offset(1000000000.1234567)
402
        self.assertTrue(isinstance(offset, int))
403
        eighteen_hours = 18 * 3600
404
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
405
406
407
class TestLinks(tests.TestCaseInTempDir):
408
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
409
    def test_dereference_path(self):
4241.14.3 by Vincent Ladeuil
Cleanup imports.
410
        self.requireFeature(tests.SymlinkFeature)
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
411
        cwd = osutils.realpath('.')
412
        os.mkdir('bar')
413
        bar_path = osutils.pathjoin(cwd, 'bar')
414
        # Using './' to avoid bug #1213894 (first path component not
415
        # dereferenced) in Python 2.4.1 and earlier
416
        self.assertEqual(bar_path, osutils.realpath('./bar'))
417
        os.symlink('bar', 'foo')
418
        self.assertEqual(bar_path, osutils.realpath('./foo'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
419
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
420
        # Does not dereference terminal symlinks
421
        foo_path = osutils.pathjoin(cwd, 'foo')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
422
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
423
424
        # Dereferences parent symlinks
425
        os.mkdir('bar/baz')
426
        baz_path = osutils.pathjoin(bar_path, 'baz')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
427
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
428
429
        # Dereferences parent symlinks that are the first path element
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
430
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
431
432
        # Dereferences parent symlinks in absolute paths
433
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
434
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
435
2568.1.1 by John Arbash Meinel
(Elliot Murphy) Use os.lstat rather than os.stat for osutils.make_readonly/make_writeable
436
    def test_changing_access(self):
437
        f = file('file', 'w')
438
        f.write('monkey')
439
        f.close()
440
441
        # Make a file readonly
442
        osutils.make_readonly('file')
2949.6.2 by Alexander Belchenko
more changes osutils.lstat -> os.lstat
443
        mode = os.lstat('file').st_mode
2568.1.1 by John Arbash Meinel
(Elliot Murphy) Use os.lstat rather than os.stat for osutils.make_readonly/make_writeable
444
        self.assertEqual(mode, mode & 0777555)
445
446
        # Make a file writable
447
        osutils.make_writable('file')
2949.6.2 by Alexander Belchenko
more changes osutils.lstat -> os.lstat
448
        mode = os.lstat('file').st_mode
2568.1.1 by John Arbash Meinel
(Elliot Murphy) Use os.lstat rather than os.stat for osutils.make_readonly/make_writeable
449
        self.assertEqual(mode, mode | 0200)
450
451
        if osutils.has_symlinks():
452
            # should not error when handed a symlink
453
            os.symlink('nonexistent', 'dangling')
454
            osutils.make_readonly('dangling')
455
            osutils.make_writable('dangling')
456
3287.18.26 by Matt McClure
Addresses concerns raised in
457
    def test_host_os_dereferences_symlinks(self):
458
        osutils.host_os_dereferences_symlinks()
459
2324.2.1 by Dmitry Vasiliev
kind_marker() optimization
460
4241.14.3 by Vincent Ladeuil
Cleanup imports.
461
class TestCanonicalRelPath(tests.TestCaseInTempDir):
3794.5.36 by Mark Hammond
test for, and fix problem with canonical_relpath when the tail does not exist.
462
4241.14.3 by Vincent Ladeuil
Cleanup imports.
463
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
3794.5.36 by Mark Hammond
test for, and fix problem with canonical_relpath when the tail does not exist.
464
465
    def test_canonical_relpath_simple(self):
466
        f = file('MixedCaseName', 'w')
467
        f.close()
4707.1.1 by Vincent Ladeuil
Fix OSX and FreeBSD failures.
468
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
469
        self.failUnlessEqual('work/MixedCaseName', actual)
3794.5.36 by Mark Hammond
test for, and fix problem with canonical_relpath when the tail does not exist.
470
471
    def test_canonical_relpath_missing_tail(self):
472
        os.mkdir('MixedCaseParent')
4707.1.1 by Vincent Ladeuil
Fix OSX and FreeBSD failures.
473
        actual = osutils.canonical_relpath(self.test_base_dir,
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
474
                                           'mixedcaseparent/nochild')
475
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
3794.5.36 by Mark Hammond
test for, and fix problem with canonical_relpath when the tail does not exist.
476
477
4634.70.2 by John Arbash Meinel
Fix bug #322807, teach cicp_canonical_relpath how to handle
478
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
479
480
    def assertRelpath(self, expected, base, path):
481
        actual = osutils._cicp_canonical_relpath(base, path)
482
        self.assertEqual(expected, actual)
483
484
    def test_simple(self):
485
        self.build_tree(['MixedCaseName'])
486
        base = osutils.realpath(self.get_transport('.').local_abspath('.'))
487
        self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
488
489
    def test_subdir_missing_tail(self):
490
        self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
491
        base = osutils.realpath(self.get_transport('.').local_abspath('.'))
492
        self.assertRelpath('MixedCaseParent/a_child', base,
493
                           'MixedCaseParent/a_child')
494
        self.assertRelpath('MixedCaseParent/a_child', base,
495
                           'MixedCaseParent/A_Child')
496
        self.assertRelpath('MixedCaseParent/not_child', base,
497
                           'MixedCaseParent/not_child')
498
4634.70.6 by John Arbash Meinel
Mix up the test a bit. On Windows we don't run the '/' test
499
    def test_at_root_slash(self):
4634.70.2 by John Arbash Meinel
Fix bug #322807, teach cicp_canonical_relpath how to handle
500
        # We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
501
        # check...
4634.70.4 by John Arbash Meinel
Take spiv's suggestion and move the '/' test to another test case.
502
        if osutils.MIN_ABS_PATHLENGTH > 1:
4634.70.6 by John Arbash Meinel
Mix up the test a bit. On Windows we don't run the '/' test
503
            raise tests.TestSkipped('relpath requires %d chars'
504
                                    % osutils.MIN_ABS_PATHLENGTH)
4634.70.4 by John Arbash Meinel
Take spiv's suggestion and move the '/' test to another test case.
505
        self.assertRelpath('foo', '/', '/foo')
4634.70.2 by John Arbash Meinel
Fix bug #322807, teach cicp_canonical_relpath how to handle
506
4634.70.6 by John Arbash Meinel
Mix up the test a bit. On Windows we don't run the '/' test
507
    def test_at_root_drive(self):
508
        if sys.platform != 'win32':
509
            raise tests.TestNotApplicable('we can only test drive-letter relative'
510
                                          ' paths on Windows where we have drive'
511
                                          ' letters.')
4634.70.2 by John Arbash Meinel
Fix bug #322807, teach cicp_canonical_relpath how to handle
512
        # see bug #322807
513
        # The specific issue is that when at the root of a drive, 'abspath'
514
        # returns "C:/" or just "/". However, the code assumes that abspath
515
        # always returns something like "C:/foo" or "/foo" (no trailing slash).
516
        self.assertRelpath('foo', 'C:/', 'C:/foo')
517
        self.assertRelpath('foo', 'X:/', 'X:/foo')
518
        self.assertRelpath('foo', 'X:/', 'X://foo')
519
520
4241.14.3 by Vincent Ladeuil
Cleanup imports.
521
class TestPumpFile(tests.TestCase):
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
522
    """Test pumpfile method."""
4241.14.3 by Vincent Ladeuil
Cleanup imports.
523
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
524
    def setUp(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
525
        tests.TestCase.setUp(self)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
526
        # create a test datablock
527
        self.block_size = 512
528
        pattern = '0123456789ABCDEF'
529
        self.test_data = pattern * (3 * self.block_size / len(pattern))
530
        self.test_data_len = len(self.test_data)
531
532
    def test_bracket_block_size(self):
533
        """Read data in blocks with the requested read size bracketing the
534
        block size."""
535
        # make sure test data is larger than max read size
536
        self.assertTrue(self.test_data_len > self.block_size)
537
4241.14.3 by Vincent Ladeuil
Cleanup imports.
538
        from_file = file_utils.FakeReadFile(self.test_data)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
539
        to_file = StringIO()
540
541
        # read (max / 2) bytes and verify read size wasn't affected
542
        num_bytes_to_read = self.block_size / 2
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
543
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
544
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
545
        self.assertEqual(from_file.get_read_count(), 1)
546
547
        # read (max) bytes and verify read size wasn't affected
548
        num_bytes_to_read = self.block_size
549
        from_file.reset_read_count()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
550
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
551
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
552
        self.assertEqual(from_file.get_read_count(), 1)
553
554
        # read (max + 1) bytes and verify read size was limited
555
        num_bytes_to_read = self.block_size + 1
556
        from_file.reset_read_count()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
557
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
558
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
559
        self.assertEqual(from_file.get_read_count(), 2)
560
561
        # finish reading the rest of the data
562
        num_bytes_to_read = self.test_data_len - to_file.tell()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
563
        osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
564
565
        # report error if the data wasn't equal (we only report the size due
566
        # to the length of the data)
567
        response_data = to_file.getvalue()
568
        if response_data != self.test_data:
569
            message = "Data not equal.  Expected %d bytes, received %d."
570
            self.fail(message % (len(response_data), self.test_data_len))
571
572
    def test_specified_size(self):
573
        """Request a transfer larger than the maximum block size and verify
574
        that the maximum read doesn't exceed the block_size."""
575
        # make sure test data is larger than max read size
576
        self.assertTrue(self.test_data_len > self.block_size)
577
578
        # retrieve data in blocks
4241.14.3 by Vincent Ladeuil
Cleanup imports.
579
        from_file = file_utils.FakeReadFile(self.test_data)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
580
        to_file = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
581
        osutils.pumpfile(from_file, to_file, self.test_data_len,
582
                         self.block_size)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
583
584
        # verify read size was equal to the maximum read size
585
        self.assertTrue(from_file.get_max_read_size() > 0)
586
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
587
        self.assertEqual(from_file.get_read_count(), 3)
588
589
        # report error if the data wasn't equal (we only report the size due
590
        # to the length of the data)
591
        response_data = to_file.getvalue()
592
        if response_data != self.test_data:
593
            message = "Data not equal.  Expected %d bytes, received %d."
594
            self.fail(message % (len(response_data), self.test_data_len))
595
596
    def test_to_eof(self):
597
        """Read to end-of-file and verify that the reads are not larger than
598
        the maximum read size."""
599
        # make sure test data is larger than max read size
600
        self.assertTrue(self.test_data_len > self.block_size)
601
602
        # retrieve data to EOF
4241.14.3 by Vincent Ladeuil
Cleanup imports.
603
        from_file = file_utils.FakeReadFile(self.test_data)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
604
        to_file = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
605
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
606
607
        # verify read size was equal to the maximum read size
608
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
609
        self.assertEqual(from_file.get_read_count(), 4)
610
611
        # report error if the data wasn't equal (we only report the size due
612
        # to the length of the data)
613
        response_data = to_file.getvalue()
614
        if response_data != self.test_data:
615
            message = "Data not equal.  Expected %d bytes, received %d."
616
            self.fail(message % (len(response_data), self.test_data_len))
617
618
    def test_defaults(self):
619
        """Verifies that the default arguments will read to EOF -- this
620
        test verifies that any existing usages of pumpfile will not be broken
621
        with this new version."""
622
        # retrieve data using default (old) pumpfile method
4241.14.3 by Vincent Ladeuil
Cleanup imports.
623
        from_file = file_utils.FakeReadFile(self.test_data)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
624
        to_file = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
625
        osutils.pumpfile(from_file, to_file)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
626
627
        # report error if the data wasn't equal (we only report the size due
628
        # to the length of the data)
629
        response_data = to_file.getvalue()
630
        if response_data != self.test_data:
631
            message = "Data not equal.  Expected %d bytes, received %d."
632
            self.fail(message % (len(response_data), self.test_data_len))
633
3956.2.1 by John Arbash Meinel
Add report_activity to osutils.pumpfile
634
    def test_report_activity(self):
635
        activity = []
636
        def log_activity(length, direction):
637
            activity.append((length, direction))
638
        from_file = StringIO(self.test_data)
639
        to_file = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
640
        osutils.pumpfile(from_file, to_file, buff_size=500,
641
                         report_activity=log_activity, direction='read')
3956.2.1 by John Arbash Meinel
Add report_activity to osutils.pumpfile
642
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
643
                          (36, 'read')], activity)
644
645
        from_file = StringIO(self.test_data)
646
        to_file = StringIO()
647
        del activity[:]
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
648
        osutils.pumpfile(from_file, to_file, buff_size=500,
649
                         report_activity=log_activity, direction='write')
3956.2.1 by John Arbash Meinel
Add report_activity to osutils.pumpfile
650
        self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
651
                          (36, 'write')], activity)
652
653
        # And with a limited amount of data
654
        from_file = StringIO(self.test_data)
655
        to_file = StringIO()
656
        del activity[:]
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
657
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
658
                         report_activity=log_activity, direction='read')
3956.2.1 by John Arbash Meinel
Add report_activity to osutils.pumpfile
659
        self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
660
661
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
662
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
663
class TestPumpStringFile(tests.TestCase):
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
664
665
    def test_empty(self):
666
        output = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
667
        osutils.pump_string_file("", output)
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
668
        self.assertEqual("", output.getvalue())
669
670
    def test_more_than_segment_size(self):
671
        output = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
672
        osutils.pump_string_file("123456789", output, 2)
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
673
        self.assertEqual("123456789", output.getvalue())
674
675
    def test_segment_size(self):
676
        output = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
677
        osutils.pump_string_file("12", output, 2)
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
678
        self.assertEqual("12", output.getvalue())
679
680
    def test_segment_size_multiple(self):
681
        output = StringIO()
4241.9.3 by Vincent Ladeuil
Fix two more test failures specific to OSX.
682
        osutils.pump_string_file("1234", output, 2)
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
683
        self.assertEqual("1234", output.getvalue())
684
685
4555.2.1 by John Arbash Meinel
Fix bug #394227, osutils.relpath() could get into an infinite loop.
686
class TestRelpath(tests.TestCase):
687
688
    def test_simple_relpath(self):
689
        cwd = osutils.getcwd()
690
        subdir = cwd + '/subdir'
691
        self.assertEqual('subdir', osutils.relpath(cwd, subdir))
692
4555.2.3 by John Arbash Meinel
Fix a trivial bug that should have been caught earlier. :)
693
    def test_deep_relpath(self):
694
        cwd = osutils.getcwd()
695
        subdir = cwd + '/sub/subsubdir'
696
        self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
697
4555.2.1 by John Arbash Meinel
Fix bug #394227, osutils.relpath() could get into an infinite loop.
698
    def test_not_relative(self):
699
        self.assertRaises(errors.PathNotChild,
700
                          osutils.relpath, 'C:/path', 'H:/path')
701
        self.assertRaises(errors.PathNotChild,
702
                          osutils.relpath, 'C:/', 'H:/path')
703
704
4241.14.3 by Vincent Ladeuil
Cleanup imports.
705
class TestSafeUnicode(tests.TestCase):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
706
707
    def test_from_ascii_string(self):
708
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
709
1534.3.2 by Robert Collins
An extra test for John.
710
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
711
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
712
1534.3.2 by Robert Collins
An extra test for John.
713
    def test_from_unicode_string_unicode_contents(self):
714
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
715
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
716
    def test_from_utf8_string(self):
717
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
718
719
    def test_bad_utf8_string(self):
4241.14.3 by Vincent Ladeuil
Cleanup imports.
720
        self.assertRaises(errors.BzrBadParameterNotUnicode,
1185.65.29 by Robert Collins
Implement final review suggestions.
721
                          osutils.safe_unicode,
722
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
723
724
4241.14.3 by Vincent Ladeuil
Cleanup imports.
725
class TestSafeUtf8(tests.TestCase):
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
726
727
    def test_from_ascii_string(self):
728
        f = 'foobar'
729
        self.assertEqual('foobar', osutils.safe_utf8(f))
730
731
    def test_from_unicode_string_ascii_contents(self):
732
        self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
733
734
    def test_from_unicode_string_unicode_contents(self):
735
        self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
736
737
    def test_from_utf8_string(self):
738
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
739
740
    def test_bad_utf8_string(self):
4241.14.3 by Vincent Ladeuil
Cleanup imports.
741
        self.assertRaises(errors.BzrBadParameterNotUnicode,
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
742
                          osutils.safe_utf8, '\xbb\xbb')
743
744
4241.14.3 by Vincent Ladeuil
Cleanup imports.
745
class TestSafeRevisionId(tests.TestCase):
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
746
747
    def test_from_ascii_string(self):
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
748
        # this shouldn't give a warning because it's getting an ascii string
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
749
        self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
750
751
    def test_from_unicode_string_ascii_contents(self):
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
752
        self.assertEqual('bargam',
753
                         osutils.safe_revision_id(u'bargam', warn=False))
754
755
    def test_from_unicode_deprecated(self):
756
        self.assertEqual('bargam',
757
            self.callDeprecated([osutils._revision_id_warning],
758
                                osutils.safe_revision_id, u'bargam'))
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
759
760
    def test_from_unicode_string_unicode_contents(self):
761
        self.assertEqual('bargam\xc2\xae',
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
762
                         osutils.safe_revision_id(u'bargam\xae', warn=False))
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
763
764
    def test_from_utf8_string(self):
765
        self.assertEqual('foo\xc2\xae',
766
                         osutils.safe_revision_id('foo\xc2\xae'))
767
2249.5.9 by John Arbash Meinel
Update WorkingTree to use safe_revision_id when appropriate
768
    def test_none(self):
769
        """Currently, None is a valid revision_id"""
770
        self.assertEqual(None, osutils.safe_revision_id(None))
771
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
772
4241.14.3 by Vincent Ladeuil
Cleanup imports.
773
class TestSafeFileId(tests.TestCase):
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
774
775
    def test_from_ascii_string(self):
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
776
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
777
778
    def test_from_unicode_string_ascii_contents(self):
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
779
        self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
780
781
    def test_from_unicode_deprecated(self):
782
        self.assertEqual('bargam',
783
            self.callDeprecated([osutils._file_id_warning],
784
                                osutils.safe_file_id, u'bargam'))
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
785
786
    def test_from_unicode_string_unicode_contents(self):
787
        self.assertEqual('bargam\xc2\xae',
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
788
                         osutils.safe_file_id(u'bargam\xae', warn=False))
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
789
790
    def test_from_utf8_string(self):
791
        self.assertEqual('foo\xc2\xae',
792
                         osutils.safe_file_id('foo\xc2\xae'))
793
794
    def test_none(self):
795
        """Currently, None is a valid revision_id"""
796
        self.assertEqual(None, osutils.safe_file_id(None))
797
798
4241.14.3 by Vincent Ladeuil
Cleanup imports.
799
class TestWin32Funcs(tests.TestCase):
800
    """Test that _win32 versions of os utilities return appropriate paths."""
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
801
802
    def test_abspath(self):
803
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
804
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
2279.4.1 by Alexander Belchenko
Reimplementation of ntpath.abspath in Python for Windows98: unicode safe, UNC path safe
805
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
806
        self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
807
808
    def test_realpath(self):
809
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
810
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
811
812
    def test_pathjoin(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
813
        self.assertEqual('path/to/foo',
814
                         osutils._win32_pathjoin('path', 'to', 'foo'))
815
        self.assertEqual('C:/foo',
816
                         osutils._win32_pathjoin('path\\to', 'C:\\foo'))
817
        self.assertEqual('C:/foo',
818
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
819
        self.assertEqual('path/to/foo',
820
                         osutils._win32_pathjoin('path/to/', 'foo'))
821
        self.assertEqual('/foo',
822
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
823
        self.assertEqual('/foo',
824
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
825
826
    def test_normpath(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
827
        self.assertEqual('path/to/foo',
828
                         osutils._win32_normpath(r'path\\from\..\to\.\foo'))
829
        self.assertEqual('path/to/foo',
830
                         osutils._win32_normpath('path//from/../to/./foo'))
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
831
832
    def test_getcwd(self):
1711.5.2 by John Arbash Meinel
win32 likes to return lowercase drive letters sometimes, and uppercase at other times. normalize this
833
        cwd = osutils._win32_getcwd()
834
        os_cwd = os.getcwdu()
835
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
836
        # win32 is inconsistent whether it returns lower or upper case
837
        # and even if it was consistent the user might type the other
838
        # so we force it to uppercase
839
        # running python.exe under cmd.exe return capital C:\\
840
        # running win32 python inside a cygwin shell returns lowercase
841
        self.assertEqual(os_cwd[0].upper(), cwd[0])
842
843
    def test_fixdrive(self):
844
        self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
845
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
846
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
847
2279.4.1 by Alexander Belchenko
Reimplementation of ntpath.abspath in Python for Windows98: unicode safe, UNC path safe
848
    def test_win98_abspath(self):
849
        # absolute path
850
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
851
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
852
        # UNC path
853
        self.assertEqual('//HOST/path', osutils._win98_abspath(r'\\HOST\path'))
854
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
855
        # relative path
856
        cwd = osutils.getcwd().rstrip('/')
857
        drive = osutils._nt_splitdrive(cwd)[0]
858
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
859
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
860
        # unicode path
861
        u = u'\u1234'
862
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
863
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
864
4241.14.3 by Vincent Ladeuil
Cleanup imports.
865
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
866
    """Test win32 functions that create files."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
867
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
868
    def test_getcwd(self):
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
869
        self.requireFeature(tests.UnicodeFilenameFeature)
870
        os.mkdir(u'mu-\xb5')
1830.3.9 by John Arbash Meinel
Use a directory name that doesn't get messed up on Mac for getcwd() test.
871
        os.chdir(u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
872
        # TODO: jam 20060427 This will probably fail on Mac OSX because
873
        #       it will change the normalization of B\xe5gfors
874
        #       Consider using a different unicode character, or make
875
        #       osutils.getcwd() renormalize the path.
1830.3.9 by John Arbash Meinel
Use a directory name that doesn't get messed up on Mac for getcwd() test.
876
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
877
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
878
    def test_minimum_path_selection(self):
879
        self.assertEqual(set(),
880
            osutils.minimum_path_selection([]))
4325.3.7 by Johan Walles
Style fixes for minimum_path_selection().
881
        self.assertEqual(set(['a']),
882
            osutils.minimum_path_selection(['a']))
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
883
        self.assertEqual(set(['a', 'b']),
884
            osutils.minimum_path_selection(['a', 'b']))
885
        self.assertEqual(set(['a/', 'b']),
886
            osutils.minimum_path_selection(['a/', 'b']))
887
        self.assertEqual(set(['a/', 'b']),
888
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
4325.3.3 by Johan Walles
Add unit test and fix for minimum_path_selection() vs directory names with
889
        self.assertEqual(set(['a-b', 'a', 'a0b']),
890
            osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
891
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
892
    def test_mkdtemp(self):
893
        tmpdir = osutils._win32_mkdtemp(dir='.')
894
        self.assertFalse('\\' in tmpdir)
895
896
    def test_rename(self):
897
        a = open('a', 'wb')
898
        a.write('foo\n')
899
        a.close()
900
        b = open('b', 'wb')
901
        b.write('baz\n')
902
        b.close()
903
904
        osutils._win32_rename('b', 'a')
905
        self.failUnlessExists('a')
906
        self.failIfExists('b')
907
        self.assertFileEqual('baz\n', 'a')
908
1711.7.6 by John Arbash Meinel
Change _win32_rename() so that it raises ENOENT *before* it tries any renaming.
909
    def test_rename_missing_file(self):
910
        a = open('a', 'wb')
911
        a.write('foo\n')
912
        a.close()
913
914
        try:
915
            osutils._win32_rename('b', 'a')
916
        except (IOError, OSError), e:
917
            self.assertEqual(errno.ENOENT, e.errno)
918
        self.assertFileEqual('foo\n', 'a')
919
920
    def test_rename_missing_dir(self):
921
        os.mkdir('a')
922
        try:
923
            osutils._win32_rename('b', 'a')
924
        except (IOError, OSError), e:
925
            self.assertEqual(errno.ENOENT, e.errno)
926
927
    def test_rename_current_dir(self):
928
        os.mkdir('a')
929
        os.chdir('a')
930
        # You can't rename the working directory
931
        # doing rename non-existant . usually
932
        # just raises ENOENT, since non-existant
933
        # doesn't exist.
934
        try:
935
            osutils._win32_rename('b', '.')
936
        except (IOError, OSError), e:
937
            self.assertEqual(errno.ENOENT, e.errno)
938
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
939
    def test_splitpath(self):
940
        def check(expected, path):
941
            self.assertEqual(expected, osutils.splitpath(path))
942
943
        check(['a'], 'a')
944
        check(['a', 'b'], 'a/b')
945
        check(['a', 'b'], 'a/./b')
946
        check(['a', '.b'], 'a/.b')
947
        check(['a', '.b'], 'a\\.b')
948
949
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
950
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
951
4370.1.1 by Ian Clatworthy
add osutils.parent_directories() API
952
class TestParentDirectories(tests.TestCaseInTempDir):
953
    """Test osutils.parent_directories()"""
954
955
    def test_parent_directories(self):
956
        self.assertEqual([], osutils.parent_directories('a'))
957
        self.assertEqual(['a'], osutils.parent_directories('a/b'))
958
        self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
959
960
4241.14.3 by Vincent Ladeuil
Cleanup imports.
961
class TestMacFuncsDirs(tests.TestCaseInTempDir):
1830.3.11 by John Arbash Meinel
Create a mac version of 'getcwd()' which normalizes the path.
962
    """Test mac special functions that require directories."""
963
964
    def test_getcwd(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
965
        self.requireFeature(tests.UnicodeFilenameFeature)
966
        os.mkdir(u'B\xe5gfors')
1830.3.11 by John Arbash Meinel
Create a mac version of 'getcwd()' which normalizes the path.
967
        os.chdir(u'B\xe5gfors')
968
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
969
970
    def test_getcwd_nonnorm(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
971
        self.requireFeature(tests.UnicodeFilenameFeature)
1830.3.11 by John Arbash Meinel
Create a mac version of 'getcwd()' which normalizes the path.
972
        # Test that _mac_getcwd() will normalize this path
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
973
        os.mkdir(u'Ba\u030agfors')
1830.3.11 by John Arbash Meinel
Create a mac version of 'getcwd()' which normalizes the path.
974
        os.chdir(u'Ba\u030agfors')
975
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
976
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
977
4241.14.3 by Vincent Ladeuil
Cleanup imports.
978
class TestChunksToLines(tests.TestCase):
3890.2.4 by John Arbash Meinel
Add a new function that can convert 'chunks' format to a 'lines' format.
979
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
980
    def test_smoketest(self):
981
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
982
                         osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
983
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
984
                         osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
985
3734.2.21 by Vincent Ladeuil
Give test a better name.
986
    def test_osutils_binding(self):
3734.2.20 by Vincent Ladeuil
Fix failing test when CompiledChunksToLines is not available.
987
        from bzrlib.tests import test__chunks_to_lines
988
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
989
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
990
        else:
991
            from bzrlib._chunks_to_lines_py import chunks_to_lines
992
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
3890.2.5 by John Arbash Meinel
More tests for edge cases.
993
3890.2.4 by John Arbash Meinel
Add a new function that can convert 'chunks' format to a 'lines' format.
994
4241.14.3 by Vincent Ladeuil
Cleanup imports.
995
class TestSplitLines(tests.TestCase):
1666.1.6 by Robert Collins
Make knit the default format.
996
997
    def test_split_unicode(self):
998
        self.assertEqual([u'foo\n', u'bar\xae'],
999
                         osutils.split_lines(u'foo\nbar\xae'))
1000
        self.assertEqual([u'foo\n', u'bar\xae\n'],
1001
                         osutils.split_lines(u'foo\nbar\xae\n'))
1002
1003
    def test_split_with_carriage_returns(self):
1004
        self.assertEqual(['foo\rbar\n'],
1005
                         osutils.split_lines('foo\rbar\n'))
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
1006
1007
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1008
class TestWalkDirs(tests.TestCaseInTempDir):
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
1009
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1010
    def assertExpectedBlocks(self, expected, result):
1011
        self.assertEqual(expected,
1012
                         [(dirinfo, [line[0:3] for line in block])
1013
                          for dirinfo, block in result])
1014
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
1015
    def test_walkdirs(self):
1016
        tree = [
1017
            '.bzr',
1018
            '0file',
1019
            '1dir/',
1020
            '1dir/0file',
1021
            '1dir/1dir/',
1022
            '2file'
1023
            ]
1024
        self.build_tree(tree)
1025
        expected_dirblocks = [
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
1026
                (('', '.'),
1027
                 [('0file', '0file', 'file'),
1028
                  ('1dir', '1dir', 'directory'),
1029
                  ('2file', '2file', 'file'),
1030
                 ]
1031
                ),
1032
                (('1dir', './1dir'),
1033
                 [('1dir/0file', '0file', 'file'),
1034
                  ('1dir/1dir', '1dir', 'directory'),
1035
                 ]
1036
                ),
1037
                (('1dir/1dir', './1dir/1dir'),
1038
                 [
1039
                 ]
1040
                ),
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
1041
            ]
1042
        result = []
1043
        found_bzrdir = False
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
1044
        for dirdetail, dirblock in osutils.walkdirs('.'):
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
1045
            if len(dirblock) and dirblock[0][1] == '.bzr':
1046
                # this tests the filtering of selected paths
1047
                found_bzrdir = True
1048
                del dirblock[0]
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
1049
            result.append((dirdetail, dirblock))
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
1050
1051
        self.assertTrue(found_bzrdir)
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1052
        self.assertExpectedBlocks(expected_dirblocks, result)
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
1053
        # you can search a subdir only, with a supplied prefix.
1054
        result = []
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
1055
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
1056
            result.append(dirblock)
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1057
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
1058
4095.1.3 by Martin Pool
Add test for failures inside pyrex readdir
1059
    def test_walkdirs_os_error(self):
1060
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1061
        # Pyrex readdir didn't raise useful messages if it had an error
1062
        # reading the directory
1063
        if sys.platform == 'win32':
1064
            raise tests.TestNotApplicable(
1065
                "readdir IOError not tested on win32")
1066
        os.mkdir("test-unreadable")
1067
        os.chmod("test-unreadable", 0000)
1068
        # must chmod it back so that it can be removed
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
1069
        self.addCleanup(os.chmod, "test-unreadable", 0700)
4095.1.3 by Martin Pool
Add test for failures inside pyrex readdir
1070
        # The error is not raised until the generator is actually evaluated.
1071
        # (It would be ok if it happened earlier but at the moment it
1072
        # doesn't.)
4133.1.1 by Vincent Ladeuil
Fix bzrlib.tests.test_osutils.TestWalkDirs.test_walkdirs_os_error
1073
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
1074
        self.assertEquals('./test-unreadable', e.filename)
1075
        self.assertEquals(errno.EACCES, e.errno)
4133.1.2 by Vincent Ladeuil
Fixed as per Martin's remark about the intent of the test :-}
1076
        # Ensure the message contains the file name
1077
        self.assertContainsRe(str(e), "\./test-unreadable")
4095.1.3 by Martin Pool
Add test for failures inside pyrex readdir
1078
2255.7.27 by John Arbash Meinel
Add a _walkdirs_utf8 which returns utf8 paths instead of Unicode. Approx 20% faster in walking utf8 filesystems
1079
    def test__walkdirs_utf8(self):
1080
        tree = [
1081
            '.bzr',
1082
            '0file',
1083
            '1dir/',
1084
            '1dir/0file',
1085
            '1dir/1dir/',
1086
            '2file'
1087
            ]
1088
        self.build_tree(tree)
1089
        expected_dirblocks = [
1090
                (('', '.'),
1091
                 [('0file', '0file', 'file'),
1092
                  ('1dir', '1dir', 'directory'),
1093
                  ('2file', '2file', 'file'),
1094
                 ]
1095
                ),
1096
                (('1dir', './1dir'),
1097
                 [('1dir/0file', '0file', 'file'),
1098
                  ('1dir/1dir', '1dir', 'directory'),
1099
                 ]
1100
                ),
1101
                (('1dir/1dir', './1dir/1dir'),
1102
                 [
1103
                 ]
1104
                ),
1105
            ]
1106
        result = []
1107
        found_bzrdir = False
1108
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1109
            if len(dirblock) and dirblock[0][1] == '.bzr':
1110
                # this tests the filtering of selected paths
1111
                found_bzrdir = True
1112
                del dirblock[0]
1113
            result.append((dirdetail, dirblock))
1114
1115
        self.assertTrue(found_bzrdir)
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1116
        self.assertExpectedBlocks(expected_dirblocks, result)
1117
2255.7.27 by John Arbash Meinel
Add a _walkdirs_utf8 which returns utf8 paths instead of Unicode. Approx 20% faster in walking utf8 filesystems
1118
        # you can search a subdir only, with a supplied prefix.
1119
        result = []
1120
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1121
            result.append(dirblock)
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1122
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
2255.7.27 by John Arbash Meinel
Add a _walkdirs_utf8 which returns utf8 paths instead of Unicode. Approx 20% faster in walking utf8 filesystems
1123
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1124
    def _filter_out_stat(self, result):
1125
        """Filter out the stat value from the walkdirs result"""
1126
        for dirdetail, dirblock in result:
1127
            new_dirblock = []
1128
            for info in dirblock:
1129
                # Ignore info[3] which is the stat
1130
                new_dirblock.append((info[0], info[1], info[2], info[4]))
1131
            dirblock[:] = new_dirblock
1132
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1133
    def _save_platform_info(self):
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1134
        cur_winver = win32utils.winver
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1135
        cur_fs_enc = osutils._fs_enc
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1136
        cur_dir_reader = osutils._selected_dir_reader
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1137
        def restore():
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1138
            win32utils.winver = cur_winver
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1139
            osutils._fs_enc = cur_fs_enc
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1140
            osutils._selected_dir_reader = cur_dir_reader
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1141
        self.addCleanup(restore)
1142
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1143
    def assertDirReaderIs(self, expected):
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1144
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1145
        # Force it to redetect
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1146
        osutils._selected_dir_reader = None
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1147
        # Nothing to list, but should still trigger the selection logic
3557.2.5 by John Arbash Meinel
Test that the empty-directory logic for all _walkdirs implementations is correct.
1148
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1149
        self.assertIsInstance(osutils._selected_dir_reader, expected)
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1150
1151
    def test_force_walkdirs_utf8_fs_utf8(self):
3696.3.5 by Robert Collins
Streamline _walkdirs_utf8 for utf8 file systems, reducing time to traverse a mozilla tree from 1s to .6 seconds. (Robert Collins)
1152
        self.requireFeature(UTF8DirReaderFeature)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1153
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1154
        win32utils.winver = None # Avoid the win32 detection code
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1155
        osutils._fs_enc = 'UTF-8'
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1156
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1157
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1158
    def test_force_walkdirs_utf8_fs_ascii(self):
3696.3.5 by Robert Collins
Streamline _walkdirs_utf8 for utf8 file systems, reducing time to traverse a mozilla tree from 1s to .6 seconds. (Robert Collins)
1159
        self.requireFeature(UTF8DirReaderFeature)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1160
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1161
        win32utils.winver = None # Avoid the win32 detection code
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1162
        osutils._fs_enc = 'US-ASCII'
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1163
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1164
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1165
    def test_force_walkdirs_utf8_fs_ANSI(self):
3696.3.5 by Robert Collins
Streamline _walkdirs_utf8 for utf8 file systems, reducing time to traverse a mozilla tree from 1s to .6 seconds. (Robert Collins)
1166
        self.requireFeature(UTF8DirReaderFeature)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1167
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1168
        win32utils.winver = None # Avoid the win32 detection code
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1169
        osutils._fs_enc = 'ANSI_X3.4-1968'
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1170
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1171
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1172
    def test_force_walkdirs_utf8_fs_latin1(self):
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1173
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1174
        win32utils.winver = None # Avoid the win32 detection code
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1175
        osutils._fs_enc = 'latin1'
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1176
        self.assertDirReaderIs(osutils.UnicodeDirReader)
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1177
1178
    def test_force_walkdirs_utf8_nt(self):
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1179
        # Disabled because the thunk of the whole walkdirs api is disabled.
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1180
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1181
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1182
        win32utils.winver = 'Windows NT'
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1183
        from bzrlib._walkdirs_win32 import Win32ReadDir
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1184
        self.assertDirReaderIs(Win32ReadDir)
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1185
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1186
    def test_force_walkdirs_utf8_98(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1187
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
3557.2.4 by John Arbash Meinel
Cleanup the tests a bit, and add a test that we downgrade if os.name isn't 'nt'
1188
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
1189
        win32utils.winver = 'Windows 98'
4241.14.5 by Vincent Ladeuil
Some more cleanup (without typo).
1190
        self.assertDirReaderIs(osutils.UnicodeDirReader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
1191
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1192
    def test_unicode_walkdirs(self):
1193
        """Walkdirs should always return unicode paths."""
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1194
        self.requireFeature(tests.UnicodeFilenameFeature)
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1195
        name0 = u'0file-\xb6'
1196
        name1 = u'1dir-\u062c\u0648'
1197
        name2 = u'2file-\u0633'
1198
        tree = [
1199
            name0,
1200
            name1 + '/',
1201
            name1 + '/' + name0,
1202
            name1 + '/' + name1 + '/',
1203
            name2,
1204
            ]
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1205
        self.build_tree(tree)
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1206
        expected_dirblocks = [
1207
                ((u'', u'.'),
1208
                 [(name0, name0, 'file', './' + name0),
1209
                  (name1, name1, 'directory', './' + name1),
1210
                  (name2, name2, 'file', './' + name2),
1211
                 ]
1212
                ),
1213
                ((name1, './' + name1),
1214
                 [(name1 + '/' + name0, name0, 'file', './' + name1
1215
                                                        + '/' + name0),
1216
                  (name1 + '/' + name1, name1, 'directory', './' + name1
1217
                                                            + '/' + name1),
1218
                 ]
1219
                ),
1220
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
1221
                 [
1222
                 ]
1223
                ),
1224
            ]
1225
        result = list(osutils.walkdirs('.'))
1226
        self._filter_out_stat(result)
1227
        self.assertEqual(expected_dirblocks, result)
1228
        result = list(osutils.walkdirs(u'./'+name1, name1))
1229
        self._filter_out_stat(result)
1230
        self.assertEqual(expected_dirblocks[1:], result)
1231
1232
    def test_unicode__walkdirs_utf8(self):
1233
        """Walkdirs_utf8 should always return utf8 paths.
1234
1235
        The abspath portion might be in unicode or utf-8
1236
        """
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1237
        self.requireFeature(tests.UnicodeFilenameFeature)
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1238
        name0 = u'0file-\xb6'
1239
        name1 = u'1dir-\u062c\u0648'
1240
        name2 = u'2file-\u0633'
1241
        tree = [
1242
            name0,
1243
            name1 + '/',
1244
            name1 + '/' + name0,
1245
            name1 + '/' + name1 + '/',
1246
            name2,
1247
            ]
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1248
        self.build_tree(tree)
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1249
        name0 = name0.encode('utf8')
1250
        name1 = name1.encode('utf8')
1251
        name2 = name2.encode('utf8')
1252
1253
        expected_dirblocks = [
1254
                (('', '.'),
1255
                 [(name0, name0, 'file', './' + name0),
1256
                  (name1, name1, 'directory', './' + name1),
1257
                  (name2, name2, 'file', './' + name2),
1258
                 ]
1259
                ),
1260
                ((name1, './' + name1),
1261
                 [(name1 + '/' + name0, name0, 'file', './' + name1
1262
                                                        + '/' + name0),
1263
                  (name1 + '/' + name1, name1, 'directory', './' + name1
1264
                                                            + '/' + name1),
1265
                 ]
1266
                ),
1267
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
1268
                 [
1269
                 ]
1270
                ),
1271
            ]
1272
        result = []
1273
        # For ease in testing, if walkdirs_utf8 returns Unicode, assert that
1274
        # all abspaths are Unicode, and encode them back into utf8.
1275
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1276
            self.assertIsInstance(dirdetail[0], str)
1277
            if isinstance(dirdetail[1], unicode):
2324.2.4 by Dmitry Vasiliev
Fixed test_unicode__walkdirs_utf8 test
1278
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1279
                dirblock = [list(info) for info in dirblock]
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1280
                for info in dirblock:
1281
                    self.assertIsInstance(info[4], unicode)
1282
                    info[4] = info[4].encode('utf8')
1283
            new_dirblock = []
1284
            for info in dirblock:
1285
                self.assertIsInstance(info[0], str)
1286
                self.assertIsInstance(info[1], str)
1287
                self.assertIsInstance(info[4], str)
1288
                # Remove the stat information
1289
                new_dirblock.append((info[0], info[1], info[2], info[4]))
1290
            result.append((dirdetail, new_dirblock))
1291
        self.assertEqual(expected_dirblocks, result)
1292
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1293
    def test__walkdirs_utf8_with_unicode_fs(self):
1294
        """UnicodeDirReader should be a safe fallback everywhere
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1295
1296
        The abspath portion should be in unicode
1297
        """
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1298
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1299
        # Use the unicode reader. TODO: split into driver-and-driven unit
1300
        # tests.
1301
        self._save_platform_info()
1302
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1303
        name0u = u'0file-\xb6'
1304
        name1u = u'1dir-\u062c\u0648'
1305
        name2u = u'2file-\u0633'
1306
        tree = [
1307
            name0u,
1308
            name1u + '/',
1309
            name1u + '/' + name0u,
1310
            name1u + '/' + name1u + '/',
1311
            name2u,
1312
            ]
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1313
        self.build_tree(tree)
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1314
        name0 = name0u.encode('utf8')
1315
        name1 = name1u.encode('utf8')
1316
        name2 = name2u.encode('utf8')
1317
1318
        # All of the abspaths should be in unicode, all of the relative paths
1319
        # should be in utf8
1320
        expected_dirblocks = [
1321
                (('', '.'),
1322
                 [(name0, name0, 'file', './' + name0u),
1323
                  (name1, name1, 'directory', './' + name1u),
1324
                  (name2, name2, 'file', './' + name2u),
1325
                 ]
1326
                ),
1327
                ((name1, './' + name1u),
1328
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1329
                                                        + '/' + name0u),
1330
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1331
                                                            + '/' + name1u),
1332
                 ]
1333
                ),
1334
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1335
                 [
1336
                 ]
1337
                ),
1338
            ]
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1339
        result = list(osutils._walkdirs_utf8('.'))
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1340
        self._filter_out_stat(result)
1341
        self.assertEqual(expected_dirblocks, result)
1342
3696.3.4 by John Arbash Meinel
Update the osutils test to find the objects in the right locations.
1343
    def test__walkdirs_utf8_win32readdir(self):
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1344
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
1345
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1346
        from bzrlib._walkdirs_win32 import Win32ReadDir
1347
        self._save_platform_info()
3696.3.4 by John Arbash Meinel
Update the osutils test to find the objects in the right locations.
1348
        osutils._selected_dir_reader = Win32ReadDir()
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
1349
        name0u = u'0file-\xb6'
1350
        name1u = u'1dir-\u062c\u0648'
1351
        name2u = u'2file-\u0633'
1352
        tree = [
1353
            name0u,
1354
            name1u + '/',
1355
            name1u + '/' + name0u,
1356
            name1u + '/' + name1u + '/',
1357
            name2u,
1358
            ]
1359
        self.build_tree(tree)
1360
        name0 = name0u.encode('utf8')
1361
        name1 = name1u.encode('utf8')
1362
        name2 = name2u.encode('utf8')
1363
1364
        # All of the abspaths should be in unicode, all of the relative paths
1365
        # should be in utf8
1366
        expected_dirblocks = [
1367
                (('', '.'),
1368
                 [(name0, name0, 'file', './' + name0u),
1369
                  (name1, name1, 'directory', './' + name1u),
1370
                  (name2, name2, 'file', './' + name2u),
1371
                 ]
1372
                ),
1373
                ((name1, './' + name1u),
1374
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1375
                                                        + '/' + name0u),
1376
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1377
                                                            + '/' + name1u),
1378
                 ]
1379
                ),
1380
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1381
                 [
1382
                 ]
1383
                ),
1384
            ]
3696.3.4 by John Arbash Meinel
Update the osutils test to find the objects in the right locations.
1385
        result = list(osutils._walkdirs_utf8(u'.'))
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
1386
        self._filter_out_stat(result)
1387
        self.assertEqual(expected_dirblocks, result)
1388
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1389
    def assertStatIsCorrect(self, path, win32stat):
1390
        os_stat = os.stat(path)
1391
        self.assertEqual(os_stat.st_size, win32stat.st_size)
3504.4.6 by John Arbash Meinel
Start exposing the times on the stat, this now seems to be a complete walkdirs implementation.
1392
        self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
1393
        self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
1394
        self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1395
        self.assertEqual(os_stat.st_dev, win32stat.st_dev)
1396
        self.assertEqual(os_stat.st_ino, win32stat.st_ino)
1397
        self.assertEqual(os_stat.st_mode, win32stat.st_mode)
1398
1399
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1400
        """make sure our Stat values are valid"""
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1401
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1402
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1403
        from bzrlib._walkdirs_win32 import Win32ReadDir
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1404
        name0u = u'0file-\xb6'
1405
        name0 = name0u.encode('utf8')
1406
        self.build_tree([name0u])
1407
        # I hate to sleep() here, but I'm trying to make the ctime different
1408
        # from the mtime
1409
        time.sleep(2)
1410
        f = open(name0u, 'ab')
1411
        try:
1412
            f.write('just a small update')
1413
        finally:
1414
            f.close()
1415
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1416
        result = Win32ReadDir().read_dir('', u'.')
1417
        entry = result[0]
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1418
        self.assertEqual((name0, name0, 'file'), entry[:3])
1419
        self.assertEqual(u'./' + name0u, entry[4])
1420
        self.assertStatIsCorrect(entry[4], entry[3])
3504.4.6 by John Arbash Meinel
Start exposing the times on the stat, this now seems to be a complete walkdirs implementation.
1421
        self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1422
1423
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1424
        """make sure our Stat values are valid"""
4241.14.4 by Vincent Ladeuil
Clean up test_osutils.py.
1425
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1426
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1427
        from bzrlib._walkdirs_win32 import Win32ReadDir
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1428
        name0u = u'0dir-\u062c\u0648'
1429
        name0 = name0u.encode('utf8')
1430
        self.build_tree([name0u + '/'])
1431
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1432
        result = Win32ReadDir().read_dir('', u'.')
1433
        entry = result[0]
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1434
        self.assertEqual((name0, name0, 'directory'), entry[:3])
1435
        self.assertEqual(u'./' + name0u, entry[4])
1436
        self.assertStatIsCorrect(entry[4], entry[3])
1437
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
1438
    def assertPathCompare(self, path_less, path_greater):
1439
        """check that path_less and path_greater compare correctly."""
1440
        self.assertEqual(0, osutils.compare_paths_prefix_order(
1441
            path_less, path_less))
1442
        self.assertEqual(0, osutils.compare_paths_prefix_order(
1443
            path_greater, path_greater))
1444
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
1445
            path_less, path_greater))
1446
        self.assertEqual(1, osutils.compare_paths_prefix_order(
1447
            path_greater, path_less))
1448
1449
    def test_compare_paths_prefix_order(self):
1450
        # root before all else
1451
        self.assertPathCompare("/", "/a")
1452
        # alpha within a dir
1453
        self.assertPathCompare("/a", "/b")
1454
        self.assertPathCompare("/b", "/z")
1455
        # high dirs before lower.
1456
        self.assertPathCompare("/z", "/a/a")
1773.3.2 by Robert Collins
New corner case from John Meinel, showing up the need to check the directory lexographically outside of a single tree's root. Fixed.
1457
        # except if the deeper dir should be output first
1458
        self.assertPathCompare("/a/b/c", "/d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
1459
        # lexical betwen dirs of the same height
1460
        self.assertPathCompare("/a/z", "/z/z")
1461
        self.assertPathCompare("/a/c/z", "/a/d/e")
1462
1463
        # this should also be consistent for no leading / paths
1464
        # root before all else
1465
        self.assertPathCompare("", "a")
1466
        # alpha within a dir
1467
        self.assertPathCompare("a", "b")
1468
        self.assertPathCompare("b", "z")
1469
        # high dirs before lower.
1470
        self.assertPathCompare("z", "a/a")
1773.3.2 by Robert Collins
New corner case from John Meinel, showing up the need to check the directory lexographically outside of a single tree's root. Fixed.
1471
        # except if the deeper dir should be output first
1472
        self.assertPathCompare("a/b/c", "d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
1473
        # lexical betwen dirs of the same height
1474
        self.assertPathCompare("a/z", "z/z")
1475
        self.assertPathCompare("a/c/z", "a/d/e")
1476
1773.3.3 by Robert Collins
Add new tests John Meinel asked for.
1477
    def test_path_prefix_sorting(self):
1478
        """Doing a sort on path prefix should match our sample data."""
1479
        original_paths = [
1480
            'a',
1481
            'a/b',
1482
            'a/b/c',
1483
            'b',
1484
            'b/c',
1485
            'd',
1486
            'd/e',
1487
            'd/e/f',
1488
            'd/f',
1489
            'd/g',
1490
            'g',
1491
            ]
1492
1493
        dir_sorted_paths = [
1494
            'a',
1495
            'b',
1496
            'd',
1497
            'g',
1498
            'a/b',
1499
            'a/b/c',
1500
            'b/c',
1501
            'd/e',
1502
            'd/f',
1503
            'd/g',
1504
            'd/e/f',
1505
            ]
1506
1507
        self.assertEqual(
1508
            dir_sorted_paths,
1509
            sorted(original_paths, key=osutils.path_prefix_key))
1510
        # using the comparison routine shoudl work too:
1511
        self.assertEqual(
1512
            dir_sorted_paths,
1513
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1711.4.10 by John Arbash Meinel
Pull out sys.stdout.encoding handling into a separate function so it can be tested, and used elsewhere.
1514
1515
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1516
class TestCopyTree(tests.TestCaseInTempDir):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1517
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1518
    def test_copy_basic_tree(self):
1519
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1520
        osutils.copy_tree('source', 'target')
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
1521
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1522
        self.assertEqual(['c'], os.listdir('target/b'))
1523
1524
    def test_copy_tree_target_exists(self):
1525
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
1526
                         'target/'])
1527
        osutils.copy_tree('source', 'target')
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
1528
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1529
        self.assertEqual(['c'], os.listdir('target/b'))
1530
1907.3.2 by John Arbash Meinel
Updated the copy_tree function to allow overriding functionality.
1531
    def test_copy_tree_symlinks(self):
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1532
        self.requireFeature(tests.SymlinkFeature)
1907.3.2 by John Arbash Meinel
Updated the copy_tree function to allow overriding functionality.
1533
        self.build_tree(['source/'])
1534
        os.symlink('a/generic/path', 'source/lnk')
1535
        osutils.copy_tree('source', 'target')
1536
        self.assertEqual(['lnk'], os.listdir('target'))
1537
        self.assertEqual('a/generic/path', os.readlink('target/lnk'))
1538
1539
    def test_copy_tree_handlers(self):
1540
        processed_files = []
1541
        processed_links = []
1542
        def file_handler(from_path, to_path):
1543
            processed_files.append(('f', from_path, to_path))
1544
        def dir_handler(from_path, to_path):
1545
            processed_files.append(('d', from_path, to_path))
1546
        def link_handler(from_path, to_path):
1547
            processed_links.append((from_path, to_path))
1548
        handlers = {'file':file_handler,
1549
                    'directory':dir_handler,
1550
                    'symlink':link_handler,
1551
                   }
1552
1553
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1554
        if osutils.has_symlinks():
1555
            os.symlink('a/generic/path', 'source/lnk')
1556
        osutils.copy_tree('source', 'target', handlers=handlers)
1557
1558
        self.assertEqual([('d', 'source', 'target'),
1559
                          ('f', 'source/a', 'target/a'),
1560
                          ('d', 'source/b', 'target/b'),
1561
                          ('f', 'source/b/c', 'target/b/c'),
1562
                         ], processed_files)
1563
        self.failIfExists('target')
1564
        if osutils.has_symlinks():
1565
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1566
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1567
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1568
class TestSetUnsetEnv(tests.TestCase):
1963.1.5 by John Arbash Meinel
Create an osutils helper function for modifying the environment
1569
    """Test updating the environment"""
1570
1571
    def setUp(self):
1572
        super(TestSetUnsetEnv, self).setUp()
1573
1574
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
1575
                         'Environment was not cleaned up properly.'
1576
                         ' Variable BZR_TEST_ENV_VAR should not exist.')
1577
        def cleanup():
1578
            if 'BZR_TEST_ENV_VAR' in os.environ:
1579
                del os.environ['BZR_TEST_ENV_VAR']
1580
1581
        self.addCleanup(cleanup)
1582
1583
    def test_set(self):
1584
        """Test that we can set an env variable"""
1585
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1586
        self.assertEqual(None, old)
1587
        self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
1588
1589
    def test_double_set(self):
1590
        """Test that we get the old value out"""
1591
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1592
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
1593
        self.assertEqual('foo', old)
1594
        self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
1595
1596
    def test_unicode(self):
1597
        """Environment can only contain plain strings
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1598
1963.1.5 by John Arbash Meinel
Create an osutils helper function for modifying the environment
1599
        So Unicode strings must be encoded.
1600
        """
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1601
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
2785.1.5 by Alexander Belchenko
support for non-ascii BZR_HOME in show_version()
1602
        if uni_val is None:
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1603
            raise tests.TestSkipped(
1604
                'Cannot find a unicode character that works in encoding %s'
1605
                % (osutils.get_user_encoding(),))
1963.1.5 by John Arbash Meinel
Create an osutils helper function for modifying the environment
1606
1607
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1608
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1609
1610
    def test_unset(self):
1611
        """Test that passing None will remove the env var"""
1612
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1613
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1614
        self.assertEqual('foo', old)
1615
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1616
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1617
2215.6.2 by James Henstridge
add some simple tests for local_time_offset()
1618
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1619
class TestSizeShaFile(tests.TestCaseInTempDir):
3368.2.49 by Ian Clatworthy
added osutils.size_sha_file() with tests
1620
1621
    def test_sha_empty(self):
1622
        self.build_tree_contents([('foo', '')])
1623
        expected_sha = osutils.sha_string('')
1624
        f = open('foo')
1625
        self.addCleanup(f.close)
1626
        size, sha = osutils.size_sha_file(f)
1627
        self.assertEqual(0, size)
1628
        self.assertEqual(expected_sha, sha)
1629
1630
    def test_sha_mixed_endings(self):
1631
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1632
        self.build_tree_contents([('foo', text)])
1633
        expected_sha = osutils.sha_string(text)
1634
        f = open('foo')
1635
        self.addCleanup(f.close)
1636
        size, sha = osutils.size_sha_file(f)
1637
        self.assertEqual(38, size)
1638
        self.assertEqual(expected_sha, sha)
1639
1640
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1641
class TestShaFileByName(tests.TestCaseInTempDir):
2922.1.2 by John Arbash Meinel
Add tests for sha_file_by_name.
1642
1643
    def test_sha_empty(self):
1644
        self.build_tree_contents([('foo', '')])
1645
        expected_sha = osutils.sha_string('')
1646
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1647
1648
    def test_sha_mixed_endings(self):
1649
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1650
        self.build_tree_contents([('foo', text)])
1651
        expected_sha = osutils.sha_string(text)
1652
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
3089.3.9 by Ian Clatworthy
add test for resource loading
1653
1654
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1655
class TestResourceLoading(tests.TestCaseInTempDir):
3089.3.9 by Ian Clatworthy
add test for resource loading
1656
1657
    def test_resource_string(self):
1658
        # test resource in bzrlib
1659
        text = osutils.resource_string('bzrlib', 'debug.py')
3959.1.4 by Martin Pool
test_resource_string shouldn't depend on the precise source file contents
1660
        self.assertContainsRe(text, "debug_flags = set()")
3089.3.9 by Ian Clatworthy
add test for resource loading
1661
        # test resource under bzrlib
1662
        text = osutils.resource_string('bzrlib.ui', 'text.py')
1663
        self.assertContainsRe(text, "class TextUIFactory")
1664
        # test unsupported package
1665
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1666
            'yyy.xx')
1667
        # test unknown resource
1668
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
4183.6.4 by Martin Pool
Separate out re_compile_checked
1669
1670
4241.14.3 by Vincent Ladeuil
Cleanup imports.
1671
class TestReCompile(tests.TestCase):
4183.6.4 by Martin Pool
Separate out re_compile_checked
1672
1673
    def test_re_compile_checked(self):
1674
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1675
        self.assertTrue(r.match('aaaa'))
1676
        self.assertTrue(r.match('aAaA'))
1677
1678
    def test_re_compile_checked_error(self):
1679
        # like https://bugs.launchpad.net/bzr/+bug/251352
1680
        err = self.assertRaises(
1681
            errors.BzrCommandError,
1682
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1683
        self.assertEqual(
1684
            "Invalid regular expression in test case: '*': "
1685
            "nothing to repeat",
1686
            str(err))
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
1687
1688
1689
class TestDirReader(tests.TestCaseInTempDir):
1690
1691
    # Set by load_tests
1692
    _dir_reader_class = None
1693
    _native_to_unicode = None
1694
1695
    def setUp(self):
1696
        tests.TestCaseInTempDir.setUp(self)
1697
1698
        # Save platform specific info and reset it
1699
        cur_dir_reader = osutils._selected_dir_reader
1700
1701
        def restore():
1702
            osutils._selected_dir_reader = cur_dir_reader
1703
        self.addCleanup(restore)
1704
1705
        osutils._selected_dir_reader = self._dir_reader_class()
1706
1707
    def _get_ascii_tree(self):
1708
        tree = [
1709
            '0file',
1710
            '1dir/',
1711
            '1dir/0file',
1712
            '1dir/1dir/',
1713
            '2file'
1714
            ]
1715
        expected_dirblocks = [
1716
                (('', '.'),
1717
                 [('0file', '0file', 'file'),
1718
                  ('1dir', '1dir', 'directory'),
1719
                  ('2file', '2file', 'file'),
1720
                 ]
1721
                ),
1722
                (('1dir', './1dir'),
1723
                 [('1dir/0file', '0file', 'file'),
1724
                  ('1dir/1dir', '1dir', 'directory'),
1725
                 ]
1726
                ),
1727
                (('1dir/1dir', './1dir/1dir'),
1728
                 [
1729
                 ]
1730
                ),
1731
            ]
1732
        return tree, expected_dirblocks
1733
1734
    def test_walk_cur_dir(self):
1735
        tree, expected_dirblocks = self._get_ascii_tree()
1736
        self.build_tree(tree)
1737
        result = list(osutils._walkdirs_utf8('.'))
1738
        # Filter out stat and abspath
1739
        self.assertEqual(expected_dirblocks,
1740
                         [(dirinfo, [line[0:3] for line in block])
1741
                          for dirinfo, block in result])
1742
1743
    def test_walk_sub_dir(self):
1744
        tree, expected_dirblocks = self._get_ascii_tree()
1745
        self.build_tree(tree)
1746
        # you can search a subdir only, with a supplied prefix.
1747
        result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1748
        # Filter out stat and abspath
1749
        self.assertEqual(expected_dirblocks[1:],
1750
                         [(dirinfo, [line[0:3] for line in block])
1751
                          for dirinfo, block in result])
1752
1753
    def _get_unicode_tree(self):
1754
        name0u = u'0file-\xb6'
1755
        name1u = u'1dir-\u062c\u0648'
1756
        name2u = u'2file-\u0633'
1757
        tree = [
1758
            name0u,
1759
            name1u + '/',
1760
            name1u + '/' + name0u,
1761
            name1u + '/' + name1u + '/',
1762
            name2u,
1763
            ]
1764
        name0 = name0u.encode('UTF-8')
1765
        name1 = name1u.encode('UTF-8')
1766
        name2 = name2u.encode('UTF-8')
1767
        expected_dirblocks = [
1768
                (('', '.'),
1769
                 [(name0, name0, 'file', './' + name0u),
1770
                  (name1, name1, 'directory', './' + name1u),
1771
                  (name2, name2, 'file', './' + name2u),
1772
                 ]
1773
                ),
1774
                ((name1, './' + name1u),
1775
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1776
                                                        + '/' + name0u),
1777
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1778
                                                            + '/' + name1u),
1779
                 ]
1780
                ),
1781
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1782
                 [
1783
                 ]
1784
                ),
1785
            ]
1786
        return tree, expected_dirblocks
1787
4241.14.7 by Vincent Ladeuil
Add a test for symlinks name handling.
1788
    def _filter_out(self, raw_dirblocks):
1789
        """Filter out a walkdirs_utf8 result.
1790
1791
        stat field is removed, all native paths are converted to unicode
1792
        """
1793
        filtered_dirblocks = []
1794
        for dirinfo, block in raw_dirblocks:
4241.14.6 by Vincent Ladeuil
Start DirReader parametrized tests.
1795
            dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1796
            details = []
1797
            for line in block:
1798
                details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
4241.14.7 by Vincent Ladeuil
Add a test for symlinks name handling.
1799
            filtered_dirblocks.append((dirinfo, details))
1800
        return filtered_dirblocks
1801
1802
    def test_walk_unicode_tree(self):
4241.14.25 by Vincent Ladeuil
Fix PQM failures.
1803
        self.requireFeature(tests.UnicodeFilenameFeature)
4241.14.7 by Vincent Ladeuil
Add a test for symlinks name handling.
1804
        tree, expected_dirblocks = self._get_unicode_tree()
1805
        self.build_tree(tree)
1806
        result = list(osutils._walkdirs_utf8('.'))
1807
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1808
1809
    def test_symlink(self):
1810
        self.requireFeature(tests.SymlinkFeature)
4241.14.25 by Vincent Ladeuil
Fix PQM failures.
1811
        self.requireFeature(tests.UnicodeFilenameFeature)
4241.14.14 by Vincent Ladeuil
Test and implements osutils.readlink().
1812
        target = u'target\N{Euro Sign}'
1813
        link_name = u'l\N{Euro Sign}nk'
4241.14.7 by Vincent Ladeuil
Add a test for symlinks name handling.
1814
        os.symlink(target, link_name)
1815
        target_utf8 = target.encode('UTF-8')
1816
        link_name_utf8 = link_name.encode('UTF-8')
1817
        expected_dirblocks = [
1818
                (('', '.'),
1819
                 [(link_name_utf8, link_name_utf8,
1820
                   'symlink', './' + link_name),],
1821
                 )]
1822
        result = list(osutils._walkdirs_utf8('.'))
1823
        self.assertEqual(expected_dirblocks, self._filter_out(result))
4241.14.14 by Vincent Ladeuil
Test and implements osutils.readlink().
1824
1825
1826
class TestReadLink(tests.TestCaseInTempDir):
1827
    """Exposes os.readlink() problems and the osutils solution.
1828
1829
    The only guarantee offered by os.readlink(), starting with 2.6, is that a
1830
    unicode string will be returned if a unicode string is passed.
1831
4241.14.25 by Vincent Ladeuil
Fix PQM failures.
1832
    But prior python versions failed to properly encode the passed unicode
4241.14.14 by Vincent Ladeuil
Test and implements osutils.readlink().
1833
    string.
1834
    """
4331.1.3 by Vincent Ladeuil
Revert previous patch, PQM now presents a unicode friendly file system encoding.
1835
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
4241.14.14 by Vincent Ladeuil
Test and implements osutils.readlink().
1836
1837
    def setUp(self):
1838
        super(tests.TestCaseInTempDir, self).setUp()
1839
        self.link = u'l\N{Euro Sign}ink'
1840
        self.target = u'targe\N{Euro Sign}t'
1841
        os.symlink(self.target, self.link)
1842
1843
    def test_os_readlink_link_encoding(self):
1844
        if sys.version_info < (2, 6):
1845
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1846
        else:
1847
            self.assertEquals(self.target,  os.readlink(self.link))
1848
1849
    def test_os_readlink_link_decoding(self):
1850
        self.assertEquals(self.target.encode(osutils._fs_enc),
1851
                          os.readlink(self.link.encode(osutils._fs_enc)))
4398.4.3 by Vincent Ladeuil
Detect # cores on win32 and Solaris too.
1852
1853
1854
class TestConcurrency(tests.TestCase):
1855
1856
    def test_local_concurrency(self):
1857
        concurrency = osutils.local_concurrency()
1858
        self.assertIsInstance(concurrency, int)
4574.3.3 by Martin Pool
Add test for failure to load extensions
1859
1860
1861
class TestFailedToLoadExtension(tests.TestCase):
1862
1863
    def _try_loading(self):
1864
        try:
1865
            import bzrlib._fictional_extension_py
1866
        except ImportError, e:
4574.3.8 by Martin Pool
Only mutter extension load errors when they occur, and record for later
1867
            osutils.failed_to_load_extension(e)
4574.3.3 by Martin Pool
Add test for failure to load extensions
1868
            return True
1869
4574.3.8 by Martin Pool
Only mutter extension load errors when they occur, and record for later
1870
    def setUp(self):
1871
        super(TestFailedToLoadExtension, self).setUp()
1872
        self.saved_failures = osutils._extension_load_failures[:]
1873
        del osutils._extension_load_failures[:]
1874
        self.addCleanup(self.restore_failures)
1875
1876
    def restore_failures(self):
1877
        osutils._extension_load_failures = self.saved_failures
1878
4574.3.3 by Martin Pool
Add test for failure to load extensions
1879
    def test_failure_to_load(self):
4574.3.8 by Martin Pool
Only mutter extension load errors when they occur, and record for later
1880
        self._try_loading()
1881
        self.assertLength(1, osutils._extension_load_failures)
1882
        self.assertEquals(osutils._extension_load_failures[0],
1883
            "No module named _fictional_extension_py")
1884
4695.4.1 by Martin Pool
Give a shorter/cleaner message for missing extensions
1885
    def test_report_extension_load_failures_no_warning(self):
4574.3.8 by Martin Pool
Only mutter extension load errors when they occur, and record for later
1886
        self.assertTrue(self._try_loading())
1887
        warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
4695.4.1 by Martin Pool
Give a shorter/cleaner message for missing extensions
1888
        # it used to give a Python warning; it no longer does
1889
        self.assertLength(0, warnings)
1890
1891
    def test_report_extension_load_failures_message(self):
1892
        log = StringIO()
1893
        trace.push_log_file(log)
1894
        self.assertTrue(self._try_loading())
1895
        osutils.report_extension_load_failures()
1896
        self.assertContainsRe(
1897
            log.getvalue(),
1898
            r"bzr: warning: some compiled extensions could not be loaded; "
1899
            "see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1900
            )