~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Martin Pool
  • Date: 2006-05-17 07:09:13 UTC
  • mfrom: (1668.1.15 bzr-0.8.mbp)
  • mto: This revision was merged to the branch mainline in revision 1710.
  • Revision ID: mbp@sourcefrog.net-20060517070913-723e387ac91d55c0
merge 0.8 fix branch, including register-branch plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import unittest
 
3
 
 
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
 
8
 
 
9
 
 
10
class MoreTests(TestCaseWithTransport):
 
11
 
 
12
    def test_relpath(self):
 
13
        """test for branch path lookups
 
14
    
 
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
 
20
        from bzrlib.osutils import rmtree
 
21
        
 
22
        savedir = os.getcwdu()
 
23
        dtmp = tempfile.mkdtemp()
 
24
        # On Mac OSX, /tmp actually expands to /private/tmp
 
25
        dtmp = realpath(dtmp)
 
26
 
 
27
        def rp(p):
 
28
            return relpath(dtmp, p)
 
29
        
 
30
        try:
 
31
            # check paths inside dtmp while standing outside it
 
32
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
 
33
 
 
34
            # root = nothing
 
35
            self.assertEqual(rp(dtmp), '')
 
36
 
 
37
            self.assertRaises(PathNotChild,
 
38
                              rp,
 
39
                              '/etc')
 
40
 
 
41
            # now some near-miss operations -- note that
 
42
            # os.path.commonprefix gets these wrong!
 
43
            self.assertRaises(PathNotChild,
 
44
                              rp,
 
45
                              dtmp.rstrip('\\/') + '2')
 
46
 
 
47
            self.assertRaises(PathNotChild,
 
48
                              rp,
 
49
                              dtmp.rstrip('\\/') + '2/foo')
 
50
 
 
51
            # now operations based on relpath of files in current
 
52
            # directory, or nearby
 
53
            os.chdir(dtmp)
 
54
 
 
55
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
56
 
 
57
            self.assertEqual(rp('foo'), 'foo')
 
58
 
 
59
            self.assertEqual(rp('./foo'), 'foo')
 
60
 
 
61
            self.assertEqual(rp(abspath('foo')), 'foo')
 
62
 
 
63
            self.assertRaises(PathNotChild,
 
64
                              rp, '../foo')
 
65
 
 
66
        finally:
 
67
            os.chdir(savedir)
 
68
            rmtree(dtmp)