15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
from bzrlib import (
23
from bzrlib.tests import TestCaseWithTransport, TestCase
24
from bzrlib.branch import Branch
25
from bzrlib.errors import PathNotChild
26
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
29
class MoreTests(TestCaseWithTransport):
28
class MoreTests(tests.TestCaseWithTransport):
31
30
def test_relpath(self):
32
31
"""test for branch path lookups
35
34
job: given a path (either relative to cwd or absolute), work out
36
35
if it is inside a branch and return the path relative to the base.
40
savedir = os.getcwdu()
41
37
dtmp = osutils.mkdtemp()
38
self.addCleanup(osutils.rmtree, dtmp)
42
39
# On Mac OSX, /tmp actually expands to /private/tmp
43
40
dtmp = realpath(dtmp)
46
43
return relpath(dtmp, p)
49
# check paths inside dtmp while standing outside it
50
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
53
self.assertEqual(rp(dtmp), '')
55
self.assertRaises(PathNotChild,
59
# now some near-miss operations -- note that
60
# os.path.commonprefix gets these wrong!
61
self.assertRaises(PathNotChild,
63
dtmp.rstrip('\\/') + '2')
65
self.assertRaises(PathNotChild,
67
dtmp.rstrip('\\/') + '2/foo')
69
# now operations based on relpath of files in current
70
# directory, or nearby
73
self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
75
self.assertEqual(rp('foo'), 'foo')
77
self.assertEqual(rp('./foo'), 'foo')
79
self.assertEqual(rp(abspath('foo')), 'foo')
81
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')