bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
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):  | 
|
| 
1711.5.2
by John Arbash Meinel
 win32 likes to return lowercase drive letters sometimes, and uppercase at other times. normalize this  | 
179  | 
cwd = osutils._win32_getcwd()  | 
180  | 
os_cwd = os.getcwdu()  | 
|
181  | 
self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])  | 
|
182  | 
        # win32 is inconsistent whether it returns lower or upper case
 | 
|
183  | 
        # and even if it was consistent the user might type the other
 | 
|
184  | 
        # so we force it to uppercase
 | 
|
185  | 
        # running python.exe under cmd.exe return capital C:\\
 | 
|
186  | 
        # running win32 python inside a cygwin shell returns lowercase
 | 
|
187  | 
self.assertEqual(os_cwd[0].upper(), cwd[0])  | 
|
188  | 
||
189  | 
def test_fixdrive(self):  | 
|
190  | 
self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))  | 
|
191  | 
self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))  | 
|
192  | 
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.  | 
193  | 
|
194  | 
||
195  | 
class TestWin32FuncsDirs(TestCaseInTempDir):  | 
|
196  | 
"""Test win32 functions that create files."""  | 
|
197  | 
||
198  | 
def test_getcwd(self):  | 
|
199  | 
        # Make sure getcwd can handle unicode filenames
 | 
|
200  | 
try:  | 
|
| 
1830.3.9
by John Arbash Meinel
 Use a directory name that doesn't get messed up on Mac for getcwd() test.  | 
201  | 
os.mkdir(u'mu-\xb5')  | 
| 
1685.1.31
by John Arbash Meinel
 Adding tests for the rest of the _win32 functions.  | 
202  | 
except UnicodeError:  | 
203  | 
raise TestSkipped("Unable to create Unicode filename")  | 
|
204  | 
||
| 
1830.3.9
by John Arbash Meinel
 Use a directory name that doesn't get messed up on Mac for getcwd() test.  | 
205  | 
os.chdir(u'mu-\xb5')  | 
| 
1685.1.31
by John Arbash Meinel
 Adding tests for the rest of the _win32 functions.  | 
206  | 
        # TODO: jam 20060427 This will probably fail on Mac OSX because
 | 
207  | 
        #       it will change the normalization of B\xe5gfors
 | 
|
208  | 
        #       Consider using a different unicode character, or make
 | 
|
209  | 
        #       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.  | 
210  | 
self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')  | 
| 
1685.1.31
by John Arbash Meinel
 Adding tests for the rest of the _win32 functions.  | 
211  | 
|
212  | 
def test_mkdtemp(self):  | 
|
213  | 
tmpdir = osutils._win32_mkdtemp(dir='.')  | 
|
214  | 
self.assertFalse('\\' in tmpdir)  | 
|
215  | 
||
216  | 
def test_rename(self):  | 
|
217  | 
a = open('a', 'wb')  | 
|
218  | 
a.write('foo\n')  | 
|
219  | 
a.close()  | 
|
220  | 
b = open('b', 'wb')  | 
|
221  | 
b.write('baz\n')  | 
|
222  | 
b.close()  | 
|
223  | 
||
224  | 
osutils._win32_rename('b', 'a')  | 
|
225  | 
self.failUnlessExists('a')  | 
|
226  | 
self.failIfExists('b')  | 
|
227  | 
self.assertFileEqual('baz\n', 'a')  | 
|
228  | 
||
| 
1711.7.6
by John Arbash Meinel
 Change _win32_rename() so that it raises ENOENT *before* it tries any renaming.  | 
229  | 
def test_rename_missing_file(self):  | 
230  | 
a = open('a', 'wb')  | 
|
231  | 
a.write('foo\n')  | 
|
232  | 
a.close()  | 
|
233  | 
||
234  | 
try:  | 
|
235  | 
osutils._win32_rename('b', 'a')  | 
|
236  | 
except (IOError, OSError), e:  | 
|
237  | 
self.assertEqual(errno.ENOENT, e.errno)  | 
|
238  | 
self.assertFileEqual('foo\n', 'a')  | 
|
239  | 
||
240  | 
def test_rename_missing_dir(self):  | 
|
241  | 
os.mkdir('a')  | 
|
242  | 
try:  | 
|
243  | 
osutils._win32_rename('b', 'a')  | 
|
244  | 
except (IOError, OSError), e:  | 
|
245  | 
self.assertEqual(errno.ENOENT, e.errno)  | 
|
246  | 
||
247  | 
def test_rename_current_dir(self):  | 
|
248  | 
os.mkdir('a')  | 
|
249  | 
os.chdir('a')  | 
|
250  | 
        # You can't rename the working directory
 | 
