/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 breezy/tests/test_urlutils.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2015, 2016 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
19
19
import os
20
20
import sys
21
21
 
22
 
from bzrlib import osutils, urlutils, win32utils
23
 
from bzrlib.errors import InvalidURL, InvalidURLJoin, InvalidRebaseURLs
24
 
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
 
22
from .. import osutils, urlutils, win32utils
 
23
from ..errors import (
 
24
    PathNotChild,
 
25
    )
 
26
from ..sixish import text_type
 
27
from . import features, TestCaseInTempDir, TestCase, TestSkipped
25
28
 
26
29
 
27
30
class TestUrlToPath(TestCase):
28
31
 
29
32
    def test_basename(self):
30
 
        # bzrlib.urlutils.basename
31
 
        # Test bzrlib.urlutils.split()
 
33
        # breezy.urlutils.basename
 
34
        # Test breezy.urlutils.split()
32
35
        basename = urlutils.basename
33
36
        if sys.platform == 'win32':
34
 
            self.assertRaises(InvalidURL, basename, 'file:///path/to/foo')
 
37
            self.assertRaises(urlutils.InvalidURL, basename,
 
38
                    'file:///path/to/foo')
35
39
            self.assertEqual('foo', basename('file:///C|/foo'))
36
40
            self.assertEqual('foo', basename('file:///C:/foo'))
37
41
            self.assertEqual('', basename('file:///C:/'))
121
125
 
122
126
        # Normalize verifies URLs when they are not unicode
123
127
        # (indicating they did not come from the user)
124
 
        self.assertRaises(InvalidURL, normalize_url, 'http://host/\xb5')
125
 
        self.assertRaises(InvalidURL, normalize_url, 'http://host/ ')
 
128
        self.assertRaises(urlutils.InvalidURL, normalize_url,
 
129
                'http://host/\xb5')
 
130
        self.assertRaises(urlutils.InvalidURL, normalize_url, 'http://host/ ')
126
131
 
127
132
    def test_url_scheme_re(self):
128
133
        # Test paths that may be URLs
156
161
        # Weird stuff
157
162
        # Can't have slashes or colons in the scheme
158
163
        test_one('/path/to/://foo', None)
159
 
        test_one('path:path://foo', None)
 
164
        test_one('scheme:stuff://foo', ('scheme', 'stuff://foo'))
160
165
        # Must have more than one character for scheme
161
166
        test_one('C://foo', None)
162
167
        test_one('ab://foo', ('ab', 'foo'))
163
168
 
164
169
    def test_dirname(self):
165
 
        # Test bzrlib.urlutils.dirname()
 
170
        # Test breezy.urlutils.dirname()
166
171
        dirname = urlutils.dirname
167
172
        if sys.platform == 'win32':
168
 
            self.assertRaises(InvalidURL, dirname, 'file:///path/to/foo')
 
173
            self.assertRaises(urlutils.InvalidURL, dirname,
 
174
                'file:///path/to/foo')
169
175
            self.assertEqual('file:///C|/', dirname('file:///C|/foo'))
170
176
            self.assertEqual('file:///C|/', dirname('file:///C|/'))
171
177
        else:
195
201
            dirname('path/to/foo/', exclude_trailing_slash=False))
196
202
        self.assertEqual('path/..', dirname('path/../foo'))
197
203
        self.assertEqual('../path', dirname('../path/foo'))
 
204
    
 
205
    def test_is_url(self):
 
206
        self.assertTrue(urlutils.is_url('http://foo/bar'))
 
207
        self.assertTrue(urlutils.is_url('bzr+ssh://foo/bar'))
 
208
        self.assertTrue(urlutils.is_url('lp:foo/bar'))
 
209
        self.assertTrue(urlutils.is_url('file:///foo/bar'))
 
210
        self.assertFalse(urlutils.is_url(''))
 
211
        self.assertFalse(urlutils.is_url('foo'))
 
212
        self.assertFalse(urlutils.is_url('foo/bar'))
 
213
        self.assertFalse(urlutils.is_url('/foo'))
 
214
        self.assertFalse(urlutils.is_url('/foo/bar'))
 
215
        self.assertFalse(urlutils.is_url('C:/'))
 
216
        self.assertFalse(urlutils.is_url('C:/foo'))
 
217
        self.assertFalse(urlutils.is_url('C:/foo/bar'))
198
218
 
199
219
    def test_join(self):
200
220
        def test(expected, *args):
210
230
        test('http://foo/bar/baz', 'http://foo', 'bar/baz')
