1
# Copyright (C) 2005, 2006 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.tests import TestCaseWithTransport, TestCase
21
from bzrlib.branch import Branch
22
from bzrlib.errors import PathNotChild
23
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
26
class MoreTests(TestCaseWithTransport):
28
def test_relpath(self):
29
"""test for branch path lookups
31
bzrlib.osutils._relpath do a simple but subtle
32
job: given a path (either relative to cwd or absolute), work out
33
if it is inside a branch and return the path relative to the base.
36
from bzrlib.osutils import rmtree
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()),
23
class ValidateRevisionId(TestBase):
25
from bzrlib.revision import validate_revision_id
26
validate_revision_id('mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
28
self.assertRaises(ValueError,
33
self.assertRaises(ValueError,
35
'mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe\n')
38
self.assertRaises(ValueError,
40
' mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
42
self.assertRaises(ValueError,
44
'Martin Pool <mbp@sourcefrog.net>-20050311061123-96a255005c7c9dbe')
48
class Revert(InTempDir):
49
"""Test selected-file revert"""
51
b = Branch('.', init=True)
53
self.build_tree(['hello.txt'])
54
file('hello.txt', 'w').write('initial hello')
56
self.assertRaises(NotVersionedError,
57
b.revert, ['hello.txt'])
60
b.commit('create initial hello.txt')
62
self.check_file_contents('hello.txt', 'initial hello')
63
file('hello.txt', 'w').write('new hello')
64
self.check_file_contents('hello.txt', 'new hello')
66
# revert file modified since last revision
67
b.revert(['hello.txt'])
68
self.check_file_contents('hello.txt', 'initial hello')
69
self.check_file_contents('hello.txt~', 'new hello')
71
# reverting again clobbers the backup
72
b.revert(['hello.txt'])
73
self.check_file_contents('hello.txt', 'initial hello')
74
self.check_file_contents('hello.txt~', 'initial hello')
78
class RenameDirs(InTempDir):
79
"""Test renaming directories and the files within them."""
81
b = Branch('.', init=True)
82
self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
83
b.add(['dir', 'dir/sub', 'dir/sub/file'])
85
b.commit('create initial state')
87
# TODO: lift out to a test helper that checks the shape of
90
revid = b.revision_history()[0]
91
self.log('first revision_id is {%s}' % revid)
93
inv = b.get_revision_inventory(revid)
94
self.log('contents of inventory: %r' % inv.entries())
96
self.check_inventory_shape(inv,
97
['dir', 'dir/sub', 'dir/sub/file'])
99
b.rename_one('dir', 'newdir')
101
self.check_inventory_shape(b.inventory,
102
['newdir', 'newdir/sub', 'newdir/sub/file'])
104
b.rename_one('newdir/sub', 'newdir/newsub')
105
self.check_inventory_shape(b.inventory,
106
['newdir', 'newdir/newsub',
107
'newdir/newsub/file'])
112
class BranchPathTestCase(TestBase):
113
"""test for branch path lookups
115
Branch.relpath and bzrlib.branch._relpath do a simple but subtle
116
job: given a path (either relative to cwd or absolute), work out
117
if it is inside a branch and return the path relative to the base.
121
from bzrlib.branch import _relpath
122
import tempfile, shutil
38
124
savedir = os.getcwdu()
39
125
dtmp = tempfile.mkdtemp()
40
# On Mac OSX, /tmp actually expands to /private/tmp
44
return relpath(dtmp, p)
128
return _relpath(dtmp, p)
47
131
# check paths inside dtmp while standing outside it
48
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
132
self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
51
135
self.assertEqual(rp(dtmp), '')
53
self.assertRaises(PathNotChild,
137
self.assertRaises(NotBranchError,
57
141
# now some near-miss operations -- note that
58
142
# os.path.commonprefix gets these wrong!
59
self.assertRaises(PathNotChild,
143
self.assertRaises(NotBranchError,
61
145
dtmp.rstrip('\\/') + '2')
63
self.assertRaises(PathNotChild,
147
self.assertRaises(NotBranchError,
65
149
dtmp.rstrip('\\/') + '2/foo')