/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
1
# Copyright (C) 2005, 2006, 2007 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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
22
import socket
23
import stat
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
24
import sys
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
25
import time
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
26
27
import bzrlib
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
    )
1685.1.9 by John Arbash Meinel
Updated LocalTransport so that it's base is now a URL rather than a local path. This helps consistency with all other functions. To do so, I added local_abspath() which returns the local path, and local_path_to/from_url
34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
35
from bzrlib.osutils import (
36
        is_inside_any,
37
        is_inside_or_parent_of_any,
38
        pathjoin,
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
39
        pumpfile,
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
40
        pump_string_file,
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
41
        )
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.
42
from bzrlib.tests import (
1739.2.7 by Robert Collins
Update readdir pyrex source files and usage in line with current practice.
43
        adapt_tests,
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
44
        Feature,
2785.1.5 by Alexander Belchenko
support for non-ascii BZR_HOME in show_version()
45
        probe_unicode_in_user_encoding,
1739.2.7 by Robert Collins
Update readdir pyrex source files and usage in line with current practice.
46
        split_suite_by_re,
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.
47
        StringIOWrapper,
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
48
        SymlinkFeature,
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
49
        TestCase,
50
        TestCaseInTempDir,
1739.2.7 by Robert Collins
Update readdir pyrex source files and usage in line with current practice.
51
        TestScenarioApplier,
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.
52
        TestSkipped,
53
        )
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
54
from bzrlib.tests.file_utils import (
55
    FakeReadFile,
56
    )
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
57
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
58
59
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)
60
class _UTF8DirReaderFeature(Feature):
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
61
62
    def _probe(self):
63
        try:
64
            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)
65
            self.reader = _readdir_pyx.UTF8DirReader
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
66
            return True
67
        except ImportError:
68
            return False
69
70
    def feature_name(self):
1739.2.13 by Robert Collins
Fix typo in ReadDirFeature.
71
        return 'bzrlib._readdir_pyx'
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
72
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)
73
UTF8DirReaderFeature = _UTF8DirReaderFeature()
1739.2.12 by Robert Collins
Add ReadDirFeature as per John's review.
74
75
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
76
class TestOSUtils(TestCaseInTempDir):
77
2249.2.1 by John Arbash Meinel
(John Arbash Meinel) hard-code the whitespace chars to avoid problems in some locales.
78
    def test_contains_whitespace(self):
79
        self.failUnless(osutils.contains_whitespace(u' '))
80
        self.failUnless(osutils.contains_whitespace(u'hello there'))
81
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
82
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
83
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
84
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
85
86
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
87
        # is whitespace, but we do not.
88
        self.failIf(osutils.contains_whitespace(u''))
89
        self.failIf(osutils.contains_whitespace(u'hellothere'))
90
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
91
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
92
    def test_fancy_rename(self):
93
        # This should work everywhere
94
        def rename(a, b):
95
            osutils.fancy_rename(a, b,
96
                    rename_func=os.rename,
97
                    unlink_func=os.unlink)
98
99
        open('a', 'wb').write('something in a\n')
100
        rename('a', 'b')
101
        self.failIfExists('a')
102
        self.failUnlessExists('b')
103
        self.check_file_contents('b', 'something in a\n')
104
105
        open('a', 'wb').write('new something in a\n')
106
        rename('b', 'a')
107
108
        self.check_file_contents('a', 'something in a\n')
109
110
    def test_rename(self):
111
        # Rename should be semi-atomic on all platforms
112
        open('a', 'wb').write('something in a\n')
113
        osutils.rename('a', 'b')
114
        self.failIfExists('a')
115
        self.failUnlessExists('b')
116
        self.check_file_contents('b', 'something in a\n')
117
118
        open('a', 'wb').write('new something in a\n')
119
        osutils.rename('b', 'a')
120
121
        self.check_file_contents('a', 'something in a\n')
122
123
    # TODO: test fancy_rename using a MemoryTransport
124
2978.8.2 by Alexander Belchenko
teach fancy_rename to handle change case renames in possible case-insensitive filesystem
125
    def test_rename_change_case(self):
126
        # 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)
127
        self.build_tree(['a', 'b/'])
128
        osutils.rename('a', 'A')
129
        osutils.rename('b', 'B')
2978.8.2 by Alexander Belchenko
teach fancy_rename to handle change case renames in possible case-insensitive filesystem
130
        # we can't use failUnlessExists on case-insensitive filesystem
131
        # so try to check shape of the tree
2978.8.1 by Alexander Belchenko
Rename on Windows is able to change filename case. (#77740)
132
        shape = sorted(os.listdir('.'))
133
        self.assertEquals(['A', 'B'], shape)
134
1553.5.5 by Martin Pool
New utility routine rand_chars
135
    def test_01_rand_chars_empty(self):
136
        result = osutils.rand_chars(0)
137
        self.assertEqual(result, '')
138
139
    def test_02_rand_chars_100(self):
140
        result = osutils.rand_chars(100)
141
        self.assertEqual(len(result), 100)
142
        self.assertEqual(type(result), str)
143
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
144
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
145
    def test_is_inside(self):
146
        is_inside = osutils.is_inside
147
        self.assertTrue(is_inside('src', 'src/foo.c'))
148
        self.assertFalse(is_inside('src', 'srccontrol'))
149
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
150
        self.assertTrue(is_inside('foo.c', 'foo.c'))
151
        self.assertFalse(is_inside('foo.c', ''))
152
        self.assertTrue(is_inside('', 'foo.c'))
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
153
2729.2.4 by Martin Pool
move some osutils-related tests from test_inv to test_osutils
154
    def test_is_inside_any(self):
155
        SRC_FOO_C = pathjoin('src', 'foo.c')
156
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
157
                         (['src'], SRC_FOO_C),
158
                         (['src'], 'src'),
159
                         ]:
160
            self.assert_(is_inside_any(dirs, fn))
161
        for dirs, fn in [(['src'], 'srccontrol'),
162
                         (['src'], 'srccontrol/foo')]:
163
            self.assertFalse(is_inside_any(dirs, fn))
164
165
    def test_is_inside_or_parent_of_any(self):
166
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
167
                         (['src'], 'src/foo.c'),
168
                         (['src/bar.c'], 'src'),
169
                         (['src/bar.c', 'bla/foo.c'], 'src'),
170
                         (['src'], 'src'),
171
                         ]:
172
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
173
            
174
        for dirs, fn in [(['src'], 'srccontrol'),
175
                         (['srccontrol/foo.c'], 'src'),
176
                         (['src'], 'srccontrol/foo')]:
177
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
178
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
179
    def test_rmtree(self):
180
        # Check to remove tree with read-only files/dirs
181
        os.mkdir('dir')
182
        f = file('dir/file', 'w')
183
        f.write('spam')
184
        f.close()
185
        # would like to also try making the directory readonly, but at the
186
        # moment python shutil.rmtree doesn't handle that properly - it would
187
        # need to chmod the directory before removing things inside it - deferred
188
        # for now -- mbp 20060505
189
        # osutils.make_readonly('dir')
