~bzr-pqm/bzr/bzr.dev

768 by Martin Pool
- start some tests for directory renames
1
import os
2
import unittest
3
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
4
from bzrlib.tests import TestCaseWithTransport, TestCase
768 by Martin Pool
- start some tests for directory renames
5
from bzrlib.branch import ScratchBranch, Branch
1185.31.42 by John Arbash Meinel
Updated whitebox text for new PathNotChild exception
6
from bzrlib.errors import PathNotChild
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
7
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
778 by Martin Pool
- simple revert of text files
8
9
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
10
class MoreTests(TestCaseWithTransport):
771 by Martin Pool
- more tests of directory renames
11
1102 by Martin Pool
- merge test refactoring from robertc
12
    def test_relpath(self):
13
        """test for branch path lookups
14
    
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
15
        bzrlib.osutils._relpath do a simple but subtle
1102 by Martin Pool
- merge test refactoring from robertc
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
        """
601 by Martin Pool
- whitebox tests for branch path handling
19
        import tempfile, shutil
20
        
21
        savedir = os.getcwdu()
22
        dtmp = tempfile.mkdtemp()
907.1.7 by John Arbash Meinel
Fixed test failure on Mac OSX.
23
        # On Mac OSX, /tmp actually expands to /private/tmp
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
24
        dtmp = realpath(dtmp)
601 by Martin Pool
- whitebox tests for branch path handling
25
26
        def rp(p):
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
27
            return relpath(dtmp, p)
601 by Martin Pool
- whitebox tests for branch path handling
28
        
29
        try:
30
            # check paths inside dtmp while standing outside it
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
31
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
601 by Martin Pool
- whitebox tests for branch path handling
32
33
            # root = nothing
34
            self.assertEqual(rp(dtmp), '')
35
1185.31.42 by John Arbash Meinel
Updated whitebox text for new PathNotChild exception
36
            self.assertRaises(PathNotChild,
601 by Martin Pool
- whitebox tests for branch path handling
37
                              rp,
38
                              '/etc')
39
40
            # now some near-miss operations -- note that
41
            # os.path.commonprefix gets these wrong!
1185.31.42 by John Arbash Meinel
Updated whitebox text for new PathNotChild exception
42
            self.assertRaises(PathNotChild,
601 by Martin Pool
- whitebox tests for branch path handling
43
                              rp,
44
                              dtmp.rstrip('\\/') + '2')
45
1185.31.42 by John Arbash Meinel
Updated whitebox text for new PathNotChild exception
46
            self.assertRaises(PathNotChild,
601 by Martin Pool
- whitebox tests for branch path handling
47
                              rp,
48
                              dtmp.rstrip('\\/') + '2/foo')
49
50
            # now operations based on relpath of files in current
51
            # directory, or nearby
52
            os.chdir(dtmp)
53
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
54
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
601 by Martin Pool
- whitebox tests for branch path handling
55
56
            self.assertEqual(rp('foo'), 'foo')
57
58
            self.assertEqual(rp('./foo'), 'foo')
59
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
60
            self.assertEqual(rp(abspath('foo')), 'foo')
601 by Martin Pool
- whitebox tests for branch path handling
61
1185.31.42 by John Arbash Meinel
Updated whitebox text for new PathNotChild exception
62
            self.assertRaises(PathNotChild,
601 by Martin Pool
- whitebox tests for branch path handling
63
                              rp, '../foo')
64
65
        finally:
66
            os.chdir(savedir)
67
            shutil.rmtree(dtmp)