/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
1
# Copyright (C) 2005, 2006 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
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
19
import errno
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
20
import os
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
21
import socket
22
import stat
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
23
import sys
24
25
import bzrlib
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
26
from bzrlib import (
27
    errors,
28
    osutils,
29
    )
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
30
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
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.
31
from bzrlib.tests import (
32
        StringIOWrapper,
33
        TestCase, 
34
        TestCaseInTempDir, 
35
        TestSkipped,
36
        )
1532 by Robert Collins
Merge in John Meinels integration branch.
37
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
38
39
class TestOSUtils(TestCaseInTempDir):
40
2249.2.1 by John Arbash Meinel
(John Arbash Meinel) hard-code the whitespace chars to avoid problems in some locales.
41
    def test_contains_whitespace(self):
42
        self.failUnless(osutils.contains_whitespace(u' '))
43
        self.failUnless(osutils.contains_whitespace(u'hello there'))
44
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
45
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
46
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
47
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
48
49
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
50
        # is whitespace, but we do not.
51
        self.failIf(osutils.contains_whitespace(u''))
52
        self.failIf(osutils.contains_whitespace(u'hellothere'))
53
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
54
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
55
    def test_fancy_rename(self):
56
        # This should work everywhere
57
        def rename(a, b):
58
            osutils.fancy_rename(a, b,
59
                    rename_func=os.rename,
60
                    unlink_func=os.unlink)
61
62
        open('a', 'wb').write('something in a\n')
63
        rename('a', 'b')
64
        self.failIfExists('a')
65
        self.failUnlessExists('b')
66
        self.check_file_contents('b', 'something in a\n')
67
68
        open('a', 'wb').write('new something in a\n')
69
        rename('b', 'a')
70
71
        self.check_file_contents('a', 'something in a\n')
72
73
    def test_rename(self):
74
        # Rename should be semi-atomic on all platforms
75
        open('a', 'wb').write('something in a\n')
76
        osutils.rename('a', 'b')
77
        self.failIfExists('a')
78
        self.failUnlessExists('b')
79
        self.check_file_contents('b', 'something in a\n')
80
81
        open('a', 'wb').write('new something in a\n')
82
        osutils.rename('b', 'a')
83
84
        self.check_file_contents('a', 'something in a\n')
85
86
    # TODO: test fancy_rename using a MemoryTransport
87
1553.5.5 by Martin Pool
New utility routine rand_chars
88
    def test_01_rand_chars_empty(self):
89
        result = osutils.rand_chars(0)
90
        self.assertEqual(result, '')
91
92
    def test_02_rand_chars_100(self):
93
        result = osutils.rand_chars(100)
94
        self.assertEqual(len(result), 100)
95
        self.assertEqual(type(result), str)
96
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
97
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
98
    def test_is_inside(self):
99
        is_inside = osutils.is_inside
100
        self.assertTrue(is_inside('src', 'src/foo.c'))
101
        self.assertFalse(is_inside('src', 'srccontrol'))
102
        self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
103
        self.assertTrue(is_inside('foo.c', 'foo.c'))
104
        self.assertFalse(is_inside('foo.c', ''))
105
        self.assertTrue(is_inside('', 'foo.c'))
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
106
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
107
    def test_rmtree(self):
108
        # Check to remove tree with read-only files/dirs
109
        os.mkdir('dir')
110
        f = file('dir/file', 'w')
111
        f.write('spam')
112
        f.close()
113
        # would like to also try making the directory readonly, but at the
114
        # moment python shutil.rmtree doesn't handle that properly - it would
115
        # need to chmod the directory before removing things inside it - deferred
116
        # for now -- mbp 20060505
117
        # osutils.make_readonly('dir')
118
        osutils.make_readonly('dir/file')
119
120
        osutils.rmtree('dir')
121
122
        self.failIfExists('dir/file')
123
        self.failIfExists('dir')
124
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
125
    def test_file_kind(self):
126
        self.build_tree(['file', 'dir/'])
127
        self.assertEquals('file', osutils.file_kind('file'))
128
        self.assertEquals('directory', osutils.file_kind('dir/'))
