/brz/remove-bazaar

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