]>
Commit | Line | Data |
---|---|---|
3fd1520c | 1 | #include "clar_libgit2.h" |
279afd2a CMN |
2 | #include "buffer.h" |
3 | #include "refspec.h" | |
eff5b499 | 4 | #include "remote.h" |
3a2626f3 | 5 | |
9462c471 VM |
6 | static git_remote *_remote; |
7 | static git_repository *_repo; | |
8 | static const git_refspec *_refspec; | |
3a2626f3 CMN |
9 | |
10 | void test_network_remotes__initialize(void) | |
11 | { | |
e497b16c | 12 | _repo = cl_git_sandbox_init("testrepo.git"); |
9462c471 | 13 | |
9462c471 VM |
14 | cl_git_pass(git_remote_load(&_remote, _repo, "test")); |
15 | ||
16 | _refspec = git_remote_fetchspec(_remote); | |
17 | cl_assert(_refspec != NULL); | |
3a2626f3 CMN |
18 | } |
19 | ||
20 | void test_network_remotes__cleanup(void) | |
21 | { | |
9462c471 | 22 | git_remote_free(_remote); |
9094d30b SC |
23 | _remote = NULL; |
24 | ||
e497b16c | 25 | cl_git_sandbox_cleanup(); |
3a2626f3 CMN |
26 | } |
27 | ||
28 | void test_network_remotes__parsing(void) | |
29 | { | |
8689a69d SC |
30 | git_remote *_remote2 = NULL; |
31 | ||
946a6dc4 VM |
32 | cl_assert_equal_s(git_remote_name(_remote), "test"); |
33 | cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); | |
8689a69d SC |
34 | cl_assert(git_remote_pushurl(_remote) == NULL); |
35 | ||
df705148 | 36 | cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH), |
eff5b499 | 37 | "git://github.com/libgit2/libgit2"); |
df705148 | 38 | cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH), |
eff5b499 SC |
39 | "git://github.com/libgit2/libgit2"); |
40 | ||
8689a69d SC |
41 | cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl")); |
42 | cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl"); | |
43 | cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2"); | |
44 | cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2"); | |
eff5b499 | 45 | |
df705148 | 46 | cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH), |
eff5b499 | 47 | "git://github.com/libgit2/fetchlibgit2"); |
df705148 | 48 | cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH), |
eff5b499 SC |
49 | "git://github.com/libgit2/pushlibgit2"); |
50 | ||
8689a69d SC |
51 | git_remote_free(_remote2); |
52 | } | |
53 | ||
54 | void test_network_remotes__pushurl(void) | |
55 | { | |
56 | cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/notlibgit2")); | |
57 | cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/notlibgit2"); | |
58 | ||
59 | cl_git_pass(git_remote_set_pushurl(_remote, NULL)); | |
60 | cl_assert(git_remote_pushurl(_remote) == NULL); | |
3a2626f3 CMN |
61 | } |
62 | ||
58448910 RW |
63 | void test_network_remotes__parsing_ssh_remote(void) |
64 | { | |
65 | cl_assert( git_remote_valid_url("git@github.com:libgit2/libgit2.git") ); | |
66 | } | |
67 | ||
7a544966 | 68 | void test_network_remotes__parsing_local_path_fails_if_path_not_found(void) |
58448910 RW |
69 | { |
70 | cl_assert( !git_remote_valid_url("/home/git/repos/libgit2.git") ); | |
71 | } | |
72 | ||
7a544966 RW |
73 | void test_network_remotes__supported_transport_methods_are_supported(void) |
74 | { | |
e497b16c | 75 | cl_assert( git_remote_supported_url("git://github.com/libgit2/libgit2") ); |
7a544966 RW |
76 | } |
77 | ||
78 | void test_network_remotes__unsupported_transport_methods_are_unsupported(void) | |
79 | { | |
80 | cl_assert( !git_remote_supported_url("git@github.com:libgit2/libgit2.git") ); | |
81 | } | |
82 | ||
3a2626f3 CMN |
83 | void test_network_remotes__refspec_parsing(void) |
84 | { | |
946a6dc4 VM |
85 | cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); |
86 | cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*"); | |
3a2626f3 CMN |
87 | } |
88 | ||
bcb8c007 CMN |
89 | void test_network_remotes__set_fetchspec(void) |
90 | { | |
91 | cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*")); | |
92 | _refspec = git_remote_fetchspec(_remote); | |
946a6dc4 VM |
93 | cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); |
94 | cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); | |
bcb8c007 CMN |
95 | } |
96 | ||
97 | void test_network_remotes__set_pushspec(void) | |
98 | { | |
99 | cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*")); | |
100 | _refspec = git_remote_pushspec(_remote); | |
946a6dc4 VM |
101 | cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); |
102 | cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); | |
bcb8c007 CMN |
103 | } |
104 | ||
89e5ed98 CMN |
105 | void test_network_remotes__save(void) |
106 | { | |
107 | git_remote_free(_remote); | |
26290cd1 | 108 | _remote = NULL; |
89e5ed98 CMN |
109 | |
110 | /* Set up the remote and save it to config */ | |
baaa8a44 | 111 | cl_git_pass(git_remote_new(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2", NULL)); |
89e5ed98 CMN |
112 | cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*")); |
113 | cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*")); | |
8689a69d | 114 | cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push")); |
89e5ed98 CMN |
115 | cl_git_pass(git_remote_save(_remote)); |
116 | git_remote_free(_remote); | |
117 | _remote = NULL; | |
118 | ||
119 | /* Load it from config and make sure everything matches */ | |
120 | cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); | |
121 | ||
122 | _refspec = git_remote_fetchspec(_remote); | |
123 | cl_assert(_refspec != NULL); | |
946a6dc4 VM |
124 | cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); |
125 | cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/upstream/*"); | |
d05e2c64 | 126 | cl_assert(git_refspec_force(_refspec) == 0); |
89e5ed98 CMN |
127 | |
128 | _refspec = git_remote_pushspec(_remote); | |
129 | cl_assert(_refspec != NULL); | |
946a6dc4 VM |
130 | cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); |
131 | cl_assert_equal_s(git_refspec_dst(_refspec), "refs/heads/*"); | |
8689a69d SC |
132 | |
133 | cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); | |
134 | cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push"); | |
413d5563 SC |
135 | |
136 | /* remove the pushurl again and see if we can save that too */ | |
137 | cl_git_pass(git_remote_set_pushurl(_remote, NULL)); | |
138 | cl_git_pass(git_remote_save(_remote)); | |
139 | git_remote_free(_remote); | |
140 | _remote = NULL; | |
141 | ||
142 | cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); | |
143 | cl_assert(git_remote_pushurl(_remote) == NULL); | |
89e5ed98 CMN |
144 | } |
145 | ||
3a2626f3 CMN |
146 | void test_network_remotes__fnmatch(void) |
147 | { | |
3fbcac89 VM |
148 | cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master")); |
149 | cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch")); | |
3a2626f3 CMN |
150 | } |
151 | ||
152 | void test_network_remotes__transform(void) | |
153 | { | |
154 | char ref[1024]; | |
155 | ||
156 | memset(ref, 0x0, sizeof(ref)); | |
9462c471 | 157 | cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master")); |
946a6dc4 | 158 | cl_assert_equal_s(ref, "refs/remotes/test/master"); |
3a2626f3 | 159 | } |
279afd2a CMN |
160 | |
161 | void test_network_remotes__transform_r(void) | |
162 | { | |
163 | git_buf buf = GIT_BUF_INIT; | |
164 | ||
165 | cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master")); | |
946a6dc4 | 166 | cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master"); |
771cde43 | 167 | git_buf_free(&buf); |
279afd2a | 168 | } |
9554cd51 CMN |
169 | |
170 | void test_network_remotes__missing_refspecs(void) | |
171 | { | |
172 | git_config *cfg; | |
173 | ||
174 | git_remote_free(_remote); | |
175 | ||
176 | cl_git_pass(git_repository_config(&cfg, _repo)); | |
177 | cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); | |
178 | cl_git_pass(git_remote_load(&_remote, _repo, "specless")); | |
179 | ||
180 | git_config_free(cfg); | |
181 | } | |
8171998f CMN |
182 | |
183 | void test_network_remotes__list(void) | |
184 | { | |
185 | git_strarray list; | |
186 | git_config *cfg; | |
187 | ||
188 | cl_git_pass(git_remote_list(&list, _repo)); | |
6edefa14 | 189 | cl_assert(list.count == 4); |
8171998f CMN |
190 | git_strarray_free(&list); |
191 | ||
192 | cl_git_pass(git_repository_config(&cfg, _repo)); | |
193 | cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); | |
194 | cl_git_pass(git_remote_list(&list, _repo)); | |
6edefa14 | 195 | cl_assert(list.count == 5); |
8171998f CMN |
196 | git_strarray_free(&list); |
197 | ||
198 | git_config_free(cfg); | |
199 | } | |
9fb70f37 | 200 | |
201 | void test_network_remotes__loading_a_missing_remote_returns_ENOTFOUND(void) | |
202 | { | |
203 | cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago")); | |
204 | } | |
a209a025 | 205 | |
032ba9e4 | 206 | void test_network_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void) |
207 | { | |
208 | cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_load(&_remote, _repo, "Inv@{id")); | |
209 | } | |
210 | ||
d27bf665 | 211 | /* |
212 | * $ git remote add addtest http://github.com/libgit2/libgit2 | |
213 | * | |
214 | * $ cat .git/config | |
215 | * [...] | |
216 | * [remote "addtest"] | |
217 | * url = http://github.com/libgit2/libgit2 | |
a146ba9e | 218 | * fetch = +refs/heads/\*:refs/remotes/addtest/\* |
d27bf665 | 219 | */ |
a209a025 CMN |
220 | void test_network_remotes__add(void) |
221 | { | |
222 | git_remote_free(_remote); | |
223 | cl_git_pass(git_remote_add(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2")); | |
224 | git_remote_free(_remote); | |
225 | ||
226 | cl_git_pass(git_remote_load(&_remote, _repo, "addtest")); | |
227 | _refspec = git_remote_fetchspec(_remote); | |
228 | cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*")); | |
d27bf665 | 229 | cl_assert(git_refspec_force(_refspec) == 1); |
a209a025 | 230 | cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/addtest/*")); |
8689a69d | 231 | cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2"); |
a209a025 | 232 | } |
218c88a9 | 233 | |
e497b16c | 234 | void test_network_remotes__cannot_add_a_nameless_remote(void) |
235 | { | |
236 | git_remote *remote; | |
237 | ||
032ba9e4 | 238 | cl_assert_equal_i( |
239 | GIT_EINVALIDSPEC, | |
240 | git_remote_add(&remote, _repo, NULL, "git://github.com/libgit2/libgit2")); | |
e497b16c | 241 | } |
242 | ||
243 | void test_network_remotes__cannot_save_a_nameless_remote(void) | |
244 | { | |
245 | git_remote *remote; | |
246 | ||
247 | cl_git_pass(git_remote_new(&remote, _repo, NULL, "git://github.com/libgit2/libgit2", NULL)); | |
248 | ||
032ba9e4 | 249 | cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_save(remote)); |
e497b16c | 250 | git_remote_free(remote); |
032ba9e4 | 251 | } |
e497b16c | 252 | |
032ba9e4 | 253 | void test_network_remotes__cannot_add_a_remote_with_an_invalid_name(void) |
254 | { | |
255 | git_remote *remote; | |
e497b16c | 256 | |
032ba9e4 | 257 | cl_assert_equal_i( |
258 | GIT_EINVALIDSPEC, | |
259 | git_remote_add(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2")); | |
260 | ||
261 | cl_assert_equal_i( | |
262 | GIT_EINVALIDSPEC, | |
263 | git_remote_add(&remote, _repo, "", "git://github.com/libgit2/libgit2")); | |
264 | } | |
265 | ||
266 | void test_network_remotes__cannot_initialize_a_remote_with_an_invalid_name(void) | |
267 | { | |
268 | git_remote *remote; | |
269 | ||
270 | cl_assert_equal_i( | |
271 | GIT_EINVALIDSPEC, | |
272 | git_remote_new(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2", NULL)); | |
273 | ||
274 | cl_assert_equal_i( | |
275 | GIT_EINVALIDSPEC, | |
276 | git_remote_new(&remote, _repo, "", "git://github.com/libgit2/libgit2", NULL)); | |
e497b16c | 277 | } |
278 | ||
218c88a9 CMN |
279 | void test_network_remotes__tagopt(void) |
280 | { | |
281 | const char *opt; | |
282 | git_config *cfg; | |
283 | ||
284 | cl_git_pass(git_repository_config(&cfg, _repo)); | |
285 | ||
286 | git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL); | |
287 | cl_git_pass(git_remote_save(_remote)); | |
288 | cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); | |
289 | cl_assert(!strcmp(opt, "--tags")); | |
290 | ||
291 | git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE); | |
292 | cl_git_pass(git_remote_save(_remote)); | |
293 | cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); | |
294 | cl_assert(!strcmp(opt, "--no-tags")); | |
295 | ||
296 | git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO); | |
297 | cl_git_pass(git_remote_save(_remote)); | |
298 | cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND); | |
299 | ||
300 | git_config_free(cfg); | |
301 | } | |
a2a61894 | 302 | |
303 | void test_network_remotes__cannot_load_with_an_empty_url(void) | |
304 | { | |
305 | git_remote *remote; | |
306 | ||
307 | cl_git_fail(git_remote_load(&remote, _repo, "empty-remote-url")); | |
1fe99aee | 308 | cl_assert(giterr_last()->klass == GITERR_INVALID); |
a2a61894 | 309 | } |
10711769 BS |
310 | |
311 | void test_network_remotes__check_structure_version(void) | |
312 | { | |
313 | git_transport transport = GIT_TRANSPORT_INIT; | |
314 | const git_error *err; | |
315 | ||
316 | git_remote_free(_remote); | |
317 | cl_git_pass(git_remote_new(&_remote, _repo, NULL, "test-protocol://localhost", NULL)); | |
318 | ||
319 | transport.version = 0; | |
320 | cl_git_fail(git_remote_set_transport(_remote, &transport)); | |
321 | err = giterr_last(); | |
322 | cl_assert_equal_i(GITERR_INVALID, err->klass); | |
323 | ||
324 | giterr_clear(); | |
325 | transport.version = 1024; | |
326 | cl_git_fail(git_remote_set_transport(_remote, &transport)); | |
327 | err = giterr_last(); | |
328 | cl_assert_equal_i(GITERR_INVALID, err->klass); | |
329 | } | |
a71c27cc BS |
330 | |
331 | void test_network_remotes__dangling(void) | |
332 | { | |
333 | cl_git_pass(git_remote_new(&_remote, NULL, "upstream", "git://github.com/libgit2/libgit2", NULL)); | |
a71c27cc BS |
334 | |
335 | cl_git_pass(git_remote_rename(_remote, "newname", NULL, NULL)); | |
336 | cl_assert_equal_s(git_remote_name(_remote), "newname"); | |
b914e17d BS |
337 | |
338 | cl_git_fail(git_remote_save(_remote)); | |
339 | cl_git_fail(git_remote_update_tips(_remote)); | |
340 | ||
341 | cl_git_pass(git_remote_set_repository(_remote, _repo)); | |
342 | cl_git_pass(git_remote_save(_remote)); | |
a71c27cc | 343 | } |