~bzr-pqm/bzr/bzr.dev

1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
1
# Copyright (C) 2005 by Canonical Ltd
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
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
26
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
1532 by Robert Collins
Merge in John Meinels integration branch.
27
import bzrlib.osutils as osutils
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.
28
from bzrlib.tests import (
29
        StringIOWrapper,
30
        TestCase, 
31
        TestCaseInTempDir, 
32
        TestSkipped,
33
        )
1532 by Robert Collins
Merge in John Meinels integration branch.
34
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
35
36
class TestOSUtils(TestCaseInTempDir):
37
38
    def test_fancy_rename(self):
39
        # This should work everywhere
40
        def rename(a, b):
41
            osutils.fancy_rename(a, b,
42
                    rename_func=os.rename,
43
                    unlink_func=os.unlink)
44
45
        open('a', 'wb').write('something in a\n')
46
        rename('a', 'b')
47
        self.failIfExists('a')
48
        self.failUnlessExists('b')
49
        self.check_file_contents('b', 'something in a\n')
50
51
        open('a', 'wb').write('new something in a\n')
52
        rename('b', 'a')
53
54
        self.check_file_contents('a', 'something in a\n')
55
56
    def test_rename(self):
57
        # Rename should be semi-atomic on all platforms
58
        open('a', 'wb').write('something in a\n')
59
        osutils.rename('a', 'b')
60
        self.failIfExists('a')
61
        self.failUnlessExists('b')
62
        self.check_file_contents('b', 'something in a\n')
63
64
        open('a', 'wb').write('new something in a\n')
65
        osutils.rename('b', 'a')
66
67
        self.check_file_contents('a', 'something in a\n')
68
69
    # TODO: test fancy_rename using a MemoryTransport
70
1553.5.5 by Martin Pool
New utility routine rand_chars
71
    def test_01_rand_chars_empty(self):
72
        result = osutils.rand_chars(0)
73
        self.assertEqual(result, '')
74
75
    def test_02_rand_chars_100(self):
76
        result = osutils.rand_chars(100)
77
        self.assertEqual(len(result), 100)
78
        self.assertEqual(type(result), str)
79
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
80
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
81
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
82
    def test_rmtree(self):
83
        # Check to remove tree with read-only files/dirs
84
        os.mkdir('dir')
85
        f = file('dir/file', 'w')
86
        f.write('spam')
87
        f.close()
88
        # would like to also try making the directory readonly, but at the
89
        # moment python shutil.rmtree doesn't handle that properly - it would
90
        # need to chmod the directory before removing things inside it - deferred
91
        # for now -- mbp 20060505
92
        # osutils.make_readonly('dir')
93
        osutils.make_readonly('dir/file')
94
95
        osutils.rmtree('dir')
96
97
        self.failIfExists('dir/file')
98
        self.failIfExists('dir')
99
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
100
    def test_file_kind(self):
101
        self.build_tree(['file', 'dir/'])
102
        self.assertEquals('file', osutils.file_kind('file'))
103
        self.assertEquals('directory', osutils.file_kind('dir/'))
104
        if osutils.has_symlinks():
105
            os.symlink('symlink', 'symlink')
106
            self.assertEquals('symlink', osutils.file_kind('symlink'))
1732.1.28 by John Arbash Meinel
Add tests for fancy file types.
107
        
108
        # TODO: jam 20060529 Test a block device
109
        try:
110
            os.lstat('/dev/null')
111
        except OSError, e:
112
            if e.errno not in (errno.ENOENT,):
113
                raise
114
        else:
115
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
116
117
        mkfifo = getattr(os, 'mkfifo', None)
118
        if mkfifo:
119
            mkfifo('fifo')
120
            try:
121
                self.assertEquals('fifo', osutils.file_kind('fifo'))
122
            finally:
123
                os.remove('fifo')
124
125
        AF_UNIX = getattr(socket, 'AF_UNIX', None)
126
        if AF_UNIX:
127
            s = socket.socket(AF_UNIX)
128
            s.bind('socket')
129
            try:
130
                self.assertEquals('socket', osutils.file_kind('socket'))
131
            finally:
132
                os.remove('socket')
1732.1.10 by John Arbash Meinel
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup
133
1692.7.6 by Martin Pool
[patch] force deletion of trees containing readonly files (alexander)
134
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
135
class TestSafeUnicode(TestCase):
136
137
    def test_from_ascii_string(self):
