4
from bzrlib.tests import TestCaseWithTransport, TestCase
5
from bzrlib.branch import ScratchBranch, Branch
6
from bzrlib.errors import PathNotChild
7
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
10
class MoreTests(TestCaseWithTransport):
12
def test_relpath(self):
13
"""test for branch path lookups
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.
15
bzrlib.osutils._relpath do a simple but subtle
16
job: given a path (either relative to cwd or absolute), work out
17
if it is inside a branch and return the path relative to the base.
20
from bzrlib.osutils import rmtree
17
from bzrlib.branch import _relpath
18
import tempfile, shutil
22
20
savedir = os.getcwdu()
23
21
dtmp = tempfile.mkdtemp()
24
# On Mac OSX, /tmp actually expands to /private/tmp
28
return relpath(dtmp, p)
24
return _relpath(dtmp, p)
31
27
# check paths inside dtmp while standing outside it
32
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
28
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
35
31
self.assertEqual(rp(dtmp), '')
37
self.assertRaises(PathNotChild,
33
self.assertRaises(NotBranchError,
41
37
# now some near-miss operations -- note that
42
38
# os.path.commonprefix gets these wrong!
43
self.assertRaises(PathNotChild,
39
self.assertRaises(NotBranchError,
45
41
dtmp.rstrip('\\/') + '2')
47
self.assertRaises(PathNotChild,
43
self.assertRaises(NotBranchError,
49
45
dtmp.rstrip('\\/') + '2/foo')
59
55
self.assertEqual(rp('./foo'), 'foo')
61
self.assertEqual(rp(abspath('foo')), 'foo')
57
self.assertEqual(rp(os.path.abspath('foo')), 'foo')
63
self.assertRaises(PathNotChild,
59
self.assertRaises(NotBranchError,
67
if __name__ == '__main__':