~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Vincent Ladeuil
  • Date: 2011-04-11 09:38:59 UTC
  • mfrom: (5609.33.1 2.3)
  • mto: This revision was merged to the branch mainline in revision 5783.
  • Revision ID: v.ladeuil+lp@free.fr-20110411093859-0gyunr0tkvnoskrm
Merge 2.3 to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
class BranchPathTestCase(TestCase):
9
 
    """test for branch path lookups
10
 
 
11
 
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
12
 
    job: given a path (either relative to cwd or absolute), work out
13
 
    if it is inside a branch and return the path relative to the base.
14
 
    """
15
 
    
16
 
    def runTest(self):
17
 
        from bzrlib.branch import _relpath
18
 
        import tempfile, shutil
19
 
        
 
1
# Copyright (C) 2005, 2006, 2008, 2009, 2011 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import os
 
18
 
 
19
from bzrlib import (
 
20
    osutils,
 
21
    )
 
22
from bzrlib.tests import TestCaseWithTransport
 
23
from bzrlib.errors import PathNotChild
 
24
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
 
25
 
 
26
 
 
27
class MoreTests(TestCaseWithTransport):
 
28
 
 
29
    def test_relpath(self):
 
30
        """test for branch path lookups
 
31
 
 
32
        bzrlib.osutils._relpath do a simple but subtle
 
33
        job: given a path (either relative to cwd or absolute), work out
 
34
        if it is inside a branch and return the path relative to the base.
 
35
        """
 
36
        import tempfile
 
37
 
20
38
        savedir = os.getcwdu()
21
 
        dtmp = tempfile.mkdtemp()
 
39
        dtmp = osutils.mkdtemp()
 
40
        # On Mac OSX, /tmp actually expands to /private/tmp
 
41
        dtmp = realpath(dtmp)
22
42
 
23
43
        def rp(p):
24
 
            return _relpath(dtmp, p)
25
 
        
 
44
            return relpath(dtmp, p)
 
45
 
26
46
        try:
27
47
            # check paths inside dtmp while standing outside it
28
 
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
 
48
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
29
49
 
30
50
            # root = nothing
31
51
            self.assertEqual(rp(dtmp), '')
32
52
 
33
 
            self.assertRaises(NotBranchError,
 
53
            self.assertRaises(PathNotChild,
34
54
                              rp,
35
55
                              '/etc')
36
56
 
37
57
            # now some near-miss operations -- note that
38
58
            # os.path.commonprefix gets these wrong!
39
 
            self.assertRaises(NotBranchError,
 
59
            self.assertRaises(PathNotChild,
40
60
                              rp,
41
61
                              dtmp.rstrip('\\/') + '2')
42
62
 
43
 
            self.assertRaises(NotBranchError,
 
63
            self.assertRaises(PathNotChild,
44
64
                              rp,
45
65
                              dtmp.rstrip('\\/') + '2/foo')
46
66
 
54
74
 
55
75
            self.assertEqual(rp('./foo'), 'foo')
56
76
 
57
 
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
 
77
            self.assertEqual(rp(abspath('foo')), 'foo')
58
78
 
59
 
            self.assertRaises(NotBranchError,
 
79
            self.assertRaises(PathNotChild,
60
80
                              rp, '../foo')
61
81
 
62
82
        finally:
63
83
            os.chdir(savedir)
64
 
            shutil.rmtree(dtmp)
65
 
 
66
 
                              
67
 
if __name__ == '__main__':
68
 
    unittest.main()
69
 
    
 
84
            osutils.rmtree(dtmp)