97
90
self.failIfExists('dir/file')
98
91
self.failIfExists('dir')
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'))
108
# TODO: jam 20060529 Test a block device
110
os.lstat('/dev/null')
112
if e.errno not in (errno.ENOENT,):
115
self.assertEquals('chardev', osutils.file_kind('/dev/null'))
117
mkfifo = getattr(os, 'mkfifo', None)
121
self.assertEquals('fifo', osutils.file_kind('fifo'))
125
AF_UNIX = getattr(socket, 'AF_UNIX', None)
127
s = socket.socket(AF_UNIX)
130
self.assertEquals('socket', osutils.file_kind('socket'))
134
def test_get_umask(self):
135
if sys.platform == 'win32':
136
# umask always returns '0', no way to set it
137
self.assertEqual(0, osutils.get_umask())
140
orig_umask = osutils.get_umask()
143
self.assertEqual(0222, osutils.get_umask())
145
self.assertEqual(0022, osutils.get_umask())
147
self.assertEqual(0002, osutils.get_umask())
149
self.assertEqual(0027, osutils.get_umask())
154
94
class TestSafeUnicode(TestCase):
174
class TestWin32Funcs(TestCase):
175
"""Test that the _win32 versions of os utilities return appropriate paths."""
177
def test_abspath(self):
178
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
179
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
181
def test_realpath(self):
182
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
183
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
185
def test_pathjoin(self):
186
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
187
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
188
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
189
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
190
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
191
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
193
def test_normpath(self):
194
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
195
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
197
def test_getcwd(self):
198
cwd = osutils._win32_getcwd()
199
os_cwd = os.getcwdu()
200
self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
201
# win32 is inconsistent whether it returns lower or upper case
202
# and even if it was consistent the user might type the other
203
# so we force it to uppercase
204
# running python.exe under cmd.exe return capital C:\\
205
# running win32 python inside a cygwin shell returns lowercase
206
self.assertEqual(os_cwd[0].upper(), cwd[0])
208
def test_fixdrive(self):
209
self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
210
self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
211
self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
214
class TestWin32FuncsDirs(TestCaseInTempDir):
215
"""Test win32 functions that create files."""
217
def test_getcwd(self):
218
# Make sure getcwd can handle unicode filenames
222
raise TestSkipped("Unable to create Unicode filename")
225
# TODO: jam 20060427 This will probably fail on Mac OSX because
226
# it will change the normalization of B\xe5gfors
227
# Consider using a different unicode character, or make
228
# osutils.getcwd() renormalize the path.
229
self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
231
def test_mkdtemp(self):
232
tmpdir = osutils._win32_mkdtemp(dir='.')
233
self.assertFalse('\\' in tmpdir)
235
def test_rename(self):
243
osutils._win32_rename('b', 'a')
244
self.failUnlessExists('a')
245
self.failIfExists('b')
246
self.assertFileEqual('baz\n', 'a')
248
def test_rename_missing_file(self):
254
osutils._win32_rename('b', 'a')
255
except (IOError, OSError), e:
256
self.assertEqual(errno.ENOENT, e.errno)
257
self.assertFileEqual('foo\n', 'a')
259
def test_rename_missing_dir(self):
262
osutils._win32_rename('b', 'a')
263
except (IOError, OSError), e:
264
self.assertEqual(errno.ENOENT, e.errno)
266
def test_rename_current_dir(self):
269
# You can't rename the working directory
270
# doing rename non-existant . usually
271
# just raises ENOENT, since non-existant
274
osutils._win32_rename('b', '.')
275
except (IOError, OSError), e:
276
self.assertEqual(errno.ENOENT, e.errno)
279
class TestMacFuncsDirs(TestCaseInTempDir):
280
"""Test mac special functions that require directories."""
282
def test_getcwd(self):
283
# On Mac, this will actually create Ba\u030agfors
284
# but chdir will still work, because it accepts both paths
286
os.mkdir(u'B\xe5gfors')
288
raise TestSkipped("Unable to create Unicode filename")
290
os.chdir(u'B\xe5gfors')
291
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
293
def test_getcwd_nonnorm(self):
294
# Test that _mac_getcwd() will normalize this path
296
os.mkdir(u'Ba\u030agfors')
298
raise TestSkipped("Unable to create Unicode filename")
300
os.chdir(u'Ba\u030agfors')
301
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
303
114
class TestSplitLines(TestCase):
305
116
def test_split_unicode(self):
311
122
def test_split_with_carriage_returns(self):
312
123
self.assertEqual(['foo\rbar\n'],
313
124
osutils.split_lines('foo\rbar\n'))
316
class TestWalkDirs(TestCaseInTempDir):
318
def test_walkdirs(self):
327
self.build_tree(tree)
328
expected_dirblocks = [
330
[('0file', '0file', 'file'),
331
('1dir', '1dir', 'directory'),
332
('2file', '2file', 'file'),
336
[('1dir/0file', '0file', 'file'),
337
('1dir/1dir', '1dir', 'directory'),
340
(('1dir/1dir', './1dir/1dir'),
347
for dirdetail, dirblock in osutils.walkdirs('.'):
348
if len(dirblock) and dirblock[0][1] == '.bzr':
349
# this tests the filtering of selected paths
352
result.append((dirdetail, dirblock))
354
self.assertTrue(found_bzrdir)
355
self.assertEqual(expected_dirblocks,
356
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
357
# you can search a subdir only, with a supplied prefix.
359
for dirblock in osutils.walkdirs('./1dir', '1dir'):
360
result.append(dirblock)
361
self.assertEqual(expected_dirblocks[1:],
362
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
364
def assertPathCompare(self, path_less, path_greater):
365
"""check that path_less and path_greater compare correctly."""
366
self.assertEqual(0, osutils.compare_paths_prefix_order(
367
path_less, path_less))
368
self.assertEqual(0, osutils.compare_paths_prefix_order(
369
path_greater, path_greater))
370
self.assertEqual(-1, osutils.compare_paths_prefix_order(
371
path_less, path_greater))
372
self.assertEqual(1, osutils.compare_paths_prefix_order(
373
path_greater, path_less))
375
def test_compare_paths_prefix_order(self):
376
# root before all else
377
self.assertPathCompare("/", "/a")
379
self.assertPathCompare("/a", "/b")
380
self.assertPathCompare("/b", "/z")
381
# high dirs before lower.
382
self.assertPathCompare("/z", "/a/a")
383
# except if the deeper dir should be output first
384
self.assertPathCompare("/a/b/c", "/d/g")
385
# lexical betwen dirs of the same height
386
self.assertPathCompare("/a/z", "/z/z")
387
self.assertPathCompare("/a/c/z", "/a/d/e")
389
# this should also be consistent for no leading / paths
390
# root before all else
391
self.assertPathCompare("", "a")
393
self.assertPathCompare("a", "b")
394
self.assertPathCompare("b", "z")
395
# high dirs before lower.
396
self.assertPathCompare("z", "a/a")
397
# except if the deeper dir should be output first
398
self.assertPathCompare("a/b/c", "d/g")
399
# lexical betwen dirs of the same height
400
self.assertPathCompare("a/z", "z/z")
401
self.assertPathCompare("a/c/z", "a/d/e")
403
def test_path_prefix_sorting(self):
404
"""Doing a sort on path prefix should match our sample data."""
435
sorted(original_paths, key=osutils.path_prefix_key))
436
# using the comparison routine shoudl work too:
439
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
442
class TestTerminalEncoding(TestCase):
443
"""Test the auto-detection of proper terminal encoding."""
446
self._stdout = sys.stdout
447
self._stderr = sys.stderr
448
self._stdin = sys.stdin
449
self._user_encoding = bzrlib.user_encoding
451
self.addCleanup(self._reset)
453
sys.stdout = StringIOWrapper()
454
sys.stdout.encoding = 'stdout_encoding'
455
sys.stderr = StringIOWrapper()
456
sys.stderr.encoding = 'stderr_encoding'
457
sys.stdin = StringIOWrapper()
458
sys.stdin.encoding = 'stdin_encoding'
459
bzrlib.user_encoding = 'user_encoding'
462
sys.stdout = self._stdout
463
sys.stderr = self._stderr
464
sys.stdin = self._stdin
465
bzrlib.user_encoding = self._user_encoding
467
def test_get_terminal_encoding(self):
468
# first preference is stdout encoding
469
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
471
sys.stdout.encoding = None
472
# if sys.stdout is None, fall back to sys.stdin
473
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
475
sys.stdin.encoding = None
476
# and in the worst case, use bzrlib.user_encoding
477
self.assertEqual('user_encoding', osutils.get_terminal_encoding())