Hash :
b914e17d
Author :
Date :
2012-12-12T12:23:24
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
#include "clar_libgit2.h"
#include "buffer.h"
#include "refspec.h"
#include "remote.h"
static git_remote *_remote;
static git_repository *_repo;
static const git_refspec *_refspec;
void test_network_remotes__initialize(void)
{
_repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_remote_load(&_remote, _repo, "test"));
_refspec = git_remote_fetchspec(_remote);
cl_assert(_refspec != NULL);
}
void test_network_remotes__cleanup(void)
{
git_remote_free(_remote);
_remote = NULL;
cl_git_sandbox_cleanup();
}
void test_network_remotes__parsing(void)
{
git_remote *_remote2 = NULL;
cl_assert_equal_s(git_remote_name(_remote), "test");
cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
cl_assert(git_remote_pushurl(_remote) == NULL);
cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH),
"git://github.com/libgit2/libgit2");
cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH),
"git://github.com/libgit2/libgit2");
cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl"));
cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl");
cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2");
cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2");
cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH),
"git://github.com/libgit2/fetchlibgit2");
cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH),
"git://github.com/libgit2/pushlibgit2");
git_remote_free(_remote2);
}
void test_network_remotes__pushurl(void)
{
cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/notlibgit2"));
cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/notlibgit2");
cl_git_pass(git_remote_set_pushurl(_remote, NULL));
cl_assert(git_remote_pushurl(_remote) == NULL);
}
void test_network_remotes__parsing_ssh_remote(void)
{
cl_assert( git_remote_valid_url("git@github.com:libgit2/libgit2.git") );
}
void test_network_remotes__parsing_local_path_fails_if_path_not_found(void)
{
cl_assert( !git_remote_valid_url("/home/git/repos/libgit2.git") );
}
void test_network_remotes__supported_transport_methods_are_supported(void)
{
cl_assert( git_remote_supported_url("git://github.com/libgit2/libgit2") );
}
void test_network_remotes__unsupported_transport_methods_are_unsupported(void)
{
cl_assert( !git_remote_supported_url("git@github.com:libgit2/libgit2.git") );
}
void test_network_remotes__refspec_parsing(void)
{
cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*");
}
void test_network_remotes__set_fetchspec(void)
{
cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*"));
_refspec = git_remote_fetchspec(_remote);
cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
}
void test_network_remotes__set_pushspec(void)
{
cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*"));
_refspec = git_remote_pushspec(_remote);
cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
}
void test_network_remotes__save(void)
{
git_remote_free(_remote);
/* Set up the remote and save it to config */
cl_git_pass(git_remote_new(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2", NULL));
cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*"));
cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*"));
cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push"));
cl_git_pass(git_remote_save(_remote));
git_remote_free(_remote);
_remote = NULL;
/* Load it from config and make sure everything matches */
cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
_refspec = git_remote_fetchspec(_remote);
cl_assert(_refspec != NULL);
cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/upstream/*");
cl_assert(git_refspec_force(_refspec) == 0);
_refspec = git_remote_pushspec(_remote);
cl_assert(_refspec != NULL);
cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/heads/*");
cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push");
/* remove the pushurl again and see if we can save that too */
cl_git_pass(git_remote_set_pushurl(_remote, NULL));
cl_git_pass(git_remote_save(_remote));
git_remote_free(_remote);
_remote = NULL;
cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
cl_assert(git_remote_pushurl(_remote) == NULL);
}
void test_network_remotes__fnmatch(void)
{
cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
}
void test_network_remotes__transform(void)
{
char ref[1024];
memset(ref, 0x0, sizeof(ref));
cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master"));
cl_assert_equal_s(ref, "refs/remotes/test/master");
}
void test_network_remotes__transform_r(void)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master"));
cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master");
git_buf_free(&buf);
}
void test_network_remotes__missing_refspecs(void)
{
git_config *cfg;
git_remote_free(_remote);
cl_git_pass(git_repository_config(&cfg, _repo));
cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
cl_git_pass(git_remote_load(&_remote, _repo, "specless"));
git_config_free(cfg);
}
void test_network_remotes__list(void)
{
git_strarray list;
git_config *cfg;
cl_git_pass(git_remote_list(&list, _repo));
cl_assert(list.count == 4);
git_strarray_free(&list);
cl_git_pass(git_repository_config(&cfg, _repo));
cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
cl_git_pass(git_remote_list(&list, _repo));
cl_assert(list.count == 5);
git_strarray_free(&list);
git_config_free(cfg);
}
void test_network_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
{
cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago"));
}
void test_network_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_load(&_remote, _repo, "Inv@{id"));
}
/*
* $ git remote add addtest http://github.com/libgit2/libgit2
*
* $ cat .git/config
* [...]
* [remote "addtest"]
* url = http://github.com/libgit2/libgit2
* fetch = +refs/heads/\*:refs/remotes/addtest/\*
*/
void test_network_remotes__add(void)
{
git_remote_free(_remote);
cl_git_pass(git_remote_add(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2"));
git_remote_free(_remote);
cl_git_pass(git_remote_load(&_remote, _repo, "addtest"));
_refspec = git_remote_fetchspec(_remote);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
cl_assert(git_refspec_force(_refspec) == 1);
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/addtest/*"));
cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2");
}
void test_network_remotes__cannot_add_a_nameless_remote(void)
{
git_remote *remote;
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_remote_add(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
}
void test_network_remotes__cannot_save_a_nameless_remote(void)
{
git_remote *remote;
cl_git_pass(git_remote_new(&remote, _repo, NULL, "git://github.com/libgit2/libgit2", NULL));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_save(remote));
git_remote_free(remote);
}
void test_network_remotes__cannot_add_a_remote_with_an_invalid_name(void)
{
git_remote *remote;
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_remote_add(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2"));
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_remote_add(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
}
void test_network_remotes__cannot_initialize_a_remote_with_an_invalid_name(void)
{
git_remote *remote;
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_remote_new(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2", NULL));
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_remote_new(&remote, _repo, "", "git://github.com/libgit2/libgit2", NULL));
}
void test_network_remotes__tagopt(void)
{
const char *opt;
git_config *cfg;
cl_git_pass(git_repository_config(&cfg, _repo));
git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
cl_git_pass(git_remote_save(_remote));
cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
cl_assert(!strcmp(opt, "--tags"));
git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
cl_git_pass(git_remote_save(_remote));
cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
cl_assert(!strcmp(opt, "--no-tags"));
git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
cl_git_pass(git_remote_save(_remote));
cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND);
git_config_free(cfg);
}
void test_network_remotes__cannot_load_with_an_empty_url(void)
{
git_remote *remote;
cl_git_fail(git_remote_load(&remote, _repo, "empty-remote-url"));
cl_assert(giterr_last()->klass == GITERR_INVALID);
}
void test_network_remotes__check_structure_version(void)
{
git_transport transport = GIT_TRANSPORT_INIT;
const git_error *err;
git_remote_free(_remote);
cl_git_pass(git_remote_new(&_remote, _repo, NULL, "test-protocol://localhost", NULL));
transport.version = 0;
cl_git_fail(git_remote_set_transport(_remote, &transport));
err = giterr_last();
cl_assert_equal_i(GITERR_INVALID, err->klass);
giterr_clear();
transport.version = 1024;
cl_git_fail(git_remote_set_transport(_remote, &transport));
err = giterr_last();
cl_assert_equal_i(GITERR_INVALID, err->klass);
}
void test_network_remotes__dangling(void)
{
cl_git_pass(git_remote_new(&_remote, NULL, "upstream", "git://github.com/libgit2/libgit2", NULL));
cl_git_pass(git_remote_rename(_remote, "newname", NULL, NULL));
cl_assert_equal_s(git_remote_name(_remote), "newname");
cl_git_fail(git_remote_save(_remote));
cl_git_fail(git_remote_update_tips(_remote));
cl_git_pass(git_remote_set_repository(_remote, _repo));
cl_git_pass(git_remote_save(_remote));
}