Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / tests / test_missing_obj_finder.py @ 959

History | View | Annotate | Download (10.2 KB)

1
# test_missing_obj_finder.py -- tests for MissingObjectFinder
2
# Copyright (C) 2012 syntevo GmbH
3
#
4
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
5
# General Public License as public by the Free Software Foundation; version 2.0
6
# or (at your option) any later version. You can redistribute it and/or
7
# modify it under the terms of either of these two licenses.
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
#
15
# You should have received a copy of the licenses; if not, see
16
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
17
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
18
# License, Version 2.0.
19
#
20

    
21
from dulwich.object_store import (
22
    MemoryObjectStore,
23
    )
24
from dulwich.objects import (
25
    Blob,
26
    )
27
from dulwich.tests import TestCase
28
from dulwich.tests.utils import (
29
    make_object,
30
    make_tag,
31
    build_commit_graph,
32
    )
33

    
34

    
35
class MissingObjectFinderTest(TestCase):
36

    
37
    def setUp(self):
38
        super(MissingObjectFinderTest, self).setUp()
39
        self.store = MemoryObjectStore()
40
        self.commits = []
41

    
42
    def cmt(self, n):
43
        return self.commits[n-1]
44

    
45
    def assertMissingMatch(self, haves, wants, expected):
46
        for sha, path in self.store.find_missing_objects(haves, wants):
47
            self.assertTrue(sha in expected,
48
                "(%s,%s) erroneously reported as missing" % (sha, path))
49
            expected.remove(sha)
50

    
51
        self.assertEqual(len(expected), 0,
52
            "some objects are not reported as missing: %s" % (expected, ))
53

    
54

    
55
class MOFLinearRepoTest(MissingObjectFinderTest):
56

    
57
    def setUp(self):
58
        super(MOFLinearRepoTest, self).setUp()
59
        f1_1 = make_object(Blob, data=b'f1') # present in 1, removed in 3
60
        f2_1 = make_object(Blob, data=b'f2') # present in all revisions, changed in 2 and 3
61
        f2_2 = make_object(Blob, data=b'f2-changed')
62
        f2_3 = make_object(Blob, data=b'f2-changed-again')
63
        f3_2 = make_object(Blob, data=b'f3') # added in 2, left unmodified in 3
64

    
65
        commit_spec = [[1], [2, 1], [3, 2]]
66
        trees = {1: [(b'f1', f1_1), (b'f2', f2_1)],
67
                 2: [(b'f1', f1_1), (b'f2', f2_2), (b'f3', f3_2)],
68
                 3: [(b'f2', f2_3), (b'f3', f3_2)] }
69
        # commit 1: f1 and f2
70
        # commit 2: f3 added, f2 changed. Missing shall report commit id and a
71
        # tree referenced by commit
72
        # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
73
        # be reported as modified
74
        self.commits = build_commit_graph(self.store, commit_spec, trees)
75
        self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
76
        self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
77
        self.missing_1_3 = [
78
            self.cmt(2).id, self.cmt(3).id,
79
            self.cmt(2).tree, self.cmt(3).tree,
80
            f2_2.id, f3_2.id, f2_3.id]
81

    
82
    def test_1_to_2(self):
83
        self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id],
84
            self.missing_1_2)
85

    
86
    def test_2_to_3(self):
87
        self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id],
88
            self.missing_2_3)
89

    
90
    def test_1_to_3(self):
91
        self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id],
92
            self.missing_1_3)
93

    
94
    def test_bogus_haves(self):
95
        """Ensure non-existent SHA in haves are tolerated"""
96
        bogus_sha = self.cmt(2).id[::-1]
97
        haves = [self.cmt(1).id, bogus_sha]
98
        wants = [self.cmt(3).id]
99
        self.assertMissingMatch(haves, wants, self.missing_1_3)
100

    
101
    def test_bogus_wants_failure(self):
102
        """Ensure non-existent SHA in wants are not tolerated"""
103
        bogus_sha = self.cmt(2).id[::-1]
104
        haves = [self.cmt(1).id]
105
        wants = [self.cmt(3).id, bogus_sha]
106
        self.assertRaises(KeyError, self.store.find_missing_objects,
107
            haves, wants)
108

    
109
    def test_no_changes(self):
110
        self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
111

    
112

    
113
class MOFMergeForkRepoTest(MissingObjectFinderTest):
114
    # 1 --- 2 --- 4 --- 6 --- 7
115
    #          \        /
116
    #           3  ---
117
    #            \
118
    #             5
119

    
120
    def setUp(self):
121
        super(MOFMergeForkRepoTest, self).setUp()
122
        f1_1 = make_object(Blob, data=b'f1')
123
        f1_2 = make_object(Blob, data=b'f1-2')
124
        f1_4 = make_object(Blob, data=b'f1-4')
125
        f1_7 = make_object(Blob, data=b'f1-2') # same data as in rev 2
126
        f2_1 = make_object(Blob, data=b'f2')
127
        f2_3 = make_object(Blob, data=b'f2-3')
128
        f3_3 = make_object(Blob, data=b'f3')
129
        f3_5 = make_object(Blob, data=b'f3-5')
130
        commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
131
        trees = {1: [(b'f1', f1_1), (b'f2', f2_1)],
132
                2: [(b'f1', f1_2), (b'f2', f2_1)], # f1 changed
133
                # f3 added, f2 changed
134
                3: [(b'f1', f1_2), (b'f2', f2_3), (b'f3', f3_3)],
135
                4: [(b'f1', f1_4), (b'f2', f2_1)],  # f1 changed
136
                5: [(b'f1', f1_2), (b'f3', f3_5)], # f2 removed, f3 changed
137
                6: [(b'f1', f1_4), (b'f2', f2_3), (b'f3', f3_3)], # merged 3 and 4
138
                # f1 changed to match rev2. f3 removed
139
                7: [(b'f1', f1_7), (b'f2', f2_3)]}