129
        if osutils.has_symlinks():
130
            os.symlink('symlink', 'symlink')
131
            self.assertEquals('symlink', osutils.file_kind('symlink'))
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
132
        
133
        # TODO: jam 20060529 Test a block device
134
        try:
135
            os.lstat('/dev/null')
136
        except OSError, e:
137
            if e.errno not in (errno.ENOENT,):
138
                raise
139
        else:
140
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
141
142
        mkfifo = getattr(os, 'mkfifo', None)
143
        if mkfifo:
144
            mkfifo('fifo')
145
            try:
146
                self.assertEquals('fifo', osutils.file_kind('fifo'))
147
            finally:
148
                os.remove('fifo')
149
150
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
151
        if AF_UNIX:
152
            s = socket.socket(AF_UNIX)
153
            s.bind('socket')
154
            try:
155
                self.assertEquals('socket', osutils.file_kind('socket'))
156
            finally:
157
                os.remove('socket')
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
158
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
159
    def test_get_umask(self):
160
        if sys.platform == 'win32':
161
            # umask always returns '0', no way to set it
162
            self.assertEqual(0, osutils.get_umask())
163
            return
164
165
        orig_umask = osutils.get_umask()
166
        try:
167
            os.umask(0222)
168
            self.assertEqual(0222, osutils.get_umask())
169
            os.umask(0022)
170
            self.assertEqual(0022, osutils.get_umask())
171
            os.umask(0002)
172
            self.assertEqual(0002, osutils.get_umask())
173
            os.umask(0027)
174
            self.assertEqual(0027, osutils.get_umask())
175
        finally:
176
            os.umask(orig_umask)
177
1957.1.15 by John Arbash Meinel
Review feedback from Robert
178
    def assertFormatedDelta(self, expected, seconds):
179
        """Assert osutils.format_delta formats as expected"""
180
        actual = osutils.format_delta(seconds)
181
        self.assertEqual(expected, actual)
182
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
183
    def test_format_delta(self):
1957.1.15 by John Arbash Meinel
Review feedback from Robert
184
        self.assertFormatedDelta('0 seconds ago', 0)
185
        self.assertFormatedDelta('1 second ago', 1)
186
        self.assertFormatedDelta('10 seconds ago', 10)
187
        self.assertFormatedDelta('59 seconds ago', 59)
188
        self.assertFormatedDelta('89 seconds ago', 89)
189
        self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
190
        self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
191
        self.assertFormatedDelta('3 minutes, 1 second ago', 181)
192
        self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
193
        self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
194
        self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
195
        self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
196
        self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
197
        self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
198
        self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
199
        self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
200
        self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
201
        self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
202
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
203
        self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
204
        self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
205
        self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
206
        self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
207
208
        # We handle when time steps the wrong direction because computers
209
        # don't have synchronized clocks.
1957.1.15 by John Arbash Meinel
Review feedback from Robert
210
        self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
211
        self.assertFormatedDelta('1 second in the future', -1)
212
        self.assertFormatedDelta('2 seconds in the future', -2)
1957.1.4 by John Arbash Meinel
create a helper for formatting a time delta
213
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
214
    def test_dereference_path(self):
2091.3.6 by Aaron Bentley
Add symlink test guards
215
        if not osutils.has_symlinks():
216
            raise TestSkipped('Symlinks are not supported on this platform')
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
217
        cwd = osutils.realpath('.')
218
        os.mkdir('bar')
219
        bar_path = osutils.pathjoin(cwd, 'bar')
220
        # Using './' to avoid bug #1213894 (first path component not
221
        # dereferenced) in Python 2.4.1 and earlier
222
        self.assertEqual(bar_path, osutils.realpath('./bar'))
223
        os.symlink('bar', 'foo')
224
        self.assertEqual(bar_path, osutils.realpath('./foo'))
225
        
226
        # Does not dereference terminal symlinks
227
        foo_path = osutils.pathjoin(cwd, 'foo')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
228
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
229
230
        # Dereferences parent symlinks
231
        os.mkdir('bar/baz')
