1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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
23
from bzrlib.tests import TestCaseWithTransport, TestCase
24
from bzrlib.branch import Branch
25
from bzrlib.errors import PathNotChild
26
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
29
class MoreTests(TestCaseWithTransport):
31
def test_relpath(self):
32
"""test for branch path lookups
34
bzrlib.osutils._relpath do a simple but subtle
35
job: given a path (either relative to cwd or absolute), work out
36
if it is inside a branch and return the path relative to the base.
6
from bzrlib.selftest import InTempDir, TestBase
7
from bzrlib.branch import ScratchBranch, Branch
8
from bzrlib.errors import NotBranchError, NotVersionedError
11
class Revert(InTempDir):
12
"""Test selected-file revert"""
14
b = Branch('.', init=True)
16
self.build_tree(['hello.txt'])
17
file('hello.txt', 'w').write('initial hello')
19
self.assertRaises(NotVersionedError,
20
b.revert, ['hello.txt'])
23
b.commit('create initial hello.txt')
25
self.check_file_contents('hello.txt', 'initial hello')
26
file('hello.txt', 'w').write('new hello')
27
self.check_file_contents('hello.txt', 'new hello')
29
# revert file modified since last revision
30
b.revert(['hello.txt'])
31
self.check_file_contents('hello.txt', 'initial hello')
33
# reverting again causes no change
34
b.revert(['hello.txt'])
35
self.check_file_contents('hello.txt', 'initial hello')
39
class RenameDirs(InTempDir):
40
"""Test renaming directories and the files within them."""
42
b = Branch('.', init=True)
43
self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
44
b.add(['dir', 'dir/sub', 'dir/sub/file'])
46
b.commit('create initial state')
48
# TODO: lift out to a test helper that checks the shape of
51
revid = b.revision_history()[0]
52
self.log('first revision_id is {%s}' % revid)
54
inv = b.get_revision_inventory(revid)
55
self.log('contents of inventory: %r' % inv.entries())
57
self.check_inventory_shape(inv,
58
['dir', 'dir/sub', 'dir/sub/file'])
60
b.rename_one('dir', 'newdir')
62
self.check_inventory_shape(b.inventory,
63
['newdir', 'newdir/sub', 'newdir/sub/file'])
65
b.rename_one('newdir/sub', 'newdir/newsub')
66
self.check_inventory_shape(b.inventory,
67
['newdir', 'newdir/newsub',
68
'newdir/newsub/file'])
73
class BranchPathTestCase(TestBase):
74
"""test for branch path lookups
76
Branch.relpath and bzrlib.branch._relpath do a simple but subtle
77
job: given a path (either relative to cwd or absolute), work out
78
if it is inside a branch and return the path relative to the base.
82
from bzrlib.branch import _relpath
83
import tempfile, shutil
40
85
savedir = os.getcwdu()
41
dtmp = osutils.mkdtemp()
42
# On Mac OSX, /tmp actually expands to /private/tmp
86
dtmp = tempfile.mkdtemp()
46
return relpath(dtmp, p)
89
return _relpath(dtmp, p)
49
92
# check paths inside dtmp while standing outside it
50
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
93
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
53
96
self.assertEqual(rp(dtmp), '')
55
self.assertRaises(PathNotChild,
98
self.assertRaises(NotBranchError,
59
102
# now some near-miss operations -- note that
60
103
# os.path.commonprefix gets these wrong!
61
self.assertRaises(PathNotChild,
104
self.assertRaises(NotBranchError,
63
106
dtmp.rstrip('\\/') + '2')
65
self.assertRaises(PathNotChild,
108
self.assertRaises(NotBranchError,
67
110
dtmp.rstrip('\\/') + '2/foo')