1
# Copyright (C) 2005 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
17
from cStringIO import StringIO
20
from bzrlib.branch import Branch
21
from bzrlib.diff import internal_diff
22
from bzrlib.inventory import Inventory, InventoryEntry, ROOT_ID
23
from bzrlib.osutils import has_symlinks
24
from bzrlib.selftest import TestCase, TestCaseInTempDir
27
class TestInventory(TestCase):
29
def test_is_within(self):
30
from bzrlib.osutils import is_inside_any
32
SRC_FOO_C = os.path.join('src', 'foo.c')
33
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
37
self.assert_(is_inside_any(dirs, fn))
39
for dirs, fn in [(['src'], 'srccontrol'),
40
(['src'], 'srccontrol/foo')]:
41
self.assertFalse(is_inside_any(dirs, fn))
44
"""Test detection of files within selected directories."""
47
for args in [('src', 'directory', 'src-id'),
48
('doc', 'directory', 'doc-id'),
49
('src/hello.c', 'file'),
50
('src/bye.c', 'file', 'bye-id'),
51
('Makefile', 'file')]:
54
self.assertEqual(inv.path2id('src'), 'src-id')
55
self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
57
self.assert_('src-id' in inv)
60
def test_version(self):
61
"""Inventory remembers the text's version."""
63
ie = inv.add_path('foo.txt', 'file')
67
class TestInventoryEntry(TestCaseInTempDir):
69
def test_file_kind_character(self):
70
file = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
71
self.assertEqual(file.kind_character(), '')
73
def test_dir_kind_character(self):
74
dir = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
75
self.assertEqual(dir.kind_character(), '/')
77
def test_link_kind_character(self):
78
dir = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
79
self.assertEqual(dir.kind_character(), '')
81
def test_dir_detect_changes(self):
82
left = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
84
left.executable = True
85
left.symlink_target='foo'
86
right = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
88
right.symlink_target='bar'
89
self.assertEqual((False, False), left.detect_changes(right))
90
self.assertEqual((False, False), right.detect_changes(left))
92
def test_file_detect_changes(self):
93
left = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
95
right = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
97
self.assertEqual((False, False), left.detect_changes(right))
98
self.assertEqual((False, False), right.detect_changes(left))
99
left.executable = True
100
self.assertEqual((False, True), left.detect_changes(right))
101
self.assertEqual((False, True), right.detect_changes(left))
102
right.text_sha1 = 321
103
self.assertEqual((True, True), left.detect_changes(right))
104
self.assertEqual((True, True), right.detect_changes(left))
106
def test_symlink_detect_changes(self):
107
left = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
109
left.executable = True
110
left.symlink_target='foo'
111
right = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
112
right.text_sha1 = 321
113
right.symlink_target='foo'
114
self.assertEqual((False, False), left.detect_changes(right))
115
self.assertEqual((False, False), right.detect_changes(left))
116
left.symlink_target = 'different'
117
self.assertEqual((True, False), left.detect_changes(right))
118
self.assertEqual((True, False), right.detect_changes(left))
120
def test_file_has_text(self):
121
file = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
122
self.failUnless(file.has_text())
124
def test_directory_has_text(self):
125
dir = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
126
self.failIf(dir.has_text())
128
def test_link_has_text(self):
129
link = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
130
self.failIf(link.has_text())
133
class TestEntryDiffing(TestCaseInTempDir):
136
super(TestEntryDiffing, self).setUp()
137
self.branch = Branch.initialize('.')
138
print >> open('file', 'wb'), 'foo'
139
self.branch.add(['file'], ['fileid'])
141
os.symlink('target1', 'symlink')
142
self.branch.add(['symlink'], ['linkid'])
143
self.branch.commit('message_1', rev_id = '1')
144
print >> open('file', 'wb'), 'bar'
147
os.symlink('target2', 'symlink')
148
self.tree_1 = self.branch.revision_tree('1')
149
self.inv_1 = self.branch.get_inventory('1')
150
self.file_1 = self.inv_1['fileid']
151
self.tree_2 = self.branch.working_tree()
152
self.inv_2 = self.branch.inventory
153
self.file_2 = self.inv_2['fileid']
155
self.link_1 = self.inv_1['linkid']
156
self.link_2 = self.inv_2['linkid']
158
def test_file_diff_deleted(self):
160
self.file_1.diff(internal_diff,
161
"old_label", self.tree_1,
162
"/dev/null", None, None,
164
self.assertEqual(output.getvalue(), "--- old_label\n"
170
def test_file_diff_added(self):
172
self.file_1.diff(internal_diff,
173
"new_label", self.tree_1,
174
"/dev/null", None, None,
175
output, reverse=True)
176
self.assertEqual(output.getvalue(), "--- /dev/null\n"
182
def test_file_diff_changed(self):
184
self.file_1.diff(internal_diff,
185
"/dev/null", self.tree_1,
186
"new_label", self.file_2, self.tree_2,
188
self.assertEqual(output.getvalue(), "--- /dev/null\n"
195
def test_link_diff_deleted(self):
197
self.link_1.diff(internal_diff,
198
"old_label", self.tree_1,
199
"/dev/null", None, None,
201
self.assertEqual(output.getvalue(),
202
"=== target was 'target1'\n")
204
def test_link_diff_added(self):
206
self.link_1.diff(internal_diff,
207
"new_label", self.tree_1,
208
"/dev/null", None, None,
209
output, reverse=True)
210
self.assertEqual(output.getvalue(),
211
"=== target is 'target1'\n")
213
def test_link_diff_changed(self):
215
self.link_1.diff(internal_diff,
216
"/dev/null", self.tree_1,
217
"new_label", self.link_2, self.tree_2,
219
self.assertEqual(output.getvalue(),
220
"=== target changed 'target1' => 'target2'\n")