~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, Patch Queue Manager, Jelmer Vernooij
  • Date: 2017-01-17 16:20:41 UTC
  • mfrom: (6619.1.2 trunk)
  • Revision ID: tarmac-20170117162041-oo62uk1qsmgc9j31
Merge 2.7 into trunk including fixes for bugs #1622039, #1644003, #1579093 and #1645017. [r=vila]

Show diffs side-by-side

added added

removed removed

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