~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_urlutils.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from bzrlib import osutils, urlutils, win32utils
24
24
import bzrlib
25
 
from bzrlib.errors import InvalidURL, InvalidURLJoin, InvalidRebaseURLs
 
25
from bzrlib.errors import InvalidURL, InvalidURLJoin
26
26
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
27
27
 
28
28
 
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
 
 
124
118
        # Normalize verifies URLs when they are not unicode
125
119
        # (indicating they did not come from the user)
126
120
        self.assertRaises(InvalidURL, normalize_url, 'http://host/\xb5')
203
197
            joined = urlutils.join(*args)
204
198
            self.assertEqual(expected, joined)
205
199
 
 
200
        # Test a single element
 
201
        test('foo', 'foo')
 
202
 
206
203
        # Test relative path joining
207
 
        test('foo', 'foo') # relative fragment with nothing is preserved.
208
204
        test('foo/bar', 'foo', 'bar')
209
205
        test('http://foo/bar', 'http://foo', 'bar')
210
206
        test('http://foo/bar', 'http://foo', '.', 'bar')
211
207
        test('http://foo/baz', 'http://foo', 'bar', '../baz')
212
208
        test('http://foo/bar/baz', 'http://foo', 'bar/baz')
213
209
        test('http://foo/baz', 'http://foo', 'bar/../baz')
214
 
        test('http://foo/baz', 'http://foo/bar/', '../baz')
215
210
 
216
211
        # Absolute paths
217
 
        test('http://foo', 'http://foo') # abs url with nothing is preserved.
218
212
        test('http://bar', 'http://foo', 'http://bar')
219
213
        test('sftp://bzr/foo', 'http://foo', 'bar', 'sftp://bzr/foo')
220
214
        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/')
224
215
 
225
216
        # From a base path
226
217
        test('file:///foo', 'file:///', 'foo')
230
221
        
231
222
        # Invalid joinings
232
223
        # Cannot go above root
233
 
        # Implicitly at root:
