~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_urlutils.py

  • Committer: Andrew Starr-Bochicchio
  • Date: 2014-03-30 17:59:29 UTC
  • mto: This revision was merged to the branch mainline in revision 6592.
  • Revision ID: a.starr.b@gmail.com-20140330175929-rd97jstcbau2j1gy
Use LooseVersion from distutils to check Cython version in order to handle non-integers in the version string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2012 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 sys
21
21
 
22
22
from bzrlib import osutils, urlutils, win32utils
23
 
from bzrlib.errors import InvalidURL, InvalidURLJoin, InvalidRebaseURLs
 
23
from bzrlib.errors import (
 
24
    InvalidURL,
 
25
    InvalidURLJoin,
 
26
    InvalidRebaseURLs,
 
27
    PathNotChild,
 
28
    )
24
29
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
25
30
 
26
31
 
457
462
        self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
458
463
        self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
459
464
        self.assertRaises(InvalidURL, extract, 'file://', '/path')
 
465
        # Root drives without slash treated as invalid, see bug #841322
 
466
        self.assertEqual(('file:///C:', '/'), extract('file://', '/C:/'))
 
467
        self.assertRaises(InvalidURL, extract, 'file://', '/C:')
 
468
        # Invalid without drive separator or following forward slash
 
469
        self.assertRaises(InvalidURL, extract, 'file://', '/C')
 
470
        self.assertRaises(InvalidURL, extract, 'file://', '/C:ool')
460
471
 
461
472
    def test_split(self):
462
473
        # Test bzrlib.urlutils.split()
495
506
 
496
507
    def test_split_segment_parameters_raw(self):
497
508
        split_segment_parameters_raw = urlutils.split_segment_parameters_raw
 
509
        # Check relative references with absolute paths
498
510
        self.assertEquals(("/some/path", []),
499
511
            split_segment_parameters_raw("/some/path"))
500
512
        self.assertEquals(("/some/path", ["tip"]),
505
517
            split_segment_parameters_raw("/somedir/path,heads%2Ftip"))
506
518
        self.assertEquals(("/somedir/path", ["heads%2Ftip", "bar"]),
507
519
            split_segment_parameters_raw("/somedir/path,heads%2Ftip,bar"))
