176
171
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
178
173
def test_getcwd(self):
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])
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'))
174
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
195
177
class TestWin32FuncsDirs(TestCaseInTempDir):
226
208
self.failIfExists('b')
227
209
self.assertFileEqual('baz\n', 'a')
229
def test_rename_missing_file(self):
235
osutils._win32_rename('b', 'a')
236
except (IOError, OSError), e:
237
self.assertEqual(errno.ENOENT, e.errno)
238
self.assertFileEqual('foo\n', 'a')
240
def test_rename_missing_dir(self):
243
osutils._win32_rename('b', 'a')
244
except (IOError, OSError), e:
245
self.assertEqual(errno.ENOENT, e.errno)
247
def test_rename_current_dir(self):
250
# You can't rename the working directory
251
# doing rename non-existant . usually
252
# just raises ENOENT, since non-existant
255
osutils._win32_rename('b', '.')
256
except (IOError, OSError), e:
257
self.assertEqual(errno.ENOENT, e.errno)
260
class TestMacFuncsDirs(TestCaseInTempDir):
261
"""Test mac special functions that require directories."""
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
267
os.mkdir(u'B\xe5gfors')
269
raise TestSkipped("Unable to create Unicode filename")
271
os.chdir(u'B\xe5gfors')
272
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
274
def test_getcwd_nonnorm(self):
275
# Test that _mac_getcwd() will normalize this path
277
os.mkdir(u'Ba\u030agfors')
279
raise TestSkipped("Unable to create Unicode filename")
281
os.chdir(u'Ba\u030agfors')
282
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
284
212
class TestSplitLines(TestCase):
338
266
self.assertEqual(expected_dirblocks[1:],
339
267
[[line[0:3] for line in block] for block in result])
341
def assertPathCompare(self, path_less, path_greater):
342
"""check that path_less and path_greater compare correctly."""
343
self.assertEqual(0, osutils.compare_paths_prefix_order(
344
path_less, path_less))
345
self.assertEqual(0, osutils.compare_paths_prefix_order(
346
path_greater, path_greater))
347
self.assertEqual(-1, osutils.compare_paths_prefix_order(
348
path_less, path_greater))
349
self.assertEqual(1, osutils.compare_paths_prefix_order(
350
path_greater, path_less))
352
def test_compare_paths_prefix_order(self):
353
# root before all else
354
self.assertPathCompare("/", "/a")
356
self.assertPathCompare("/a", "/b")
357
self.assertPathCompare("/b", "/z")
358
# high dirs before lower.
359
self.assertPathCompare("/z", "/a/a")
360
# except if the deeper dir should be output first
361
self.assertPathCompare("/a/b/c", "/d/g")
362
# lexical betwen dirs of the same height
363
self.assertPathCompare("/a/z", "/z/z")
364
self.assertPathCompare("/a/c/z", "/a/d/e")
366
# this should also be consistent for no leading / paths
367
# root before all else
368
self.assertPathCompare("", "a")
370
self.assertPathCompare("a", "b")
371
self.assertPathCompare("b", "z")
372
# high dirs before lower.
373
self.assertPathCompare("z", "a/a")
374
# except if the deeper dir should be output first
375
self.assertPathCompare("a/b/c", "d/g")
376
# lexical betwen dirs of the same height
377
self.assertPathCompare("a/z", "z/z")
378
self.assertPathCompare("a/c/z", "a/d/e")
380
def test_path_prefix_sorting(self):
381
"""Doing a sort on path prefix should match our sample data."""
412
sorted(original_paths, key=osutils.path_prefix_key))
413
# using the comparison routine shoudl work too:
416
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
419
class TestTerminalEncoding(TestCase):
420
"""Test the auto-detection of proper terminal encoding."""
423
self._stdout = sys.stdout
424
self._stderr = sys.stderr
425
self._stdin = sys.stdin
426
self._user_encoding = bzrlib.user_encoding
428
self.addCleanup(self._reset)
430
sys.stdout = StringIOWrapper()
431
sys.stdout.encoding = 'stdout_encoding'
432
sys.stderr = StringIOWrapper()
433
sys.stderr.encoding = 'stderr_encoding'
434
sys.stdin = StringIOWrapper()
435
sys.stdin.encoding = 'stdin_encoding'
436
bzrlib.user_encoding = 'user_encoding'
439
sys.stdout = self._stdout
440
sys.stderr = self._stderr
441
sys.stdin = self._stdin
442
bzrlib.user_encoding = self._user_encoding
444
def test_get_terminal_encoding(self):
445
# first preference is stdout encoding
446
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
448
sys.stdout.encoding = None
449
# if sys.stdout is None, fall back to sys.stdin
450
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
452
sys.stdin.encoding = None
453
# and in the worst case, use bzrlib.user_encoding
454
self.assertEqual('user_encoding', osutils.get_terminal_encoding())