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.
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
17
from bzrlib.branch import _relpath
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.
18
19
import tempfile, shutil
20
21
savedir = os.getcwdu()
21
22
dtmp = tempfile.mkdtemp()
23
# On Mac OSX, /tmp actually expands to /private/tmp
24
return _relpath(dtmp, p)
27
return relpath(dtmp, p)
27
30
# check paths inside dtmp while standing outside it
28
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
31
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
31
34
self.assertEqual(rp(dtmp), '')
33
self.assertRaises(NotBranchError,
36
self.assertRaises(PathNotChild,
37
40
# now some near-miss operations -- note that
38
41
# os.path.commonprefix gets these wrong!
39
self.assertRaises(NotBranchError,
42
self.assertRaises(PathNotChild,
41
44
dtmp.rstrip('\\/') + '2')
43
self.assertRaises(NotBranchError,
46
self.assertRaises(PathNotChild,
45
48
dtmp.rstrip('\\/') + '2/foo')
55
58
self.assertEqual(rp('./foo'), 'foo')
57
self.assertEqual(rp(os.path.abspath('foo')), 'foo')
60
self.assertEqual(rp(abspath('foo')), 'foo')
59
self.assertRaises(NotBranchError,
62
self.assertRaises(PathNotChild,
64
67
shutil.rmtree(dtmp)
67
if __name__ == '__main__':