211
231
        test('http://foo/baz', 'http://foo', 'bar/../baz')
212
232
        test('http://foo/baz', 'http://foo/bar/', '../baz')
 
233
        test('lp:foo/bar', 'lp:foo', 'bar')
 
234
        test('lp:foo/bar/baz', 'lp:foo', 'bar/baz')
213
235
 
214
236
        # Absolute paths
215
237
        test('http://foo', 'http://foo') # abs url with nothing is preserved.
219
241
        test('http://bar/', 'http://foo', 'http://bar/')
220
242
        test('http://bar/a', 'http://foo', 'http://bar/a')
221
243
        test('http://bar/a/', 'http://foo', 'http://bar/a/')
 
244
        test('lp:bar', 'http://foo', 'lp:bar')
 
245
        test('lp:bar', 'lp:foo', 'lp:bar')
 
246
        test('file:///stuff', 'lp:foo', 'file:///stuff')
222
247
 
223
248
        # From a base path
224
249
        test('file:///foo', 'file:///', 'foo')
229
254
        # Invalid joinings
230
255
        # Cannot go above root
231
256
        # Implicitly at root:
232
 
        self.assertRaises(InvalidURLJoin, urlutils.join,
 
257
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.join,
233
258
                'http://foo', '../baz')
234
 
        self.assertRaises(InvalidURLJoin, urlutils.join,
 
259
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.join,
235
260
                'http://foo', '/..')
236
261
        # Joining from a path explicitly under the root.
237
 
        self.assertRaises(InvalidURLJoin, urlutils.join,
 
262
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.join,
238
263
                'http://foo/a', '../../b')
239
264
 
240
265
    def test_joinpath(self):
266
291
 
267
292
        # Invalid joinings
268
293
        # Cannot go above root
269
 
        self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '../baz')
270
 
        self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '..')
271
 
        self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '/..')
 
294
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
 
295
                '../baz')
 
296
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
 
297
                '..')
 
298
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
 
299
                '/..')
 
300
 
 
301
    def test_join_segment_parameters_raw(self):
 
302
        join_segment_parameters_raw = urlutils.join_segment_parameters_raw
 
303
        self.assertEqual("/somedir/path", 
 
304
            join_segment_parameters_raw("/somedir/path"))
 
305
        self.assertEqual("/somedir/path,rawdata", 
 
306
            join_segment_parameters_raw("/somedir/path", "rawdata"))
 
307
        self.assertRaises(urlutils.InvalidURLJoin,
 
308
            join_segment_parameters_raw, "/somedir/path",
 
309
                "rawdata1,rawdata2,rawdata3")
 
310
        self.assertEqual("/somedir/path,bla,bar",
 
311
            join_segment_parameters_raw("/somedir/path", "bla", "bar"))
 
312
        self.assertEqual("/somedir,exist=some/path,bla,bar",
 
313
            join_segment_parameters_raw("/somedir,exist=some/path",
 
314
                "bla", "bar"))
 
315
        self.assertRaises(TypeError, join_segment_parameters_raw, 
 
316
            "/somepath", 42)
 
317
 
 
318
    def test_join_segment_parameters(self):
 
319
        join_segment_parameters = urlutils.join_segment_parameters
 
320
        self.assertEqual("/somedir/path", 
 
321
            join_segment_parameters("/somedir/path", {}))
 
322
        self.assertEqual("/somedir/path,key1=val1", 
 
323
            join_segment_parameters("/somedir/path", {"key1": "val1"}))
 
324
        self.assertRaises(urlutils.InvalidURLJoin,
 
325
            join_segment_parameters, "/somedir/path",
 
326
            {"branch": "brr,brr,brr"})
 
327
        self.assertRaises(urlutils.InvalidURLJoin,
 
328
            join_segment_parameters, "/somedir/path", {"key1=val1": "val2"})
 
329
        self.assertEqual("/somedir/path,key1=val1,key2=val2",
 
330
            join_segment_parameters("/somedir/path", {
 
331
                "key1": "val1", "key2": "val2"}))
 
332
        self.assertEqual("/somedir/path,key1=val1,key2=val2",
 
333
            join_segment_parameters("/somedir/path,key1=val1", {
 
334
                "key2": "val2"}))
 
335
        self.assertEqual("/somedir/path,key1=val2",
 
336
            join_segment_parameters("/somedir/path,key1=val1", {
 
337
                "key1": "val2"}))
 
338
        self.assertEqual("/somedir,exist=some/path,key1=val1",
 
339
            join_segment_parameters("/somedir,exist=some/path",
 
340
                {"key1": "val1"}))
 