190
        osutils.make_readonly('dir/file')
191
192
        osutils.rmtree('dir')
193
194
        self.failIfExists('dir/file')
195
        self.failIfExists('dir')
196
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
197
    def test_file_kind(self):
198
        self.build_tree(['file', 'dir/'])
199
        self.assertEquals('file', osutils.file_kind('file'))
200
        self.assertEquals('directory', osutils.file_kind('dir/'))
201
        if osutils.has_symlinks():
202
            os.symlink('symlink', 'symlink')
203
            self.assertEquals('symlink', osutils.file_kind('symlink'))
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
204
        
205
        # TODO: jam 20060529 Test a block device
206
        try:
207
            os.lstat('/dev/null')
208
        except OSError, e:
209
            if e.errno not in (errno.ENOENT,):
210
                raise
211
        else:
212
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
213
214
        mkfifo = getattr(os, 'mkfifo', None)
215
        if mkfifo:
216
            mkfifo('fifo')
217
            try:
218
                self.assertEquals('fifo', osutils.file_kind('fifo'))
219
            finally:
220
                os.remove('fifo')
221
222
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
223
        if AF_UNIX:
224
            s = socket.socket(AF_UNIX)
225
            s.bind('socket')
226
            try:
227
                self.assertEquals('socket', osutils.file_kind('socket'))
228
            finally:
229
                os.remove('socket')
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
230
1551.10.27 by Aaron Bentley
Add a kind marker for subtrees
231
    def test_kind_marker(self):
232
        self.assertEqual(osutils.kind_marker('file'), '')
233
        self.assertEqual(osutils.kind_marker('directory'), '/')
234
        self.assertEqual(osutils.kind_marker('symlink'), '@')
1551.10.28 by Aaron Bentley
change kind marker to '+'
235
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
1551.10.27 by Aaron Bentley
Add a kind marker for subtrees
236
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
237
    def test_get_umask(self):
238
        if sys.platform == 'win32':
239
            # umask always returns '0', no way to set it
240
            self.assertEqual(0, osutils.get_umask())
241
            return
242
243
        orig_umask = osutils.get_umask()
244
        try:
245
            os.umask(0222)
246
            self.assertEqual(0222, osutils.get_umask())
247
            os.umask(0022)
248
            self.assertEqual(0022, osutils.get_umask())
249
            os.umask(0002)
250
            self.assertEqual(0002, osutils.get_umask())
251
            os.umask(0027)
252
            self.assertEqual(0027, osutils.get_umask())
253
        finally:
254
            os.umask(orig_umask)
255
1957.1.15 by John Arbash Meinel
Review feedback from Robert
256
    def assertFormatedDelta(self, expected, seconds):
257
        """Assert osutils.format_delta formats as expected"""
258
        actual = osutils.format_delta(seconds)
259
        self.assertEqual(expected, actual)
260
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
261
    def test_format_delta(self):
1957.1.15 by John Arbash Meinel
Review feedback from Robert
262
        self.assertFormatedDelta('0 seconds ago', 0)
263
        self.assertFormatedDelta('1 second ago', 1)
264
        self.assertFormatedDelta('10 seconds ago', 10)
265
        self.assertFormatedDelta('59 seconds ago', 59)
266
        self.assertFormatedDelta('89 seconds ago', 89)
267
        self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
268
        self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
269
        self.assertFormatedDelta('3 minutes, 1 second ago', 181)
270
        self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
271
        self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
272
        self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
273
        self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
274
        self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
275
        self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
276
        self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
277
        self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
278
        self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
279
        self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
280
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
281
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
282
        self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
283
        self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
284
        self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
285
286
        # We handle when time steps the wrong direction because computers
287
        # don't have synchronized clocks.
1957.1.15 by John Arbash Meinel
Review feedback from Robert
288
        self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
289
        self.assertFormatedDelta('1 second in the future', -1)
290
        self.assertFormatedDelta('2 seconds in the future', -2)
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
291
3144.1.1 by Lukáš Lalinský
Fixed error reporting of unsupported timezone format.
292
    def test_format_date(self):
293
        self.assertRaises(errors.UnsupportedTimezoneFormat,
294
            osutils.format_date, 0, timezone='foo')
