1
# Copyright (C) 2005, 2006, 2008, 2009, 2011 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008-2012 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
19
20
from bzrlib import (
22
from bzrlib.tests import TestCaseWithTransport
23
from bzrlib.errors import PathNotChild
24
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
27
class MoreTests(TestCaseWithTransport):
28
class MoreTests(tests.TestCaseWithTransport):
29
30
def test_relpath(self):
30
31
"""test for branch path lookups
33
34
job: given a path (either relative to cwd or absolute), work out
34
35
if it is inside a branch and return the path relative to the base.
38
savedir = os.getcwdu()
39
37
dtmp = osutils.mkdtemp()
38
self.addCleanup(osutils.rmtree, dtmp)
40
39
# On Mac OSX, /tmp actually expands to /private/tmp
41
40
dtmp = realpath(dtmp)
44
43
return relpath(dtmp, p)
47
# check paths inside dtmp while standing outside it
48
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
51
self.assertEqual(rp(dtmp), '')
53
self.assertRaises(PathNotChild,
57
# now some near-miss operations -- note that
58
# os.path.commonprefix gets these wrong!
59
self.assertRaises(PathNotChild,
61
dtmp.rstrip('\\/') + '2')
63
self.assertRaises(PathNotChild,
65
dtmp.rstrip('\\/') + '2/foo')
67
# now operations based on relpath of files in current
68
# directory, or nearby
71
self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
73
self.assertEqual(rp('foo'), 'foo')
75
self.assertEqual(rp('./foo'), 'foo')
77
self.assertEqual(rp(abspath('foo')), 'foo')
79
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')