341
        self.assertEqual("/,key1=val1,key2=val2",
 
342
            join_segment_parameters("/,key1=val1", {"key2": "val2"}))
 
343
        self.assertRaises(TypeError,
 
344
            join_segment_parameters, "/,key1=val1", {"foo": 42})
272
345
 
273
346
    def test_function_type(self):
274
347
        if sys.platform == 'win32':
275
 
            self.assertEqual(urlutils._win32_local_path_to_url, urlutils.local_path_to_url)
276
 
            self.assertEqual(urlutils._win32_local_path_from_url, urlutils.local_path_from_url)
 
348
            self.assertEqual(urlutils._win32_local_path_to_url,
 
349
                urlutils.local_path_to_url)
 
350
            self.assertEqual(urlutils._win32_local_path_from_url,
 
351
                urlutils.local_path_from_url)
277
352
        else:
278
 
            self.assertEqual(urlutils._posix_local_path_to_url, urlutils.local_path_to_url)
279
 
            self.assertEqual(urlutils._posix_local_path_from_url, urlutils.local_path_from_url)
 
353
            self.assertEqual(urlutils._posix_local_path_to_url,
 
354
                urlutils.local_path_to_url)
 
355
            self.assertEqual(urlutils._posix_local_path_from_url,
 
356
                urlutils.local_path_from_url)
280
357
 
281
358
    def test_posix_local_path_to_url(self):
282
359
        to_url = urlutils._posix_local_path_to_url
283
360
        self.assertEqual('file:///path/to/foo',
284
361
            to_url('/path/to/foo'))
285
362
 
 
363
        self.assertEqual('file:///path/to/foo%2Cbar',
 
364
            to_url('/path/to/foo,bar'))
 
365
 
286
366
        try:
287
367
            result = to_url(u'/path/to/r\xe4ksm\xf6rg\xe5s')
288
368
        except UnicodeError:
289
369
            raise TestSkipped("local encoding cannot handle unicode")
290
370
 
