1
# Copyright (C) 2004, 2005 by Canonical Ltd
1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.branch import Branch
21
20
import bzrlib.errors
22
from bzrlib.osutils import abspath, realpath, getcwd
23
from bzrlib.urlutils import local_path_from_url, local_path_to_url, escape
24
from bzrlib.tests import TestCaseWithTransport
21
from bzrlib.osutils import getcwd
22
from bzrlib.tests import (
23
TestCaseWithTransport,
27
from bzrlib import urlutils
27
30
"""Tests for Branch parent URL"""
32
35
def test_no_default_parent(self):
33
36
"""Branches should have no parent by default"""
34
37
b = self.make_branch('.')
35
self.assertEquals(b.get_parent(), None)
38
self.assertEqual(None, b.get_parent())
37
40
def test_set_get_parent(self):
38
41
"""Set, re-get and reset the parent"""
39
b = self.make_branch('.')
42
b = self.make_branch('subdir')
40
43
url = 'http://bazaar-vcs.org/bzr/bzr.dev'
42
self.assertEquals(b.get_parent(), url)
43
self.assertEqual(b.control_files.get('parent').read().strip('\n'), url)
45
self.assertEqual(url, b.get_parent())
46
self.assertEqual(url, b._get_parent_location())
46
self.assertEquals(b.get_parent(), None)
49
self.assertEqual(None, b.get_parent())
48
51
b.set_parent('../other_branch')
51
self.assertEquals(b.get_parent(), local_path_to_url('../other_branch'))
52
path = local_path_to_url('../yanb')
53
expected_parent = urlutils.join(self.get_url('subdir'),
55
self.assertEqual(expected_parent, b.get_parent())
56
path = urlutils.join(self.get_url('subdir'), '../yanb')
54
self.assertEqual(b.control_files.get('parent').read().strip('\n'),
56
self.assertEqual(b.get_parent(), path)
58
self.assertEqual('../yanb', b._get_parent_location())
59
self.assertEqual(path, b.get_parent())
59
62
self.assertRaises(bzrlib.errors.InvalidURL, b.set_parent, u'\xb5')
60
b.set_parent(escape(u'\xb5'))
61
self.assertEqual(b.control_files.get('parent').read().strip('\n'),
64
self.assertEqual(b.get_parent(), b.base + '%C2%B5')
63
b.set_parent(urlutils.escape(u'\xb5'))
64
self.assertEqual('%C2%B5', b._get_parent_location())
66
self.assertEqual(b.base + '%C2%B5', b.get_parent())
68
# Handle the case for older style absolute local paths
69
if sys.platform == 'win32':
70
# TODO: jam 20060515 Do we want to special case Windows local
71
# paths as well? Nobody has complained about it.
74
b._set_parent_location('/local/abs/path')
75
self.assertEqual('file:///local/abs/path', b.get_parent())
77
def test_get_invalid_parent(self):
78
b = self.make_branch('.')
81
n_dirs = len(cwd.split('/'))
83
# Force the relative path to be something invalid
84
# This should attempt to go outside the filesystem
85
path = ('../'*(n_dirs+5)) + 'foo'
86
b._set_parent_location(path)
88
# With an invalid branch parent, just return None
89
self.assertRaises(bzrlib.errors.InaccessibleParent, b.get_parent)
91
def test_win32_set_parent_on_another_drive(self):
92
if sys.platform != 'win32':
93
raise TestSkipped('windows-specific test')
94
b = self.make_branch('.')
95
base_url = b.abspath('.')
96
if not base_url.startswith('file:///'):
97
raise TestNotApplicable('this test should be run with local base')
98
base = urlutils.local_path_from_url(base_url)
99
other = 'file:///B:/path'
101
other = 'file:///C:/path'
103
self.assertEquals(other, b._get_parent_location())