~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testinv.py

  • Committer: Aaron Bentley
  • Date: 2005-10-04 13:16:14 UTC
  • mfrom: (1393.1.44)
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1419.
  • Revision ID: abentley@panoramicfeedback.com-20051004131614-14e3446338b38f53
Merged the latest from newformat

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 cStringIO import StringIO
17
18
import os
18
 
from bzrlib.selftest import TestCase
19
19
 
20
 
from bzrlib.inventory import Inventory, InventoryEntry
 
20
from bzrlib.branch import Branch
 
21
from bzrlib.diff import internal_diff
 
22
from bzrlib.inventory import Inventory, ROOT_ID
 
23
import bzrlib.inventory as inventory
 
24
from bzrlib.osutils import has_symlinks
 
25
from bzrlib.selftest import TestCase, TestCaseInTempDir
21
26
 
22
27
 
23
28
class TestInventory(TestCase):
59
64
        ie = inv.add_path('foo.txt', 'file')
60
65
        ## XXX
61
66
 
 
67
 
 
68
class TestInventoryEntry(TestCaseInTempDir):
 
69
 
 
70
    def test_file_kind_character(self):
 
71
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
72
        self.assertEqual(file.kind_character(), '')
 
73
 
 
74
    def test_dir_kind_character(self):
 
75
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
76
        self.assertEqual(dir.kind_character(), '/')
 
77
 
 
78
    def test_link_kind_character(self):
 
79
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
80
        self.assertEqual(dir.kind_character(), '')
 
81
 
 
82
    def test_dir_detect_changes(self):
 
83
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
84
        left.text_sha1 = 123
 
85
        left.executable = True
 
86
        left.symlink_target='foo'
 
87
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
88
        right.text_sha1 = 321
 
89
        right.symlink_target='bar'
 
90
        self.assertEqual((False, False), left.detect_changes(right))
 
91
        self.assertEqual((False, False), right.detect_changes(left))
 
92
 
 
93
    def test_file_detect_changes(self):
 
94
        left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
95
        left.text_sha1 = 123
 
96
        right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
97
        right.text_sha1 = 123
 
98
        self.assertEqual((False, False), left.detect_changes(right))
 
99
        self.assertEqual((False, False), right.detect_changes(left))
 
100
        left.executable = True
 
101
        self.assertEqual((False, True), left.detect_changes(right))
 
102
        self.assertEqual((False, True), right.detect_changes(left))
 
103
        right.text_sha1 = 321
 
104
        self.assertEqual((True, True), left.detect_changes(right))
 
105
        self.assertEqual((True, True), right.detect_changes(left))
 
106
 
 
107
    def test_symlink_detect_changes(self):
 
108
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
109
        left.text_sha1 = 123
 
110
        left.executable = True
 
111
        left.symlink_target='foo'
 
112
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
113
        right.text_sha1 = 321
 
114
        right.symlink_target='foo'
 
115
        self.assertEqual((False, False), left.detect_changes(right))
 
116
        self.assertEqual((False, False), right.detect_changes(left))
 
117
        left.symlink_target = 'different'
 
118
        self.assertEqual((True, False), left.detect_changes(right))
 
119
        self.assertEqual((True, False), right.detect_changes(left))
 
120
 
 
121
    def test_file_has_text(self):
 
122
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
123
        self.failUnless(file.has_text())
 
124
 
 
125
    def test_directory_has_text(self):
 
126
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
127
        self.failIf(dir.has_text())
 
128
 
 
129
    def test_link_has_text(self):
 
130
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
131
        self.failIf(link.has_text())
 
132
 
 
133
 
 
134
class TestEntryDiffing(TestCaseInTempDir):
 
135
 
 
136
    def setUp(self):
 
137
        super(TestEntryDiffing, self).setUp()
 
138
        self.branch = Branch.initialize('.')
 
139
        print >> open('file', 'wb'), 'foo'
 
140
        self.branch.add(['file'], ['fileid'])
 
141
        if has_symlinks():
 
142
            os.symlink('target1', 'symlink')
 
143
            self.branch.add(['symlink'], ['linkid'])
 
144
        self.branch.commit('message_1', rev_id = '1')
 
145
        print >> open('file', 'wb'), 'bar'
 
146
        if has_symlinks():
 
147
            os.unlink('symlink')
 
148
            os.symlink('target2', 'symlink')
 
149
        self.tree_1 = self.branch.revision_tree('1')
 
150
        self.inv_1 = self.branch.get_inventory('1')
 
151
        self.file_1 = self.inv_1['fileid']
 
152
        self.tree_2 = self.branch.working_tree()
 
153
        self.inv_2 = self.branch.inventory
 
154
        self.file_2 = self.inv_2['fileid']
 
155
        if has_symlinks():
 
156
            self.link_1 = self.inv_1['linkid']
 
157
            self.link_2 = self.inv_2['linkid']
 
158
 
 
159
    def test_file_diff_deleted(self):
 
160
        output = StringIO()
 
161
        self.file_1.diff(internal_diff, 
 
162
                          "old_label", self.tree_1,
 
163
                          "/dev/null", None, None,
 
164
                          output)
 
165
        self.assertEqual(output.getvalue(), "--- old_label\n"
 
166
                                            "+++ /dev/null\n"
 
167
                                            "@@ -1,1 +0,0 @@\n"
 
168
                                            "-foo\n"
 
169
                                            "\n")
 
170
 
 
171
    def test_file_diff_added(self):
 
172
        output = StringIO()
 
173
        self.file_1.diff(internal_diff, 
 
174
                          "new_label", self.tree_1,
 
175
                          "/dev/null", None, None,
 
176
                          output, reverse=True)
 
177
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
 
178
                                            "+++ new_label\n"
 
179
                                            "@@ -0,0 +1,1 @@\n"
 
180
                                            "+foo\n"
 
181
                                            "\n")
 
182
 
 
183
    def test_file_diff_changed(self):
 
184
        output = StringIO()
 
185
        self.file_1.diff(internal_diff, 
 
186
                          "/dev/null", self.tree_1, 
 
187
                          "new_label", self.file_2, self.tree_2,
 
188
                          output)
 
189
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
 
190
                                            "+++ new_label\n"
 
191
                                            "@@ -1,1 +1,1 @@\n"
 
192
                                            "-foo\n"
 
193
                                            "+bar\n"
 
194
                                            "\n")
 
195
        
 
196
    def test_link_diff_deleted(self):
 
197
        output = StringIO()
 
198
        self.link_1.diff(internal_diff, 
 
199
                          "old_label", self.tree_1,
 
200
                          "/dev/null", None, None,
 
201
                          output)
 
202
        self.assertEqual(output.getvalue(),
 
203
                         "=== target was 'target1'\n")
 
204
 
 
205
    def test_link_diff_added(self):
 
206
        output = StringIO()
 
207
        self.link_1.diff(internal_diff, 
 
208
                          "new_label", self.tree_1,
 
209
                          "/dev/null", None, None,
 
210
                          output, reverse=True)
 
211
        self.assertEqual(output.getvalue(),
 
212
                         "=== target is 'target1'\n")
 
213
 
 
214
    def test_link_diff_changed(self):
 
215
        output = StringIO()
 
216
        self.link_1.diff(internal_diff, 
 
217
                          "/dev/null", self.tree_1, 
 
218
                          "new_label", self.link_2, self.tree_2,
 
219
                          output)
 
220
        self.assertEqual(output.getvalue(),
 
221
                         "=== target changed 'target1' => 'target2'\n")