1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
4 static git_repository
*_repo
;
5 static git_config
*_config
;
7 #define TEST_URL "http://github.com/libgit2/libgit2.git"
9 void test_remote_create__initialize(void)
11 cl_fixture_sandbox("testrepo.git");
13 cl_git_pass(git_repository_open(&_repo
, "testrepo.git"));
15 cl_git_pass(git_repository_config(&_config
, _repo
));
18 void test_remote_create__cleanup(void)
20 git_config_free(_config
);
22 git_repository_free(_repo
);
24 cl_fixture_cleanup("testrepo.git");
27 void test_remote_create__manual(void)
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
));
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
);
37 git_remote_free(remote
);
40 void test_remote_create__named(void)
46 size_t section_count
= count_config_entries_match(_repo
, "remote\\.");
48 cl_git_pass(git_remote_create(&remote
, _repo
, "valid-name", TEST_URL
));
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
);
54 cl_git_pass(git_repository_config_snapshot(&cfg
, _repo
));
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/*");
59 cl_git_pass(git_config_get_string(&cfg_val
, cfg
, "remote.valid-name.url"));
60 cl_assert_equal_s(cfg_val
, TEST_URL
);
62 cl_assert_equal_i(section_count
+ 2, count_config_entries_match(_repo
, "remote\\."));
65 git_remote_free(remote
);
68 void test_remote_create__named_fail_on_invalid_name(void)
70 const char *names
[] = {
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
);
88 void test_remote_create__named_fail_on_invalid_url(void)
90 git_remote
*remote
= NULL
;
92 cl_git_fail_with(GIT_ERROR
, git_remote_create(&remote
, _repo
, "bad-url", ""));
93 cl_assert_equal_p(remote
, NULL
);
96 void test_remote_create__named_fail_on_conflicting_name(void)
98 git_remote
*remote
= NULL
;
100 cl_git_fail_with(GIT_EEXISTS
, git_remote_create(&remote
, _repo
, "test", TEST_URL
));
101 cl_assert_equal_p(remote
, NULL
);
104 void test_remote_create__with_fetchspec(void)
108 size_t section_count
= count_config_entries_match(_repo
, "remote\\.");
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
);
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\\."));
120 git_strarray_dispose(&array
);
121 git_remote_free(remote
);
124 void test_remote_create__with_empty_fetchspec(void)
128 size_t section_count
= count_config_entries_match(_repo
, "remote\\.");
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\\."));
135 git_strarray_dispose(&array
);
136 git_remote_free(remote
);
139 void test_remote_create__with_fetchspec_invalid_name(void)
141 git_remote
*remote
= NULL
;
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
);
147 void test_remote_create__with_fetchspec_invalid_url(void)
149 git_remote
*remote
= NULL
;
151 cl_git_fail_with(GIT_EINVALIDSPEC
, git_remote_create_with_fetchspec(&remote
, _repo
, NULL
, "", NULL
));
152 cl_assert_equal_p(remote
, NULL
);
155 void test_remote_create__anonymous(void)
159 size_t section_count
= count_config_entries_match(_repo
, "remote\\.");
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
);
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\\."));
170 git_strarray_dispose(&array
);
171 git_remote_free(remote
);
174 void test_remote_create__anonymous_invalid_url(void)
176 git_remote
*remote
= NULL
;
178 cl_git_fail_with(GIT_EINVALIDSPEC
, git_remote_create_anonymous(&remote
, _repo
, ""));
179 cl_assert_equal_p(remote
, NULL
);
182 void test_remote_create__detached(void)
187 size_t section_count
= count_config_entries_match(_repo
, "remote\\.");
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
);
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\\."));
198 git_strarray_dispose(&array
);
199 git_remote_free(remote
);
202 void test_remote_create__detached_invalid_url(void)
204 git_remote
*remote
= NULL
;
206 cl_git_fail_with(GIT_EINVALIDSPEC
, git_remote_create_detached(&remote
, ""));
207 cl_assert_equal_p(remote
, NULL
);
210 void test_remote_create__with_opts_named(void)
214 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
216 opts
.name
= "test-new";
217 opts
.repository
= _repo
;
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
);
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]);
228 git_strarray_dispose(&array
);
229 git_remote_free(remote
);
232 void test_remote_create__with_opts_named_and_fetchspec(void)
236 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
238 opts
.name
= "test-new";
239 opts
.repository
= _repo
;
240 opts
.fetchspec
= "+refs/*:refs/*";
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
);
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]);
251 git_strarray_dispose(&array
);
252 git_remote_free(remote
);
255 void test_remote_create__with_opts_named_no_fetchspec(void)
259 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
261 opts
.name
= "test-new";
262 opts
.repository
= _repo
;
263 opts
.flags
= GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC
;
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
);
270 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
271 cl_assert_equal_i(0, array
.count
);
273 git_strarray_dispose(&array
);
274 git_remote_free(remote
);
277 void test_remote_create__with_opts_anonymous(void)
281 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
283 opts
.repository
= _repo
;
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
);
290 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
291 cl_assert_equal_i(0, array
.count
);
293 git_strarray_dispose(&array
);
294 git_remote_free(remote
);
297 void test_remote_create__with_opts_detached(void)
301 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
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
);
308 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
309 cl_assert_equal_i(0, array
.count
);
311 git_strarray_dispose(&array
);
313 git_remote_free(remote
);
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
);
320 cl_git_pass(git_remote_get_fetch_refspecs(&array
, remote
));
321 cl_assert_equal_i(0, array
.count
);
323 git_strarray_dispose(&array
);
325 git_remote_free(remote
);
329 void test_remote_create__with_opts_insteadof_disabled(void)
332 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
334 opts
.repository
= _repo
;
335 opts
.flags
= GIT_REMOTE_CREATE_SKIP_INSTEADOF
;
337 cl_git_pass(git_remote_create_with_opts(&remote
, "http://example.com/libgit2/libgit2", &opts
));
339 cl_assert_equal_s(git_remote_url(remote
), "http://example.com/libgit2/libgit2");
340 cl_assert_equal_p(git_remote_pushurl(remote
), NULL
);
342 git_remote_free(remote
);
345 static int create_with_name(git_remote
**remote
, git_repository
*repo
, const char *name
, const char *url
)
347 git_remote_create_options opts
= GIT_REMOTE_CREATE_OPTIONS_INIT
;
349 opts
.repository
= repo
;
352 return git_remote_create_with_opts(remote
, url
, &opts
);
355 void test_remote_create__with_opts_invalid_name(void)
357 const char *names
[] = {
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
);
374 void test_remote_create__with_opts_conflicting_name(void)
376 git_remote
*remote
= NULL
;
378 cl_git_fail_with(GIT_EEXISTS
, create_with_name(&remote
, _repo
, "test", TEST_URL
));
379 cl_assert_equal_p(remote
, NULL
);
382 void test_remote_create__with_opts_invalid_url(void)
384 git_remote
*remote
= NULL
;
386 cl_git_fail_with(GIT_EINVALIDSPEC
, create_with_name(&remote
, _repo
, "test-new", ""));
387 cl_assert_equal_p(remote
, NULL
);