234
224
        self.assertRaises(InvalidURLJoin, urlutils.join,
235
225
                '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, '/', '/..')
274
226
 
275
227
    def test_function_type(self):
276
228
        if sys.platform == 'win32':
291
243
            raise TestSkipped("local encoding cannot handle unicode")
292
244
 
293
245
        self.assertEqual('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
294
 
        self.assertFalse(isinstance(result, unicode))
295
246
 
296
247
    def test_posix_local_path_from_url(self):
297
248
        from_url = urlutils._posix_local_path_from_url
316
267
        #     to_url('C:/path/to/foo '))
317
268
        self.assertEqual('file:///C:/path/to/f%20oo',
318
269
            to_url('C:/path/to/f oo'))
319
 
        
320
 
        self.assertEqual('file:///', to_url('/'))
321
270
 
322
271
        try:
323
272
            result = to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s')
325
274
            raise TestSkipped("local encoding cannot handle unicode")
326
275
 
327
276
        self.assertEqual('file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
328
 
        self.assertFalse(isinstance(result, unicode))
329
277
 
330
278
    def test_win32_unc_path_to_url(self):
331
279
        to_url = urlutils._win32_local_path_to_url
340
288
            raise TestSkipped("local encoding cannot handle unicode")
341
289
 
342
290
        self.assertEqual('file://HOST/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
343
 
        self.assertFalse(isinstance(result, unicode))
 
291
 
344
292
 
345
293
    def test_win32_local_path_from_url(self):
346
294
        from_url = urlutils._win32_local_path_from_url
350
298
            from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
351
299
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
352
300
            from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
353
 
        self.assertEqual('/', from_url('file:///'))
354
301
 
355
302
        self.assertRaises(InvalidURL, from_url, '/path/to/foo')
356
303
        # Not a valid _win32 url, no drive letter
496
443
    def test_escape(self):
497
444
        self.assertEqual('%25', urlutils.escape('%'))
498
445
        self.assertEqual('%C3%A5', urlutils.escape(u'\xe5'))
499
 
        self.assertFalse(isinstance(urlutils.escape(u'\xe5'), unicode))
500
446
 
501
447
    def test_unescape(self):
502
448
        self.assertEqual('%', urlutils.unescape('%25'))
547
493
        #test('.', 'http://host/', 'http://host')
548
494
        test('http://host', 'http://host/', 'http://host')
549
495
 
550
 
        # On Windows file:///C:/path/to and file:///D:/other/path
551
 
        # should not use relative url over the non-existent '/' directory.
552
 
        if sys.platform == 'win32':
553
 
            # on the same drive
554
 
            test('../../other/path',
555
 
                'file:///C:/path/to', 'file:///C:/other/path')
556
 
            #~next two tests is failed, i.e. urlutils.relative_url expects
557
 
            #~to see normalized file URLs?
558
 
            #~test('../../other/path',
559
 
            #~    'file:///C:/path/to', 'file:///c:/other/path')
560
 
            #~test('../../other/path',
561
 
            #~    'file:///C:/path/to', 'file:///C|/other/path')
562
 
 
563
 
            # check UNC paths too
564
 
            test('../../other/path',
565
 
                'file://HOST/base/path/to', 'file://HOST/base/other/path')
566
 
            # on different drives
567
 
            test('file:///D:/other/path',
568
 
                'file:///C:/path/to', 'file:///D:/other/path')
569
 
            # TODO: strictly saying in UNC path //HOST/base is full analog
570
 
            # of drive letter for hard disk, and this situation is also
571
 
            # should be exception from rules. [bialix 20071221]
572
 
 
573
496
 
574
497
class TestCwdToURL(TestCaseInTempDir):
575
498
    """Test that local_path_to_url works base on the cwd"""
599
522
        #   u'/dod\xe9' => '/dod\xc3\xa9'
600
523
        url = urlutils.local_path_to_url('.')
601
524
        self.assertEndsWith(url, '/dod%C3%A9')
602
 
 
603
 
 
604
 
class TestDeriveToLocation(TestCase):
605
 
    """Test that the mapping of FROM_LOCATION to TO_LOCATION works."""
606
 
 
607
 
    def test_to_locations_derived_from_paths(self):
608
 
        derive = urlutils.derive_to_location
609
 
        self.assertEqual("bar", derive("bar"))
610
 
        self.assertEqual("bar", derive("../bar"))
611
 
        self.assertEqual("bar", derive("/foo/bar"))
612
 
        self.assertEqual("bar", derive("c:/foo/bar"))
613
 
        self.assertEqual("bar", derive("c:bar"))
614
 
 
615
 
    def test_to_locations_derived_from_urls(self):
616
 
        derive = urlutils.derive_to_location
617
 
        self.assertEqual("bar", derive("http://foo/bar"))
618
 
        self.assertEqual("bar", derive("bzr+ssh://foo/bar"))
619
 
        self.assertEqual("foo-bar", derive("lp:foo-bar"))
620
 
 
621
 
 
622
 
class TestRebaseURL(TestCase):
623
 
    """Test the behavior of rebase_url."""
624
 
 
625
 
    def test_non_relative(self):
626
 
        result = urlutils.rebase_url('file://foo', 'file://foo',
627
 
                                     'file://foo/bar')
628
 
        self.assertEqual('file://foo', result)
629
 
        result = urlutils.rebase_url('/foo', 'file://foo',
630
 
                                     'file://foo/bar')
631
 
        self.assertEqual('/foo', result)
632
 
 
633
 
    def test_different_ports(self):
634
 
        e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
635
 
                              'foo', 'http://bar:80', 'http://bar:81')
636
 
        self.assertEqual(str(e), "URLs differ by more than path:"
637
 
                         " 'http://bar:80' and 'http://bar:81'")
638
 
 
639
 
    def test_different_hosts(self):
640
 
        e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
641
 
                              'foo', 'http://bar', 'http://baz')
642
 
        self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
643
 
                         " and 'http://baz'")
644
 
 
645
 
    def test_different_protocol(self):
646
 
        e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
647
 
                              'foo', 'http://bar', 'ftp://bar')
648
 
        self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
649
 
                         " and 'ftp://bar'")
650
 
 
651
 
    def test_rebase_success(self):
652
 
        self.assertEqual('../bar', urlutils.rebase_url('bar', 'http://baz/',
653
 
                         'http://baz/qux'))
654
 
        self.assertEqual('qux/bar', urlutils.rebase_url('bar',
655
 
                         'http://baz/qux', 'http://baz/'))
656
 
        self.assertEqual('.', urlutils.rebase_url('foo',
657
 
                         'http://bar/', 'http://bar/foo/'))
658
 
        self.assertEqual('qux/bar', urlutils.rebase_url('../bar',
659
 
                         'http://baz/qux/foo', 'http://baz/'))
660
 
 
661
 
    def test_determine_relative_path(self):
662
 
        self.assertEqual('../../baz/bar',
663
 
                         urlutils.determine_relative_path(
664
 
                         '/qux/quxx', '/baz/bar'))
665
 
        self.assertEqual('..',
666
 
                         urlutils.determine_relative_path(
667
 
                         '/bar/baz', '/bar'))
668
 
        self.assertEqual('baz',
669
 
                         urlutils.determine_relative_path(
670
 
                         '/bar', '/bar/baz'))
671
 
        self.assertEqual('.', urlutils.determine_relative_path(
672
 
                         '/bar', '/bar'))