4
from bzrlib.tests import TestCaseInTempDir, TestCase
5
from bzrlib.branch import ScratchBranch, Branch
6
from bzrlib.errors import PathNotChild
7
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
10
class TestBranch(TestCaseInTempDir):
12
def test_no_changes(self):
13
from bzrlib.errors import PointlessCommit
15
b = Branch.initialize(u'.')
17
self.build_tree(['hello.txt'])
19
self.assertRaises(PointlessCommit,
20
b.working_tree().commit,
21
'commit without adding',
22
allow_pointless=False)
24
b.working_tree().commit('commit pointless tree',
27
b.working_tree().add('hello.txt')
29
b.working_tree().commit('commit first added file',
30
allow_pointless=False)
32
self.assertRaises(PointlessCommit,
33
b.working_tree().commit,
34
'commit after adding file',
35
allow_pointless=False)
37
b.working_tree().commit('commit pointless revision with one file',
41
class MoreTests(TestCaseInTempDir):
43
def test_rename_dirs(self):
44
"""Test renaming directories and the files within them."""
45
b = Branch.initialize(u'.')
46
self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
47
b.working_tree().add(['dir', 'dir/sub', 'dir/sub/file'])
49
b.working_tree().commit('create initial state')
51
# TODO: lift out to a test helper that checks the shape of
54
revid = b.revision_history()[0]
55
self.log('first revision_id is {%s}' % revid)
57
inv = b.get_revision_inventory(revid)
58
self.log('contents of inventory: %r' % inv.entries())
60
self.check_inventory_shape(inv,
61
['dir', 'dir/sub', 'dir/sub/file'])
63
b.working_tree().rename_one('dir', 'newdir')
65
self.check_inventory_shape(b.working_tree().read_working_inventory(),
66
['newdir', 'newdir/sub', 'newdir/sub/file'])
68
b.working_tree().rename_one('newdir/sub', 'newdir/newsub')
69
self.check_inventory_shape(b.working_tree().read_working_inventory(),
70
['newdir', 'newdir/newsub',
71
'newdir/newsub/file'])
73
def test_relpath(self):
74
"""test for branch path lookups
76
bzrlib.osutils._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.
80
import tempfile, shutil
82
savedir = os.getcwdu()
83
dtmp = tempfile.mkdtemp()
84
# On Mac OSX, /tmp actually expands to /private/tmp
88
return relpath(dtmp, p)
91
# check paths inside dtmp while standing outside it
92
self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
95
self.assertEqual(rp(dtmp), '')
97
self.assertRaises(PathNotChild,
101
# now some near-miss operations -- note that
102
# os.path.commonprefix gets these wrong!
103
self.assertRaises(PathNotChild,
105
dtmp.rstrip('\\/') + '2')
107
self.assertRaises(PathNotChild,
109
dtmp.rstrip('\\/') + '2/foo')
111
# now operations based on relpath of files in current
112
# directory, or nearby
115
self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
117
self.assertEqual(rp('foo'), 'foo')
119
self.assertEqual(rp('./foo'), 'foo')
121
self.assertEqual(rp(abspath('foo')), 'foo')
123
self.assertRaises(PathNotChild,