1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from bzrlib.selftest import TestCase
19
from bzrlib.inventory import Inventory, InventoryEntry
22
class TestInventory(TestCase):
24
def test_is_within(self):
25
from bzrlib.osutils import is_inside_any
27
for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
28
(['src'], 'src/foo.c'),
31
self.assert_(is_inside_any(dirs, fn))
33
for dirs, fn in [(['src'], 'srccontrol'),
34
(['src'], 'srccontrol/foo')]:
35
self.assertFalse(is_inside_any(dirs, fn))
38
"""Test detection of files within selected directories."""
41
for args in [('src', 'directory', 'src-id'),
42
('doc', 'directory', 'doc-id'),
43
('src/hello.c', 'file'),
44
('src/bye.c', 'file', 'bye-id'),
45
('Makefile', 'file')]:
48
self.assertEqual(inv.path2id('src'), 'src-id')
49
self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
51
self.assert_('src-id' in inv)
54
def test_version(self):
55
"""Inventory remembers the text's version."""
57
ie = inv.add_path('foo.txt', 'file')
18
from bzrlib import errors, inventory, osutils
19
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
20
InventoryDirectory, InventoryEntry, TreeReference)
21
from bzrlib.tests import TestCase
24
class TestInventoryEntry(TestCase):
26
def test_file_kind_character(self):
27
file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
28
self.assertEqual(file.kind_character(), '')
30
def test_dir_kind_character(self):
31
dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
32
self.assertEqual(dir.kind_character(), '/')
34
def test_link_kind_character(self):
35
dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
36
self.assertEqual(dir.kind_character(), '')
38
def test_dir_detect_changes(self):
39
left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
41
left.executable = True
42
left.symlink_target='foo'
43
right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
45
right.symlink_target='bar'
46
self.assertEqual((False, False), left.detect_changes(right))
47
self.assertEqual((False, False), right.detect_changes(left))
49
def test_file_detect_changes(self):
50
left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
52
right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
54
self.assertEqual((False, False), left.detect_changes(right))
55
self.assertEqual((False, False), right.detect_changes(left))
56
left.executable = True
57
self.assertEqual((False, True), left.detect_changes(right))
58
self.assertEqual((False, True), right.detect_changes(left))
60
self.assertEqual((True, True), left.detect_changes(right))
61
self.assertEqual((True, True), right.detect_changes(left))
63
def test_symlink_detect_changes(self):
64
left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
66
left.executable = True
67
left.symlink_target='foo'
68
right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
70
right.symlink_target='foo'
71
self.assertEqual((False, False), left.detect_changes(right))
72
self.assertEqual((False, False), right.detect_changes(left))
73
left.symlink_target = 'different'
74
self.assertEqual((True, False), left.detect_changes(right))
75
self.assertEqual((True, False), right.detect_changes(left))
77
def test_file_has_text(self):
78
file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
79
self.failUnless(file.has_text())
81
def test_directory_has_text(self):
82
dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
83
self.failIf(dir.has_text())
85
def test_link_has_text(self):
86
link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
87
self.failIf(link.has_text())
89
def test_make_entry(self):
90
self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
91
inventory.InventoryFile)
92
self.assertIsInstance(inventory.make_entry("symlink", "name", ROOT_ID),
93
inventory.InventoryLink)
94
self.assertIsInstance(inventory.make_entry("directory", "name", ROOT_ID),
95
inventory.InventoryDirectory)
97
def test_make_entry_non_normalized(self):
98
orig_normalized_filename = osutils.normalized_filename
101
osutils.normalized_filename = osutils._accessible_normalized_filename
102
entry = inventory.make_entry("file", u'a\u030a', ROOT_ID)
103
self.assertEqual(u'\xe5', entry.name)
104
self.assertIsInstance(entry, inventory.InventoryFile)
106
osutils.normalized_filename = osutils._inaccessible_normalized_filename
107
self.assertRaises(errors.InvalidNormalization,
108
inventory.make_entry, 'file', u'a\u030a', ROOT_ID)
110
osutils.normalized_filename = orig_normalized_filename
113
class TestDescribeChanges(TestCase):
115
def test_describe_change(self):
116
# we need to test the following change combinations:
122
# renamed/reparented and modified
123
# change kind (perhaps can't be done yet?)
124
# also, merged in combination with all of these?
125
old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
126
old_a.text_sha1 = '123132'
128
new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
129
new_a.text_sha1 = '123132'
132
self.assertChangeDescription('unchanged', old_a, new_a)
135
new_a.text_sha1 = 'abcabc'
136
self.assertChangeDescription('modified', old_a, new_a)
138
self.assertChangeDescription('added', None, new_a)
139
self.assertChangeDescription('removed', old_a, None)
140
# perhaps a bit questionable but seems like the most reasonable thing...
141
self.assertChangeDescription('unchanged', None, None)
143
# in this case it's both renamed and modified; show a rename and
145
new_a.name = 'newfilename'
146
self.assertChangeDescription('modified and renamed', old_a, new_a)
148
# reparenting is 'renaming'
149
new_a.name = old_a.name
150
new_a.parent_id = 'somedir-id'
151
self.assertChangeDescription('modified and renamed', old_a, new_a)
153
# reset the content values so its not modified
154
new_a.text_size = old_a.text_size
155
new_a.text_sha1 = old_a.text_sha1
156
new_a.name = old_a.name
158
new_a.name = 'newfilename'
159
self.assertChangeDescription('renamed', old_a, new_a)
161
# reparenting is 'renaming'
162
new_a.name = old_a.name
163
new_a.parent_id = 'somedir-id'
164
self.assertChangeDescription('renamed', old_a, new_a)
166
def assertChangeDescription(self, expected_change, old_ie, new_ie):
167
change = InventoryEntry.describe_change(old_ie, new_ie)
168
self.assertEqual(expected_change, change)