/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Martin Pool
  • Date: 2006-06-20 03:30:14 UTC
  • mfrom: (1793 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1797.
  • Revision ID: mbp@sourcefrog.net-20060620033014-e19ce470e2ce6561
[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""Tests for the osutils wrapper.
18
 
"""
 
17
"""Tests for the osutils wrapper."""
19
18
 
20
19
import errno
21
20
import os
24
23
import sys
25
24
 
26
25
import bzrlib
27
 
from bzrlib.errors import BzrBadParameterNotUnicode
 
26
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
28
27
import bzrlib.osutils as osutils
29
 
from bzrlib.tests import TestCaseInTempDir, TestCase
 
28
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
30
29
 
31
30
 
32
31
class TestOSUtils(TestCaseInTempDir):
148
147
                          '\xbb\xbb')
149
148
 
150
149
 
 
150
class TestWin32Funcs(TestCase):
 
151
    """Test that the _win32 versions of os utilities return appropriate paths."""
 
152
 
 
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'))
 
156
 
 
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'))
 
160
 
 
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'))
 
168
 
 
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'))
 
172
 
 
173
    def test_getcwd(self):
 
174
        self.assertEqual(os.getcwdu().replace('\\', '/'), osutils._win32_getcwd())
 
175
 
 
176
 
 
177
class TestWin32FuncsDirs(TestCaseInTempDir):
 
178
    """Test win32 functions that create files."""
 
179
    
 
180
    def test_getcwd(self):
 
181
        # Make sure getcwd can handle unicode filenames
 
182
        try:
 
183
            os.mkdir(u'B\xe5gfors')
 
184
        except UnicodeError:
 
185
            raise TestSkipped("Unable to create Unicode filename")
 
186
 
 
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'))
 
193
 
 
194
    def test_mkdtemp(self):
 
195
        tmpdir = osutils._win32_mkdtemp(dir='.')
 
196
        self.assertFalse('\\' in tmpdir)
 
197
 
 
198
    def test_rename(self):
 
199
        a = open('a', 'wb')
 
200
        a.write('foo\n')
 
201
        a.close()
 
202
        b = open('b', 'wb')
 
203
        b.write('baz\n')
 
204
        b.close()
 
205
 
 
206
        osutils._win32_rename('b', 'a')
 
207
        self.failUnlessExists('a')
 
208
        self.failIfExists('b')
 
209
        self.assertFileEqual('baz\n', 'a')
 
210
 
 
211
 
151
212
class TestSplitLines(TestCase):
152
213
 
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'))
 
223
 
 
224
 
 
225
class TestWalkDirs(TestCaseInTempDir):
 
226
 
 
227
    def test_walkdirs(self):
 
228
        tree = [
 
229
            '.bzr',
 
230
            '0file',
 
231
            '1dir/',
 
232
            '1dir/0file',
 
233
            '1dir/1dir/',
 
234
            '2file'
 
235
            ]
 
236
        self.build_tree(tree)
 
237
        expected_dirblocks = [
 
238
                [
 
239
                    ('0file', '0file', 'file'),
 
240
                    ('1dir', '1dir', 'directory'),
 
241
                    ('2file', '2file', 'file'),
 
242
                ],
 
243
                [
 
244
                    ('1dir/0file', '0file', 'file'),
 
245
                    ('1dir/1dir', '1dir', 'directory'),
 
246
                ],
 
247
                [
 
248
                ],
 
249
            ]
 
250
        result = []
 
251
        found_bzrdir = False
 
252
        for dirblock in osutils.walkdirs('.'):
 
253
            if len(dirblock) and dirblock[0][1] == '.bzr':
 
254
                # this tests the filtering of selected paths
 
255
                found_bzrdir = True
 
256
                del dirblock[0]
 
257
            result.append(dirblock)
 
258
 
 
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.
 
263
        result = []
 
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])
 
268
 
 
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))
 
279
 
 
280
    def test_compare_paths_prefix_order(self):
 
281
        # root before all else
 
282
        self.assertPathCompare("/", "/a")
 
283
        # alpha within a dir
 
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")
 
293
 
 
294
        # this should also be consistent for no leading / paths
 
295
        # root before all else
 
296
        self.assertPathCompare("", "a")
 
297
        # alpha within a dir
 
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")
 
307
 
 
308
    def test_path_prefix_sorting(self):
 
309
        """Doing a sort on path prefix should match our sample data."""
 
310
        original_paths = [
 
311
            'a',
 
312
            'a/b',
 
313
            'a/b/c',
 
314
            'b',
 
315
            'b/c',
 
316
            'd',
 
317
            'd/e',
 
318
            'd/e/f',
 
319
            'd/f',
 
320
            'd/g',
 
321
            'g',
 
322
            ]
 
323
 
 
324
        dir_sorted_paths = [
 
325
            'a',
 
326
            'b',
 
327
            'd',
 
328
            'g',
 
329
            'a/b',
 
330
            'a/b/c',
 
331
            'b/c',
 
332
            'd/e',
 
333
            'd/f',
 
334
            'd/g',
 
335
            'd/e/f',
 
336
            ]
 
337
 
 
338
        self.assertEqual(
 
339
            dir_sorted_paths,
 
340
            sorted(original_paths, key=osutils.path_prefix_key))
 
341
        # using the comparison routine shoudl work too:
 
342
        self.assertEqual(
 
343
            dir_sorted_paths,
 
344
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))