]> git.proxmox.com Git - libgit2.git/blame - tests-clar/network/remotes.c
errors: Introduce EINVALIDSPEC error code
[libgit2.git] / tests-clar / network / remotes.c
CommitLineData
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
6static git_remote *_remote;
7static git_repository *_repo;
8static const git_refspec *_refspec;
3a2626f3
CMN
9
10void 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
20void 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
28void 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
54void 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
63void test_network_remotes__parsing_ssh_remote(void)
64{
65 cl_assert( git_remote_valid_url("git@github.com:libgit2/libgit2.git") );
66}
67
7a544966 68void 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
73void 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
78void 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
83void 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
89void 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
97void 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
105void test_network_remotes__save(void)
106{
107 git_remote_free(_remote);
108
109 /* Set up the remote and save it to config */
baaa8a44 110 cl_git_pass(git_remote_new(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2", NULL));
89e5ed98
CMN
111 cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*"));
112 cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*"));
8689a69d 113 cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push"));
89e5ed98
CMN
114 cl_git_pass(git_remote_save(_remote));
115 git_remote_free(_remote);
116 _remote = NULL;
117
118 /* Load it from config and make sure everything matches */
119 cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
120
121 _refspec = git_remote_fetchspec(_remote);
122 cl_assert(_refspec != NULL);
946a6dc4
VM
123 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
124 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/upstream/*");
d05e2c64 125 cl_assert(git_refspec_force(_refspec) == 0);
89e5ed98
CMN
126
127 _refspec = git_remote_pushspec(_remote);
128 cl_assert(_refspec != NULL);
946a6dc4
VM
129 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
130 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/heads/*");
8689a69d
SC
131
132 cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
133 cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push");
413d5563
SC
134
135 /* remove the pushurl again and see if we can save that too */
136 cl_git_pass(git_remote_set_pushurl(_remote, NULL));
137 cl_git_pass(git_remote_save(_remote));
138 git_remote_free(_remote);
139 _remote = NULL;
140
141 cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
142 cl_assert(git_remote_pushurl(_remote) == NULL);
89e5ed98
CMN
143}
144
3a2626f3
CMN
145void test_network_remotes__fnmatch(void)
146{
3fbcac89
VM
147 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
148 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
3a2626f3
CMN
149}
150
151void test_network_remotes__transform(void)
152{
153 char ref[1024];
154
155 memset(ref, 0x0, sizeof(ref));
9462c471 156 cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master"));
946a6dc4 157 cl_assert_equal_s(ref, "refs/remotes/test/master");
3a2626f3 158}
279afd2a
CMN
159
160void test_network_remotes__transform_r(void)
161{
162 git_buf buf = GIT_BUF_INIT;
163
164 cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master"));
946a6dc4 165 cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master");
771cde43 166 git_buf_free(&buf);
279afd2a 167}
9554cd51
CMN
168
169void test_network_remotes__missing_refspecs(void)
170{
171 git_config *cfg;
172
173 git_remote_free(_remote);
174
175 cl_git_pass(git_repository_config(&cfg, _repo));
176 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
177 cl_git_pass(git_remote_load(&_remote, _repo, "specless"));
178
179 git_config_free(cfg);
180}
8171998f
CMN
181
182void test_network_remotes__list(void)
183{
184 git_strarray list;
185 git_config *cfg;
186
187 cl_git_pass(git_remote_list(&list, _repo));
6edefa14 188 cl_assert(list.count == 4);
8171998f
CMN
189 git_strarray_free(&list);
190
191 cl_git_pass(git_repository_config(&cfg, _repo));
192 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
193 cl_git_pass(git_remote_list(&list, _repo));
6edefa14 194 cl_assert(list.count == 5);
8171998f
CMN
195 git_strarray_free(&list);
196
197 git_config_free(cfg);
198}
9fb70f37 199
200void test_network_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
201{
202 cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago"));
203}
a209a025 204
d27bf665 205/*
206 * $ git remote add addtest http://github.com/libgit2/libgit2
207 *
208 * $ cat .git/config
209 * [...]
210 * [remote "addtest"]
211 * url = http://github.com/libgit2/libgit2
a146ba9e 212 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
d27bf665 213 */
a209a025
CMN
214void test_network_remotes__add(void)
215{
216 git_remote_free(_remote);
217 cl_git_pass(git_remote_add(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2"));
218 git_remote_free(_remote);
219
220 cl_git_pass(git_remote_load(&_remote, _repo, "addtest"));
221 _refspec = git_remote_fetchspec(_remote);
222 cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
d27bf665 223 cl_assert(git_refspec_force(_refspec) == 1);
a209a025 224 cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/addtest/*"));
8689a69d 225 cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2");
a209a025 226}
218c88a9 227
e497b16c 228void test_network_remotes__cannot_add_a_nameless_remote(void)
229{
230 git_remote *remote;
231
232 cl_git_fail(git_remote_add(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
233 cl_git_fail(git_remote_add(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
234}
235
236void test_network_remotes__cannot_save_a_nameless_remote(void)
237{
238 git_remote *remote;
239
240 cl_git_pass(git_remote_new(&remote, _repo, NULL, "git://github.com/libgit2/libgit2", NULL));
241
242 cl_git_fail(git_remote_save(remote));
243 git_remote_free(remote);
244
245 cl_git_pass(git_remote_new(&remote, _repo, "", "git://github.com/libgit2/libgit2", NULL));
246
247 cl_git_fail(git_remote_save(remote));
248 git_remote_free(remote);
249}
250
218c88a9
CMN
251void test_network_remotes__tagopt(void)
252{
253 const char *opt;
254 git_config *cfg;
255
256 cl_git_pass(git_repository_config(&cfg, _repo));
257
258 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
259 cl_git_pass(git_remote_save(_remote));
260 cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
261 cl_assert(!strcmp(opt, "--tags"));
262
263 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
264 cl_git_pass(git_remote_save(_remote));
265 cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
266 cl_assert(!strcmp(opt, "--no-tags"));
267
268 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
269 cl_git_pass(git_remote_save(_remote));
270 cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND);
271
272 git_config_free(cfg);
273}
a2a61894 274
275void test_network_remotes__cannot_load_with_an_empty_url(void)
276{
277 git_remote *remote;
278
279 cl_git_fail(git_remote_load(&remote, _repo, "empty-remote-url"));
1fe99aee 280 cl_assert(giterr_last()->klass == GITERR_INVALID);
a2a61894 281}