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))
121
class TestEntryDiffing(TestCaseInTempDir):
124
super(TestEntryDiffing, self).setUp()
125
self.branch = Branch.initialize('.')
126
print >> open('file', 'wb'), 'foo'
127
self.branch.add(['file'], ['fileid'])
129
os.symlink('target1', 'symlink')
130
self.branch.add(['symlink'], ['linkid'])
131
self.branch.commit('message_1', rev_id = '1')
132
print >> open('file', 'wb'), 'bar'
135
os.symlink('target2', 'symlink')
136
self.tree_1 = self.branch.revision_tree('1')
137
self.inv_1 = self.branch.get_inventory('1')
138
self.file_1 = self.inv_1['fileid']
139
self.tree_2 = self.branch.working_tree()
140
self.inv_2 = self.branch.inventory
141
self.file_2 = self.inv_2['fileid']
143
self.link_1 = self.inv_1['linkid']
144
self.link_2 = self.inv_2['linkid']
146
def test_file_diff_deleted(self):
148
self.file_1.diff(internal_diff,
149
"old_label", self.tree_1,
150
"/dev/null", None, None,
152
self.assertEqual(output.getvalue(), "--- old_label\n"
158
def test_file_diff_added(self):
160
self.file_1.diff(internal_diff,
161
"new_label", self.tree_1,
162
"/dev/null", None, None,
163
output, reverse=True)
164
self.assertEqual(output.getvalue(), "--- /dev/null\n"
170
def test_file_diff_changed(self):
172
self.file_1.diff(internal_diff,
173
"/dev/null", self.tree_1,
174
"new_label", self.file_2, self.tree_2,
176
self.assertEqual(output.getvalue(), "--- /dev/null\n"
183
def test_link_diff_deleted(self):
185
self.link_1.diff(internal_diff,
186
"old_label", self.tree_1,
187
"/dev/null", None, None,
189
self.assertEqual(output.getvalue(),
190
"=== target was 'target1'\n")
192
def test_link_diff_added(self):
194
self.link_1.diff(internal_diff,
195
"new_label", self.tree_1,
196
"/dev/null", None, None,
197
output, reverse=True)
198
self.assertEqual(output.getvalue(),
199
"=== target is 'target1'\n")
201
def test_link_diff_changed(self):
203
self.link_1.diff(internal_diff,
204
"/dev/null", self.tree_1,
205
"new_label", self.link_2, self.tree_2,
207
self.assertEqual(output.getvalue(),
208
"=== target changed 'target1' => 'target2'\n")