291
371
        self.assertEqual('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
292
 
        self.assertFalse(isinstance(result, unicode))
 
372
        self.assertTrue(isinstance(result, str))
293
373
 
294
374
    def test_posix_local_path_from_url(self):
295
375
        from_url = urlutils._posix_local_path_from_url
296
376
        self.assertEqual('/path/to/foo',
297
377
            from_url('file:///path/to/foo'))
 
378
        self.assertEqual('/path/to/foo',
 
379
            from_url('file:///path/to/foo,branch=foo'))
298
380
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
299
381
            from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
300
382
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
302
384
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
303
385
            from_url('file://localhost/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
304
386
 
305
 
        self.assertRaises(InvalidURL, from_url, '/path/to/foo')
 
387
        self.assertRaises(urlutils.InvalidURL, from_url, '/path/to/foo')
306
388
        self.assertRaises(
307
 
            InvalidURL, from_url,
 
389
            urlutils.InvalidURL, from_url,
308
390
            'file://remotehost/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s')
309
391
 
310
392
    def test_win32_local_path_to_url(self):
322
404
 
323
405
        self.assertEqual('file:///', to_url('/'))
324
406
 
 
407
        self.assertEqual('file:///C:/path/to/foo%2Cbar',
 
408
            to_url('C:/path/to/foo,bar'))
325
409
        try:
326
410
            result = to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s')
327
411
        except UnicodeError:
328
412
            raise TestSkipped("local encoding cannot handle unicode")
329
413
 
330
414
        self.assertEqual('file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
331
 
        self.assertFalse(isinstance(result, unicode))
 
415
        self.assertFalse(isinstance(result, text_type))
332
416
 
333
417
    def test_win32_unc_path_to_url(self):
 
418
        self.requireFeature(features.win32_feature)
334
419
        to_url = urlutils._win32_local_path_to_url
335
420
        self.assertEqual('file://HOST/path',
336
421
            to_url(r'\\HOST\path'))
343
428
            raise TestSkipped("local encoding cannot handle unicode")
344
429
 
345
430
        self.assertEqual('file://HOST/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
346
 
        self.assertFalse(isinstance(result, unicode))
 
431
        self.assertFalse(isinstance(result, text_type))
347
432
 
348
433
    def test_win32_local_path_from_url(self):
349
434
        from_url = urlutils._win32_local_path_from_url
354
439
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
355
440
            from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
356
441
        self.assertEqual('/', from_url('file:///'))
 
442
        self.assertEqual('C:/path/to/foo',
 
443
            from_url('file:///C|/path/to/foo,branch=foo'))
357
444
 
358
 
        self.assertRaises(InvalidURL, from_url, '/path/to/foo')
 
445
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:///C:')
 
446
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:///c')
 
447
        self.assertRaises(urlutils.InvalidURL, from_url, '/path/to/foo')
359
448
        # Not a valid _win32 url, no drive letter
360
 
        self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
 
449
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:///path/to/foo')
361
450
 
362
451
    def test_win32_unc_path_from_url(self):
363
452
        from_url = urlutils._win32_local_path_from_url
364
453
        self.assertEqual('//HOST/path', from_url('file://HOST/path'))
 
454
        self.assertEqual('//HOST/path',
 
455
            from_url('file://HOST/path,branch=foo'))
365
456
        # despite IE allows 2, 4, 5 and 6 slashes in URL to another machine
366
457
        # we want to use only 2 slashes
367
458
        # Firefox understand only 5 slashes in URL, but it's ugly
368
 
        self.assertRaises(InvalidURL, from_url, 'file:////HOST/path')
369
 
        self.assertRaises(InvalidURL, from_url, 'file://///HOST/path')
370
 
        self.assertRaises(InvalidURL, from_url, 'file://////HOST/path')
 
459
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:////HOST/path')
 
460
        self.assertRaises(urlutils.InvalidURL, from_url, 'file://///HOST/path')
 
461
        self.assertRaises(urlutils.InvalidURL, from_url, 'file://////HOST/path')
371
462
        # check for file://C:/ instead of file:///C:/
372
 
        self.assertRaises(InvalidURL, from_url, 'file://C:/path')
 
463
        self.assertRaises(urlutils.InvalidURL, from_url, 'file://C:/path')
373
464
 
374
465
    def test_win32_extract_drive_letter(self):
375
466
        extract = urlutils._win32_extract_drive_letter
376
467
        self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
377
468
        self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
378
 
        self.assertRaises(InvalidURL, extract, 'file://', '/path')
 
469
        self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/path')
 
470
        # Root drives without slash treated as invalid, see bug #841322
 
471
        self.assertEqual(('file:///C:', '/'), extract('file://', '/C:/'))
 
472
        self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/C:')
 
473
        # Invalid without drive separator or following forward slash
 
474
        self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/C')
 
475
        self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/C:ool')
379
476
 
380
477
    def test_split(self):
381
 
        # Test bzrlib.urlutils.split()
 
478
        # Test breezy.urlutils.split()
382
479
        split = urlutils.split
383
480
        if sys.platform == 'win32':
384
 
            self.assertRaises(InvalidURL, split, 'file:///path/to/foo')
 
481
            self.assertRaises(urlutils.InvalidURL, split, 'file:///path/to/foo')
385
482
            self.assertEqual(('file:///C|/', 'foo'), split('file:///C|/foo'))
386
483
            self.assertEqual(('file:///C:/', ''), split('file:///C:/'))
387
484
        else:
412
509
        self.assertEqual(('path/..', 'foo'), split('path/../foo'))
413
510
        self.assertEqual(('../path', 'foo'), split('../path/foo'))
414
511
 
 
512
    def test_split_segment_parameters_raw(self):
 
513
        split_segment_parameters_raw = urlutils.split_segment_parameters_raw
 
514
        # Check relative references with absolute paths
 
515
        self.assertEqual(("/some/path", []),
 
516
            split_segment_parameters_raw("/some/path"))
 
517
        self.assertEqual(("/some/path", ["tip"]),
 
518
            split_segment_parameters_raw("/some/path,tip"))
 
519
        self.assertEqual(("/some,dir/path", ["tip"]),
 
520
            split_segment_parameters_raw("/some,dir/path,tip"))
 
521
        self.assertEqual(("/somedir/path", ["heads%2Ftip"]),
 
522
            split_segment_parameters_raw("/somedir/path,heads%2Ftip"))
 
523
        self.assertEqual(("/somedir/path", ["heads%2Ftip", "bar"]),
 
524
            split_segment_parameters_raw("/somedir/path,heads%2Ftip,bar"))
 
525
        # Check relative references with relative paths
 
526
        self.assertEqual(("", ["key1=val1"]),
 
527
            split_segment_parameters_raw(",key1=val1"))
 
528
        self.assertEqual(("foo/", ["key1=val1"]),
 
529
            split_segment_parameters_raw("foo/,key1=val1"))
 
530
        self.assertEqual(("foo", ["key1=val1"]),
 
531
            split_segment_parameters_raw("foo,key1=val1"))
 
532
        self.assertEqual(("foo/base,la=bla/other/elements", []),
 
533
            split_segment_parameters_raw("foo/base,la=bla/other/elements"))
 
534
        self.assertEqual(("foo/base,la=bla/other/elements", ["a=b"]),
 
535
            split_segment_parameters_raw("foo/base,la=bla/other/elements,a=b"))
 
536
        # TODO: Check full URLs as well as relative references
 
537
 
 
538
    def test_split_segment_parameters(self):
 
539
        split_segment_parameters = urlutils.split_segment_parameters
 
540
        # Check relative references with absolute paths
 
541
        self.assertEqual(("/some/path", {}),
 
542
            split_segment_parameters("/some/path"))
 
543
        self.assertEqual(("/some/path", {"branch": "tip"}),
 
544
            split_segment_parameters("/some/path,branch=tip"))
 
545
        self.assertEqual(("/some,dir/path", {"branch": "tip"}),
 
546
            split_segment_parameters("/some,dir/path,branch=tip"))
 
547
        self.assertEqual(("/somedir/path", {"ref": "heads%2Ftip"}),
 
548
            split_segment_parameters("/somedir/path,ref=heads%2Ftip"))
 
549
        self.assertEqual(("/somedir/path",
 
550
            {"ref": "heads%2Ftip", "key1": "val1"}),
 
551
            split_segment_parameters(
 
552
                "/somedir/path,ref=heads%2Ftip,key1=val1"))
 
553
        self.assertEqual(("/somedir/path", {"ref": "heads%2F=tip"}),
 
554
            split_segment_parameters("/somedir/path,ref=heads%2F=tip"))
 
555
        # Check relative references with relative paths
 
556
        self.assertEqual(("", {"key1": "val1"}),
 
557
            split_segment_parameters(",key1=val1"))
 
558
        self.assertEqual(("foo/", {"key1": "val1"}),
 
559
            split_segment_parameters("foo/,key1=val1"))
 
560
        self.assertEqual(("foo/base,key1=val1/other/elements", {}),
 
561
            split_segment_parameters("foo/base,key1=val1/other/elements"))
 
562
        self.assertEqual(("foo/base,key1=val1/other/elements",
 
563
            {"key2": "val2"}), split_segment_parameters(
 
564
                "foo/base,key1=val1/other/elements,key2=val2"))
 
565
        # TODO: Check full URLs as well as relative references
 
566
 
415
567
    def test_win32_strip_local_trailing_slash(self):
416
568
        strip = urlutils._win32_strip_local_trailing_slash
417
569
        self.assertEqual('file://', strip('file://'))
459
611
        # Test that URLs are converted to nice unicode strings for display
460
612
        def test(expected, url, encoding='utf-8'):
461
613
            disp_url = urlutils.unescape_for_display(url, encoding=encoding)
462
 
            self.assertIsInstance(disp_url, unicode)
 
614
            self.assertIsInstance(disp_url, text_type)
463
615
            self.assertEqual(expected, disp_url)
464
616
 
465
617
        test('http://foo', 'http://foo')
499
651
    def test_escape(self):
500
652
        self.assertEqual('%25', urlutils.escape('%'))
501
653
        self.assertEqual('%C3%A5', urlutils.escape(u'\xe5'))
502
 
        self.assertFalse(isinstance(urlutils.escape(u'\xe5'), unicode))
 
654
        self.assertFalse(isinstance(urlutils.escape(u'\xe5'), text_type))
503
655
 
504
656
    def test_escape_tildes(self):
505
657
        self.assertEqual('~foo', urlutils.escape('~foo'))
508
660
        self.assertEqual('%', urlutils.unescape('%25'))
509
661
        self.assertEqual(u'\xe5', urlutils.unescape('%C3%A5'))
510
662
 
511
 
        self.assertRaises(InvalidURL, urlutils.unescape, u'\xe5')
512
 
        self.assertRaises(InvalidURL, urlutils.unescape, '\xe5')
513
 
        self.assertRaises(InvalidURL, urlutils.unescape, '%E5')
 
663
        self.assertRaises(urlutils.InvalidURL, urlutils.unescape, u'\xe5')
 
664
        self.assertRaises(urlutils.InvalidURL, urlutils.unescape, '\xe5')
 
665
        self.assertRaises(urlutils.InvalidURL, urlutils.unescape, '%E5')
514
666
 
515
667
    def test_escape_unescape(self):
516
668
        self.assertEqual(u'\xe5', urlutils.unescape(urlutils.escape(u'\xe5')))
578
730
 
579
731
 
580
732
class TestCwdToURL(TestCaseInTempDir):
581
 
    """Test that local_path_to_url works base on the cwd"""
 
733
    """Test that local_path_to_url works based on the cwd"""
582
734
 
583
735
    def test_dot(self):
584
736
        # This test will fail if getcwd is not ascii
589
741
        self.assertEndsWith(url, '/mytest')
590
742
 
591
743
    def test_non_ascii(self):
592
 
        if win32utils.winver == 'Windows 98':
593
 
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
594
 
 
595
744
        try:
596
745
            os.mkdir(u'dod\xe9')
597
746
        except UnicodeError:
637
786
        self.assertEqual('/foo', result)
638
787
 
639
788
    def test_different_ports(self):
640
 
        e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
 
789
        e = self.assertRaises(urlutils.InvalidRebaseURLs, urlutils.rebase_url,
641
790
                              'foo', 'http://bar:80', 'http://bar:81')
642
791
        self.assertEqual(str(e), "URLs differ by more than path:"
643
792
                         " 'http://bar:80' and 'http://bar:81'")
644
793
 
645
794
    def test_different_hosts(self):
646
 
        e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
 
795
        e = self.assertRaises(urlutils.InvalidRebaseURLs, urlutils.rebase_url,
647
796
                              'foo', 'http://bar', 'http://baz')
648
797
        self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
649
798
                         " and 'http://baz'")
650
799
 
651
800
    def test_different_protocol(self):
652
 
        e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
 
801
        e = self.assertRaises(urlutils.InvalidRebaseURLs, urlutils.rebase_url,
653
802
                              'foo', 'http://bar', 'ftp://bar')
654
803
        self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
655
804
                         " and 'ftp://bar'")
680
829
 
681
830
class TestParseURL(TestCase):
682
831
 
683
 
    def test_parse_url(self):
684
 
        self.assertEqual(urlutils.parse_url('http://example.com:80/one'),
685
 
            ('http', None, None, 'example.com', 80, '/one'))
686
 
        self.assertEqual(urlutils.parse_url('http://[1:2:3::40]/one'),
687
 
                ('http', None, None, '1:2:3::40', None, '/one'))
688
 
        self.assertEqual(urlutils.parse_url('http://[1:2:3::40]:80/one'),
689
 
                ('http', None, None, '1:2:3::40', 80, '/one'))
 
832
    def test_parse_simple(self):
 
833
        parsed = urlutils.parse_url('http://example.com:80/one')
 
834
        self.assertEqual(('http', None, None, 'example.com', 80, '/one'),
 
835
            parsed)
 
836
 
 
837
    def test_ipv6(self):
 
838
        parsed = urlutils.parse_url('http://[1:2:3::40]/one')
 
839
        self.assertEqual(('http', None, None, '1:2:3::40', None, '/one'),
 
840
            parsed)
 
841
 
 
842
    def test_ipv6_port(self):
 
843
        parsed = urlutils.parse_url('http://[1:2:3::40]:80/one')
 
844
        self.assertEqual(('http', None, None, '1:2:3::40', 80, '/one'),
 
845
            parsed)
 
846
 
 
847
 
 
848
class TestURL(TestCase):
 
849
 
 
850
    def test_parse_simple(self):
 
851
        parsed = urlutils.URL.from_string('http://example.com:80/one')
 
852
        self.assertEqual('http', parsed.scheme)
 
853
        self.assertIs(None, parsed.user)
 
854
        self.assertIs(None, parsed.password)
 
855
        self.assertEqual('example.com', parsed.host)
 
856
        self.assertEqual(80, parsed.port)
 
857
        self.assertEqual('/one', parsed.path)
 
858
 
 
859
    def test_ipv6(self):
 
860
        parsed = urlutils.URL.from_string('http://[1:2:3::40]/one')
 
861
        self.assertEqual('http', parsed.scheme)
 
862
        self.assertIs(None, parsed.port)
 
863
        self.assertIs(None, parsed.user)
 
864
        self.assertIs(None, parsed.password)
 
865
        self.assertEqual('1:2:3::40', parsed.host)
 
866
        self.assertEqual('/one', parsed.path)
 
867
 
 
868
    def test_ipv6_port(self):
 
869
        parsed = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
 
870
        self.assertEqual('http', parsed.scheme)
 
871
        self.assertEqual('1:2:3::40', parsed.host)
 
872
        self.assertIs(None, parsed.user)
 
873
        self.assertIs(None, parsed.password)
 
874
        self.assertEqual(80, parsed.port)
 
875
        self.assertEqual('/one', parsed.path)
 
876
 
 
877
    def test_quoted(self):
 
878
        parsed = urlutils.URL.from_string(
 
879
            'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
 
880
        self.assertEqual(parsed.quoted_host, 'ex%41mple.com')
 
881
        self.assertEqual(parsed.host, 'exAmple.com')
 
882
        self.assertEqual(parsed.port, 2222)
 
883
        self.assertEqual(parsed.quoted_user, 'ro%62ey')
 
884
        self.assertEqual(parsed.user, 'robey')
 
885
        self.assertEqual(parsed.quoted_password, 'h%40t')
 
886
        self.assertEqual(parsed.password, 'h@t')
 
887
        self.assertEqual(parsed.path, '/path')
 
888
 
 
889
    def test_eq(self):
 
890
        parsed1 = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
 
891
        parsed2 = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
 
892
        self.assertEqual(parsed1, parsed2)
 
893
        self.assertEqual(parsed1, parsed1)
 
894
        parsed2.path = '/two'
 
895
        self.assertNotEqual(parsed1, parsed2)
 
896
 
 
897
    def test_repr(self):
 
898
        parsed = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
 
899
        self.assertEqual(
 
900
            "<URL('http', None, None, '1:2:3::40', 80, '/one')>",
 
901
            repr(parsed))
 
902
 
 
903
    def test_str(self):
 
904
        parsed = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
 
905
        self.assertEqual('http://[1:2:3::40]:80/one', str(parsed))
 
906
 
 
907
    def test__combine_paths(self):
 
908
        combine = urlutils.URL._combine_paths
 
909
        self.assertEqual('/home/sarah/project/foo',
 
910
                         combine('/home/sarah', 'project/foo'))
 
911
        self.assertEqual('/etc',
 
912
                         combine('/home/sarah', '../../etc'))
 
913
        self.assertEqual('/etc',
 
914
                         combine('/home/sarah', '../../../etc'))
 
915
        self.assertEqual('/etc',
 
916
                         combine('/home/sarah', '/etc'))
 
917
 
 
918
    def test_clone(self):
 
919
        url = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
 
920
        url1 = url.clone("two")
 
921
        self.assertEqual("/one/two", url1.path)
 
922
        url2 = url.clone("/two")
 
923
        self.assertEqual("/two", url2.path)
 
924
        url3 = url.clone()
 
925
        self.assertIsNot(url, url3)
 
926
        self.assertEqual(url, url3)
 
927
 
 
928
 
 
929
class TestFileRelpath(TestCase):
 
930
 
 
931
    # GZ 2011-11-18: A way to override all path handling functions to one
 
932
    #                platform or another for testing would be nice.
 
933
 
 
934
    def _with_posix_paths(self):
 
935
        self.overrideAttr(urlutils, "local_path_from_url",
 
936
            urlutils._posix_local_path_from_url)
 
937
        self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH", len("file:///"))
 
938
        self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
 
939
        self.overrideAttr(osutils, "abspath", osutils._posix_abspath)
 
940
        self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
 
941
        self.overrideAttr(osutils, "pathjoin", osutils.posixpath.join)
 
942
        self.overrideAttr(osutils, "split", osutils.posixpath.split)
 
943
        self.overrideAttr(osutils, "MIN_ABS_PATHLENGTH", 1)
 
944
 
 
945
    def _with_win32_paths(self):
 
946
        self.overrideAttr(urlutils, "local_path_from_url",
 
947
            urlutils._win32_local_path_from_url)
 
948
        self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH",
 
949
            urlutils.WIN32_MIN_ABS_FILEURL_LENGTH)
 
950
        self.overrideAttr(osutils, "abspath", osutils._win32_abspath)
 
951
        self.overrideAttr(osutils, "normpath", osutils._win32_normpath)
 
952
        self.overrideAttr(osutils, "pathjoin", osutils._win32_pathjoin)
 
953
        self.overrideAttr(osutils, "split", osutils.ntpath.split)
 
954
        self.overrideAttr(osutils, "MIN_ABS_PATHLENGTH", 3)
 
955
 
 
956
    def test_same_url_posix(self):
 
957
        self._with_posix_paths()
 
958
        self.assertEqual("",
 
959
            urlutils.file_relpath("file:///a", "file:///a"))
 
960
        self.assertEqual("",
 
961
            urlutils.file_relpath("file:///a", "file:///a/"))
 
962
        self.assertEqual("",
 
963
            urlutils.file_relpath("file:///a/", "file:///a"))
 
964
 
 
965
    def test_same_url_win32(self):
 
966
        self._with_win32_paths()
 
967
        self.assertEqual("",
 
968
            urlutils.file_relpath("file:///A:/", "file:///A:/"))
 
969
        self.assertEqual("",
 
970
            urlutils.file_relpath("file:///A|/", "file:///A:/"))
 
971
        self.assertEqual("",
 
972
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b/"))
 
973
        self.assertEqual("",
 
974
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/"))
 
975
        self.assertEqual("",
 
976
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b"))
 
977
 
 
978
    def test_child_posix(self):
 
979
        self._with_posix_paths()
 
980
        self.assertEqual("b",
 
981
            urlutils.file_relpath("file:///a", "file:///a/b"))
 
982
        self.assertEqual("b",
 
983
            urlutils.file_relpath("file:///a/", "file:///a/b"))
 
984
        self.assertEqual("b/c",
 
985
            urlutils.file_relpath("file:///a", "file:///a/b/c"))
 
986
 
 
987
    def test_child_win32(self):
 
988
        self._with_win32_paths()
 
989
        self.assertEqual("b",
 
990
            urlutils.file_relpath("file:///A:/", "file:///A:/b"))
 
991
        self.assertEqual("b",
 
992
            urlutils.file_relpath("file:///A|/", "file:///A:/b"))
 
993
        self.assertEqual("c",
 
994
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/c"))
 
995
        self.assertEqual("c",
 
996
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b/c"))
 
997
        self.assertEqual("c/d",
 
998
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/c/d"))
 
999
 
 
1000
    def test_sibling_posix(self):
 
1001
        self._with_posix_paths()
 
1002
        self.assertRaises(PathNotChild,
 
1003
            urlutils.file_relpath, "file:///a/b", "file:///a/c")
 
1004
        self.assertRaises(PathNotChild,
 
1005
            urlutils.file_relpath, "file:///a/b/", "file:///a/c")
 
1006
        self.assertRaises(PathNotChild,
 
1007
            urlutils.file_relpath, "file:///a/b/", "file:///a/c/")
 
1008
 
 
1009
    def test_sibling_win32(self):
 
1010
        self._with_win32_paths()
 
1011
        self.assertRaises(PathNotChild,
 
1012
            urlutils.file_relpath, "file:///A:/b", "file:///A:/c")
 
1013
        self.assertRaises(PathNotChild,
 
1014
            urlutils.file_relpath, "file:///A:/b/", "file:///A:/c")
 
1015
        self.assertRaises(PathNotChild,
 
1016
            urlutils.file_relpath, "file:///A:/b/", "file:///A:/c/")
 
1017
 
 
1018
    def test_parent_posix(self):
 
1019
        self._with_posix_paths()
 
1020
        self.assertRaises(PathNotChild,
 
1021
            urlutils.file_relpath, "file:///a/b", "file:///a")
 
1022
        self.assertRaises(PathNotChild,
 
1023
            urlutils.file_relpath, "file:///a/b", "file:///a/")
 
1024
 
 
1025
    def test_parent_win32(self):
 
1026
        self._with_win32_paths()
 
1027
        self.assertRaises(PathNotChild,
 
1028
            urlutils.file_relpath, "file:///A:/b", "file:///A:/")
 
1029
        self.assertRaises(PathNotChild,
 
1030
            urlutils.file_relpath, "file:///A:/b/c", "file:///A:/b")
 
1031
 
 
1032
 
 
1033
class QuoteTests(TestCase):
 
1034
 
 
1035
    def test_quote(self):
 
1036
        self.assertEqual('abc%20def', urlutils.quote('abc def'))
 
1037
        self.assertEqual('abc%2Fdef', urlutils.quote('abc/def', safe=''))
 
1038
        self.assertEqual('abc/def', urlutils.quote('abc/def', safe='/'))
 
1039
 
 
1040
    def test_quote_tildes(self):
 
1041
        self.assertEqual('%7Efoo', urlutils.quote('~foo'))
 
1042
        self.assertEqual('~foo', urlutils.quote('~foo', safe='/~'))
 
1043
 
 
1044
    def test_unquote(self):
 
1045
        self.assertEqual('%', urlutils.unquote('%25'))
 
1046
        self.assertEqual('\xc3\xa5', urlutils.unquote('%C3%A5'))
 
1047
        self.assertEqual(u"\xe5", urlutils.unquote(u'\xe5'))