~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Ian Clatworthy
  • Date: 2009-12-03 23:21:16 UTC
  • mfrom: (4852.4.1 RCStoVCS)
  • mto: This revision was merged to the branch mainline in revision 4860.
  • Revision ID: ian.clatworthy@canonical.com-20091203232116-f8igfvc6muqrn4yx
Revision Control -> Version Control in docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008-2012 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
18
19
 
19
 
import bzrlib
20
20
from bzrlib import (
21
 
    errors,
22
21
    osutils,
23
 
    tests,
24
22
    )
 
23
from bzrlib.tests import TestCaseWithTransport, TestCase
 
24
from bzrlib.branch import Branch
 
25
from bzrlib.errors import PathNotChild
25
26
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
26
27
 
27
28
 
28
 
class MoreTests(tests.TestCaseWithTransport):
 
29
class MoreTests(TestCaseWithTransport):
29
30
 
30
31
    def test_relpath(self):
31
32
        """test for branch path lookups
34
35
        job: given a path (either relative to cwd or absolute), work out
35
36
        if it is inside a branch and return the path relative to the base.
36
37
        """
 
38
        import tempfile
 
39
 
 
40
        savedir = os.getcwdu()
37
41
        dtmp = osutils.mkdtemp()
38
 
        self.addCleanup(osutils.rmtree, dtmp)
39
42
        # On Mac OSX, /tmp actually expands to /private/tmp
40
43
        dtmp = realpath(dtmp)
41
44
 
42
45
        def rp(p):
43
46
            return relpath(dtmp, p)
44
47
 
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')
 
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)