155
class TestWin32Funcs(TestCase):
156
"""Test that the _win32 versions of os utilities return appropriate paths."""
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'))
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'))
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'))
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'))
178
def test_getcwd(self):
179
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
182
class TestWin32FuncsDirs(TestCaseInTempDir):
183
"""Test win32 functions that create files."""
185
def test_getcwd(self):
186
# Make sure getcwd can handle unicode filenames
188
os.mkdir(u'B\xe5gfors')
190
raise TestSkipped("Unable to create Unicode filename")
192
os.chdir(u'B\xe5gfors')
193
# TODO: jam 20060427 This will probably fail on Mac OSX because
194
# it will change the normalization of B\xe5gfors
195
# Consider using a different unicode character, or make
196
# osutils.getcwd() renormalize the path.
197
self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
199
def test_mkdtemp(self):
200
tmpdir = osutils._win32_mkdtemp(dir='.')
201
self.assertFalse('\\' in tmpdir)
203
def test_rename(self):
211
osutils._win32_rename('b', 'a')
212
self.failUnlessExists('a')
213
self.failIfExists('b')
214
self.assertFileEqual('baz\n', 'a')
151
217
class TestSplitLines(TestCase):
153
219
def test_split_unicode(self):
159
225
def test_split_with_carriage_returns(self):
160
226
self.assertEqual(['foo\rbar\n'],
161
227
osutils.split_lines('foo\rbar\n'))
230
class TestWalkDirs(TestCaseInTempDir):
232
def test_walkdirs(self):
241
self.build_tree(tree)
242
expected_dirblocks = [
244
('0file', '0file', 'file'),
245
('1dir', '1dir', 'directory'),
246
('2file', '2file', 'file'),
249
('1dir/0file', '0file', 'file'),
250
('1dir/1dir', '1dir', 'directory'),
257
for dirblock in osutils.walkdirs('.'):
258
if len(dirblock) and dirblock[0][1] == '.bzr':
259
# this tests the filtering of selected paths
262
result.append(dirblock)
264
self.assertTrue(found_bzrdir)
265
self.assertEqual(expected_dirblocks,
266
[[line[0:3] for line in block] for block in result])
267
# you can search a subdir only, with a supplied prefix.
269
for dirblock in osutils.walkdirs('1dir', '1dir'):
270
result.append(dirblock)
271
self.assertEqual(expected_dirblocks[1:],
272
[[line[0:3] for line in block] for block in result])
274
def assertPathCompare(self, path_less, path_greater):
275
"""check that path_less and path_greater compare correctly."""
276
self.assertEqual(0, osutils.compare_paths_prefix_order(
277
path_less, path_less))
278
self.assertEqual(0, osutils.compare_paths_prefix_order(
279
path_greater, path_greater))
280
self.assertEqual(-1, osutils.compare_paths_prefix_order(
281
path_less, path_greater))
282
self.assertEqual(1, osutils.compare_paths_prefix_order(
283
path_greater, path_less))
285
def test_compare_paths_prefix_order(self):
286
# root before all else
287
self.assertPathCompare("/", "/a")
289
self.assertPathCompare("/a", "/b")
290
self.assertPathCompare("/b", "/z")
291
# high dirs before lower.
292
self.assertPathCompare("/z", "/a/a")
293
# except if the deeper dir should be output first
294
self.assertPathCompare("/a/b/c", "/d/g")
295
# lexical betwen dirs of the same height
296
self.assertPathCompare("/a/z", "/z/z")
297
self.assertPathCompare("/a/c/z", "/a/d/e")
299
# this should also be consistent for no leading / paths
300
# root before all else
301
self.assertPathCompare("", "a")
303
self.assertPathCompare("a", "b")
304
self.assertPathCompare("b", "z")
305
# high dirs before lower.
306
self.assertPathCompare("z", "a/a")
307
# except if the deeper dir should be output first
308
self.assertPathCompare("a/b/c", "d/g")
309
# lexical betwen dirs of the same height
310
self.assertPathCompare("a/z", "z/z")
311
self.assertPathCompare("a/c/z", "a/d/e")
313
def test_path_prefix_sorting(self):
314
"""Doing a sort on path prefix should match our sample data."""
345
sorted(original_paths, key=osutils.path_prefix_key))
346
# using the comparison routine shoudl work too:
349
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
352
class TestTerminalEncoding(TestCase):
353
"""Test the auto-detection of proper terminal encoding."""
356
self._stdout = sys.stdout
357
self._stderr = sys.stderr
358
self._stdin = sys.stdin
359
self._user_encoding = bzrlib.user_encoding
361
self.addCleanup(self._reset)
363
sys.stdout = StringIOWrapper()
364
sys.stdout.encoding = 'stdout_encoding'
365
sys.stderr = StringIOWrapper()
366
sys.stderr.encoding = 'stderr_encoding'
367
sys.stdin = StringIOWrapper()
368
sys.stdin.encoding = 'stdin_encoding'
369
bzrlib.user_encoding = 'user_encoding'
372
sys.stdout = self._stdout
373
sys.stderr = self._stderr
374
sys.stdin = self._stdin
375
bzrlib.user_encoding = self._user_encoding
377
def test_get_terminal_encoding(self):
378
# first preference is stdout encoding
379
self.assertEqual('stdout_encoding', osutils.get_terminal_encoding())
381
sys.stdout.encoding = None
382
# if sys.stdout is None, fall back to sys.stdin
383
self.assertEqual('stdin_encoding', osutils.get_terminal_encoding())
385
sys.stdin.encoding = None
386
# and in the worst case, use bzrlib.user_encoding
387
self.assertEqual('user_encoding', osutils.get_terminal_encoding())