|
251  | 
        # doing rename non-existant . usually
 | 
|
252  | 
        # just raises ENOENT, since non-existant
 | 
|
253  | 
        # doesn't exist.
 | 
|
254  | 
try:  | 
|
255  | 
osutils._win32_rename('b', '.')  | 
|
256  | 
except (IOError, OSError), e:  | 
|
257  | 
self.assertEqual(errno.ENOENT, e.errno)  | 
|
258  | 
||
| 
1685.1.31
by John Arbash Meinel
 Adding tests for the rest of the _win32 functions.  | 
259  | 
|
| 
1830.3.11
by John Arbash Meinel
 Create a mac version of 'getcwd()' which normalizes the path.  | 
260  | 
class TestMacFuncsDirs(TestCaseInTempDir):  | 
261  | 
"""Test mac special functions that require directories."""  | 
|
262  | 
||
263  | 
def test_getcwd(self):  | 
|
264  | 
        # On Mac, this will actually create Ba\u030agfors
 | 
|
265  | 
        # but chdir will still work, because it accepts both paths
 | 
|
266  | 
try:  | 
|
267  | 
os.mkdir(u'B\xe5gfors')  | 
|
268  | 
except UnicodeError:  | 
|
269  | 
raise TestSkipped("Unable to create Unicode filename")  | 
|
270  | 
||
271  | 
os.chdir(u'B\xe5gfors')  | 
|
272  | 
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')  | 
|
273  | 
||
274  | 
def test_getcwd_nonnorm(self):  | 
|
275  | 
        # Test that _mac_getcwd() will normalize this path
 | 
|
276  | 
try:  | 
|
277  | 
os.mkdir(u'Ba\u030agfors')  | 
|
278  | 
except UnicodeError:  | 
|
279  | 
raise TestSkipped("Unable to create Unicode filename")  | 
|
280  | 
||
281  | 
os.chdir(u'Ba\u030agfors')  | 
|
282  | 
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')  | 
|
283  | 
||
| 
1666.1.6
by Robert Collins
 Make knit the default format.  | 
