1
# Copyright (C) 2005, 2006, 2008-2012 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
4
from bzrlib.tests import TestCaseWithTransport, TestCase
5
from bzrlib.branch import ScratchBranch, Branch
6
from bzrlib.errors import PathNotChild
7
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
10
class MoreTests(TestCaseWithTransport):
28
class MoreTests(tests.TestCaseWithTransport):
12
30
def test_relpath(self):
13
31
"""test for branch path lookups
15
33
bzrlib.osutils._relpath do a simple but subtle
16
34
job: given a path (either relative to cwd or absolute), work out
17
35
if it is inside a branch and return the path relative to the base.
20
from bzrlib.osutils import rmtree
22
savedir = os.getcwdu()
23
dtmp = tempfile.mkdtemp()
37
dtmp = osutils.mkdtemp()
38
self.addCleanup(osutils.rmtree, dtmp)
24
39
# On Mac OSX, /tmp actually expands to /private/tmp
25
40
dtmp = realpath(dtmp)
28
43
return relpath(dtmp, p)
31
# check paths inside dtmp while standing outside it
32
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
35
self.assertEqual(rp(dtmp), '')
37
self.assertRaises(PathNotChild,
41
# now some near-miss operations -- note that
42
# os.path.commonprefix gets these wrong!
43
self.assertRaises(PathNotChild,
45
dtmp.rstrip('\\/') + '2')
47
self.assertRaises(PathNotChild,
49
dtmp.rstrip('\\/') + '2/foo')
51
# now operations based on relpath of files in current
52
# directory, or nearby
55
self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
57
self.assertEqual(rp('foo'), 'foo')
59
self.assertEqual(rp('./foo'), 'foo')
61
self.assertEqual(rp(abspath('foo')), 'foo')
63
self.assertRaises(PathNotChild,
45
# check paths inside dtmp while standing outside it
46
self.assertEqual('foo', rp(pathjoin(dtmp, 'foo')))
49
self.assertEqual('', rp(dtmp))
50
self.assertRaises(errors.PathNotChild, rp, '/etc')
52
# now some near-miss operations -- note that
53
# os.path.commonprefix gets these wrong!
54
self.assertRaises(errors.PathNotChild, rp, dtmp.rstrip('\\/') + '2')
55
self.assertRaises(errors.PathNotChild, rp, dtmp.rstrip('\\/') + '2/foo')
57
# now operations based on relpath of files in current
58
# directory, or nearby
61
self.assertEqual('foo/bar/quux', rp('foo/bar/quux'))
62
self.assertEqual('foo', rp('foo'))
63
self.assertEqual('foo', rp('./foo'))
64
self.assertEqual('foo', rp(abspath('foo')))
65
self.assertRaises(errors.PathNotChild, rp, '../foo')