1
# Copyright (C) 2005, 2006 by Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
20
from bzrlib.tests import TestCaseWithTransport, TestCase
 
 
21
from bzrlib.branch import Branch
 
 
22
from bzrlib.errors import PathNotChild
 
 
23
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
 
 
26
class MoreTests(TestCaseWithTransport):
 
 
28
    def test_relpath(self):
 
 
29
        """test for branch path lookups
 
 
31
        bzrlib.osutils._relpath do a simple but subtle
 
 
32
        job: given a path (either relative to cwd or absolute), work out
 
 
33
        if it is inside a branch and return the path relative to the base.
 
 
36
        from bzrlib.osutils import rmtree
 
 
38
        savedir = os.getcwdu()
 
 
39
        dtmp = tempfile.mkdtemp()
 
 
40
        # On Mac OSX, /tmp actually expands to /private/tmp
 
 
44
            return relpath(dtmp, p)
 
 
47
            # check paths inside dtmp while standing outside it
 
 
48
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
 
 
51
            self.assertEqual(rp(dtmp), '')
 
 
53
            self.assertRaises(PathNotChild,
 
 
57
            # now some near-miss operations -- note that
 
 
58
            # os.path.commonprefix gets these wrong!
 
 
59
            self.assertRaises(PathNotChild,
 
 
61
                              dtmp.rstrip('\\/') + '2')
 
 
63
            self.assertRaises(PathNotChild,
 
 
65
                              dtmp.rstrip('\\/') + '2/foo')
 
 
67
            # now operations based on relpath of files in current
 
 
68
            # directory, or nearby
 
 
71
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
 
73
            self.assertEqual(rp('foo'), 'foo')
 
 
75
            self.assertEqual(rp('./foo'), 'foo')
 
 
77
            self.assertEqual(rp(abspath('foo')), 'foo')
 
 
79
            self.assertRaises(PathNotChild,