]> git.proxmox.com Git - libgit2.git/blob - tests/remote/create.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / remote / create.c
1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
3
4 static git_repository *_repo;
5 static git_config *_config;
6
7 #define TEST_URL "http://github.com/libgit2/libgit2.git"
8
9 void test_remote_create__initialize(void)
10 {
11 cl_fixture_sandbox("testrepo.git");
12
13 cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
14
15 cl_git_pass(git_repository_config(&_config, _repo));
16 }
17
18 void test_remote_create__cleanup(void)
19 {
20 git_config_free(_config);
21
22 git_repository_free(_repo);
23
24 cl_fixture_cleanup("testrepo.git");
25 }
26
27 void test_remote_create__manual(void)
28 {
29 git_remote *remote;
30 cl_git_pass(git_config_set_string(_config, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"));
31 cl_git_pass(git_config_set_string(_config, "remote.origin.url", TEST_URL));
32
33 cl_git_pass(git_remote_lookup(&remote, _repo, "origin"));
34 cl_assert_equal_s(git_remote_name(remote), "origin");
35 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
36
37 git_remote_free(remote);
38 }
39
40 void test_remote_create__named(void)
41 {
42 git_remote *remote;
43 git_config *cfg;
44 const char *cfg_val;
45
46 size_t section_count = count_config_entries_match(_repo, "remote\\.");
47
48 cl_git_pass(git_remote_create(&remote, _repo, "valid-name", TEST_URL));
49
50 cl_assert_equal_s(git_remote_name(remote), "valid-name");
51 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
52 cl_assert_equal_p(git_remote_owner(remote), _repo);
53
54 cl_git_pass(git_repository_config_snapshot(&cfg, _repo));
55
56 cl_git_pass(git_config_get_string(&cfg_val, cfg, "remote.valid-name.fetch"));
57 cl_assert_equal_s(cfg_val, "+refs/heads/*:refs/remotes/valid-name/*");
58
59 cl_git_pass(git_config_get_string(&cfg_val, cfg, "remote.valid-name.url"));
60 cl_assert_equal_s(cfg_val, TEST_URL);
61
62 cl_assert_equal_i(section_count + 2, count_config_entries_match(_repo, "remote\\."));
63
64 git_config_free(cfg);
65 git_remote_free(remote);
66 }
67
68 void test_remote_create__named_fail_on_invalid_name(void)
69 {
70 const char *names[] = {
71 NULL,
72 "Inv@{id",
73 "",
74 "/",
75 "//",
76 ".lock",
77 "a.lock",
78 };
79 size_t i;
80
81 for (i = 0; i < ARRAY_SIZE(names); i++) {
82 git_remote *remote = NULL;
83 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create(&remote, _repo, names[i], TEST_URL));
84 cl_assert_equal_p(remote, NULL);
85 }
86 }
87
88 void test_remote_create__named_fail_on_invalid_url(void)
89 {
90 git_remote *remote = NULL;
91
92 cl_git_fail_with(GIT_ERROR, git_remote_create(&remote, _repo, "bad-url", ""));
93 cl_assert_equal_p(remote, NULL);
94 }
95
96 void test_remote_create__named_fail_on_conflicting_name(void)
97 {
98 git_remote *remote = NULL;
99
100 cl_git_fail_with(GIT_EEXISTS, git_remote_create(&remote, _repo, "test", TEST_URL));
101 cl_assert_equal_p(remote, NULL);
102 }
103
104 void test_remote_create__with_fetchspec(void)
105 {
106 git_remote *remote;
107 git_strarray array;
108 size_t section_count = count_config_entries_match(_repo, "remote\\.");
109
110 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
111 cl_assert_equal_s(git_remote_name(remote), "test-new");
112 cl_assert_equal_s(git_remote_url(remote), "git://github.com/libgit2/libgit2");
113 cl_assert_equal_p(git_remote_owner(remote), _repo);
114
115 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
116 cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
117 cl_assert_equal_i(1, array.count);
118 cl_assert_equal_i(section_count + 2, count_config_entries_match(_repo, "remote\\."));
119
120 git_strarray_dispose(&array);
121 git_remote_free(remote);
122 }
123
124 void test_remote_create__with_empty_fetchspec(void)
125 {
126 git_remote *remote;
127 git_strarray array;
128 size_t section_count = count_config_entries_match(_repo, "remote\\.");
129
130 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", NULL));
131 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
132 cl_assert_equal_i(0, array.count);
133 cl_assert_equal_i(section_count + 1, count_config_entries_match(_repo, "remote\\."));
134
135 git_strarray_dispose(&array);
136 git_remote_free(remote);
137 }
138
139 void test_remote_create__with_fetchspec_invalid_name(void)
140 {
141 git_remote *remote = NULL;
142
143 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_with_fetchspec(&remote, _repo, NULL, TEST_URL, NULL));
144 cl_assert_equal_p(remote, NULL);
145 }
146
147 void test_remote_create__with_fetchspec_invalid_url(void)
148 {
149 git_remote *remote = NULL;
150
151 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_with_fetchspec(&remote, _repo, NULL, "", NULL));
152 cl_assert_equal_p(remote, NULL);
153 }
154
155 void test_remote_create__anonymous(void)
156 {
157 git_remote *remote;
158 git_strarray array;
159 size_t section_count = count_config_entries_match(_repo, "remote\\.");
160
161 cl_git_pass(git_remote_create_anonymous(&remote, _repo, TEST_URL));
162 cl_assert_equal_s(git_remote_name(remote), NULL);
163 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
164 cl_assert_equal_p(git_remote_owner(remote), _repo);
165
166 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
167 cl_assert_equal_i(0, array.count);
168 cl_assert_equal_i(section_count, count_config_entries_match(_repo, "remote\\."));
169
170 git_strarray_dispose(&array);
171 git_remote_free(remote);
172 }
173
174 void test_remote_create__anonymous_invalid_url(void)
175 {
176 git_remote *remote = NULL;
177
178 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_anonymous(&remote, _repo, ""));
179 cl_assert_equal_p(remote, NULL);
180 }
181
182 void test_remote_create__detached(void)
183 {
184 git_remote *remote;
185 git_strarray array;
186
187 size_t section_count = count_config_entries_match(_repo, "remote\\.");
188
189 cl_git_pass(git_remote_create_detached(&remote, TEST_URL));
190 cl_assert_equal_s(git_remote_name(remote), NULL);
191 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
192 cl_assert_equal_p(git_remote_owner(remote), NULL);
193
194 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
195 cl_assert_equal_i(0, array.count);
196 cl_assert_equal_i(section_count, count_config_entries_match(_repo, "remote\\."));
197
198 git_strarray_dispose(&array);
199 git_remote_free(remote);
200 }
201
202 void test_remote_create__detached_invalid_url(void)
203 {
204 git_remote *remote = NULL;
205
206 cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_detached(&remote, ""));
207 cl_assert_equal_p(remote, NULL);
208 }
209
210 void test_remote_create__with_opts_named(void)
211 {
212 git_remote *remote;
213 git_strarray array;
214 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
215
216 opts.name = "test-new";
217 opts.repository = _repo;
218
219 cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
220 cl_assert_equal_s(git_remote_name(remote), "test-new");
221 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
222 cl_assert_equal_p(git_remote_owner(remote), _repo);
223
224 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
225 cl_assert_equal_i(1, array.count);
226 cl_assert_equal_s("+refs/heads/*:refs/remotes/test-new/*", array.strings[0]);
227
228 git_strarray_dispose(&array);
229 git_remote_free(remote);
230 }
231
232 void test_remote_create__with_opts_named_and_fetchspec(void)
233 {
234 git_remote *remote;
235 git_strarray array;
236 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
237
238 opts.name = "test-new";
239 opts.repository = _repo;
240 opts.fetchspec = "+refs/*:refs/*";
241
242 cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
243 cl_assert_equal_s(git_remote_name(remote), "test-new");
244 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
245 cl_assert_equal_p(git_remote_owner(remote), _repo);
246
247 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
248 cl_assert_equal_i(1, array.count);
249 cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
250
251 git_strarray_dispose(&array);
252 git_remote_free(remote);
253 }
254
255 void test_remote_create__with_opts_named_no_fetchspec(void)
256 {
257 git_remote *remote;
258 git_strarray array;
259 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
260
261 opts.name = "test-new";
262 opts.repository = _repo;
263 opts.flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC;
264
265 cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
266 cl_assert_equal_s(git_remote_name(remote), "test-new");
267 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
268 cl_assert_equal_p(git_remote_owner(remote), _repo);
269
270 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
271 cl_assert_equal_i(0, array.count);
272
273 git_strarray_dispose(&array);
274 git_remote_free(remote);
275 }
276
277 void test_remote_create__with_opts_anonymous(void)
278 {
279 git_remote *remote;
280 git_strarray array;
281 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
282
283 opts.repository = _repo;
284
285 cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
286 cl_assert_equal_s(git_remote_name(remote), NULL);
287 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
288 cl_assert_equal_p(git_remote_owner(remote), _repo);
289
290 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
291 cl_assert_equal_i(0, array.count);
292
293 git_strarray_dispose(&array);
294 git_remote_free(remote);
295 }
296
297 void test_remote_create__with_opts_detached(void)
298 {
299 git_remote *remote;
300 git_strarray array;
301 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
302
303 cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
304 cl_assert_equal_s(git_remote_name(remote), NULL);
305 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
306 cl_assert_equal_p(git_remote_owner(remote), NULL);
307
308 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
309 cl_assert_equal_i(0, array.count);
310
311 git_strarray_dispose(&array);
312
313 git_remote_free(remote);
314
315 cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, NULL));
316 cl_assert_equal_s(git_remote_name(remote), NULL);
317 cl_assert_equal_s(git_remote_url(remote), TEST_URL);
318 cl_assert_equal_p(git_remote_owner(remote), NULL);
319
320 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
321 cl_assert_equal_i(0, array.count);
322
323 git_strarray_dispose(&array);
324
325 git_remote_free(remote);
326 }
327
328
329 void test_remote_create__with_opts_insteadof_disabled(void)
330 {
331 git_remote *remote;
332 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
333
334 opts.repository = _repo;
335 opts.flags = GIT_REMOTE_CREATE_SKIP_INSTEADOF;
336
337 cl_git_pass(git_remote_create_with_opts(&remote, "http://example.com/libgit2/libgit2", &opts));
338
339 cl_assert_equal_s(git_remote_url(remote), "http://example.com/libgit2/libgit2");
340 cl_assert_equal_p(git_remote_pushurl(remote), NULL);
341
342 git_remote_free(remote);
343 }
344
345 static int create_with_name(git_remote **remote, git_repository *repo, const char *name, const char *url)
346 {
347 git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
348
349 opts.repository = repo;
350 opts.name = name;
351
352 return git_remote_create_with_opts(remote, url, &opts);
353 }
354
355 void test_remote_create__with_opts_invalid_name(void)
356 {
357 const char *names[] = {
358 "Inv@{id",
359 "",
360 "/",
361 "//",
362 ".lock",
363 "a.lock",
364 };
365 size_t i;
366
367 for (i = 0; i < ARRAY_SIZE(names); i++) {
368 git_remote *remote = NULL;
369 cl_git_fail_with(GIT_EINVALIDSPEC, create_with_name(&remote, _repo, names[i], TEST_URL));
370 cl_assert_equal_p(remote, NULL);
371 }
372 }
373
374 void test_remote_create__with_opts_conflicting_name(void)
375 {
376 git_remote *remote = NULL;
377
378 cl_git_fail_with(GIT_EEXISTS, create_with_name(&remote, _repo, "test", TEST_URL));
379 cl_assert_equal_p(remote, NULL);
380 }
381
382 void test_remote_create__with_opts_invalid_url(void)
383 {
384 git_remote *remote = NULL;
385
386 cl_git_fail_with(GIT_EINVALIDSPEC, create_with_name(&remote, _repo, "test-new", ""));
387 cl_assert_equal_p(remote, NULL);
388 }