508
 
        self.assertEquals(("/", ["key1=val1"]),
 
520
        # Check relative references with relative paths
 
521
        self.assertEquals(("", ["key1=val1"]),
509
522
            split_segment_parameters_raw(",key1=val1"))
510
523
        self.assertEquals(("foo/", ["key1=val1"]),
511
524
            split_segment_parameters_raw("foo/,key1=val1"))
512
 
        self.assertEquals(("/foo", ["key1=val1"]),
 
525
        self.assertEquals(("foo", ["key1=val1"]),
513
526
            split_segment_parameters_raw("foo,key1=val1"))
514
527
        self.assertEquals(("foo/base,la=bla/other/elements", []),
515
528
            split_segment_parameters_raw("foo/base,la=bla/other/elements"))
516
529
        self.assertEquals(("foo/base,la=bla/other/elements", ["a=b"]),
517
530
            split_segment_parameters_raw("foo/base,la=bla/other/elements,a=b"))
 
531
        # TODO: Check full URLs as well as relative references
518
532
 
519
533
    def test_split_segment_parameters(self):
520
534
        split_segment_parameters = urlutils.split_segment_parameters
 
535
        # Check relative references with absolute paths
521
536
        self.assertEquals(("/some/path", {}),
522
537
            split_segment_parameters("/some/path"))
523
538
        self.assertEquals(("/some/path", {"branch": "tip"}),
532
547
                "/somedir/path,ref=heads%2Ftip,key1=val1"))
533
548
        self.assertEquals(("/somedir/path", {"ref": "heads%2F=tip"}),
534
549
            split_segment_parameters("/somedir/path,ref=heads%2F=tip"))
535
 
        self.assertEquals(("/", {"key1": "val1"}),
 
550
        # Check relative references with relative paths
 
551
        self.assertEquals(("", {"key1": "val1"}),
536
552
            split_segment_parameters(",key1=val1"))
537
553
        self.assertEquals(("foo/", {"key1": "val1"}),
538
554
            split_segment_parameters("foo/,key1=val1"))
541
557
        self.assertEquals(("foo/base,key1=val1/other/elements",
542
558
            {"key2": "val2"}), split_segment_parameters(
543
559
                "foo/base,key1=val1/other/elements,key2=val2"))
 
560
        # TODO: Check full URLs as well as relative references
544
561
 
545
562
    def test_win32_strip_local_trailing_slash(self):
546
563
        strip = urlutils._win32_strip_local_trailing_slash
708
725
 
709
726
 
710
727
class TestCwdToURL(TestCaseInTempDir):
711
 
    """Test that local_path_to_url works base on the cwd"""
 
728
    """Test that local_path_to_url works based on the cwd"""
712
729
 
713
730
    def test_dot(self):
714
731
        # This test will fail if getcwd is not ascii
905
922
        url3 = url.clone()
906
923
        self.assertIsNot(url, url3)
907
924
        self.assertEquals(url, url3)
 
925
 
 
926
 
 
927
class TestFileRelpath(TestCase):
 
928
 
 
929
    # GZ 2011-11-18: A way to override all path handling functions to one
 
930
    #                platform or another for testing would be nice.
 
931
 
 
932
    def _with_posix_paths(self):
 
933
        self.overrideAttr(urlutils, "local_path_from_url",
 
934
            urlutils._posix_local_path_from_url)
 
935
        self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH", len("file:///"))
 
936
        self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
 
937
        self.overrideAttr(osutils, "abspath", osutils._posix_abspath)
 
938
        self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
 
939
        self.overrideAttr(osutils, "pathjoin", osutils.posixpath.join)
 
940
        self.overrideAttr(osutils, "split", osutils.posixpath.split)
 
941
        self.overrideAttr(osutils, "MIN_ABS_PATHLENGTH", 1)
 
942
 
 
943
    def _with_win32_paths(self):
 
944
        self.overrideAttr(urlutils, "local_path_from_url",
 
945
            urlutils._win32_local_path_from_url)
 
946
        self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH",
 
947
            urlutils.WIN32_MIN_ABS_FILEURL_LENGTH)
 
948
        self.overrideAttr(osutils, "abspath", osutils._win32_abspath)
 
949
        self.overrideAttr(osutils, "normpath", osutils._win32_normpath)
 
950
        self.overrideAttr(osutils, "pathjoin", osutils._win32_pathjoin)
 
951
        self.overrideAttr(osutils, "split", osutils.ntpath.split)
 
952
        self.overrideAttr(osutils, "MIN_ABS_PATHLENGTH", 3)
 
953
 
 
954
    def test_same_url_posix(self):
 
955
        self._with_posix_paths()
 
956
        self.assertEquals("",
 
957
            urlutils.file_relpath("file:///a", "file:///a"))
 
958
        self.assertEquals("",
 
959
            urlutils.file_relpath("file:///a", "file:///a/"))
 
960
        self.assertEquals("",
 
961
            urlutils.file_relpath("file:///a/", "file:///a"))
 
962
 
 
963
    def test_same_url_win32(self):
 
964
        self._with_win32_paths()
 
965
        self.assertEquals("",
 
966
            urlutils.file_relpath("file:///A:/", "file:///A:/"))
 
967
        self.assertEquals("",
 
968
            urlutils.file_relpath("file:///A|/", "file:///A:/"))
 
969
        self.assertEquals("",
 
970
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b/"))
 
971
        self.assertEquals("",
 
972
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/"))
 
973
        self.assertEquals("",
 
974
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b"))
 
975
 
 
976
    def test_child_posix(self):
 
977
        self._with_posix_paths()
 
978
        self.assertEquals("b",
 
979
            urlutils.file_relpath("file:///a", "file:///a/b"))
 
980
        self.assertEquals("b",
 
981
            urlutils.file_relpath("file:///a/", "file:///a/b"))
 
982
        self.assertEquals("b/c",
 
983
            urlutils.file_relpath("file:///a", "file:///a/b/c"))
 
984
 
 
985
    def test_child_win32(self):
 
986
        self._with_win32_paths()
 
987
        self.assertEquals("b",
 
988
            urlutils.file_relpath("file:///A:/", "file:///A:/b"))
 
989
        self.assertEquals("b",
 
990
            urlutils.file_relpath("file:///A|/", "file:///A:/b"))
 
991
        self.assertEquals("c",
 
992
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/c"))
 
993
        self.assertEquals("c",
 
994
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b/c"))
 
995
        self.assertEquals("c/d",
 
996
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/c/d"))
 
997
 
 
998
    def test_sibling_posix(self):
 
999
        self._with_posix_paths()
 
1000
        self.assertRaises(PathNotChild,
 
1001
            urlutils.file_relpath, "file:///a/b", "file:///a/c")
 
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
 
 
1007
    def test_sibling_win32(self):
 
1008
        self._with_win32_paths()
 
1009
        self.assertRaises(PathNotChild,
 
1010
            urlutils.file_relpath, "file:///A:/b", "file:///A:/c")
 
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
 
 
1016
    def test_parent_posix(self):
 
1017
        self._with_posix_paths()
 
1018
        self.assertRaises(PathNotChild,
 
1019
            urlutils.file_relpath, "file:///a/b", "file:///a")
 
1020
        self.assertRaises(PathNotChild,
 
1021
            urlutils.file_relpath, "file:///a/b", "file:///a/")
 
1022
 
 
1023
    def test_parent_win32(self):
 
1024
        self._with_win32_paths()
 
1025
        self.assertRaises(PathNotChild,
 
1026
            urlutils.file_relpath, "file:///A:/b", "file:///A:/")
 
1027
        self.assertRaises(PathNotChild,
 
1028
            urlutils.file_relpath, "file:///A:/b/c", "file:///A:/b")
 
1029
 
 
1030
 
 
1031
class QuoteTests(TestCase):
 
1032
 
 
1033
    def test_quote(self):
 
1034
        self.assertEqual('abc%20def', urlutils.quote('abc def'))
 
1035
        self.assertEqual('abc%2Fdef', urlutils.quote('abc/def', safe=''))
 
1036
        self.assertEqual('abc/def', urlutils.quote('abc/def', safe='/'))
 
1037
 
 
1038
    def test_quote_tildes(self):
 
1039
        self.assertEqual('%7Efoo', urlutils.quote('~foo'))
 
1040
        self.assertEqual('~foo', urlutils.quote('~foo', safe='/~'))
 
1041
 
 
1042
    def test_unquote(self):
 
1043
        self.assertEqual('%', urlutils.unquote('%25'))
 
1044
        self.assertEqual('\xc3\xa5', urlutils.unquote('%C3%A5'))
 
1045
        self.assertEqual(u"\xe5", urlutils.unquote(u'\xe5'))