140
        self.commits = build_commit_graph(self.store, commit_spec, trees)
141

    
142
        self.f1_2_id = f1_2.id
143
        self.f1_4_id = f1_4.id
144
        self.f1_7_id = f1_7.id
145
        self.f2_3_id = f2_3.id
146
        self.f3_3_id = f3_3.id
147

    
148
        self.assertEqual(f1_2.id, f1_7.id, "[sanity]")
149

    
150
    def test_have6_want7(self):
151
        # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
152
        # f1_2, however, to do so, MissingObjectFinder shall not record trees
153
        # of common commits only, but also all parent trees and tree items,
154
        # which is an overkill (i.e. in sha_done it records f1_4 as known, and
155
        # doesn't record f1_2 was known prior to that, hence can't detect f1_7
156
        # is in fact f1_2 and shall not be reported)
157
        self.assertMissingMatch([self.cmt(6).id], [self.cmt(7).id],
158
            [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id])
159

    
160
    def test_have4_want7(self):
161
        # have 4, want 7. Shall not include rev5 as it is not in the tree
162
        # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
163
        # commit hierarchy)
164
        self.assertMissingMatch([self.cmt(4).id], [self.cmt(7).id], [
165
            self.cmt(7).id, self.cmt(6).id, self.cmt(3).id,
166
            self.cmt(7).tree, self.cmt(6).tree, self.cmt(3).tree,
167
            self.f2_3_id, self.f3_3_id])
168

    
169
    def test_have1_want6(self):
170
        # have 1, want 6. Shall not include rev5
171
        self.assertMissingMatch([self.cmt(1).id], [self.cmt(6).id], [
172
            self.cmt(6).id, self.cmt(4).id, self.cmt(3).id, self.cmt(2).id,
173
            self.cmt(6).tree, self.cmt(4).tree, self.cmt(3).tree,
174
            self.cmt(2).tree, self.f1_2_id, self.f1_4_id, self.f2_3_id,
175
            self.f3_3_id])
176

    
177
    def test_have3_want6(self):
178
        # have 3, want 7. Shall not report rev2 and its tree, because
179
        # haves(3) means has parents, i.e. rev2, too
180
        # BUT shall report any changes descending rev2 (excluding rev3)
181
        # Shall NOT report f1_7 as it's techically == f1_2
182
        self.assertMissingMatch([self.cmt(3).id], [self.cmt(7).id], [
183
              self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
184
              self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
185
              self.f1_4_id])
186

    
187
    def test_have5_want7(self):
188
        # have 5, want 7. Common parent is rev2, hence children of rev2 from
189
        # a descent line other than rev5 shall be reported
190
        # expects f1_4 from rev6. f3_5 is known in rev5;
191
        # f1_7 shall be the same as f1_2 (known, too)
192
        self.assertMissingMatch([self.cmt(5).id], [self.cmt(7).id], [
193
              self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
194
              self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
195
              self.f1_4_id])
196

    
197

    
198
class MOFTagsTest(MissingObjectFinderTest):
199

    
200
    def setUp(self):
201
        super(MOFTagsTest, self).setUp()
202
        f1_1 = make_object(Blob, data=b'f1')
203
        commit_spec = [[1]]
204
        trees = {1: [(b'f1', f1_1)]}
205
        self.commits = build_commit_graph(self.store, commit_spec, trees)
206

    
207
        self._normal_tag = make_tag(self.cmt(1))
208
        self.store.add_object(self._normal_tag)
209

    
210
        self._tag_of_tag = make_tag(self._normal_tag)
211
        self.store.add_object(self._tag_of_tag)
212

    
213
        self._tag_of_tree = make_tag(self.store[self.cmt(1).tree])
214
        self.store.add_object(self._tag_of_tree)
215

    
216
        self._tag_of_blob = make_tag(f1_1)
217
        self.store.add_object(self._tag_of_blob)
218

    
219
        self._tag_of_tag_of_blob = make_tag(self._tag_of_blob)
220
        self.store.add_object(self._tag_of_tag_of_blob)
221

    
222
        self.f1_1_id = f1_1.id
223

    
224
    def test_tagged_commit(self):
225
        # The user already has the tagged commit, all they want is the tag,
226
        # so send them only the tag object.
227
        self.assertMissingMatch([self.cmt(1).id], [self._normal_tag.id],
228
                                [self._normal_tag.id])
229

    
230
    # The remaining cases are unusual, but do happen in the wild.
231
    def test_tagged_tag(self):
232
        # User already has tagged tag, send only tag of tag
233
        self.assertMissingMatch([self._normal_tag.id], [self._tag_of_tag.id],
234
                                [self._tag_of_tag.id])
235
        # User needs both tags, but already has commit
236
        self.assertMissingMatch([self.cmt(1).id], [self._tag_of_tag.id],
237
                                [self._normal_tag.id, self._tag_of_tag.id])
238

    
239
    def test_tagged_tree(self):
240
        self.assertMissingMatch(
241
            [], [self._tag_of_tree.id],
242
            [self._tag_of_tree.id, self.cmt(1).tree, self.f1_1_id])
243

    
244
    def test_tagged_blob(self):
245
        self.assertMissingMatch([], [self._tag_of_blob.id],
246
                                [self._tag_of_blob.id, self.f1_1_id])
247

    
248
    def test_tagged_tagged_blob(self):
249
        self.assertMissingMatch([], [self._tag_of_tag_of_blob.id],
250
                                [self._tag_of_tag_of_blob.id,
251
                                 self._tag_of_blob.id, self.f1_1_id])