~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: Alexander Belchenko
  • Date: 2008-03-11 08:49:42 UTC
  • mto: This revision was merged to the branch mainline in revision 3268.
  • Revision ID: bialix@ukr.net-20080311084942-w1w0w3v0m20p2pbc
use sys.version_info

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
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.
7
 
 
 
7
#
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.
12
 
 
 
12
#
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
16
16
 
17
 
from bzrlib.selftest import TestBase
18
 
 
19
 
from bzrlib.inventory import Inventory, InventoryEntry
20
 
 
21
 
class TestIsWithin(TestBase):
22
 
    def runTest(self):
23
 
        """Test detection of files within selected directories."""
24
 
        inv = Inventory()
25
 
        
26
 
        for args in [('src', 'directory', 'src-id'), 
27
 
                     ('doc', 'directory', 'doc-id'), 
28
 
                     ('src/hello.c', 'file'),
29
 
                     ('src/bye.c', 'file', 'bye-id'),
30
 
                     ('Makefile', 'file')]:
31
 
            inv.add_path(*args)
32
 
            
33
 
        self.assertEqual(inv.path2id('src'), 'src-id')
34
 
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
35
 
        
36
 
        self.assert_('src-id' in inv)
37
 
        
38
 
        
 
 
b'\\ No newline at end of file'
 
17
 
 
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
 
22
 
 
23
 
 
24
class TestInventoryEntry(TestCase):
 
25
 
 
26
    def test_file_kind_character(self):
 
27
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
28
        self.assertEqual(file.kind_character(), '')
 
29
 
 
30
    def test_dir_kind_character(self):
 
31
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
32
        self.assertEqual(dir.kind_character(), '/')
 
33
 
 
34
    def test_link_kind_character(self):
 
35
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
36
        self.assertEqual(dir.kind_character(), '')
 
37
 
 
38
    def test_dir_detect_changes(self):
 
39
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
40
        left.text_sha1 = 123
 
41
        left.executable = True
 
42
        left.symlink_target='foo'
 
43
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
44
        right.text_sha1 = 321
 
45
        right.symlink_target='bar'
 
46
        self.assertEqual((False, False), left.detect_changes(right))
 
47
        self.assertEqual((False, False), right.detect_changes(left))
 
48
 
 
49
    def test_file_detect_changes(self):
 
50
        left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
51
        left.text_sha1 = 123
 
52
        right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
53
        right.text_sha1 = 123
 
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))
 
59
        right.text_sha1 = 321
 
60
        self.assertEqual((True, True), left.detect_changes(right))
 
61
        self.assertEqual((True, True), right.detect_changes(left))
 
62
 
 
63
    def test_symlink_detect_changes(self):
 
64
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
65
        left.text_sha1 = 123
 
66
        left.executable = True
 
67
        left.symlink_target='foo'
 
68
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
69
        right.text_sha1 = 321
 
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))
 
76
 
 
77
    def test_file_has_text(self):
 
78
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
79
        self.failUnless(file.has_text())
 
80
 
 
81
    def test_directory_has_text(self):
 
82
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
83
        self.failIf(dir.has_text())
 
84
 
 
85
    def test_link_has_text(self):
 
86
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
87
        self.failIf(link.has_text())
 
88
 
 
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)
 
96
 
 
97
    def test_make_entry_non_normalized(self):
 
98
        orig_normalized_filename = osutils.normalized_filename
 
99
 
 
100
        try:
 
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)
 
105
 
 
106
            osutils.normalized_filename = osutils._inaccessible_normalized_filename
 
107
            self.assertRaises(errors.InvalidNormalization,
 
108
                    inventory.make_entry, 'file', u'a\u030a', ROOT_ID)
 
109
        finally:
 
110
            osutils.normalized_filename = orig_normalized_filename
 
111
 
 
112
 
 
113
class TestDescribeChanges(TestCase):
 
114
 
 
115
    def test_describe_change(self):
 
116
        # we need to test the following change combinations:
 
117
        # rename
 
118
        # reparent
 
119
        # modify
 
120
        # gone
 
121
        # added
 
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'
 
127
        old_a.text_size = 0
 
128
        new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
129
        new_a.text_sha1 = '123132'
 
130
        new_a.text_size = 0
 
131
 
 
132
        self.assertChangeDescription('unchanged', old_a, new_a)
 
133
 
 
134
        new_a.text_size = 10
 
135
        new_a.text_sha1 = 'abcabc'
 
136
        self.assertChangeDescription('modified', old_a, new_a)
 
137
 
 
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)
 
142
 
 
143
        # in this case it's both renamed and modified; show a rename and 
 
144
        # modification:
 
145
        new_a.name = 'newfilename'
 
146
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
147
 
 
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)
 
152
 
 
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
 
157
 
 
158
        new_a.name = 'newfilename'
 
159
        self.assertChangeDescription('renamed', old_a, new_a)
 
160
 
 
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)
 
165
 
 
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)