~bzr-pqm/bzr/bzr.dev

6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
1
# Copyright (C) 2005, 2006, 2008-2012 Canonical Ltd
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
2
#
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
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.
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
7
#
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
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.
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
12
#
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
16
768 by Martin Pool
- start some tests for directory renames
17
import os
18
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
19
import bzrlib
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
20
from bzrlib import (
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
21
    errors,
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
22
    osutils,
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
23
    tests,
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
24
    )
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
778 by Martin Pool
- simple revert of text files
26
27
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
28
class MoreTests(tests.TestCaseWithTransport):
771 by Martin Pool
- more tests of directory renames
29
1102 by Martin Pool
- merge test refactoring from robertc
30
    def test_relpath(self):
31
        """test for branch path lookups
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
32
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
33
        bzrlib.osutils._relpath do a simple but subtle
1102 by Martin Pool
- merge test refactoring from robertc
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
        """
3638.3.2 by Vincent Ladeuil
Fix all calls to tempfile.mkdtemp to osutils.mkdtemp.
37
        dtmp = osutils.mkdtemp()
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
38
        self.addCleanup(osutils.rmtree, dtmp)
907.1.7 by John Arbash Meinel
Fixed test failure on Mac OSX.
39
        # On Mac OSX, /tmp actually expands to /private/tmp
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
40
        dtmp = realpath(dtmp)
601 by Martin Pool
- whitebox tests for branch path handling
41
42
        def rp(p):
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
43
            return relpath(dtmp, p)
3638.3.2 by Vincent Ladeuil
Fix all calls to tempfile.mkdtemp to osutils.mkdtemp.
44
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
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')