150
class TestWin32Funcs(TestCase):
151
"""Test that the _win32 versions of os utilities return appropriate paths."""
153
def test_abspath(self):
154
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
155
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
157
def test_realpath(self):
158
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
159
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
161
def test_pathjoin(self):
162
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
163
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
164
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
165
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
166
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
167
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
169
def test_normpath(self):
170
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
171
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
173
def test_getcwd(self):
174
self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
177
class TestWin32FuncsDirs(TestCaseInTempDir):
178
"""Test win32 functions that create files."""
180
def test_getcwd(self):
181
# Make sure getcwd can handle unicode filenames
183
os.mkdir(u'B\xe5gfors')
185
raise TestSkipped("Unable to create Unicode filename")
187
os.chdir(u'B\xe5gfors')
188
# TODO: jam 20060427 This will probably fail on Mac OSX because
189
# it will change the normalization of B\xe5gfors
190
# Consider using a different unicode character, or make
191
# osutils.getcwd() renormalize the path.
192
self.assertTrue(osutils._win32_getcwd().endswith(u'/B\xe5gfors'))
194
def test_mkdtemp(self):
195
tmpdir = osutils._win32_mkdtemp(dir='.')
196
self.assertFalse('\\' in tmpdir)
198
def test_rename(self):
206
osutils._win32_rename('b', 'a')
207
self.failUnlessExists('a')
208
self.failIfExists('b')
209
self.assertFileEqual('baz\n', 'a')
151
212
class TestSplitLines(TestCase):
153
214
def test_split_unicode(self):
159
220
def test_split_with_carriage_returns(self):
160
221
self.assertEqual(['foo\rbar\n'],
161
222
osutils.split_lines('foo\rbar\n'))
225
class TestWalkDirs(TestCaseInTempDir):
227
def test_walkdirs(self):
236
self.build_tree(tree)
237
expected_dirblocks = [
239
('0file', '0file', 'file'),
240
('1dir', '1dir', 'directory'),
241
('2file', '2file', 'file'),
244
('1dir/0file', '0file', 'file'),
245
('1dir/1dir', '1dir', 'directory'),
252
for dirblock in osutils.walkdirs('.'):
253
if len(dirblock) and dirblock[0][1] == '.bzr':
254
# this tests the filtering of selected paths
257
result.append(dirblock)
259
self.assertTrue(found_bzrdir)
260
self.assertEqual(expected_dirblocks,
261
[[line[0:3] for line in block] for block in result])
262
# you can search a subdir only, with a supplied prefix.
264
for dirblock in osutils.walkdirs('1dir', '1dir'):
265
result.append(dirblock)
266
self.assertEqual(expected_dirblocks[1:],
267
[[line[0:3] for line in block] for block in result])
269
def assertPathCompare(self, path_less, path_greater):
270
"""check that path_less and path_greater compare correctly."""
271
self.assertEqual(0, osutils.compare_paths_prefix_order(
272
path_less, path_less))
273
self.assertEqual(0, osutils.compare_paths_prefix_order(
274
path_greater, path_greater))
275
self.assertEqual(-1, osutils.compare_paths_prefix_order(
276
path_less, path_greater))
277
self.assertEqual(1, osutils.compare_paths_prefix_order(
278
path_greater, path_less))
280
def test_compare_paths_prefix_order(self):
281
# root before all else
282
self.assertPathCompare("/", "/a")
284
self.assertPathCompare("/a", "/b")
285
self.assertPathCompare("/b", "/z")
286
# high dirs before lower.
287
self.assertPathCompare("/z", "/a/a")
288
# except if the deeper dir should be output first
289
self.assertPathCompare("/a/b/c", "/d/g")
290
# lexical betwen dirs of the same height
291
self.assertPathCompare("/a/z", "/z/z")
292
self.assertPathCompare("/a/c/z", "/a/d/e")
294
# this should also be consistent for no leading / paths
295
# root before all else
296
self.assertPathCompare("", "a")
298
self.assertPathCompare("a", "b")
299
self.assertPathCompare("b", "z")
300
# high dirs before lower.
301
self.assertPathCompare("z", "a/a")
302
# except if the deeper dir should be output first
303
self.assertPathCompare("a/b/c", "d/g")
304
# lexical betwen dirs of the same height
305
self.assertPathCompare("a/z", "z/z")
306
self.assertPathCompare("a/c/z", "a/d/e")
308
def test_path_prefix_sorting(self):
309
"""Doing a sort on path prefix should match our sample data."""
340
sorted(original_paths, key=osutils.path_prefix_key))
341
# using the comparison routine shoudl work too:
344
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))