~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Patch Queue Manager
  • Date: 2013-05-23 10:35:23 UTC
  • mfrom: (6574.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20130523103523-2wt6jmauja1n1vdt
(jameinel) Merge bzr/2.5 into trunk. (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008-2012 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
 
import unittest
19
18
 
20
 
from bzrlib.tests import TestCaseWithTransport, TestCase
21
 
from bzrlib.branch import Branch
22
 
from bzrlib.errors import PathNotChild
 
19
import bzrlib
 
20
from bzrlib import (
 
21
    errors,
 
22
    osutils,
 
23
    tests,
 
24
    )
23
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
24
26
 
25
27
 
26
 
class MoreTests(TestCaseWithTransport):
 
28
class MoreTests(tests.TestCaseWithTransport):
27
29
 
28
30
    def test_relpath(self):
29
31
        """test for branch path lookups
30
 
    
 
32
 
31
33
        bzrlib.osutils._relpath do a simple but subtle
32
34
        job: given a path (either relative to cwd or absolute), work out
33
35
        if it is inside a branch and return the path relative to the base.
34
36
        """
35
 
        import tempfile
36
 
        from bzrlib.osutils import rmtree
37
 
        
38
 
        savedir = os.getcwdu()
39
 
        dtmp = tempfile.mkdtemp()
 
37
        dtmp = osutils.mkdtemp()
 
38
        self.addCleanup(osutils.rmtree, dtmp)
40
39
        # On Mac OSX, /tmp actually expands to /private/tmp
41
40
        dtmp = realpath(dtmp)
42
41
 
43
42
        def rp(p):
44
43
            return relpath(dtmp, p)
45
 
        
46
 
        try:
47
 
            # check paths inside dtmp while standing outside it
48
 
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
49
 
 
50
 
            # root = nothing
51
 
            self.assertEqual(rp(dtmp), '')
52
 
 
53
 
            self.assertRaises(PathNotChild,
54
 
                              rp,
55
 
                              '/etc')
56
 
 
57
 
            # now some near-miss operations -- note that
58
 
            # os.path.commonprefix gets these wrong!
59
 
            self.assertRaises(PathNotChild,
60
 
                              rp,
61
 
                              dtmp.rstrip('\\/') + '2')
62
 
 
63
 
            self.assertRaises(PathNotChild,
64
 
                              rp,
65
 
                              dtmp.rstrip('\\/') + '2/foo')
66
 
 
67
 
            # now operations based on relpath of files in current
68
 
            # directory, or nearby
69
 
            os.chdir(dtmp)
70
 
 
71
 
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
72
 
 
73
 
            self.assertEqual(rp('foo'), 'foo')
74
 
 
75
 
            self.assertEqual(rp('./foo'), 'foo')
76
 
 
77
 
            self.assertEqual(rp(abspath('foo')), 'foo')
78
 
 
79
 
            self.assertRaises(PathNotChild,
80
 
                              rp, '../foo')
81
 
 
82
 
        finally:
83
 
            os.chdir(savedir)
84
 
            rmtree(dtmp)
 
44
 
 
45
        # check paths inside dtmp while standing outside it
 
46
        self.assertEqual('foo', rp(pathjoin(dtmp, 'foo')))
 
47
 
 
48
        # root = nothing
 
49
        self.assertEqual('', rp(dtmp))
 
50
        self.assertRaises(errors.PathNotChild, rp, '/etc')
 
51
 
 
52
        # now some near-miss operations -- note that
 
53
        # os.path.commonprefix gets these wrong!
 
54
        self.assertRaises(errors.PathNotChild, rp, dtmp.rstrip('\\/') + '2')
 
55
        self.assertRaises(errors.PathNotChild, rp, dtmp.rstrip('\\/') + '2/foo')
 
56
 
 
57
        # now operations based on relpath of files in current
 
58
        # directory, or nearby
 
59
 
 
60
        os.chdir(dtmp)
 
61
        self.assertEqual('foo/bar/quux', rp('foo/bar/quux'))
 
62
        self.assertEqual('foo', rp('foo'))
 
63
        self.assertEqual('foo', rp('./foo'))
 
64
        self.assertEqual('foo', rp(abspath('foo')))
 
65
        self.assertRaises(errors.PathNotChild, rp, '../foo')