138
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
139
1534.3.2 by Robert Collins
An extra test for John.
140
    def test_from_unicode_string_ascii_contents(self):
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
141
        self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
142
1534.3.2 by Robert Collins
An extra test for John.
143
    def test_from_unicode_string_unicode_contents(self):
144
        self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
145
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
146
    def test_from_utf8_string(self):
147
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
148
149
    def test_bad_utf8_string(self):
1185.65.29 by Robert Collins
Implement final review suggestions.
150
        self.assertRaises(BzrBadParameterNotUnicode,
151
                          osutils.safe_unicode,
152
                          '\xbb\xbb')
1666.1.6 by Robert Collins
Make knit the default format.
153
154
1685.1.31 by John Arbash Meinel
Adding tests for the rest of the _win32 functions.
155
class TestWin32Funcs(TestCase):
156
    """Test that the _win32 versions of os utilities return appropriate paths."""
157
158
    def test_abspath(self):
159
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
160
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
161
162
    def test_realpath(self):
163
        self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
164
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
165
166
    def test_pathjoin(self):
167
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
168
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
169
        self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
170
        self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
171
        self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
172
        self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
173
174
    def test_normpath(self):
175
        self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
176
        self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
177
178
    def test_getcwd(self):
179
        self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
180
181
182
class TestWin32FuncsDirs(TestCaseInTempDir):
183
    """Test win32 functions that create files."""
184
    
185
    def test_getcwd(self):
186
        # Make sure getcwd can handle unicode filenames
187
        try:
188
            os.mkdir(u'B\xe5gfors')
189
        except UnicodeError:
190
            raise TestSkipped("Unable to create Unicode filename")
191
192
        os.chdir(u'B\xe5gfors')
193
        # TODO: jam 20060427 This will probably fail on Mac OSX because
194
        #       it will change the normalization of B\xe5gfors
195
        #       Consider using a different unicode character, or make
196
        #       osutils.getcwd() renormalize the path.
197
        self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
198
199
    def test_mkdtemp(self):
200
        tmpdir = osutils._win32_mkdtemp(dir='.')
201
        self.assertFalse('\\' in tmpdir)
202
203
    def test_rename(self):
204
        a = open('a', 'wb')
205
        a.write('foo\n')
206
        a.close()
207
        b = open('b', 'wb')
208
        b.write('baz\n')
209
        b.close()
210
211
        osutils._win32_rename('b', 'a')
212
        self.failUnlessExists('a')
213
        self.failIfExists('b')
214
        self.assertFileEqual('baz\n', 'a')
215
216
1666.1.6 by Robert Collins
Make knit the default format.
217
class TestSplitLines(TestCase):
218
219
    def test_split_unicode(self):
220
        self.assertEqual([u'foo\n', u'bar\xae'],
221
                         osutils.split_lines(u'foo\nbar\xae'))
222
        self.assertEqual([u'foo\n', u'bar\xae\n'],
223
                         osutils.split_lines(u'foo\nbar\xae\n'))
224
225
    def test_split_with_carriage_returns(self):
