/brz/remove-bazaar

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