~bzr-pqm/bzr/bzr.dev

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