/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_urlutils.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
import re
21
21
import sys
22
22
 
23
 
from bzrlib import osutils, urlutils
 
23
from bzrlib import osutils, urlutils, win32utils
24
24
import bzrlib
25
25
from bzrlib.errors import InvalidURL, InvalidURLJoin
26
26
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
115
115
        eq('http://host/ab/%C2%B5/%C2%B5',
116
116
            normalize_url(u'http://host/ab/%C2%B5/\xb5'))
117
117
 
 
118
        # Unescape characters that don't need to be escaped
 
119
        eq('http://host/~bob%2525-._',
 
120
                normalize_url('http://host/%7Ebob%2525%2D%2E%5F'))
 
121
        eq('http://host/~bob%2525-._',
 
122
                normalize_url(u'http://host/%7Ebob%2525%2D%2E%5F'))
 
123
 
118
124
        # Normalize verifies URLs when they are not unicode
119
125
        # (indicating they did not come from the user)
120
126
        self.assertRaises(InvalidURL, normalize_url, 'http://host/\xb5')
197
203
            joined = urlutils.join(*args)
198
204
            self.assertEqual(expected, joined)
199
205
 
200
 
        # Test a single element
201
 
        test('foo', 'foo')
202
 
 
203
206
        # Test relative path joining
 
207
        test('foo', 'foo') # relative fragment with nothing is preserved.
204
208
        test('foo/bar', 'foo', 'bar')
205
209
        test('http://foo/bar', 'http://foo', 'bar')
206
210
        test('http://foo/bar', 'http://foo', '.', 'bar')
207
211
        test('http://foo/baz', 'http://foo', 'bar', '../baz')
208
212
        test('http://foo/bar/baz', 'http://foo', 'bar/baz')
209
213
        test('http://foo/baz', 'http://foo', 'bar/../baz')
 
214
        test('http://foo/baz', 'http://foo/bar/', '../baz')
210
215
 
211
216
        # Absolute paths
 
217
        test('http://foo', 'http://foo') # abs url with nothing is preserved.
212
218
        test('http://bar', 'http://foo', 'http://bar')
213
219
        test('sftp://bzr/foo', 'http://foo', 'bar', 'sftp://bzr/foo')
214
220
        test('file:///bar', 'foo', 'file:///bar')
 
221
        test('http://bar/', 'http://foo', 'http://bar/')
 
222
        test('http://bar/a', 'http://foo', 'http://bar/a')
 
223
        test('http://bar/a/', 'http://foo', 'http://bar/a/')
215
224
 
216
225
        # From a base path
217
226
        test('file:///foo', 'file:///', 'foo')
221
230
        
222
231
        # Invalid joinings
223
232
        # Cannot go above root
 
233
        # Implicitly at root:
224
234
        self.assertRaises(InvalidURLJoin, urlutils.join,
225
235
                'http://foo', '../baz')
 
236
        self.assertRaises(InvalidURLJoin, urlutils.join,
 
237
                'http://foo', '/..')
 
238
        # Joining from a path explicitly under the root.
 
239
        self.assertRaises(InvalidURLJoin, urlutils.join,
 
240
                'http://foo/a', '../../b')
 
241
 
 
242
    def test_joinpath(self):
 
243
        def test(expected, *args):
 
244
            joined = urlutils.joinpath(*args)
 
245
            self.assertEqual(expected, joined)
 
246
 
 
247
        # Test a single element
 
248
        test('foo', 'foo')
 
249
 
 
250
        # Test relative path joining
 
251
        test('foo/bar', 'foo', 'bar')
 
252
        test('foo/bar', 'foo', '.', 'bar')
 
253
        test('foo/baz', 'foo', 'bar', '../baz')
 
254
        test('foo/bar/baz', 'foo', 'bar/baz')
 
255
        test('foo/baz', 'foo', 'bar/../baz')
 
256
 
 
257
        # Test joining to an absolute path
 
258
        test('/foo', '/foo')
 
259
        test('/foo', '/foo', '.')
 
260
        test('/foo/bar', '/foo', 'bar')
 
261
        test('/', '/foo', '..')
 
262
 
 
263
        # Test joining with an absolute path
 
264
        test('/bar', 'foo', '/bar')
 
265
 
 
266
        # Test joining to a path with a trailing slash
 
267
        test('foo/bar', 'foo/', 'bar')
 
268
        
 
269
        # Invalid joinings
 