226
        self.assertEqual(['foo\rbar\n'],
227
                         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.
228
229
230
class TestWalkDirs(TestCaseInTempDir):
231
232
    def test_walkdirs(self):
233
        tree = [
234
            '.bzr',
235
            '0file',
236
            '1dir/',
237
            '1dir/0file',
238
            '1dir/1dir/',
239
            '2file'
240
            ]
241
        self.build_tree(tree)
242
        expected_dirblocks = [
243
                [
244
                    ('0file', '0file', 'file'),
245
                    ('1dir', '1dir', 'directory'),
246
                    ('2file', '2file', 'file'),
247
                ],
248
                [
249
                    ('1dir/0file', '0file', 'file'),
250
                    ('1dir/1dir', '1dir', 'directory'),
251
                ],
252
                [
253
                ],
254
            ]
255
        result = []
256
        found_bzrdir = False
257
        for dirblock in osutils.walkdirs('.'):
258
            if len(dirblock) and dirblock[0][1] == '.bzr':
259
                # this tests the filtering of selected paths
260
                found_bzrdir = True
261
                del dirblock[0]
262
            result.append(dirblock)
263
264
        self.assertTrue(found_bzrdir)
265
        self.assertEqual(expected_dirblocks,
266
            [[line[0:3] for line in block] for block in result])
1757.2.8 by Robert Collins
Teach walkdirs to walk a subdir of a tree.
267
        # you can search a subdir only, with a supplied prefix.
268
        result = []
269
        for dirblock in osutils.walkdirs('1dir', '1dir'):
270
            result.append(dirblock)
271
        self.assertEqual(expected_dirblocks[1:],
272
            [[line[0:3] for line in block] for block in result])
273
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
274
    def assertPathCompare(self, path_less, path_greater):
275
        """check that path_less and path_greater compare correctly."""
276
        self.assertEqual(0, osutils.compare_paths_prefix_order(
277
            path_less, path_less))
278
        self.assertEqual(0, osutils.compare_paths_prefix_order(
279
            path_greater, path_greater))
280
        self.assertEqual(-1, osutils.compare_paths_prefix_order(
281
            path_less, path_greater))
282
        self.assertEqual(1, osutils.compare_paths_prefix_order(
283
            path_greater, path_less))
284
285
    def test_compare_paths_prefix_order(self):
286
        # root before all else
287
        self.assertPathCompare("/", "/a")
288
        # alpha within a dir
289
        self.assertPathCompare("/a", "/b")
290
        self.assertPathCompare("/b", "/z")
291
        # high dirs before lower.
292
        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.
293
        # except if the deeper dir should be output first
294
        self.assertPathCompare("/a/b/c", "/d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
295
        # lexical betwen dirs of the same height
296
        self.assertPathCompare("/a/z", "/z/z")
297
        self.assertPathCompare("/a/c/z", "/a/d/e")
298
299
        # this should also be consistent for no leading / paths
300
        # root before all else
301
        self.assertPathCompare("", "a")
302
        # alpha within a dir
303
        self.assertPathCompare("a", "b")
304
        self.assertPathCompare("b", "z")
305
        # high dirs before lower.
306
        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.
307
        # except if the deeper dir should be output first
308
        self.assertPathCompare("a/b/c", "d/g")
1773.3.1 by Robert Collins
Add path_prefix_key and compare_paths_prefix_order utility functions.
309
        # lexical betwen dirs of the same height
310
        self.assertPathCompare("a/z", "z/z")
311
        self.assertPathCompare("a/c/z", "a/d/e")
312
1773.3.3 by Robert Collins
Add new tests John Meinel asked for.
313
    def test_path_prefix_sorting(self):
314
        """Doing a sort on path prefix should match our sample data."""
315
        original_paths = [
316
            'a',
317
            'a/b',
318
            'a/b/c',
319
            'b',
320
            'b/c',
321
            'd',
322
            'd/e',
323
            'd/e/f',
324
            'd/f',
325
            'd/g',
326
            'g',
327
            ]
328
329
        dir_sorted_paths = [
330
            'a',
331
            'b',
332
            'd',
333
            'g',
334
            'a/b',
335
            'a/b/c',
336
            'b/c',
337
            'd/e',
338
            'd/f',
339
            'd/g',
340
            'd/e/f',
341
            ]
342
343
        self.assertEqual(
344
            dir_sorted_paths,
345
            sorted(original_paths, key=osutils.path_prefix_key))
346
        # using the comparison routine shoudl work too:
347
        self.assertEqual(
348
            dir_sorted_paths,
349
            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.
350
351
352
class TestTerminalEncoding(TestCase):
353
    """Test the auto-detection of proper terminal encoding."""
354
355
    def setUp(self):
356
        self._stdout = sys.stdout
357
        self._stderr = sys.stderr
358
        self._stdin = sys.stdin
359
        self._user_encoding = bzrlib.user_encoding
360
361
        self.addCleanup(self._reset)
362
363
        sys.stdout = StringIOWrapper()
364
        sys.stdout.encoding = 'stdout_encoding'
365
        sys.stderr = StringIOWrapper()
366
        sys.stderr.encoding = 'stderr_encoding'
367
        sys.stdin = StringIOWrapper()
368
        sys.stdin.encoding = 'stdin_encoding'
369
        bzrlib.user_encoding = 'user_encoding'
370
371
    def _reset(self):
372
        sys.stdout = self._stdout
373
        sys.stderr = self._stderr
374
        sys.stdin = self._stdin
375
        bzrlib.user_encoding = self._user_encoding
376
377
    def test_get_terminal_encoding(self):
378
        # first preference is stdout encoding
379
        self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
380
381
        sys.stdout.encoding = None
382
        # if sys.stdout is None, fall back to sys.stdin
383
        self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
384
385
        sys.stdin.encoding = None
386
        # and in the worst case, use bzrlib.user_encoding
387
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
388