~bzr-pqm/bzr/bzr.dev

601 by Martin Pool
- whitebox tests for branch path handling
1
#! /usr/bin/python
2
3
from bzrlib.branch import ScratchBranch
4
from bzrlib.errors import NotBranchError
5
from unittest import TestCase
6
import os, unittest
7
8
def Reporter(TestResult):
9
    def startTest(self, test):
10
        super(Reporter, self).startTest(test)
11
        print test.id(),
12
13
    def stopTest(self, test):
14
        print
15
16
class BranchPathTestCase(TestCase):
17
    """test for branch path lookups
18
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.
22
    """
23
    
24
    def runTest(self):
25
        from bzrlib.branch import _relpath
26
        import tempfile, shutil
27
        
28
        savedir = os.getcwdu()
29
        dtmp = tempfile.mkdtemp()
30
31
        def rp(p):
32
            return _relpath(dtmp, p)
33
        
34
        try:
35
            # check paths inside dtmp while standing outside it
36
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
37
38
            # root = nothing
39
            self.assertEqual(rp(dtmp), '')
40
41
            self.assertRaises(NotBranchError,
42
                              rp,
43
                              '/etc')
44
45
            # now some near-miss operations -- note that
46
            # os.path.commonprefix gets these wrong!
47
            self.assertRaises(NotBranchError,
48
                              rp,
49
                              dtmp.rstrip('\\/') + '2')
50
51
            self.assertRaises(NotBranchError,
52
                              rp,
53
                              dtmp.rstrip('\\/') + '2/foo')
54
55
            # now operations based on relpath of files in current
56
            # directory, or nearby
57
            os.chdir(dtmp)
58
59
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
60
61
            self.assertEqual(rp('foo'), 'foo')
62
63
            self.assertEqual(rp('./foo'), 'foo')
64
65
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
66
67
            self.assertRaises(NotBranchError,
68
                              rp, '../foo')
69
70
        finally:
71
            os.chdir(savedir)
72
            shutil.rmtree(dtmp)
73
74
75
                              
76
if __name__ == '__main__':
77
    unittest.main()
78