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_grafts.py @ 959

History | View | Annotate | Download (6.37 KB)

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

    
20
"""Tests for graftpoints."""
21

    
22
import os
23
import tempfile
24
import shutil
25

    
26
from dulwich.errors import ObjectFormatException
27
from dulwich.tests import TestCase
28
from dulwich.objects import (
29
    Tree,
30
    )
31
from dulwich.repo import (
32
    parse_graftpoints,
33
    serialize_graftpoints,
34
    MemoryRepo,
35
    Repo,
36
)
37

    
38

    
39
def makesha(digit):
40
    return (str(digit).encode('ascii') * 40)[:40]
41

    
42

    
43
class GraftParserTests(TestCase):
44

    
45
    def assertParse(self, expected, graftpoints):
46
        self.assertEqual(expected, parse_graftpoints(iter(graftpoints)))
47

    
48
    def test_no_grafts(self):
49
        self.assertParse({}, [])
50

    
51
    def test_no_parents(self):
52
        self.assertParse({makesha(0): []}, [makesha(0)])
53

    
54
    def test_parents(self):
55
        self.assertParse({makesha(0): [makesha(1), makesha(2)]},
56
                         [b' '.join([makesha(0), makesha(1), makesha(2)])])
57

    
58
    def test_multiple_hybrid(self):
59
        self.assertParse(
60
            {makesha(0): [],
61
             makesha(1): [makesha(2)],
62
             makesha(3): [makesha(4), makesha(5)]},
63
            [makesha(0),
64
             b' '.join([makesha(1), makesha(2)]),
65
             b' '.join([makesha(3), makesha(4), makesha(5)])])
66

    
67

    
68
class GraftSerializerTests(TestCase):
69

    
70
    def assertSerialize(self, expected, graftpoints):
71
        self.assertEqual(
72
            sorted(expected),
73
            sorted(serialize_graftpoints(graftpoints)))
74

    
75
    def test_no_grafts(self):
76
        self.assertSerialize(b'', {})
77

    
78
    def test_no_parents(self):
79
        self.assertSerialize(makesha(0), {makesha(0): []})
80

    
81
    def test_parents(self):
82
        self.assertSerialize(b' '.join([makesha(0), makesha(1), makesha(2)]),
83
                             {makesha(0): [makesha(1), makesha(2)]})
84

    
85
    def test_multiple_hybrid(self):
86
        self.assertSerialize(
87
            b'\n'.join([
88
                makesha(0),
89
                b' '.join([makesha(1), makesha(2)]),
90
                b' '.join([makesha(3), makesha(4), makesha(5)])]),
91
            {makesha(0): [],
92
             makesha(1): [makesha(2)],
93
             makesha(3): [makesha(4), makesha(5)]})
94

    
95

    
96
class GraftsInRepositoryBase(object):
97

    
98
    def tearDown(self):
99
        super(GraftsInRepositoryBase, self).tearDown()
100

    
101
    def get_repo_with_grafts(self, grafts):
102
        r = self._repo
103
        r._add_graftpoints(grafts)
104
        return r
105

    
106
    def test_no_grafts(self):
107
        r = self.get_repo_with_grafts({})
108

    
109
        shas = [e.commit.id for e in r.get_walker()]
110
        self.assertEqual(shas, self._shas[::-1])
111

    
112
    def test_no_parents_graft(self):
113
        r = self.get_repo_with_grafts({self._repo.head(): []})
114

    
115
        self.assertEqual([e.commit.id for e in r.get_walker()],
116
                         [r.head()])
117

    
118
    def test_existing_parent_graft(self):
119
        r = self.get_repo_with_grafts({self._shas[-1]: [self._shas[0]]})
120

    
121
        self.assertEqual([e.commit.id for e in r.get_walker()],
122
                         [self._shas[-1], self._shas[0]])
123

    
124
    def test_remove_graft(self):
125
        r = self.get_repo_with_grafts({self._repo.head(): []})
126
        r._remove_graftpoints([self._repo.head()])
127

    
128
        self.assertEqual([e.commit.id for e in r.get_walker()],
129
                         self._shas[::-1])
130

    
131
    def test_object_store_fail_invalid_parents(self):
132
        r = self._repo
133

    
134
        self.assertRaises(
135
            ObjectFormatException,
136
            r._add_graftpoints,
137
            {self._shas[-1]: ['1']})
138

    
139

    
140
class GraftsInRepoTests(GraftsInRepositoryBase, TestCase):
141

    
142
    def setUp(self):
143
        super(GraftsInRepoTests, self).setUp()
144
        self._repo_dir = os.path.join(tempfile.mkdtemp())
145
        r = self._repo = Repo.init(self._repo_dir)
146
        self.addCleanup(shutil.rmtree, self._repo_dir)
147

    
148
        self._shas = []
149

    
150
        commit_kwargs = {
151
            'committer': b'Test Committer <test@nodomain.com>',
152
            'author': b'Test Author <test@nodomain.com>',
153
            'commit_timestamp': 12395,
154
            'commit_timezone': 0,
155
            'author_timestamp': 12395,
156
            'author_timezone': 0,
157
        }
158

    
159
        self._shas.append(r.do_commit(
160
            b'empty commit', **commit_kwargs))
161
        self._shas.append(r.do_commit(
162
            b'empty commit', **commit_kwargs))
163
        self._shas.append(r.do_commit(
164
            b'empty commit', **commit_kwargs))
165

    
166
    def test_init_with_empty_info_grafts(self):
167
        r = self._repo
168
        r._put_named_file(os.path.join('info', 'grafts'), b'')
169

    
170
        r = Repo(self._repo_dir)
171
        self.assertEqual({}, r._graftpoints)
172

    
173
    def test_init_with_info_grafts(self):
174
        r = self._repo
175
        r._put_named_file(
176
            os.path.join('info', 'grafts'),
177
            self._shas[-1] + b' ' + self._shas[0])
178

    
179
        r = Repo(self._repo_dir)
180
        self.assertEqual({self._shas[-1]: [self._shas[0]]}, r._graftpoints)
181

    
182

    
183
class GraftsInMemoryRepoTests(GraftsInRepositoryBase, TestCase):
184

    
185
    def setUp(self):
186
        super(GraftsInMemoryRepoTests, self).setUp()
187
        r = self._repo = MemoryRepo()
188

    
189
        self._shas = []
190

    
191
        tree = Tree()
192

    
193
        commit_kwargs = {
194
            'committer': b'Test Committer <test@nodomain.com>',
195
            'author': b'Test Author <test@nodomain.com>',
196
            'commit_timestamp': 12395,
197
            'commit_timezone': 0,
198
            'author_timestamp': 12395,
199
            'author_timezone': 0,
200
            'tree': tree.id
201
        }
202

    
203
        self._shas.append(r.do_commit(
204
            b'empty commit', **commit_kwargs))
205
        self._shas.append(r.do_commit(
206
            b'empty commit', **commit_kwargs))
207
        self._shas.append(r.do_commit(
208
            b'empty commit', **commit_kwargs))