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
def Reporter(TestResult):
9
def startTest(self, test):
10
super(Reporter, self).startTest(test)
13
def stopTest(self, test):
16
class BranchPathTestCase(TestCase):
17
"""test for branch path lookups
19
Branch.relpath and bzrlib.branch._relpath do a simple but subtle
20
job: given a path (either relative to cwd or absolute), work out
21
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.
25
from bzrlib.branch import _relpath
19
26
import tempfile, shutil
21
28
savedir = os.getcwdu()
22
29
dtmp = tempfile.mkdtemp()
23
# On Mac OSX, /tmp actually expands to /private/tmp
27
return relpath(dtmp, p)
32
return _relpath(dtmp, p)
30
35
# check paths inside dtmp while standing outside it
31
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
36
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
34
39
self.assertEqual(rp(dtmp), '')
36
self.assertRaises(PathNotChild,
41
self.assertRaises(NotBranchError,
40
45
# now some near-miss operations -- note that
41
46
# os.path.commonprefix gets these wrong!
42
self.assertRaises(PathNotChild,
47
self.assertRaises(NotBranchError,
44
49
dtmp.rstrip('\\/') + '2')
46
self.assertRaises(PathNotChild,
51
self.assertRaises(NotBranchError,
48
53
dtmp.rstrip('\\/') + '2/foo')
58
63
self.assertEqual(rp('./foo'), 'foo')
60
self.assertEqual(rp(abspath('foo')), 'foo')
65
self.assertEqual(rp(os.path.abspath('foo')), 'foo')
62
self.assertRaises(PathNotChild,
67
self.assertRaises(NotBranchError,
67
72
shutil.rmtree(dtmp)
76
if __name__ == '__main__':