1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
# Copyright (C) 2005 by Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests for the osutils wrapper."""
import errno
import os
import socket
import stat
import sys
import bzrlib
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
import bzrlib.osutils as osutils
from bzrlib.tests import (
StringIOWrapper,
TestCase,
TestCaseInTempDir,
TestSkipped,
)
class TestOSUtils(TestCaseInTempDir):
def test_fancy_rename(self):
# This should work everywhere
def rename(a, b):
osutils.fancy_rename(a, b,
rename_func=os.rename,
unlink_func=os.unlink)
open('a', 'wb').write('something in a\n')
rename('a', 'b')
self.failIfExists('a')
self.failUnlessExists('b')
self.check_file_contents('b', 'something in a\n')
open('a', 'wb').write('new something in a\n')
rename('b', 'a')
self.check_file_contents('a', 'something in a\n')
def test_rename(self):
# Rename should be semi-atomic on all platforms
open('a', 'wb').write('something in a\n')
osutils.rename('a', 'b')
self.failIfExists('a')
self.failUnlessExists('b')
self.check_file_contents('b', 'something in a\n')
open('a', 'wb').write('new something in a\n')
osutils.rename('b', 'a')
self.check_file_contents('a', 'something in a\n')
# TODO: test fancy_rename using a MemoryTransport
def test_01_rand_chars_empty(self):
result = osutils.rand_chars(0)
self.assertEqual(result, '')
def test_02_rand_chars_100(self):
result = osutils.rand_chars(100)
self.assertEqual(len(result), 100)
self.assertEqual(type(result), str)
self.assertContainsRe(result, r'^[a-z0-9]{100}$')
def test_rmtree(self):
# Check to remove tree with read-only files/dirs
os.mkdir('dir')
f = file('dir/file', 'w')
f.write('spam')
f.close()
# would like to also try making the directory readonly, but at the
# moment python shutil.rmtree doesn't handle that properly - it would
# need to chmod the directory before removing things inside it - deferred
# for now -- mbp 20060505
# osutils.make_readonly('dir')
osutils.make_readonly('dir/file')
osutils.rmtree('dir')
self.failIfExists('dir/file')
self.failIfExists('dir')
def test_file_kind(self):
self.build_tree(['file', 'dir/'])
self.assertEquals('file', osutils.file_kind('file'))
self.assertEquals('directory', osutils.file_kind('dir/'))
if osutils.has_symlinks():
os.symlink('symlink', 'symlink')
self.assertEquals('symlink', osutils.file_kind('symlink'))
# TODO: jam 20060529 Test a block device
try:
os.lstat('/dev/null')
except OSError, e:
if e.errno not in (errno.ENOENT,):
raise
else:
self.assertEquals('chardev', osutils.file_kind('/dev/null'))
mkfifo = getattr(os, 'mkfifo', None)
if mkfifo:
mkfifo('fifo')
try:
self.assertEquals('fifo', osutils.file_kind('fifo'))
finally:
os.remove('fifo')
AF_UNIX = getattr(socket, 'AF_UNIX', None)
if AF_UNIX:
s = socket.socket(AF_UNIX)
s.bind('socket')
try:
self.assertEquals('socket', osutils.file_kind('socket'))
finally:
os.remove('socket')
class TestSafeUnicode(TestCase):
def test_from_ascii_string(self):
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
def test_from_unicode_string_ascii_contents(self):
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
def test_from_utf8_string(self):
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
def test_bad_utf8_string(self):
self.assertRaises(BzrBadParameterNotUnicode,
osutils.safe_unicode,
'\xbb\xbb')
class TestWin32Funcs(TestCase):
"""Test that the _win32 versions of os utilities return appropriate paths."""
def test_abspath(self):
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
def test_realpath(self):
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
def test_pathjoin(self):
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
def test_normpath(self):
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
def test_getcwd(self):
cwd = osutils._win32_getcwd()
os_cwd = os.getcwdu()
self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
# win32 is inconsistent whether it returns lower or upper case
# and even if it was consistent the user might type the other
# so we force it to uppercase
# running python.exe under cmd.exe return capital C:\\
# running win32 python inside a cygwin shell returns lowercase
self.assertEqual(os_cwd[0].upper(), cwd[0])
def test_fixdrive(self):
self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
class TestWin32FuncsDirs(TestCaseInTempDir):
"""Test win32 functions that create files."""
def test_getcwd(self):
# Make sure getcwd can handle unicode filenames
try:
os.mkdir(u'B\xe5gfors')
except UnicodeError:
raise TestSkipped("Unable to create Unicode filename")
os.chdir(u'B\xe5gfors')
# TODO: jam 20060427 This will probably fail on Mac OSX because
# it will change the normalization of B\xe5gfors
# Consider using a different unicode character, or make
# osutils.getcwd() renormalize the path.
self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
def test_mkdtemp(self):
tmpdir = osutils._win32_mkdtemp(dir='.')
self.assertFalse('\\' in tmpdir)
def test_rename(self):
a = open('a', 'wb')
a.write('foo\n')
a.close()
b = open('b', 'wb')
b.write('baz\n')
b.close()
osutils._win32_rename('b', 'a')
self.failUnlessExists('a')
self.failIfExists('b')
self.assertFileEqual('baz\n', 'a')
def test_rename_missing_file(self):
a = open('a', 'wb')
a.write('foo\n')
a.close()
try:
osutils._win32_rename('b', 'a')
except (IOError, OSError), e:
self.assertEqual(errno.ENOENT, e.errno)
self.assertFileEqual('foo\n', 'a')
def test_rename_missing_dir(self):
os.mkdir('a')
try:
osutils._win32_rename('b', 'a')
except (IOError, OSError), e:
self.assertEqual(errno.ENOENT, e.errno)
def test_rename_current_dir(self):
os.mkdir('a')
os.chdir('a')
# You can't rename the working directory
# doing rename non-existant . usually
# just raises ENOENT, since non-existant
# doesn't exist.
try:
osutils._win32_rename('b', '.')
except (IOError, OSError), e:
self.assertEqual(errno.ENOENT, e.errno)
class TestSplitLines(TestCase):
def test_split_unicode(self):
self.assertEqual([u'foo\n', u'bar\xae'],
osutils.split_lines(u'foo\nbar\xae'))
self.assertEqual([u'foo\n', u'bar\xae\n'],
osutils.split_lines(u'foo\nbar\xae\n'))
def test_split_with_carriage_returns(self):
self.assertEqual(['foo\rbar\n'],
osutils.split_lines('foo\rbar\n'))
class TestWalkDirs(TestCaseInTempDir):
def test_walkdirs(self):
tree = [
'.bzr',
'0file',
'1dir/',
'1dir/0file',
'1dir/1dir/',
'2file'
]
self.build_tree(tree)
expected_dirblocks = [
[
('0file', '0file', 'file'),
('1dir', '1dir', 'directory'),
('2file', '2file', 'file'),
],
[
('1dir/0file', '0file', 'file'),
('1dir/1dir', '1dir', 'directory'),
],
[
],
]
result = []
found_bzrdir = False
for dirblock in osutils.walkdirs('.'):
if len(dirblock) and dirblock[0][1] == '.bzr':
# this tests the filtering of selected paths
found_bzrdir = True
del dirblock[0]
result.append(dirblock)
self.assertTrue(found_bzrdir)
self.assertEqual(expected_dirblocks,
[[line[0:3] for line in block] for block in result])
# you can search a subdir only, with a supplied prefix.
result = []
for dirblock in osutils.walkdirs('1dir', '1dir'):
result.append(dirblock)
self.assertEqual(expected_dirblocks[1:],
[[line[0:3] for line in block] for block in result])
def assertPathCompare(self, path_less, path_greater):
"""check that path_less and path_greater compare correctly."""
self.assertEqual(0, osutils.compare_paths_prefix_order(
path_less, path_less))
self.assertEqual(0, osutils.compare_paths_prefix_order(
path_greater, path_greater))
self.assertEqual(-1, osutils.compare_paths_prefix_order(
path_less, path_greater))
self.assertEqual(1, osutils.compare_paths_prefix_order(
path_greater, path_less))
def test_compare_paths_prefix_order(self):
# root before all else
self.assertPathCompare("/", "/a")
# alpha within a dir
self.assertPathCompare("/a", "/b")
self.assertPathCompare("/b", "/z")
# high dirs before lower.
self.assertPathCompare("/z", "/a/a")
# except if the deeper dir should be output first
self.assertPathCompare("/a/b/c", "/d/g")
# lexical betwen dirs of the same height
self.assertPathCompare("/a/z", "/z/z")
self.assertPathCompare("/a/c/z", "/a/d/e")
# this should also be consistent for no leading / paths
# root before all else
self.assertPathCompare("", "a")
# alpha within a dir
self.assertPathCompare("a", "b")
self.assertPathCompare("b", "z")
# high dirs before lower.
self.assertPathCompare("z", "a/a")
# except if the deeper dir should be output first
self.assertPathCompare("a/b/c", "d/g")
# lexical betwen dirs of the same height
self.assertPathCompare("a/z", "z/z")
self.assertPathCompare("a/c/z", "a/d/e")
def test_path_prefix_sorting(self):
"""Doing a sort on path prefix should match our sample data."""
original_paths = [
'a',
'a/b',
'a/b/c',
'b',
'b/c',
'd',
'd/e',
'd/e/f',
'd/f',
'd/g',
'g',
]
dir_sorted_paths = [
'a',
'b',
'd',
'g',
'a/b',
'a/b/c',
'b/c',
'd/e',
'd/f',
'd/g',
'd/e/f',
]
self.assertEqual(
dir_sorted_paths,
sorted(original_paths, key=osutils.path_prefix_key))
# using the comparison routine shoudl work too:
self.assertEqual(
dir_sorted_paths,
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
class TestTerminalEncoding(TestCase):
"""Test the auto-detection of proper terminal encoding."""
def setUp(self):
self._stdout = sys.stdout
self._stderr = sys.stderr
self._stdin = sys.stdin
self._user_encoding = bzrlib.user_encoding
self.addCleanup(self._reset)
sys.stdout = StringIOWrapper()
sys.stdout.encoding = 'stdout_encoding'
sys.stderr = StringIOWrapper()
sys.stderr.encoding = 'stderr_encoding'
sys.stdin = StringIOWrapper()
sys.stdin.encoding = 'stdin_encoding'
bzrlib.user_encoding = 'user_encoding'
def _reset(self):
sys.stdout = self._stdout
sys.stderr = self._stderr
sys.stdin = self._stdin
bzrlib.user_encoding = self._user_encoding
def test_get_terminal_encoding(self):
# first preference is stdout encoding
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
sys.stdout.encoding = None
# if sys.stdout is None, fall back to sys.stdin
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
sys.stdin.encoding = None
# and in the worst case, use bzrlib.user_encoding
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
|