295
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
296
    def test_dereference_path(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
297
        self.requireFeature(SymlinkFeature)
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
298
        cwd = osutils.realpath('.')
299
        os.mkdir('bar')
300
        bar_path = osutils.pathjoin(cwd, 'bar')
301
        # Using './' to avoid bug #1213894 (first path component not
302
        # dereferenced) in Python 2.4.1 and earlier
303
        self.assertEqual(bar_path, osutils.realpath('./bar'))
304
        os.symlink('bar', 'foo')
305
        self.assertEqual(bar_path, osutils.realpath('./foo'))
306
        
307
        # Does not dereference terminal symlinks
308
        foo_path = osutils.pathjoin(cwd, 'foo')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
309
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
310
311
        # Dereferences parent symlinks
312
        os.mkdir('bar/baz')
313
        baz_path = osutils.pathjoin(bar_path, 'baz')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
314
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
315
316
        # Dereferences parent symlinks that are the first path element
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
317
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
318
319
        # Dereferences parent symlinks in absolute paths
320
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
321
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
322
2568.1.1 by John Arbash Meinel
(Elliot Murphy) Use os.lstat rather than os.stat for osutils.make_readonly/make_writeable
323
    def test_changing_access(self):
324
        f = file('file', 'w')
325
        f.write('monkey')
326
        f.close()
327
328
        # Make a file readonly
329
        osutils.make_readonly('file')
2949.6.2 by Alexander Belchenko
more changes osutils.lstat -> os.lstat
330
        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
331
        self.assertEqual(mode, mode & 0777555)
332
333
        # Make a file writable
334
        osutils.make_writable('file')
2949.6.2 by Alexander Belchenko
more changes osutils.lstat -> os.lstat
335
        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
336
        self.assertEqual(mode, mode | 0200)
337
338
        if osutils.has_symlinks():
339
            # should not error when handed a symlink
340
            os.symlink('nonexistent', 'dangling')
341
            osutils.make_readonly('dangling')
342
            osutils.make_writable('dangling')
343
2324.2.1 by Dmitry Vasiliev
kind_marker() optimization
344
    def test_kind_marker(self):
345
        self.assertEqual("", osutils.kind_marker("file"))
346
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
347
        self.assertEqual("@", osutils.kind_marker("symlink"))
348
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
349
3287.18.26 by Matt McClure
Addresses concerns raised in
350
    def test_host_os_dereferences_symlinks(self):
351
        osutils.host_os_dereferences_symlinks()
352
2324.2.1 by Dmitry Vasiliev
kind_marker() optimization
353
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
354
class TestPumpFile(TestCase):
355
    """Test pumpfile method."""
356
    def setUp(self):
357
        # create a test datablock
358
        self.block_size = 512
359
        pattern = '0123456789ABCDEF'
360
        self.test_data = pattern * (3 * self.block_size / len(pattern))
361
        self.test_data_len = len(self.test_data)
362
363
    def test_bracket_block_size(self):
364
        """Read data in blocks with the requested read size bracketing the
365
        block size."""
366
        # make sure test data is larger than max read size
367
        self.assertTrue(self.test_data_len > self.block_size)
368
369
        from_file = FakeReadFile(self.test_data)
370
        to_file = StringIO()
371
372
        # read (max / 2) bytes and verify read size wasn't affected
373
        num_bytes_to_read = self.block_size / 2
374
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
375
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
376
        self.assertEqual(from_file.get_read_count(), 1)
377
378
        # read (max) bytes and verify read size wasn't affected
379
        num_bytes_to_read = self.block_size
380
        from_file.reset_read_count()
381
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
382
        self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
383
        self.assertEqual(from_file.get_read_count(), 1)
384
385
        # read (max + 1) bytes and verify read size was limited
386
        num_bytes_to_read = self.block_size + 1
387
        from_file.reset_read_count()
388
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
389
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
390
        self.assertEqual(from_file.get_read_count(), 2)
391
392
        # finish reading the rest of the data
393
        num_bytes_to_read = self.test_data_len - to_file.tell()
394
        pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
395
396
        # report error if the data wasn't equal (we only report the size due
397
        # to the length of the data)
398
        response_data = to_file.getvalue()
399
        if response_data != self.test_data:
400
            message = "Data not equal.  Expected %d bytes, received %d."
401
            self.fail(message % (len(response_data), self.test_data_len))
402
403
    def test_specified_size(self):
404
        """Request a transfer larger than the maximum block size and verify
405
        that the maximum read doesn't exceed the block_size."""
406
        # make sure test data is larger than max read size
407
        self.assertTrue(self.test_data_len > self.block_size)
408
409
        # retrieve data in blocks
410
        from_file = FakeReadFile(self.test_data)
411
        to_file = StringIO()
412
        pumpfile(from_file, to_file, self.test_data_len, self.block_size)
413
414
        # verify read size was equal to the maximum read size
415
        self.assertTrue(from_file.get_max_read_size() > 0)
416
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
417
        self.assertEqual(from_file.get_read_count(), 3)
418
419
        # report error if the data wasn't equal (we only report the size due
420
        # to the length of the data)
421
        response_data = to_file.getvalue()
422
        if response_data != self.test_data:
423
            message = "Data not equal.  Expected %d bytes, received %d."
424
            self.fail(message % (len(response_data), self.test_data_len))
425
426
    def test_to_eof(self):
427
        """Read to end-of-file and verify that the reads are not larger than
428
        the maximum read size."""
429
        # make sure test data is larger than max read size
430
        self.assertTrue(self.test_data_len > self.block_size)
431
432
        # retrieve data to EOF
433
        from_file = FakeReadFile(self.test_data)
434
        to_file = StringIO()
435
        pumpfile(from_file, to_file, -1, self.block_size)
436
437
        # verify read size was equal to the maximum read size
438
        self.assertEqual(from_file.get_max_read_size(), self.block_size)
439
        self.assertEqual(from_file.get_read_count(), 4)
440
441
        # report error if the data wasn't equal (we only report the size due
442
        # to the length of the data)
443
        response_data = to_file.getvalue()
444
        if response_data != self.test_data:
445
            message = "Data not equal.  Expected %d bytes, received %d."
446
            self.fail(message % (len(response_data), self.test_data_len))
447
448
    def test_defaults(self):
449
        """Verifies that the default arguments will read to EOF -- this
450
        test verifies that any existing usages of pumpfile will not be broken
451
        with this new version."""
452
        # retrieve data using default (old) pumpfile method
453
        from_file = FakeReadFile(self.test_data)
454
        to_file = StringIO()
455
        pumpfile(from_file, to_file)
456
457
        # report error if the data wasn't equal (we only report the size due
458
        # to the length of the data)
459
        response_data = to_file.getvalue()
460
        if response_data != self.test_data:
461
            message = "Data not equal.  Expected %d bytes, received %d."
462
            self.fail(message % (len(response_data), self.test_data_len))
463
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
464
465
class TestPumpStringFile(TestCase):
466
467
    def test_empty(self):
468
        output = StringIO()
469
        pump_string_file("", output)
470
        self.assertEqual("", output.getvalue())
471
472
    def test_more_than_segment_size(self):
473
        output = StringIO()
474
        pump_string_file("123456789", output, 2)
475
        self.assertEqual("123456789", output.getvalue())
476
477
    def test_segment_size(self):
478
        output = StringIO()
479
        pump_string_file("12", output, 2)
480
        self.assertEqual("12", output.getvalue())
481
482
    def test_segment_size_multiple(self):
483
        output = StringIO()
484
        pump_string_file("1234", output, 2)
485
        self.assertEqual("1234", output.getvalue())
486
487
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
488
class TestSafeUnicode(TestCase):
489
490
    def test_from_ascii_string(self):
491
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
492
1534.3.2 by Robert Collins
An extra test for John.
493
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
494
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
495
1534.3.2 by Robert Collins
An extra test for John.
496
    def test_from_unicode_string_unicode_contents(self):
497
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
498
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
499
    def test_from_utf8_string(self):
500
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
501
502
    def test_bad_utf8_string(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
503
        self.assertRaises(BzrBadParameterNotUnicode,
504
                          osutils.safe_unicode,
505
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
506
507
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
508
class TestSafeUtf8(TestCase):
509
510
    def test_from_ascii_string(self):
511
        f = 'foobar'
512
        self.assertEqual('foobar', osutils.safe_utf8(f))
513
514
    def test_from_unicode_string_ascii_contents(self):
515
        self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
516
517
    def test_from_unicode_string_unicode_contents(self):
518
        self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
519
520
    def test_from_utf8_string(self):
521
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
522
523
    def test_bad_utf8_string(self):
524
        self.assertRaises(BzrBadParameterNotUnicode,
525
                          osutils.safe_utf8, '\xbb\xbb')
526
527
528
class TestSafeRevisionId(TestCase):
529
530
    def test_from_ascii_string(self):
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
531
        # 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.
532
        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.
533
534
    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.
535
        self.assertEqual('bargam',
536
                         osutils.safe_revision_id(u'bargam', warn=False))
537
538
    def test_from_unicode_deprecated(self):
539
        self.assertEqual('bargam',
540
            self.callDeprecated([osutils._revision_id_warning],
541
                                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.
542
543
    def test_from_unicode_string_unicode_contents(self):
544
        self.assertEqual('bargam\xc2\xae',
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
545
                         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.
546
547
    def test_from_utf8_string(self):
548
        self.assertEqual('foo\xc2\xae',
549
                         osutils.safe_revision_id('foo\xc2\xae'))
550
2249.5.9 by John Arbash Meinel
Update WorkingTree to use safe_revision_id when appropriate
551
    def test_none(self):
552
        """Currently, None is a valid revision_id"""
553
        self.assertEqual(None, osutils.safe_revision_id(None))
554
2249.5.8 by John Arbash Meinel
Add osutils.safe_utf8 and safe_revision_id for the new revision_id work.
555
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
556
class TestSafeFileId(TestCase):
557
558
    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.
559
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
560
561
    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.
562
        self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
563
564
    def test_from_unicode_deprecated(self):
565
        self.assertEqual('bargam',
566
            self.callDeprecated([osutils._file_id_warning],
567
                                osutils.safe_file_id, u'bargam'))
2294.1.4 by John Arbash Meinel
Add safe_file_id as a helper in osutils.
568
569
    def test_from_unicode_string_unicode_contents(self):
570
        self.assertEqual('bargam\xc2\xae',
2309.4.4 by John Arbash Meinel
Change what warnings are raised, and add tests that they are used.
571
                         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.
572
573
    def test_from_utf8_string(self):
574
        self.assertEqual('foo\xc2\xae',
575
                         osutils.safe_file_id('foo\xc2\xae'))
576
577
    def test_none(self):
578
        """Currently, None is a valid revision_id"""
579
        self.assertEqual(None, osutils.safe_file_id(None))
580
581
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
582
class TestWin32Funcs(TestCase):
583
    """Test that the _win32 versions of os utilities return appropriate paths."""
584
585
    def test_abspath(self):
586
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
587
        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
588
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
589
        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.
590
591
    def test_realpath(self):
592
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
593
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
594
595
    def test_pathjoin(self):
596
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
597
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
598
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
599
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
600
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
601
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
602
603
    def test_normpath(self):
604
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
605
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
606
607
    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
608
        cwd = osutils._win32_getcwd()
609
        os_cwd = os.getcwdu()
610
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
611
        # win32 is inconsistent whether it returns lower or upper case
612
        # and even if it was consistent the user might type the other
613
        # so we force it to uppercase
614
        # running python.exe under cmd.exe return capital C:\\
615
        # running win32 python inside a cygwin shell returns lowercase
616
        self.assertEqual(os_cwd[0].upper(), cwd[0])
617
618
    def test_fixdrive(self):
619
        self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
620
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
621
        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.
622
2279.4.1 by Alexander Belchenko
Reimplementation of ntpath.abspath in Python for Windows98: unicode safe, UNC path safe
623
    def test_win98_abspath(self):
624
        # absolute path
625
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
626
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
627
        # UNC path
628
        self.assertEqual('//HOST/path', osutils._win98_abspath(r'\\HOST\path'))
629
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
630
        # relative path
631
        cwd = osutils.getcwd().rstrip('/')
632
        drive = osutils._nt_splitdrive(cwd)[0]
633
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
634
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
635
        # unicode path
636
        u = u'\u1234'
637
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
638
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
639
640
class TestWin32FuncsDirs(TestCaseInTempDir):
641
    """Test win32 functions that create files."""
642
    
643
    def test_getcwd(self):
2279.4.1 by Alexander Belchenko
Reimplementation of ntpath.abspath in Python for Windows98: unicode safe, UNC path safe
644
        if win32utils.winver == 'Windows 98':
645
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
646
        # Make sure getcwd can handle unicode filenames
647
        try:
1830.3.9 by John Arbash Meinel
Use a directory name that doesn't get messed up on Mac for getcwd() test.
648
            os.mkdir(u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
649
        except UnicodeError:
650
            raise TestSkipped("Unable to create Unicode filename")
651
1830.3.9 by John Arbash Meinel
Use a directory name that doesn't get messed up on Mac for getcwd() test.
652
        os.chdir(u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
653
        # TODO: jam 20060427 This will probably fail on Mac OSX because
654
        #       it will change the normalization of B\xe5gfors
655
        #       Consider using a different unicode character, or make
656
        #       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.
657
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
658
2825.7.1 by Robert Collins
* Partial commits are now approximately 40% faster by walking over the
659
    def test_minimum_path_selection(self):
660
        self.assertEqual(set(),
661
            osutils.minimum_path_selection([]))
662
        self.assertEqual(set(['a', 'b']),
663
            osutils.minimum_path_selection(['a', 'b']))
664
        self.assertEqual(set(['a/', 'b']),
665
            osutils.minimum_path_selection(['a/', 'b']))
666
        self.assertEqual(set(['a/', 'b']),
667
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
668
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
669
    def test_mkdtemp(self):
670
        tmpdir = osutils._win32_mkdtemp(dir='.')
671
        self.assertFalse('\\' in tmpdir)
672
673
    def test_rename(self):
674
        a = open('a', 'wb')
675
        a.write('foo\n')
676
        a.close()
677
        b = open('b', 'wb')
678
        b.write('baz\n')
679
        b.close()
680
681
        osutils._win32_rename('b', 'a')
682
        self.failUnlessExists('a')
683
        self.failIfExists('b')
684
        self.assertFileEqual('baz\n', 'a')
685
1711.7.6 by John Arbash Meinel
Change _win32_rename() so that it raises ENOENT *before* it tries any renaming.
686
    def test_rename_missing_file(self):
687
        a = open('a', 'wb')
688
        a.write('foo\n')
689
        a.close()
690
691
        try:
692
            osutils._win32_rename('b', 'a')
693
        except (IOError, OSError), e:
694
            self.assertEqual(errno.ENOENT, e.errno)
695
        self.assertFileEqual('foo\n', 'a')
696
697
    def test_rename_missing_dir(self):
698
        os.mkdir('a')
699
        try:
700
            osutils._win32_rename('b', 'a')
701
        except (IOError, OSError), e:
702
            self.assertEqual(errno.ENOENT, e.errno)
703
704
    def test_rename_current_dir(self):
705
        os.mkdir('a')
706
        os.chdir('a')
707
        # You can't rename the working directory
708
        # doing rename non-existant . usually
709
        # just raises ENOENT, since non-existant
710
        # doesn't exist.
711
        try:
712
            osutils._win32_rename('b', '.')
713
        except (IOError, OSError), e:
714
            self.assertEqual(errno.ENOENT, e.errno)
715
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
716
    def test_splitpath(self):
717
        def check(expected, path):
718
            self.assertEqual(expected, osutils.splitpath(path))
719
720
        check(['a'], 'a')
721
        check(['a', 'b'], 'a/b')
722
        check(['a', 'b'], 'a/./b')
723
        check(['a', '.b'], 'a/.b')
724
        check(['a', '.b'], 'a\\.b')
725
726
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
727
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
728
1830.3.11 by John Arbash Meinel
Create a mac version of 'getcwd()' which normalizes the path.
729
class TestMacFuncsDirs(TestCaseInTempDir):
730
    """Test mac special functions that require directories."""
731
732
    def test_getcwd(self):
733
        # On Mac, this will actually create Ba\u030agfors
734
        # but chdir will still work, because it accepts both paths
735
        try:
736
            os.mkdir(u'B\xe5gfors')
737
        except UnicodeError:
738
            raise TestSkipped("Unable to create Unicode filename")
739
740
        os.chdir(u'B\xe5gfors')
741
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
742
743
    def test_getcwd_nonnorm(self):
744
        # Test that _mac_getcwd() will normalize this path
745
        try:
746
            os.mkdir(u'Ba\u030agfors')
747
        except UnicodeError:
748
            raise TestSkipped("Unable to create Unicode filename")
749
750
        os.chdir(u'Ba\u030agfors')
751
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
752
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
753
1666.1.6 by Robert Collins
Make knit the default format.
754
class TestSplitLines(TestCase):
755
756
    def test_split_unicode(self):
757
        self.assertEqual([u'foo\n', u'bar\xae'],
758
                         osutils.split_lines(u'foo\nbar\xae'))
759
        self.assertEqual([u'foo\n', u'bar\xae\n'],
760
                         osutils.split_lines(u'foo\nbar\xae\n'))
761
762
    def test_split_with_carriage_returns(self):
763
        self.assertEqual(['foo\rbar\n'],
764
                         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.
765
766
767
class TestWalkDirs(TestCaseInTempDir):
768
769
    def test_walkdirs(self):
770
        tree = [
771
            '.bzr',
772
            '0file',
773
            '1dir/',
774
            '1dir/0file',
775
            '1dir/1dir/',
776
            '2file'
777
            ]
778
        self.build_tree(tree)
779
        expected_dirblocks = [
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
780
                (('', '.'),
781
                 [('0file', '0file', 'file'),
782
                  ('1dir', '1dir', 'directory'),
783
                  ('2file', '2file', 'file'),
784
                 ]
785
                ),
786
                (('1dir', './1dir'),
787
                 [('1dir/0file', '0file', 'file'),
788
                  ('1dir/1dir', '1dir', 'directory'),
789
                 ]
790
                ),
791
                (('1dir/1dir', './1dir/1dir'),
792
                 [
793
                 ]
794
                ),
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
795
            ]
796
        result = []
797
        found_bzrdir = False
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
798
        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.
799
            if len(dirblock) and dirblock[0][1] == '.bzr':
800
                # this tests the filtering of selected paths
801
                found_bzrdir = True
802
                del dirblock[0]
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
803
            result.append((dirdetail, dirblock))
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
804
805
        self.assertTrue(found_bzrdir)
806
        self.assertEqual(expected_dirblocks,
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
807
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
808
        # you can search a subdir only, with a supplied prefix.
809
        result = []
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
810
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
811
            result.append(dirblock)
812
        self.assertEqual(expected_dirblocks[1:],
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
813
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
814
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
815
    def test__walkdirs_utf8(self):
816
        tree = [
817
            '.bzr',
818
            '0file',
819
            '1dir/',
820
            '1dir/0file',
821
            '1dir/1dir/',
822
            '2file'
823
            ]
824
        self.build_tree(tree)
825
        expected_dirblocks = [
826
                (('', '.'),
827
                 [('0file', '0file', 'file'),
828
                  ('1dir', '1dir', 'directory'),
829
                  ('2file', '2file', 'file'),
830
                 ]
831
                ),
832
                (('1dir', './1dir'),
833
                 [('1dir/0file', '0file', 'file'),
834
                  ('1dir/1dir', '1dir', 'directory'),
835
                 ]
836
                ),
837
                (('1dir/1dir', './1dir/1dir'),
838
                 [
839
                 ]
840
                ),
841
            ]
842
        result = []
843
        found_bzrdir = False
844
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
845
            if len(dirblock) and dirblock[0][1] == '.bzr':
846
                # this tests the filtering of selected paths
847
                found_bzrdir = True
848
                del dirblock[0]
849
            result.append((dirdetail, dirblock))
850
851
        self.assertTrue(found_bzrdir)
852
        self.assertEqual(expected_dirblocks,
853
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
854
        # you can search a subdir only, with a supplied prefix.
855
        result = []
856
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
857
            result.append(dirblock)
858
        self.assertEqual(expected_dirblocks[1:],
859
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
860
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
861
    def _filter_out_stat(self, result):
862
        """Filter out the stat value from the walkdirs result"""
863
        for dirdetail, dirblock in result:
864
            new_dirblock = []
865
            for info in dirblock:
866
                # Ignore info[3] which is the stat
867
                new_dirblock.append((info[0], info[1], info[2], info[4]))
868
            dirblock[:] = new_dirblock
869
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
870
    def _save_platform_info(self):
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
871
        cur_winver = win32utils.winver
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
872
        cur_fs_enc = osutils._fs_enc
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
873
        cur_dir_reader = osutils._selected_dir_reader
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
874
        def restore():
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
875
            win32utils.winver = cur_winver
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
876
            osutils._fs_enc = cur_fs_enc
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
877
            osutils._selected_dir_reader = cur_dir_reader
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
878
        self.addCleanup(restore)
879
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
880
    def assertReadFSDirIs(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'
881
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
882
        # Force it to redetect
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
883
        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'
884
        # 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.
885
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
886
        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'
887
888
    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)
889
        self.requireFeature(UTF8DirReaderFeature)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
890
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
891
        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,
892
        osutils._fs_enc = 'UTF-8'
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)
893
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
894
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'
895
    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)
896
        self.requireFeature(UTF8DirReaderFeature)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
897
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
898
        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,
899
        osutils._fs_enc = 'US-ASCII'
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)
900
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
901
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'
902
    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)
903
        self.requireFeature(UTF8DirReaderFeature)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
904
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
905
        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,
906
        osutils._fs_enc = 'ANSI_X3.4-1968'
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)
907
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
908
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'
909
    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,
910
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
911
        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,
912
        osutils._fs_enc = 'latin1'
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
913
        self.assertReadFSDirIs(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'
914
915
    def test_force_walkdirs_utf8_nt(self):
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
916
        # Disabled because the thunk of the whole walkdirs api is disabled.
917
        self.requireFeature(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'
918
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
919
        win32utils.winver = 'Windows NT'
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
920
        from bzrlib._walkdirs_win32 import Win32ReadDir
921
        self.assertReadFSDirIs(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'
922
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
923
    def test_force_walkdirs_utf8_98(self):
924
        self.requireFeature(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'
925
        self._save_platform_info()
3557.2.6 by John Arbash Meinel
Switch from os.name to bzrlib.win32utils.winver.
926
        win32utils.winver = 'Windows 98'
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
927
        self.assertReadFSDirIs(osutils.UnicodeDirReader)
3557.2.3 by John Arbash Meinel
Change the logic for selecting a real _walkdirs_utf8 implementation,
928
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
929
    def test_unicode_walkdirs(self):
930
        """Walkdirs should always return unicode paths."""
931
        name0 = u'0file-\xb6'
932
        name1 = u'1dir-\u062c\u0648'
933
        name2 = u'2file-\u0633'
934
        tree = [
935
            name0,
936
            name1 + '/',
937
            name1 + '/' + name0,
938
            name1 + '/' + name1 + '/',
939
            name2,
940
            ]
941
        try:
942
            self.build_tree(tree)
943
        except UnicodeError:
944
            raise TestSkipped('Could not represent Unicode chars'
945
                              ' in current encoding.')
946
        expected_dirblocks = [
947
                ((u'', u'.'),
948
                 [(name0, name0, 'file', './' + name0),
949
                  (name1, name1, 'directory', './' + name1),
950
                  (name2, name2, 'file', './' + name2),
951
                 ]
952
                ),
953
                ((name1, './' + name1),
954
                 [(name1 + '/' + name0, name0, 'file', './' + name1
955
                                                        + '/' + name0),
956
                  (name1 + '/' + name1, name1, 'directory', './' + name1
957
                                                            + '/' + name1),
958
                 ]
959
                ),
960
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
961
                 [
962
                 ]
963
                ),
964
            ]
965
        result = list(osutils.walkdirs('.'))
966
        self._filter_out_stat(result)
967
        self.assertEqual(expected_dirblocks, result)
968
        result = list(osutils.walkdirs(u'./'+name1, name1))
969
        self._filter_out_stat(result)
970
        self.assertEqual(expected_dirblocks[1:], result)
971
972
    def test_unicode__walkdirs_utf8(self):
973
        """Walkdirs_utf8 should always return utf8 paths.
974
975
        The abspath portion might be in unicode or utf-8
976
        """
977
        name0 = u'0file-\xb6'
978
        name1 = u'1dir-\u062c\u0648'
979
        name2 = u'2file-\u0633'
980
        tree = [
981
            name0,
982
            name1 + '/',
983
            name1 + '/' + name0,
984
            name1 + '/' + name1 + '/',
985
            name2,
986
            ]
987
        try:
988
            self.build_tree(tree)
989
        except UnicodeError:
990
            raise TestSkipped('Could not represent Unicode chars'
991
                              ' in current encoding.')
992
        name0 = name0.encode('utf8')
993
        name1 = name1.encode('utf8')
994
        name2 = name2.encode('utf8')
995
996
        expected_dirblocks = [
997
                (('', '.'),
998
                 [(name0, name0, 'file', './' + name0),
999
                  (name1, name1, 'directory', './' + name1),
1000
                  (name2, name2, 'file', './' + name2),
1001
                 ]
1002
                ),
1003
                ((name1, './' + name1),
1004
                 [(name1 + '/' + name0, name0, 'file', './' + name1
1005
                                                        + '/' + name0),
1006
                  (name1 + '/' + name1, name1, 'directory', './' + name1
1007
                                                            + '/' + name1),
1008
                 ]
1009
                ),
1010
                ((name1 + '/' + name1, './' + name1 + '/' + name1),
1011
                 [
1012
                 ]
1013
                ),
1014
            ]
1015
        result = []
1016
        # For ease in testing, if walkdirs_utf8 returns Unicode, assert that
1017
        # all abspaths are Unicode, and encode them back into utf8.
1018
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1019
            self.assertIsInstance(dirdetail[0], str)
1020
            if isinstance(dirdetail[1], unicode):
2324.2.4 by Dmitry Vasiliev
Fixed test_unicode__walkdirs_utf8 test
1021
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1022
                dirblock = [list(info) for info in dirblock]
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1023
                for info in dirblock:
1024
                    self.assertIsInstance(info[4], unicode)
1025
                    info[4] = info[4].encode('utf8')
1026
            new_dirblock = []
1027
            for info in dirblock:
1028
                self.assertIsInstance(info[0], str)
1029
                self.assertIsInstance(info[1], str)
1030
                self.assertIsInstance(info[4], str)
1031
                # Remove the stat information
1032
                new_dirblock.append((info[0], info[1], info[2], info[4]))
1033
            result.append((dirdetail, new_dirblock))
1034
        self.assertEqual(expected_dirblocks, result)
1035
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1036
    def test__walkdirs_utf8_with_unicode_fs(self):
1037
        """UnicodeDirReader should be a safe fallback everywhere
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1038
1039
        The abspath portion should be in unicode
1040
        """
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1041
        # Use the unicode reader. TODO: split into driver-and-driven unit
1042
        # tests.
1043
        self._save_platform_info()
1044
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1045
        name0u = u'0file-\xb6'
1046
        name1u = u'1dir-\u062c\u0648'
1047
        name2u = u'2file-\u0633'
1048
        tree = [
1049
            name0u,
1050
            name1u + '/',
1051
            name1u + '/' + name0u,
1052
            name1u + '/' + name1u + '/',
1053
            name2u,
1054
            ]
1055
        try:
1056
            self.build_tree(tree)
1057
        except UnicodeError:
1058
            raise TestSkipped('Could not represent Unicode chars'
1059
                              ' in current encoding.')
1060
        name0 = name0u.encode('utf8')
1061
        name1 = name1u.encode('utf8')
1062
        name2 = name2u.encode('utf8')
1063
1064
        # All of the abspaths should be in unicode, all of the relative paths
1065
        # should be in utf8
1066
        expected_dirblocks = [
1067
                (('', '.'),
1068
                 [(name0, name0, 'file', './' + name0u),
1069
                  (name1, name1, 'directory', './' + name1u),
1070
                  (name2, name2, 'file', './' + name2u),
1071
                 ]
1072
                ),
1073
                ((name1, './' + name1u),
1074
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1075
                                                        + '/' + name0u),
1076
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1077
                                                            + '/' + name1u),
1078
                 ]
1079
                ),
1080
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1081
                 [
1082
                 ]
1083
                ),
1084
            ]
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1085
        result = list(osutils._walkdirs_utf8('.'))
2255.7.32 by John Arbash Meinel
Add tests that the walkdirs variants work on unicode paths.
1086
        self._filter_out_stat(result)
1087
        self.assertEqual(expected_dirblocks, result)
1088
3696.3.4 by John Arbash Meinel
Update the osutils test to find the objects in the right locations.
1089
    def test__walkdirs_utf8_win32readdir(self):
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1090
        self.requireFeature(Win32ReadDirFeature)
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
1091
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1092
        from bzrlib._walkdirs_win32 import Win32ReadDir
1093
        self._save_platform_info()
3696.3.4 by John Arbash Meinel
Update the osutils test to find the objects in the right locations.
1094
        osutils._selected_dir_reader = Win32ReadDir()
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
1095
        name0u = u'0file-\xb6'
1096
        name1u = u'1dir-\u062c\u0648'
1097
        name2u = u'2file-\u0633'
1098
        tree = [
1099
            name0u,
1100
            name1u + '/',
1101
            name1u + '/' + name0u,
1102
            name1u + '/' + name1u + '/',
1103
            name2u,
1104
            ]
1105
        self.build_tree(tree)
1106
        name0 = name0u.encode('utf8')
1107
        name1 = name1u.encode('utf8')
1108
        name2 = name2u.encode('utf8')
1109
1110
        # All of the abspaths should be in unicode, all of the relative paths
1111
        # should be in utf8
1112
        expected_dirblocks = [
1113
                (('', '.'),
1114
                 [(name0, name0, 'file', './' + name0u),
1115
                  (name1, name1, 'directory', './' + name1u),
1116
                  (name2, name2, 'file', './' + name2u),
1117
                 ]
1118
                ),
1119
                ((name1, './' + name1u),
1120
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1121
                                                        + '/' + name0u),
1122
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1123
                                                            + '/' + name1u),
1124
                 ]
1125
                ),
1126
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1127
                 [
1128
                 ]
1129
                ),
1130
            ]
3696.3.4 by John Arbash Meinel
Update the osutils test to find the objects in the right locations.
1131
        result = list(osutils._walkdirs_utf8(u'.'))
3504.4.1 by John Arbash Meinel
Write an alternative 'walkdirs' implementation that uses win32 apis.
1132
        self._filter_out_stat(result)
1133
        self.assertEqual(expected_dirblocks, result)
1134
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1135
    def assertStatIsCorrect(self, path, win32stat):
1136
        os_stat = os.stat(path)
1137
        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.
1138
        self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
1139
        self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
1140
        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.
1141
        self.assertEqual(os_stat.st_dev, win32stat.st_dev)
1142
        self.assertEqual(os_stat.st_ino, win32stat.st_ino)
1143
        self.assertEqual(os_stat.st_mode, win32stat.st_mode)
1144
1145
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1146
        """make sure our Stat values are valid"""
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1147
        self.requireFeature(Win32ReadDirFeature)
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1148
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1149
        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.
1150
        name0u = u'0file-\xb6'
1151
        name0 = name0u.encode('utf8')
1152
        self.build_tree([name0u])
1153
        # I hate to sleep() here, but I'm trying to make the ctime different
1154
        # from the mtime
1155
        time.sleep(2)
1156
        f = open(name0u, 'ab')
1157
        try:
1158
            f.write('just a small update')
1159
        finally:
1160
            f.close()
1161
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1162
        result = Win32ReadDir().read_dir('', u'.')
1163
        entry = result[0]
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1164
        self.assertEqual((name0, name0, 'file'), entry[:3])
1165
        self.assertEqual(u'./' + name0u, entry[4])
1166
        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.
1167
        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.
1168
1169
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1170
        """make sure our Stat values are valid"""
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1171
        self.requireFeature(Win32ReadDirFeature)
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1172
        self.requireFeature(tests.UnicodeFilenameFeature)
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1173
        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.
1174
        name0u = u'0dir-\u062c\u0648'
1175
        name0 = name0u.encode('utf8')
1176
        self.build_tree([name0u + '/'])
1177
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
1178
        result = Win32ReadDir().read_dir('', u'.')
1179
        entry = result[0]
3504.4.2 by John Arbash Meinel
Add a test case that shows the mtime is not being returned correctly.
1180
        self.assertEqual((name0, name0, 'directory'), entry[:3])
1181
        self.assertEqual(u'./' + name0u, entry[4])
1182
        self.assertStatIsCorrect(entry[4], entry[3])
1183
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
1184
    def assertPathCompare(self, path_less, path_greater):
1185
        """check that path_less and path_greater compare correctly."""
1186
        self.assertEqual(0, osutils.compare_paths_prefix_order(
1187
            path_less, path_less))
1188
        self.assertEqual(0, osutils.compare_paths_prefix_order(
1189
            path_greater, path_greater))
1190
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
1191
            path_less, path_greater))
1192
        self.assertEqual(1, osutils.compare_paths_prefix_order(
1193
            path_greater, path_less))
1194
1195
    def test_compare_paths_prefix_order(self):
1196
        # root before all else
1197
        self.assertPathCompare("/", "/a")
1198
        # alpha within a dir
1199
        self.assertPathCompare("/a", "/b")
1200
        self.assertPathCompare("/b", "/z")
1201
        # high dirs before lower.
1202
        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.
1203
        # except if the deeper dir should be output first
1204
        self.assertPathCompare("/a/b/c", "/d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
1205
        # lexical betwen dirs of the same height
1206
        self.assertPathCompare("/a/z", "/z/z")
1207
        self.assertPathCompare("/a/c/z", "/a/d/e")
1208
1209
        # this should also be consistent for no leading / paths
1210
        # root before all else
1211
        self.assertPathCompare("", "a")
1212
        # alpha within a dir
1213
        self.assertPathCompare("a", "b")
1214
        self.assertPathCompare("b", "z")
1215
        # high dirs before lower.
1216
        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.
1217
        # except if the deeper dir should be output first
1218
        self.assertPathCompare("a/b/c", "d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
1219
        # lexical betwen dirs of the same height
1220
        self.assertPathCompare("a/z", "z/z")
1221
        self.assertPathCompare("a/c/z", "a/d/e")
1222
1773.3.3 by Robert Collins
Add new tests John Meinel asked for.
1223
    def test_path_prefix_sorting(self):
1224
        """Doing a sort on path prefix should match our sample data."""
1225
        original_paths = [
1226
            'a',
1227
            'a/b',
1228
            'a/b/c',
1229
            'b',
1230
            'b/c',
1231
            'd',
1232
            'd/e',
1233
            'd/e/f',
1234
            'd/f',
1235
            'd/g',
1236
            'g',
1237
            ]
1238
1239
        dir_sorted_paths = [
1240
            'a',
1241
            'b',
1242
            'd',
1243
            'g',
1244
            'a/b',
1245
            'a/b/c',
1246
            'b/c',
1247
            'd/e',
1248
            'd/f',
1249
            'd/g',
1250
            'd/e/f',
1251
            ]
1252
1253
        self.assertEqual(
1254
            dir_sorted_paths,
1255
            sorted(original_paths, key=osutils.path_prefix_key))
1256
        # using the comparison routine shoudl work too:
1257
        self.assertEqual(
1258
            dir_sorted_paths,
1259
            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.
1260
1261
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1262
class TestCopyTree(TestCaseInTempDir):
1263
    
1264
    def test_copy_basic_tree(self):
1265
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1266
        osutils.copy_tree('source', 'target')
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
1267
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1268
        self.assertEqual(['c'], os.listdir('target/b'))
1269
1270
    def test_copy_tree_target_exists(self):
1271
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
1272
                         'target/'])
1273
        osutils.copy_tree('source', 'target')
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
1274
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1275
        self.assertEqual(['c'], os.listdir('target/b'))
1276
1907.3.2 by John Arbash Meinel
Updated the copy_tree function to allow overriding functionality.
1277
    def test_copy_tree_symlinks(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
1278
        self.requireFeature(SymlinkFeature)
1907.3.2 by John Arbash Meinel
Updated the copy_tree function to allow overriding functionality.
1279
        self.build_tree(['source/'])
1280
        os.symlink('a/generic/path', 'source/lnk')
1281
        osutils.copy_tree('source', 'target')
1282
        self.assertEqual(['lnk'], os.listdir('target'))
1283
        self.assertEqual('a/generic/path', os.readlink('target/lnk'))
1284
1285
    def test_copy_tree_handlers(self):
1286
        processed_files = []
1287
        processed_links = []
1288
        def file_handler(from_path, to_path):
1289
            processed_files.append(('f', from_path, to_path))
1290
        def dir_handler(from_path, to_path):
1291
            processed_files.append(('d', from_path, to_path))
1292
        def link_handler(from_path, to_path):
1293
            processed_links.append((from_path, to_path))
1294
        handlers = {'file':file_handler,
1295
                    'directory':dir_handler,
1296
                    'symlink':link_handler,
1297
                   }
1298
1299
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1300
        if osutils.has_symlinks():
1301
            os.symlink('a/generic/path', 'source/lnk')
1302
        osutils.copy_tree('source', 'target', handlers=handlers)
1303
1304
        self.assertEqual([('d', 'source', 'target'),
1305
                          ('f', 'source/a', 'target/a'),
1306
                          ('d', 'source/b', 'target/b'),
1307
                          ('f', 'source/b/c', 'target/b/c'),
1308
                         ], processed_files)
1309
        self.failIfExists('target')
1310
        if osutils.has_symlinks():
1311
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1312
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
1313
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
1314
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
1315
# [bialix] 2006/12/26
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.
1316
1963.1.5 by John Arbash Meinel
Create an osutils helper function for modifying the environment
1317
1318
class TestSetUnsetEnv(TestCase):
1319
    """Test updating the environment"""
1320
1321
    def setUp(self):
1322
        super(TestSetUnsetEnv, self).setUp()
1323
1324
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
1325
                         'Environment was not cleaned up properly.'
1326
                         ' Variable BZR_TEST_ENV_VAR should not exist.')
1327
        def cleanup():
1328
            if 'BZR_TEST_ENV_VAR' in os.environ:
1329
                del os.environ['BZR_TEST_ENV_VAR']
1330
1331
        self.addCleanup(cleanup)
1332
1333
    def test_set(self):
1334
        """Test that we can set an env variable"""
1335
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1336
        self.assertEqual(None, old)
1337
        self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
1338
1339
    def test_double_set(self):
1340
        """Test that we get the old value out"""
1341
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1342
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
1343
        self.assertEqual('foo', old)
1344
        self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
1345
1346
    def test_unicode(self):
1347
        """Environment can only contain plain strings
1348
        
1349
        So Unicode strings must be encoded.
1350
        """
2785.1.5 by Alexander Belchenko
support for non-ascii BZR_HOME in show_version()
1351
        uni_val, env_val = probe_unicode_in_user_encoding()
1352
        if uni_val is None:
1963.1.5 by John Arbash Meinel
Create an osutils helper function for modifying the environment
1353
            raise TestSkipped('Cannot find a unicode character that works in'
1354
                              ' encoding %s' % (bzrlib.user_encoding,))
1355
1356
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1357
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1358
1359
    def test_unset(self):
1360
        """Test that passing None will remove the env var"""
1361
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1362
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1363
        self.assertEqual('foo', old)
1364
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1365
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1366
2215.6.2 by James Henstridge
add some simple tests for local_time_offset()
1367
1368
class TestLocalTimeOffset(TestCase):
1369
1370
    def test_local_time_offset(self):
1371
        """Test that local_time_offset() returns a sane value."""
1372
        offset = osutils.local_time_offset()
1373
        self.assertTrue(isinstance(offset, int))
2215.6.3 by James Henstridge
narrow the range that the local_time_offset() test uses
1374
        # Test that the offset is no more than a eighteen hours in
1375
        # either direction.
1376
        # Time zone handling is system specific, so it is difficult to
1377
        # do more specific tests, but a value outside of this range is
1378
        # probably wrong.
1379
        eighteen_hours = 18 * 3600
1380
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
2215.6.2 by James Henstridge
add some simple tests for local_time_offset()
1381
1382
    def test_local_time_offset_with_timestamp(self):
1383
        """Test that local_time_offset() works with a timestamp."""
1384
        offset = osutils.local_time_offset(1000000000.1234567)
1385
        self.assertTrue(isinstance(offset, int))
2215.6.3 by James Henstridge
narrow the range that the local_time_offset() test uses
1386
        eighteen_hours = 18 * 3600
1387
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
2922.1.2 by John Arbash Meinel
Add tests for sha_file_by_name.
1388
1389
1390
class TestShaFileByName(TestCaseInTempDir):
1391
1392
    def test_sha_empty(self):
1393
        self.build_tree_contents([('foo', '')])
1394
        expected_sha = osutils.sha_string('')
1395
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1396
1397
    def test_sha_mixed_endings(self):
1398
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1399
        self.build_tree_contents([('foo', text)])
1400
        expected_sha = osutils.sha_string(text)
1401
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
3089.3.9 by Ian Clatworthy
add test for resource loading
1402
1403
1404
_debug_text = \
1405
r'''# Copyright (C) 2005, 2006 Canonical Ltd
1406
#
1407
# This program is free software; you can redistribute it and/or modify
1408
# it under the terms of the GNU General Public License as published by
1409
# the Free Software Foundation; either version 2 of the License, or
1410
# (at your option) any later version.
1411
#
1412
# This program is distributed in the hope that it will be useful,
1413
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1415
# GNU General Public License for more details.
1416
#
1417
# You should have received a copy of the GNU General Public License
1418
# along with this program; if not, write to the Free Software
1419
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1420
1421
1422
# NOTE: If update these, please also update the help for global-options in
3170.1.2 by Andrew Bennetts
Update test_resource_string for new debug.py contents.
1423
#       bzrlib/help_topics/__init__.py
3089.3.9 by Ian Clatworthy
add test for resource loading
1424
1425
debug_flags = set()
1426
"""Set of flags that enable different debug behaviour.
1427
1428
These are set with eg ``-Dlock`` on the bzr command line.
1429
1430
Options include:
1431
 
1432
 * auth - show authentication sections used
1433
 * error - show stack traces for all top level exceptions
1434
 * evil - capture call sites that do expensive or badly-scaling operations.
1435
 * fetch - trace history copying between repositories
3377.3.45 by John Arbash Meinel
Update the osutils.resource test for debug strings
1436
 * graph - trace graph traversal information
3170.1.2 by Andrew Bennetts
Update test_resource_string for new debug.py contents.
1437
 * hashcache - log every time a working file is read to determine its hash
3089.3.9 by Ian Clatworthy
add test for resource loading
1438
 * hooks - trace hook execution
1439
 * hpss - trace smart protocol requests and responses
1440
 * http - trace http connections, requests and responses
1441
 * index - trace major index operations
3172.2.1 by Andrew Bennetts
Enable use of smart revision streaming between repos with compatible models, not just between identical format repos.
1442
 * knit - trace knit operations
3089.3.9 by Ian Clatworthy
add test for resource loading
1443
 * lock - trace when lockdir locks are taken or released
1444
 * merge - emit information for debugging merges
3231.4.2 by Alexander Belchenko
fix test_resource_string.
1445
 * pack - emit information about pack operations
3089.3.9 by Ian Clatworthy
add test for resource loading
1446
1447
"""
1448
'''
1449
1450
1451
class TestResourceLoading(TestCaseInTempDir):
1452
1453
    def test_resource_string(self):
1454
        # test resource in bzrlib
1455
        text = osutils.resource_string('bzrlib', 'debug.py')
1456
        self.assertEquals(_debug_text, text)
1457
        # test resource under bzrlib
1458
        text = osutils.resource_string('bzrlib.ui', 'text.py')
1459
        self.assertContainsRe(text, "class TextUIFactory")
1460
        # test unsupported package
1461
        self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1462
            'yyy.xx')
1463
        # test unknown resource
1464
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')