/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7204.2.1 by Jelmer Vernooij
Move location_to_url to its own module.
1
# Copyright (C) 2005-2011, 2015, 2016 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for breezy.location."""
18
19
from .. import (
20
    osutils,
21
    tests,
22
    urlutils,
23
    )
24
from ..directory_service import directories
25
from ..location import (
7268.11.1 by Jelmer Vernooij
Add rewrite_url hook.
26
    hooks as location_hooks,
7204.2.1 by Jelmer Vernooij
Move location_to_url to its own module.
27
    location_to_url,
28
    )
29
30
31
class SomeDirectory(object):
32
33
    def look_up(self, name, url):
34
        return "http://bar"
35
36
37
class TestLocationToUrl(tests.TestCase):
38
39
    def get_base_location(self):
40
        path = osutils.abspath('/foo/bar')
41
        if path.startswith('/'):
42
            url = 'file://%s' % (path,)
43
        else:
44
            # On Windows, abspaths start with the drive letter, so we have to
45
            # add in the extra '/'
46
            url = 'file:///%s' % (path,)
47
        return path, url
48
49
    def test_regular_url(self):
50
        self.assertEqual("file://foo", location_to_url("file://foo"))
51
52
    def test_directory(self):
53
        directories.register("bar:", SomeDirectory, "Dummy directory")
54
        self.addCleanup(directories.remove, "bar:")
55
        self.assertEqual("http://bar", location_to_url("bar:"))
56
57
    def test_unicode_url(self):
58
        self.assertRaises(urlutils.InvalidURL, location_to_url,
59
                          b"http://fo/\xc3\xaf".decode("utf-8"))
60
61
    def test_unicode_path(self):
62
        path, url = self.get_base_location()
63
        location = path + b"\xc3\xaf".decode("utf-8")
64
        url += '%C3%AF'
65
        self.assertEqual(url, location_to_url(location))
66
67
    def test_path(self):
68
        path, url = self.get_base_location()
69
        self.assertEqual(url, location_to_url(path))
70
71
    def test_relative_file_url(self):
72
        self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
73
                         location_to_url("file:bar"))
74
75
    def test_absolute_file_url(self):
76
        self.assertEqual("file:///bar", location_to_url("file:/bar"))
7268.11.1 by Jelmer Vernooij
Add rewrite_url hook.
77
78
    def test_rewrite_hook(self):
79
        self.assertEqual(
80
            'http://foo.example.com/blah', location_to_url('http://foo.example.com/blah'))
81
        def rewrite_url(url):
82
            return url.replace('foo', 'bar')
83
        self.addCleanup(location_hooks.uninstall_named_hook, 'rewrite_url', 'test')
84
        location_hooks.install_named_hook('rewrite_url', rewrite_url, 'test')
85
        self.assertEqual(
86
            'http://bar.example.com/bar', location_to_url('http://foo.example.com/foo'))