270
        # Cannot go above root
 
271
        self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '../baz')
 
272
        self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '..')
 
273
        self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '/..')
226
274
 
227
275
    def test_function_type(self):
228
276
        if sys.platform == 'win32':
275
323
 
276
324
        self.assertEqual('file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
277
325
 
 
326
    def test_win32_unc_path_to_url(self):
 
327
        to_url = urlutils._win32_local_path_to_url
 
328
        self.assertEqual('file://HOST/path',
 
329
            to_url(r'\\HOST\path'))
 
330
        self.assertEqual('file://HOST/path',
 
331
            to_url('//HOST/path'))
 
332
 
 
333
        try:
 
334
            result = to_url(u'//HOST/path/to/r\xe4ksm\xf6rg\xe5s')
 
335
        except UnicodeError:
 
336
            raise TestSkipped("local encoding cannot handle unicode")
 
337
 
 
338
        self.assertEqual('file://HOST/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
 
339
 
 
340
 
278
341
    def test_win32_local_path_from_url(self):
279
342
        from_url = urlutils._win32_local_path_from_url
280
343
        self.assertEqual('C:/path/to/foo',
288
351
        # Not a valid _win32 url, no drive letter
289
352
        self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
290
353
 
291
 
    def test__win32_extract_drive_letter(self):
 
354
    def test_win32_unc_path_from_url(self):
 
355
        from_url = urlutils._win32_local_path_from_url
 
356
        self.assertEqual('//HOST/path', from_url('file://HOST/path'))
 
357
        # despite IE allows 2, 4, 5 and 6 slashes in URL to another machine
 
358
        # we want to use only 2 slashes
 
359
        # Firefox understand only 5 slashes in URL, but it's ugly
 
360
        self.assertRaises(InvalidURL, from_url, 'file:////HOST/path')
 
361
        self.assertRaises(InvalidURL, from_url, 'file://///HOST/path')
 
362
        self.assertRaises(InvalidURL, from_url, 'file://////HOST/path')
 
363
        # check for file://C:/ instead of file:///C:/
 
364
        self.assertRaises(InvalidURL, from_url, 'file://C:/path')
 
365
 
 
366
    def test_win32_extract_drive_letter(self):
292
367
        extract = urlutils._win32_extract_drive_letter
293
368
        self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
294
369
        self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
329
404
        self.assertEqual(('path/..', 'foo'), split('path/../foo'))
330
405
        self.assertEqual(('../path', 'foo'), split('../path/foo'))
331
406
 
332
 
    def test__win32_strip_local_trailing_slash(self):
 
407
    def test_win32_strip_local_trailing_slash(self):
333
408
        strip = urlutils._win32_strip_local_trailing_slash
334
409
        self.assertEqual('file://', strip('file://'))
335
410
        self.assertEqual('file:///', strip('file:///'))
479
554
        self.assertEndsWith(url, '/mytest')
480
555
 
481
556
    def test_non_ascii(self):
 
557
        if win32utils.winver == 'Windows 98':
 
558
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
 
559
 
482
560
        try:
483
561
            os.mkdir(u'dod\xe9')
484
562
        except UnicodeError:
492
570
        #   u'/dod\xe9' => '/dod\xc3\xa9'
493
571
        url = urlutils.local_path_to_url('.')
494
572
        self.assertEndsWith(url, '/dod%C3%A9')
 
573
 
 
574
 
 
575
class TestDeriveToLocation(TestCase):
 
576
    """Test that the mapping of FROM_LOCATION to TO_LOCATION works."""
 
577
 
 
578
    def test_to_locations_derived_from_paths(self):
 
579
        derive = urlutils.derive_to_location
 
580
        self.assertEqual("bar", derive("bar"))
 
581
        self.assertEqual("bar", derive("../bar"))
 
582
        self.assertEqual("bar", derive("/foo/bar"))
 
583
        self.assertEqual("bar", derive("c:/foo/bar"))
 
584
        self.assertEqual("bar", derive("c:bar"))
 
585
 
 
586
    def test_to_locations_derived_from_urls(self):
 
587
        derive = urlutils.derive_to_location
 
588
        self.assertEqual("bar", derive("http://foo/bar"))
 
589
        self.assertEqual("bar", derive("bzr+ssh://foo/bar"))
 
590
        self.assertEqual("foo-bar", derive("lp:foo-bar"))