~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-30 18:28:17 UTC
  • mfrom: (5967.10.2 test-cat)
  • Revision ID: pqm@pqm.ubuntu.com-20110630182817-83a5q9r9rxfkdn8r
(mbp) don't use subprocesses for testing cat (Martin Pool)

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