-
-
Notifications
You must be signed in to change notification settings - Fork 383
/
test_remote.py
382 lines (293 loc) · 11.6 KB
/
test_remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright 2010-2024 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file. (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Tests for Remote objects."""
from unittest.mock import patch
import sys
import pytest
import pygit2
from pygit2 import Oid
from pygit2.ffi import ffi
from . import utils
REMOTE_NAME = 'origin'
REMOTE_URL = 'https://github.com/libgit2/pygit2.git'
REMOTE_FETCHSPEC_SRC = 'refs/heads/*'
REMOTE_FETCHSPEC_DST = 'refs/remotes/origin/*'
REMOTE_REPO_OBJECTS = 30
REMOTE_FETCHTEST_FETCHSPECS = ['refs/tags/v1.13.2']
REMOTE_REPO_FETCH_ALL_OBJECTS = 13276
REMOTE_REPO_FETCH_HEAD_COMMIT_OBJECTS = 238
ORIGIN_REFSPEC = '+refs/heads/*:refs/remotes/origin/*'
def test_remote_create(testrepo):
name = 'upstream'
url = 'https://github.com/libgit2/pygit2.git'
remote = testrepo.remotes.create(name, url)
assert type(remote) == pygit2.Remote
assert name == remote.name
assert url == remote.url
assert remote.push_url is None
with pytest.raises(ValueError):
testrepo.remotes.create(*(name, url))
def test_remote_create_with_refspec(testrepo):
name = 'upstream'
url = 'https://github.com/libgit2/pygit2.git'
fetch = '+refs/*:refs/*'
remote = testrepo.remotes.create(name, url, fetch)
assert type(remote) == pygit2.Remote
assert name == remote.name
assert url == remote.url
assert [fetch] == remote.fetch_refspecs
assert remote.push_url is None
def test_remote_create_anonymous(testrepo):
url = 'https://github.com/libgit2/pygit2.git'
remote = testrepo.remotes.create_anonymous(url)
assert remote.name is None
assert url == remote.url
assert remote.push_url is None
assert [] == remote.fetch_refspecs
assert [] == remote.push_refspecs
def test_remote_delete(testrepo):
name = 'upstream'
url = 'https://github.com/libgit2/pygit2.git'
testrepo.remotes.create(name, url)
assert 2 == len(testrepo.remotes)
remote = testrepo.remotes[1]
assert name == remote.name
testrepo.remotes.delete(remote.name)
assert 1 == len(testrepo.remotes)
def test_remote_rename(testrepo):
remote = testrepo.remotes[0]
assert REMOTE_NAME == remote.name
problems = testrepo.remotes.rename(remote.name, 'new')
assert [] == problems
assert 'new' != remote.name
with pytest.raises(ValueError):
testrepo.remotes.rename('', '')
with pytest.raises(ValueError):
testrepo.remotes.rename(None, None)
def test_remote_set_url(testrepo):
remote = testrepo.remotes['origin']
assert REMOTE_URL == remote.url
new_url = 'https://github.com/cholin/pygit2.git'
testrepo.remotes.set_url('origin', new_url)
remote = testrepo.remotes['origin']
assert new_url == remote.url
with pytest.raises(ValueError):
testrepo.remotes.set_url('origin', '')
testrepo.remotes.set_push_url('origin', new_url)
remote = testrepo.remotes['origin']
assert new_url == remote.push_url
with pytest.raises(ValueError):
testrepo.remotes.set_push_url('origin', '')
def test_refspec(testrepo):
remote = testrepo.remotes['origin']
assert remote.refspec_count == 1
refspec = remote.get_refspec(0)
assert refspec.src == REMOTE_FETCHSPEC_SRC
assert refspec.dst == REMOTE_FETCHSPEC_DST
assert refspec.force is True
assert ORIGIN_REFSPEC == refspec.string
assert list == type(remote.fetch_refspecs)
assert 1 == len(remote.fetch_refspecs)
assert ORIGIN_REFSPEC == remote.fetch_refspecs[0]
assert refspec.src_matches('refs/heads/master')
assert refspec.dst_matches('refs/remotes/origin/master')
assert 'refs/remotes/origin/master' == refspec.transform('refs/heads/master')
assert 'refs/heads/master' == refspec.rtransform('refs/remotes/origin/master')
assert list == type(remote.push_refspecs)
assert 0 == len(remote.push_refspecs)
push_specs = remote.push_refspecs
assert list == type(push_specs)
assert 0 == len(push_specs)
testrepo.remotes.add_fetch('origin', '+refs/test/*:refs/test/remotes/*')
remote = testrepo.remotes['origin']
fetch_specs = remote.fetch_refspecs
assert list == type(fetch_specs)
assert 2 == len(fetch_specs)
assert [
'+refs/heads/*:refs/remotes/origin/*',
'+refs/test/*:refs/test/remotes/*',
] == fetch_specs
testrepo.remotes.add_push('origin', '+refs/test/*:refs/test/remotes/*')
with pytest.raises(TypeError):
testrepo.remotes.add_fetch(['+refs/*:refs/*', 5])
remote = testrepo.remotes['origin']
assert ['+refs/test/*:refs/test/remotes/*'] == remote.push_refspecs
def test_remote_list(testrepo):
assert 1 == len(testrepo.remotes)
remote = testrepo.remotes[0]
assert REMOTE_NAME == remote.name
assert REMOTE_URL == remote.url
name = 'upstream'
url = 'https://github.com/libgit2/pygit2.git'
remote = testrepo.remotes.create(name, url)
assert remote.name in testrepo.remotes.names()
assert remote.name in [x.name for x in testrepo.remotes]
@utils.requires_network
def test_ls_remotes(testrepo):
assert 1 == len(testrepo.remotes)
remote = testrepo.remotes[0]
refs = remote.ls_remotes()
assert refs
# Check that a known ref is returned.
assert next(iter(r for r in refs if r['name'] == 'refs/tags/v0.28.2'))
def test_remote_collection(testrepo):
remote = testrepo.remotes['origin']
assert REMOTE_NAME == remote.name
assert REMOTE_URL == remote.url
with pytest.raises(KeyError):
testrepo.remotes['upstream']
name = 'upstream'
url = 'https://github.com/libgit2/pygit2.git'
remote = testrepo.remotes.create(name, url)
assert remote.name in testrepo.remotes.names()
assert remote.name in [x.name for x in testrepo.remotes]
@utils.refcount
def test_remote_refcount(testrepo):
start = sys.getrefcount(testrepo)
remote = testrepo.remotes[0]
del remote
end = sys.getrefcount(testrepo)
assert start == end
def test_fetch(emptyrepo):
remote = emptyrepo.remotes[0]
stats = remote.fetch()
assert stats.received_bytes > 2700
assert stats.received_bytes < 3100
assert stats.indexed_objects == REMOTE_REPO_OBJECTS
assert stats.received_objects == REMOTE_REPO_OBJECTS
@utils.requires_network
def test_fetch_depth_zero(testrepo):
remote = testrepo.remotes[0]
stats = remote.fetch(REMOTE_FETCHTEST_FETCHSPECS, depth=0)
assert stats.indexed_objects == REMOTE_REPO_FETCH_ALL_OBJECTS
assert stats.received_objects == REMOTE_REPO_FETCH_ALL_OBJECTS
@utils.requires_network
def test_fetch_depth_one(testrepo):
remote = testrepo.remotes[0]
stats = remote.fetch(REMOTE_FETCHTEST_FETCHSPECS, depth=1)
assert stats.indexed_objects == REMOTE_REPO_FETCH_HEAD_COMMIT_OBJECTS
assert stats.received_objects == REMOTE_REPO_FETCH_HEAD_COMMIT_OBJECTS
def test_transfer_progress(emptyrepo):
class MyCallbacks(pygit2.RemoteCallbacks):
def transfer_progress(emptyrepo, stats):
emptyrepo.tp = stats
callbacks = MyCallbacks()
remote = emptyrepo.remotes[0]
stats = remote.fetch(callbacks=callbacks)
assert stats.received_bytes == callbacks.tp.received_bytes
assert stats.indexed_objects == callbacks.tp.indexed_objects
assert stats.received_objects == callbacks.tp.received_objects
def test_update_tips(emptyrepo):
remote = emptyrepo.remotes[0]
tips = [
(
'refs/remotes/origin/master',
Oid(hex='0' * 40),
Oid(hex='784855caf26449a1914d2cf62d12b9374d76ae78'),
),
(
'refs/tags/root',
Oid(hex='0' * 40),
Oid(hex='3d2962987c695a29f1f80b6c3aa4ec046ef44369'),
),
]
class MyCallbacks(pygit2.RemoteCallbacks):
def __init__(self, tips):
self.tips = tips
self.i = 0
def update_tips(self, name, old, new):
assert self.tips[self.i] == (name, old, new)
self.i += 1
callbacks = MyCallbacks(tips)
remote.fetch(callbacks=callbacks)
assert callbacks.i > 0
@pytest.fixture
def origin(tmp_path):
with utils.TemporaryRepository('barerepo.zip', tmp_path) as path:
yield pygit2.Repository(path)
@pytest.fixture
def clone(tmp_path):
clone = tmp_path / 'clone'
clone.mkdir()
with utils.TemporaryRepository('barerepo.zip', clone) as path:
yield pygit2.Repository(path)
@pytest.fixture
def remote(origin, clone):
yield clone.remotes.create('origin', origin.path)
def test_push_fast_forward_commits_to_remote_succeeds(origin, clone, remote):
tip = clone[clone.head.target]
oid = clone.create_commit(
'refs/heads/master',
tip.author,
tip.author,
'empty commit',
tip.tree.id,
[tip.id],
)
remote.push(['refs/heads/master'])
assert origin[origin.head.target].id == oid
def test_push_when_up_to_date_succeeds(origin, clone, remote):
remote.push(['refs/heads/master'])
origin_tip = origin[origin.head.target].id
clone_tip = clone[clone.head.target].id
assert origin_tip == clone_tip
def test_push_non_fast_forward_commits_to_remote_fails(origin, clone, remote):
tip = origin[origin.head.target]
origin.create_commit(
'refs/heads/master',
tip.author,
tip.author,
'some commit',
tip.tree.id,
[tip.id],
)
tip = clone[clone.head.target]
clone.create_commit(
'refs/heads/master',
tip.author,
tip.author,
'other commit',
tip.tree.id,
[tip.id],
)
with pytest.raises(pygit2.GitError):
remote.push(['refs/heads/master'])
@patch.object(pygit2.callbacks, 'RemoteCallbacks')
def test_push_options(mock_callbacks, origin, clone, remote):
remote.push(['refs/heads/master'])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 0
remote.push(['refs/heads/master'], push_options=[])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 0
remote.push(['refs/heads/master'], push_options=['foo'])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 1
assert ffi.string(remote_push_options.strings[0]) == b'foo'
remote.push(['refs/heads/master'], push_options=['Option A', 'Option B'])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 2
assert ffi.string(remote_push_options.strings[0]) == b'Option A'
assert ffi.string(remote_push_options.strings[1]) == b'Option B'