~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

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
 
        
20
 
        savedir = os.getcwdu()
21
 
        dtmp = tempfile.mkdtemp()
 
1
# Copyright (C) 2005, 2006, 2008-2012 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
import bzrlib
 
20
from bzrlib import (
 
21
    errors,
 
22
    osutils,
 
23
    tests,
 
24
    )
 
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
 
26
 
 
27
 
 
28
class MoreTests(tests.TestCaseWithTransport):
 
29
 
 
30
    def test_relpath(self):
 
31
        """test for branch path lookups
 
32
 
 
33
        bzrlib.osutils._relpath do a simple but subtle
 
34
        job: given a path (either relative to cwd or absolute), work out
 
35
        if it is inside a branch and return the path relative to the base.
 
36
        """
 
37
        dtmp = osutils.mkdtemp()
 
38
        self.addCleanup(osutils.rmtree, dtmp)
 
39
        # On Mac OSX, /tmp actually expands to /private/tmp
 
40
        dtmp = realpath(dtmp)
22
41
 
23
42
        def rp(p):
24
 
            return _relpath(dtmp, p)
25
 
        
26
 
        try:
27
 
            # check paths inside dtmp while standing outside it
28
 
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
29
 
 
30
 
            # root = nothing
31
 
            self.assertEqual(rp(dtmp), '')
32
 
 
33
 
            self.assertRaises(NotBranchError,
34
 
                              rp,
35
 
                              '/etc')
36
 
 
37
 
            # now some near-miss operations -- note that
38
 
            # os.path.commonprefix gets these wrong!
39
 
            self.assertRaises(NotBranchError,
40
 
                              rp,
41
 
                              dtmp.rstrip('\\/') + '2')
42
 
 
43
 
            self.assertRaises(NotBranchError,
44
 
                              rp,
45
 
                              dtmp.rstrip('\\/') + '2/foo')
46
 
 
47
 
            # now operations based on relpath of files in current
48
 
            # directory, or nearby
49
 
            os.chdir(dtmp)
50
 
 
51
 
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
52
 
 
53
 
            self.assertEqual(rp('foo'), 'foo')
54
 
 
55
 
            self.assertEqual(rp('./foo'), 'foo')
56
 
 
57
 
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
58
 
 
59
 
            self.assertRaises(NotBranchError,
60
 
                              rp, '../foo')
61
 
 
62
 
        finally:
63
 
            os.chdir(savedir)
64
 
            shutil.rmtree(dtmp)
65
 
 
66
 
                              
67
 
if __name__ == '__main__':
68
 
    unittest.main()
69
 
    
 
43
            return relpath(dtmp, p)
 
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')