284  | 
class TestSplitLines(TestCase):  | 
285  | 
||
286  | 
def test_split_unicode(self):  | 
|
287  | 
self.assertEqual([u'foo\n', u'bar\xae'],  | 
|
288  | 
osutils.split_lines(u'foo\nbar\xae'))  | 
|
289  | 
self.assertEqual([u'foo\n', u'bar\xae\n'],  | 
|
290  | 
osutils.split_lines(u'foo\nbar\xae\n'))  | 
|
291  | 
||
292  | 
def test_split_with_carriage_returns(self):  | 
|
293  | 
self.assertEqual(['foo\rbar\n'],  | 
|
294  | 
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.  | 
295  | 
|
296  | 
||
297  | 
class TestWalkDirs(TestCaseInTempDir):  | 
|
298  | 
||
299  | 
def test_walkdirs(self):  | 
|
300  | 
tree = [  | 
|
301  | 
'.bzr',  | 
|
302  | 
'0file',  | 
|
303  | 
'1dir/',  | 
|
304  | 
'1dir/0file',  | 
|
305  | 
'1dir/1dir/',  | 
|
306  | 
            '2file'
 | 
|
307  | 
            ]
 | 
|
308  | 
self.build_tree(tree)  | 
|
309  | 
expected_dirblocks = [  | 
|
| 
1897.1.1
by Robert Collins
 Add some useful summary data to osutils.walkdirs output.  | 
310  | 
(('', '.'),  | 
311  | 
[('0file', '0file', 'file'),  | 
|
312  | 
('1dir', '1dir', 'directory'),  | 
|
313  | 
('2file', '2file', 'file'),  | 
|
314  | 
                 ]
 | 
|
315  | 
                ),
 | 
|
316  | 
(('1dir', './1dir'),  | 
|
317  | 
[('1dir/0file', '0file', 'file'),  | 
|
318  | 
('1dir/1dir', '1dir', 'directory'),  | 
|
319  | 
                 ]
 | 
|
320  | 
                ),
 | 
|
321  | 
(('1dir/1dir', './1dir/1dir'),  | 
|
322  | 
                 [
 | 
|
323  | 
                 ]
 | 
|
324  | 
                ),
 | 
|
| 
1753.1.1
by Robert Collins
 (rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.  | 
325  | 
            ]
 | 
326  | 
result = []  | 
|
327  | 
found_bzrdir = False  | 
|
| 
1897.1.1
by Robert Collins
 Add some useful summary data to osutils.walkdirs output.  | 
328  | 
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.  | 
329  | 
if len(dirblock) and dirblock[0][1] == '.bzr':  | 
330  | 
                # this tests the filtering of selected paths
 | 
|
331  | 
found_bzrdir = True  | 
|
332  | 
del dirblock[0]  | 
|
| 
1897.1.1
by Robert Collins
 Add some useful summary data to osutils.walkdirs output.  | 
333  | 
result.append((dirdetail, dirblock))  | 
| 
1753.1.1
by Robert Collins
 (rbc, jam, mbp)Add bzrlib.osutils.walkdirs, an optimised walk-and-stat routine.  | 
334  | 
|
335  | 
self.assertTrue(found_bzrdir)  | 
|
336  | 
self.assertEqual(expected_dirblocks,  | 
|
| 
1897.1.1
by Robert Collins
 Add some useful summary data to osutils.walkdirs output.  | 
337  | 
[(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.  | 
338  | 
        # you can search a subdir only, with a supplied prefix.
 | 
339  | 
result = []  | 
|
| 
1897.1.1
by Robert Collins
 Add some useful summary data to osutils.walkdirs output.  | 
340  | 
for dirblock in osutils.walkdirs('./1dir', '1dir'):  | 
| 
1757.2.8
by Robert Collins
 Teach walkdirs to walk a subdir of a tree.  | 
341  | 
result.append(dirblock)  | 
342  | 
self.assertEqual(expected_dirblocks[1:],  | 
|
| 
1897.1.1
by Robert Collins
 Add some useful summary data to osutils.walkdirs output.  | 
343  | 
[(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.  | 
344  | 
|
| 
1773.3.1
by Robert Collins
 Add path_prefix_key and compare_paths_prefix_order utility functions.  | 
345  | 
def assertPathCompare(self, path_less, path_greater):  | 
346  | 
"""check that path_less and path_greater compare correctly."""  | 
|
347  | 
self.assertEqual(0, osutils.compare_paths_prefix_order(  | 
|
348  | 
path_less, path_less))  | 
|
349  | 
self.assertEqual(0, osutils.compare_paths_prefix_order(  | 
|
350  | 
path_greater, path_greater))  | 
|
351  | 
self.assertEqual(-1, osutils.compare_paths_prefix_order(  | 
|
352  | 
path_less, path_greater))  | 
|
353  | 
self.assertEqual(1, osutils.compare_paths_prefix_order(  | 
|
354  | 
path_greater, path_less))  | 
|
355  | 
||
356  | 
def test_compare_paths_prefix_order(self):  | 
|
357  | 
        # root before all else
 | 
|
358  | 
self.assertPathCompare("/", "/a")  | 
|
359  | 
        # alpha within a dir
 | 
|
360  | 
self.assertPathCompare("/a", "/b")  | 
|
361  | 
self.assertPathCompare("/b", "/z")  | 
|
362  | 
        # high dirs before lower.
 | 
|
363  | 
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.  | 
364  | 
        # except if the deeper dir should be output first
 | 
365  | 
self.assertPathCompare("/a/b/c", "/d/g")  | 
|
| 
1773.3.1
by Robert Collins
 Add path_prefix_key and compare_paths_prefix_order utility functions.  | 
366  | 
        # lexical betwen dirs of the same height
 | 
367  | 
self.assertPathCompare("/a/z", "/z/z")  | 
|
368  | 
self.assertPathCompare("/a/c/z", "/a/d/e")  | 
|
369  | 
||
370  | 
        # this should also be consistent for no leading / paths
 | 
|
371  | 
        # root before all else
 | 
|
372  | 
self.assertPathCompare("", "a")  | 
|
373  | 
        # alpha within a dir
 | 
|
374  | 
self.assertPathCompare("a", "b")  | 
|
375  | 
self.assertPathCompare("b", "z")  | 
|
376  | 
        # high dirs before lower.
 | 
|
377  | 
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.  | 
378  | 
        # except if the deeper dir should be output first
 | 
379  | 
self.assertPathCompare("a/b/c", "d/g")  | 
|
| 
1773.3.1
by Robert Collins
 Add path_prefix_key and compare_paths_prefix_order utility functions.  | 
380  | 
        # lexical betwen dirs of the same height
 | 
381  | 
self.assertPathCompare("a/z", "z/z")  | 
|
382  | 
self.assertPathCompare("a/c/z", "a/d/e")  | 
|
383  | 
||
| 
1773.3.3
by Robert Collins
 Add new tests John Meinel asked for.  | 
384  | 
def test_path_prefix_sorting(self):  | 
385  | 
"""Doing a sort on path prefix should match our sample data."""  | 
|
386  | 
original_paths = [  | 
|
387  | 
'a',  | 
|
388  | 
'a/b',  | 
|
389  | 
'a/b/c',  | 
|
390  | 
'b',  | 
|
391  | 
'b/c',  | 
|
392  | 
'd',  | 
|
393  | 
'd/e',  | 
|
394  | 
'd/e/f',  | 
|
395  | 
'd/f',  | 
|
396  | 
'd/g',  | 
|
397  | 
'g',  | 
|
398  | 
            ]
 | 
|
399  | 
||
400  | 
dir_sorted_paths = [  | 
|
401  | 
'a',  | 
|
402  | 
'b',  | 
|
403  | 
'd',  | 
|
404  | 
'g',  | 
|
405  | 
'a/b',  | 
|
406  | 
'a/b/c',  | 
|
407  | 
'b/c',  | 
|
408  | 
'd/e',  | 
|
409  | 
'd/f',  | 
|
410  | 
'd/g',  | 
|
411  | 
'd/e/f',  | 
|
412  | 
            ]
 | 
|
413  | 
||
414  | 
self.assertEqual(  | 
|
415  | 
dir_sorted_paths,  | 
|
416  | 
sorted(original_paths, key=osutils.path_prefix_key))  | 
|
417  | 
        # using the comparison routine shoudl work too:
 | 
|
418  | 
self.assertEqual(  | 
|
419  | 
dir_sorted_paths,  | 
|
420  | 
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.  | 
421  | 
|
422  | 
||
423  | 
class TestTerminalEncoding(TestCase):  | 
|
424  | 
"""Test the auto-detection of proper terminal encoding."""  | 
|
425  | 
||
426  | 
def setUp(self):  | 
|
427  | 
self._stdout = sys.stdout  | 
|
428  | 
self._stderr = sys.stderr  | 
|
429  | 
self._stdin = sys.stdin  | 
|
430  | 
self._user_encoding = bzrlib.user_encoding  | 
|
431  | 
||
432  | 
self.addCleanup(self._reset)  | 
|
433  | 
||
434  | 
sys.stdout = StringIOWrapper()  | 
|
435  | 
sys.stdout.encoding = 'stdout_encoding'  | 
|
436  | 
sys.stderr = StringIOWrapper()  | 
|
437  | 
sys.stderr.encoding = 'stderr_encoding'  | 
|
438  | 
sys.stdin = StringIOWrapper()  | 
|
439  | 
sys.stdin.encoding = 'stdin_encoding'  | 
|
440  | 
bzrlib.user_encoding = 'user_encoding'  | 
|
441  | 
||
442  | 
def _reset(self):  | 
|
443  | 
sys.stdout = self._stdout  | 
|
444  | 
sys.stderr = self._stderr  | 
|
445  | 
sys.stdin = self._stdin  | 
|
446  | 
bzrlib.user_encoding = self._user_encoding  | 
|
447  | 
||
448  | 
def test_get_terminal_encoding(self):  | 
|
449  | 
        # first preference is stdout encoding
 | 
|
450  | 
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())  | 
|
451  | 
||
452  | 
sys.stdout.encoding = None  | 
|
453  | 
        # if sys.stdout is None, fall back to sys.stdin
 | 
|
454  | 
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())  | 
|
455  | 
||
456  | 
sys.stdin.encoding = None  | 
|
457  | 
        # and in the worst case, use bzrlib.user_encoding
 | 
|
458  | 
self.assertEqual('user_encoding', osutils.get_terminal_encoding())  | 
|
459  |