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 Unknowns(InTempDir):
13
b = Branch('.', init=True)
15
self.build_tree(['hello.txt',
18
self.assertEquals(list(b.unknowns()),
22
class Revert(InTempDir):
23
"""Test selected-file revert"""
25
b = Branch('.', init=True)
27
self.build_tree(['hello.txt'])
28
file('hello.txt', 'w').write('initial hello')
30
self.assertRaises(NotVersionedError,
31
b.revert, ['hello.txt'])
34
b.commit('create initial hello.txt')
36
self.check_file_contents('hello.txt', 'initial hello')
37
file('hello.txt', 'w').write('new hello')
38
self.check_file_contents('hello.txt', 'new hello')
40
# revert file modified since last revision
41
b.revert(['hello.txt'])
42
self.check_file_contents('hello.txt', 'initial hello')
43
self.check_file_contents('hello.txt~', 'new hello')
45
# reverting again clobbers the backup
46
b.revert(['hello.txt'])
47
self.check_file_contents('hello.txt', 'initial hello')
48
self.check_file_contents('hello.txt~', 'initial hello')
52
class RenameDirs(InTempDir):
53
"""Test renaming directories and the files within them."""
55
b = Branch('.', init=True)
56
self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
57
b.add(['dir', 'dir/sub', 'dir/sub/file'])
59
b.commit('create initial state')
61
# TODO: lift out to a test helper that checks the shape of
64
revid = b.revision_history()[0]
65
self.log('first revision_id is {%s}' % revid)
67
inv = b.get_revision_inventory(revid)
68
self.log('contents of inventory: %r' % inv.entries())
70
self.check_inventory_shape(inv,
71
['dir', 'dir/sub', 'dir/sub/file'])
73
b.rename_one('dir', 'newdir')
75
self.check_inventory_shape(b.inventory,
76
['newdir', 'newdir/sub', 'newdir/sub/file'])
78
b.rename_one('newdir/sub', 'newdir/newsub')
79
self.check_inventory_shape(b.inventory,
80
['newdir', 'newdir/newsub',
81
'newdir/newsub/file'])
86
class BranchPathTestCase(TestBase):
87
"""test for branch path lookups
89
Branch.relpath and bzrlib.branch._relpath do a simple but subtle
90
job: given a path (either relative to cwd or absolute), work out
91
if it is inside a branch and return the path relative to the base.
95
from bzrlib.branch import _relpath
96
import tempfile, shutil
40
98
savedir = os.getcwdu()
41
dtmp = osutils.mkdtemp()
42
# On Mac OSX, /tmp actually expands to /private/tmp
99
dtmp = tempfile.mkdtemp()
46
return relpath(dtmp, p)
102
return _relpath(dtmp, p)
49
105
# check paths inside dtmp while standing outside it
50
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
106
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
53
109
self.assertEqual(rp(dtmp), '')
55
self.assertRaises(PathNotChild,
111
self.assertRaises(NotBranchError,
59
115
# now some near-miss operations -- note that
60
116
# os.path.commonprefix gets these wrong!
61
self.assertRaises(PathNotChild,
117
self.assertRaises(NotBranchError,
63
119
dtmp.rstrip('\\/') + '2')
65
self.assertRaises(PathNotChild,
121
self.assertRaises(NotBranchError,
67
123
dtmp.rstrip('\\/') + '2/foo')