232
        baz_path = osutils.pathjoin(bar_path, 'baz')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
233
        self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
234
235
        # Dereferences parent symlinks that are the first path element
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
236
        self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
237
238
        # Dereferences parent symlinks in absolute paths
239
        foo_baz_path = osutils.pathjoin(foo_path, 'baz')
2091.3.7 by Aaron Bentley
Rename real_parent to dereferenced_path
240
        self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
2091.3.5 by Aaron Bentley
Move realpath functionality into osutils
241
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
242
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
243
class TestSafeUnicode(TestCase):
244
245
    def test_from_ascii_string(self):
246
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
247
1534.3.2 by Robert Collins
An extra test for John.
248
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
249
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
250
1534.3.2 by Robert Collins
An extra test for John.
251
    def test_from_unicode_string_unicode_contents(self):
252
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
253
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
254
    def test_from_utf8_string(self):
255
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
256
257
    def test_bad_utf8_string(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
258
        self.assertRaises(BzrBadParameterNotUnicode,
259
                          osutils.safe_unicode,
260
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
261
262
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
263
class TestWin32Funcs(TestCase):
264
    """Test that the _win32 versions of os utilities return appropriate paths."""
265
266
    def test_abspath(self):
267
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
268
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
269
270
    def test_realpath(self):
271
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
272
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
273
274
    def test_pathjoin(self):
275
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
276
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
277
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
278
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
279
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
280
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
281
282
    def test_normpath(self):
283
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
284
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
285
286
    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
287
        cwd = osutils._win32_getcwd()
288
        os_cwd = os.getcwdu()
289
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
290
        # win32 is inconsistent whether it returns lower or upper case
291
        # and even if it was consistent the user might type the other
292
        # so we force it to uppercase
293
        # running python.exe under cmd.exe return capital C:\\
294
        # running win32 python inside a cygwin shell returns lowercase
295
        self.assertEqual(os_cwd[0].upper(), cwd[0])
296
297
    def test_fixdrive(self):
298
        self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
299
        self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
300
        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.
301
302
303
class TestWin32FuncsDirs(TestCaseInTempDir):
304
    """Test win32 functions that create files."""
305
    
306
    def test_getcwd(self):
307
        # Make sure getcwd can handle unicode filenames
308
        try:
1830.3.9 by John Arbash Meinel
Use a directory name that doesn't get messed up on Mac for getcwd() test.
309
            os.mkdir(u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
310
        except UnicodeError:
311
            raise TestSkipped("Unable to create Unicode filename")
312
1830.3.9 by John Arbash Meinel
Use a directory name that doesn't get messed up on Mac for getcwd() test.
313
        os.chdir(u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
314
        # TODO: jam 20060427 This will probably fail on Mac OSX because
315
        #       it will change the normalization of B\xe5gfors
316
        #       Consider using a different unicode character, or make
317
        #       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.
318
        self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
319
320
    def test_mkdtemp(self):
321
        tmpdir = osutils._win32_mkdtemp(dir='.')
322
        self.assertFalse('\\' in tmpdir)
323
324
    def test_rename(self):
325
        a = open('a', 'wb')
326
        a.write('foo\n')
327
        a.close()
328
        b = open('b', 'wb')
329
        b.write('baz\n')
330
        b.close()
331
332
        osutils._win32_rename('b', 'a')
333
        self.failUnlessExists('a')
334
        self.failIfExists('b')
335
        self.assertFileEqual('baz\n', 'a')
336
1711.7.6 by John Arbash Meinel
Change _win32_rename() so that it raises ENOENT *before* it tries any renaming.
337
    def test_rename_missing_file(self):
338
        a = open('a', 'wb')
339
        a.write('foo\n')
340
        a.close()
341
342
        try:
343
            osutils._win32_rename('b', 'a')
344
        except (IOError, OSError), e:
345
            self.assertEqual(errno.ENOENT, e.errno)
346
        self.assertFileEqual('foo\n', 'a')
347
348
    def test_rename_missing_dir(self):
349
        os.mkdir('a')
350
        try:
351
            osutils._win32_rename('b', 'a')
352
        except (IOError, OSError), e:
353
            self.assertEqual(errno.ENOENT, e.errno)
354
355
    def test_rename_current_dir(self):
356
        os.mkdir('a')
357
        os.chdir('a')
358
        # You can't rename the working directory
359
        # doing rename non-existant . usually
360
        # just raises ENOENT, since non-existant
361
        # doesn't exist.
362
        try:
363
            osutils._win32_rename('b', '.')
364
        except (IOError, OSError), e:
365
            self.assertEqual(errno.ENOENT, e.errno)
366
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
367
    def test_splitpath(self):
368
        def check(expected, path):
369
            self.assertEqual(expected, osutils.splitpath(path))
370
371
        check(['a'], 'a')
372
        check(['a', 'b'], 'a/b')
373
        check(['a', 'b'], 'a/./b')
374
        check(['a', '.b'], 'a/.b')
375
        check(['a', '.b'], 'a\\.b')
376
377
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
378
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
379
1830.3.11 by John Arbash Meinel
Create a mac version of 'getcwd()' which normalizes the path.
380
class TestMacFuncsDirs(TestCaseInTempDir):
381
    """Test mac special functions that require directories."""
382
383
    def test_getcwd(self):
384
        # On Mac, this will actually create Ba\u030agfors
385
        # but chdir will still work, because it accepts both paths
386
        try:
387
            os.mkdir(u'B\xe5gfors')
388
        except UnicodeError:
389
            raise TestSkipped("Unable to create Unicode filename")
390
391
        os.chdir(u'B\xe5gfors')
392
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
393
394
    def test_getcwd_nonnorm(self):
395
        # Test that _mac_getcwd() will normalize this path
396
        try:
397
            os.mkdir(u'Ba\u030agfors')
398
        except UnicodeError:
399
            raise TestSkipped("Unable to create Unicode filename")
400
401
        os.chdir(u'Ba\u030agfors')
402
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
403
1996.3.14 by John Arbash Meinel
lazy_import osutils and sign_my_commits
404
1666.1.6 by Robert Collins
Make knit the default format.
405
class TestSplitLines(TestCase):
406
407
    def test_split_unicode(self):
408
        self.assertEqual([u'foo\n', u'bar\xae'],
409
                         osutils.split_lines(u'foo\nbar\xae'))
410
        self.assertEqual([u'foo\n', u'bar\xae\n'],
411
                         osutils.split_lines(u'foo\nbar\xae\n'))
412
413
    def test_split_with_carriage_returns(self):
414
        self.assertEqual(['foo\rbar\n'],
415
                         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.
416
417
418
class TestWalkDirs(TestCaseInTempDir):
419
420
    def test_walkdirs(self):
421
        tree = [
422
            '.bzr',
423
            '0file',
424
            '1dir/',
425
            '1dir/0file',
426
            '1dir/1dir/',
427
            '2file'
428
            ]
429
        self.build_tree(tree)
430
        expected_dirblocks = [
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
431
                (('', '.'),
432
                 [('0file', '0file', 'file'),
433
                  ('1dir', '1dir', 'directory'),
434
                  ('2file', '2file', 'file'),
435
                 ]
436
                ),
437
                (('1dir', './1dir'),
438
                 [('1dir/0file', '0file', 'file'),
439
                  ('1dir/1dir', '1dir', 'directory'),
440
                 ]
441
                ),
442
                (('1dir/1dir', './1dir/1dir'),
443
                 [
444
                 ]
445
                ),
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
446
            ]
447
        result = []
448
        found_bzrdir = False
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
449
        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.
450
            if len(dirblock) and dirblock[0][1] == '.bzr':
451
                # this tests the filtering of selected paths
452
                found_bzrdir = True
453
                del dirblock[0]
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
454
            result.append((dirdetail, dirblock))
1753.1.1 by Robert Collins
(rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.
455
456
        self.assertTrue(found_bzrdir)
457
        self.assertEqual(expected_dirblocks,
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
458
            [(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.
459
        # you can search a subdir only, with a supplied prefix.
460
        result = []
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
461
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
462
            result.append(dirblock)
463
        self.assertEqual(expected_dirblocks[1:],
1897.1.1 by Robert Collins
Add some useful summary data to osutils.walkdirs output.
464
            [(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.
465
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
466
    def assertPathCompare(self, path_less, path_greater):
467
        """check that path_less and path_greater compare correctly."""
468
        self.assertEqual(0, osutils.compare_paths_prefix_order(
469
            path_less, path_less))
470
        self.assertEqual(0, osutils.compare_paths_prefix_order(
471
            path_greater, path_greater))
472
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
473
            path_less, path_greater))
474
        self.assertEqual(1, osutils.compare_paths_prefix_order(
475
            path_greater, path_less))
476
477
    def test_compare_paths_prefix_order(self):
478
        # root before all else
479
        self.assertPathCompare("/", "/a")
480
        # alpha within a dir
481
        self.assertPathCompare("/a", "/b")
482
        self.assertPathCompare("/b", "/z")
483
        # high dirs before lower.
484
        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.
485
        # except if the deeper dir should be output first
486
        self.assertPathCompare("/a/b/c", "/d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
487
        # lexical betwen dirs of the same height
488
        self.assertPathCompare("/a/z", "/z/z")
489
        self.assertPathCompare("/a/c/z", "/a/d/e")
490
491
        # this should also be consistent for no leading / paths
492
        # root before all else
493
        self.assertPathCompare("", "a")
494
        # alpha within a dir
495
        self.assertPathCompare("a", "b")
496
        self.assertPathCompare("b", "z")
497
        # high dirs before lower.
498
        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.
499
        # except if the deeper dir should be output first
500
        self.assertPathCompare("a/b/c", "d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
501
        # lexical betwen dirs of the same height
502
        self.assertPathCompare("a/z", "z/z")
503
        self.assertPathCompare("a/c/z", "a/d/e")
504
1773.3.3 by Robert Collins
Add new tests John Meinel asked for.
505
    def test_path_prefix_sorting(self):
506
        """Doing a sort on path prefix should match our sample data."""
507
        original_paths = [
508
            'a',
509
            'a/b',
510
            'a/b/c',
511
            'b',
512
            'b/c',
513
            'd',
514
            'd/e',
515
            'd/e/f',
516
            'd/f',
517
            'd/g',
518
            'g',
519
            ]
520
521
        dir_sorted_paths = [
522
            'a',
523
            'b',
524
            'd',
525
            'g',
526
            'a/b',
527
            'a/b/c',
528
            'b/c',
529
            'd/e',
530
            'd/f',
531
            'd/g',
532
            'd/e/f',
533
            ]
534
535
        self.assertEqual(
536
            dir_sorted_paths,
537
            sorted(original_paths, key=osutils.path_prefix_key))
538
        # using the comparison routine shoudl work too:
539
        self.assertEqual(
540
            dir_sorted_paths,
541
            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.
542
543
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
544
class TestCopyTree(TestCaseInTempDir):
545
    
546
    def test_copy_basic_tree(self):
547
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
548
        osutils.copy_tree('source', 'target')
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
549
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
550
        self.assertEqual(['c'], os.listdir('target/b'))
551
552
    def test_copy_tree_target_exists(self):
553
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
554
                         'target/'])
555
        osutils.copy_tree('source', 'target')
2095.3.1 by Martin Pool
Tests shouldn't assume os.listdir returns sorted results
556
        self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
557
        self.assertEqual(['c'], os.listdir('target/b'))
558
1907.3.2 by John Arbash Meinel
Updated the copy_tree function to allow overriding functionality.
559
    def test_copy_tree_symlinks(self):
560
        if not osutils.has_symlinks():
561
            return
562
        self.build_tree(['source/'])
563
        os.symlink('a/generic/path', 'source/lnk')
564
        osutils.copy_tree('source', 'target')
565
        self.assertEqual(['lnk'], os.listdir('target'))
566
        self.assertEqual('a/generic/path', os.readlink('target/lnk'))
567
568
    def test_copy_tree_handlers(self):
569
        processed_files = []
570
        processed_links = []
571
        def file_handler(from_path, to_path):
572
            processed_files.append(('f', from_path, to_path))
573
        def dir_handler(from_path, to_path):
574
            processed_files.append(('d', from_path, to_path))
575
        def link_handler(from_path, to_path):
576
            processed_links.append((from_path, to_path))
577
        handlers = {'file':file_handler,
578
                    'directory':dir_handler,
579
                    'symlink':link_handler,
580
                   }
581
582
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
583
        if osutils.has_symlinks():
584
            os.symlink('a/generic/path', 'source/lnk')
585
        osutils.copy_tree('source', 'target', handlers=handlers)
586
587
        self.assertEqual([('d', 'source', 'target'),
588
                          ('f', 'source/a', 'target/a'),
589
                          ('d', 'source/b', 'target/b'),
590
                          ('f', 'source/b/c', 'target/b/c'),
591
                         ], processed_files)
592
        self.failIfExists('target')
593
        if osutils.has_symlinks():
594
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
595
1907.3.1 by John Arbash Meinel
create a copy_tree wrapper around walkdirs()
596
2192.1.2 by Alexander Belchenko
Tests for osutils.get_terminal_encoding()
597
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
598
# [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.
599
1963.1.5 by John Arbash Meinel
Create an osutils helper function for modifying the environment
600
601
class TestSetUnsetEnv(TestCase):
602
    """Test updating the environment"""
603
604
    def setUp(self):
605
        super(TestSetUnsetEnv, self).setUp()
606
607
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
608
                         'Environment was not cleaned up properly.'
609
                         ' Variable BZR_TEST_ENV_VAR should not exist.')
610
        def cleanup():
611
            if 'BZR_TEST_ENV_VAR' in os.environ:
612
                del os.environ['BZR_TEST_ENV_VAR']
613
614
        self.addCleanup(cleanup)
615
616
    def test_set(self):
617
        """Test that we can set an env variable"""
618
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
619
        self.assertEqual(None, old)
620
        self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
621
622
    def test_double_set(self):
623
        """Test that we get the old value out"""
624
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
625
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
626
        self.assertEqual('foo', old)
627
        self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
628
629
    def test_unicode(self):
630
        """Environment can only contain plain strings
631
        
632
        So Unicode strings must be encoded.
633
        """
634
        # Try a few different characters, to see if we can get
635
        # one that will be valid in the user_encoding
636
        possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
637
        for uni_val in possible_vals:
638
            try:
639
                env_val = uni_val.encode(bzrlib.user_encoding)
640
            except UnicodeEncodeError:
641
                # Try a different character
642
                pass
643
            else:
644
                break
645
        else:
646
            raise TestSkipped('Cannot find a unicode character that works in'
647
                              ' encoding %s' % (bzrlib.user_encoding,))
648
649
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
650
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
651
652
    def test_unset(self):
653
        """Test that passing None will remove the env var"""
654
        osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
655
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
656
        self.assertEqual('foo', old)
657
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
658
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
659
2215.6.2 by James Henstridge
add some simple tests for local_time_offset()
660
661
class TestLocalTimeOffset(TestCase):
662
663
    def test_local_time_offset(self):
664
        """Test that local_time_offset() returns a sane value."""
665
        offset = osutils.local_time_offset()
666
        self.assertTrue(isinstance(offset, int))
2215.6.3 by James Henstridge
narrow the range that the local_time_offset() test uses
667
        # Test that the offset is no more than a eighteen hours in
668
        # either direction.
669
        # Time zone handling is system specific, so it is difficult to
670
        # do more specific tests, but a value outside of this range is
671
        # probably wrong.
672
        eighteen_hours = 18 * 3600
673
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
2215.6.2 by James Henstridge
add some simple tests for local_time_offset()
674
675
    def test_local_time_offset_with_timestamp(self):
676
        """Test that local_time_offset() works with a timestamp."""
677
        offset = osutils.local_time_offset(1000000000.1234567)
678
        self.assertTrue(isinstance(offset, int))
2215.6.3 by James Henstridge
narrow the range that the local_time_offset() test uses
679
        eighteen_hours = 18 * 3600
680
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)