3
from bzrlib.branch import ScratchBranch
4
from bzrlib.errors import NotBranchError
5
from unittest import TestCase
8
class BranchPathTestCase(TestCase):
9
"""test for branch path lookups
11
Branch.relpath and bzrlib.branch._relpath do a simple but subtle
12
job: given a path (either relative to cwd or absolute), work out
13
if it is inside a branch and return the path relative to the base.
17
from bzrlib.branch import _relpath
18
import tempfile, shutil
1
# Copyright (C) 2005, 2006, 2008, 2009, 2011 Canonical Ltd
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.
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.
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
22
from bzrlib.tests import TestCaseWithTransport
23
from bzrlib.errors import PathNotChild
24
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
27
class MoreTests(TestCaseWithTransport):
29
def test_relpath(self):
30
"""test for branch path lookups
32
bzrlib.osutils._relpath do a simple but subtle
33
job: given a path (either relative to cwd or absolute), work out
34
if it is inside a branch and return the path relative to the base.
20
38
savedir = os.getcwdu()
21
dtmp = tempfile.mkdtemp()
39
dtmp = osutils.mkdtemp()
40
# On Mac OSX, /tmp actually expands to /private/tmp
24
return _relpath(dtmp, p)
44
return relpath(dtmp, p)
27
47
# check paths inside dtmp while standing outside it
28
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
48
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
31
51
self.assertEqual(rp(dtmp), '')
33
self.assertRaises(NotBranchError,
53
self.assertRaises(PathNotChild,
37
57
# now some near-miss operations -- note that
38
58
# os.path.commonprefix gets these wrong!
39
self.assertRaises(NotBranchError,
59
self.assertRaises(PathNotChild,
41
61
dtmp.rstrip('\\/') + '2')
43
self.assertRaises(NotBranchError,
63
self.assertRaises(PathNotChild,
45
65
dtmp.rstrip('\\/') + '2/foo')