132
132
os.remove('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())
135
154
class TestSafeUnicode(TestCase):
176
195
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
178
197
def test_getcwd(self):
179
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
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'))
182
214
class TestWin32FuncsDirs(TestCaseInTempDir):
185
217
def test_getcwd(self):
186
218
# Make sure getcwd can handle unicode filenames
188
os.mkdir(u'B\xe5gfors')
189
221
except UnicodeError:
190
222
raise TestSkipped("Unable to create Unicode filename")
192
os.chdir(u'B\xe5gfors')
193
225
# TODO: jam 20060427 This will probably fail on Mac OSX because
194
226
# it will change the normalization of B\xe5gfors
195
227
# Consider using a different unicode character, or make
196
228
# osutils.getcwd() renormalize the path.
197
self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
229
self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
199
231
def test_mkdtemp(self):
200
232
tmpdir = osutils._win32_mkdtemp(dir='.')
213
245
self.failIfExists('b')
214
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')
217
303
class TestSplitLines(TestCase):
241
327
self.build_tree(tree)
242
328
expected_dirblocks = [
244
('0file', '0file', 'file'),
245
('1dir', '1dir', 'directory'),
246
('2file', '2file', 'file'),
249
('1dir/0file', '0file', 'file'),
250
('1dir/1dir', '1dir', 'directory'),
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'),
256
346
found_bzrdir = False
257
for dirblock in osutils.walkdirs('.'):
347
for dirdetail, dirblock in osutils.walkdirs('.'):
258
348
if len(dirblock) and dirblock[0][1] == '.bzr':
259
349
# this tests the filtering of selected paths
260
350
found_bzrdir = True
262
result.append(dirblock)
352
result.append((dirdetail, dirblock))
264
354
self.assertTrue(found_bzrdir)
265
355
self.assertEqual(expected_dirblocks,
266
[[line[0:3] for line in block] for block in result])
356
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
267
357
# you can search a subdir only, with a supplied prefix.
269
for dirblock in osutils.walkdirs('1dir', '1dir'):
359
for dirblock in osutils.walkdirs('./1dir', '1dir'):
270
360
result.append(dirblock)
271
361
self.assertEqual(expected_dirblocks[1:],
272
[[line[0:3] for line in block] for block in result])
362
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
274
364
def assertPathCompare(self, path_less, path_greater):
275
365
"""check that path_less and path_greater compare correctly."""
349
439
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
442
class TestCopyTree(TestCaseInTempDir):
444
def test_copy_basic_tree(self):
445
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
446
osutils.copy_tree('source', 'target')
447
self.assertEqual(['a', 'b'], os.listdir('target'))
448
self.assertEqual(['c'], os.listdir('target/b'))
450
def test_copy_tree_target_exists(self):
451
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
453
osutils.copy_tree('source', 'target')
454
self.assertEqual(['a', 'b'], os.listdir('target'))
455
self.assertEqual(['c'], os.listdir('target/b'))
457
def test_copy_tree_symlinks(self):
458
if not osutils.has_symlinks():
460
self.build_tree(['source/'])
461
os.symlink('a/generic/path', 'source/lnk')
462
osutils.copy_tree('source', 'target')
463
self.assertEqual(['lnk'], os.listdir('target'))
464
self.assertEqual('a/generic/path', os.readlink('target/lnk'))
466
def test_copy_tree_handlers(self):
469
def file_handler(from_path, to_path):
470
processed_files.append(('f', from_path, to_path))
471
def dir_handler(from_path, to_path):
472
processed_files.append(('d', from_path, to_path))
473
def link_handler(from_path, to_path):
474
processed_links.append((from_path, to_path))
475
handlers = {'file':file_handler,
476
'directory':dir_handler,
477
'symlink':link_handler,
480
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
481
if osutils.has_symlinks():
482
os.symlink('a/generic/path', 'source/lnk')
483
osutils.copy_tree('source', 'target', handlers=handlers)
485
self.assertEqual([('d', 'source', 'target'),
486
('f', 'source/a', 'target/a'),
487
('d', 'source/b', 'target/b'),
488
('f', 'source/b/c', 'target/b/c'),
490
self.failIfExists('target')
491
if osutils.has_symlinks():
492
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
352
495
class TestTerminalEncoding(TestCase):
353
496
"""Test the auto-detection of proper terminal encoding."""