create.c 10.9 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "config/config_helpers.h"
3 4 5

static git_repository *_repo;
static git_config *_config;
6 7 8

#define TEST_URL "http://github.com/libgit2/libgit2.git"

9
void test_remote_create__initialize(void)
10 11 12 13 14 15 16 17
{
	cl_fixture_sandbox("testrepo.git");

	cl_git_pass(git_repository_open(&_repo, "testrepo.git"));

	cl_git_pass(git_repository_config(&_config, _repo));
}

18
void test_remote_create__cleanup(void)
19
{
20
	git_config_free(_config);
21

22
	git_repository_free(_repo);
23

24 25 26
	cl_fixture_cleanup("testrepo.git");
}

27
void test_remote_create__manual(void)
28
{
29 30
	git_remote *remote;
	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 34

	cl_git_pass(git_remote_lookup(&remote, _repo, "origin"));
	cl_assert_equal_s(git_remote_name(remote), "origin");
35 36 37 38 39 40 41 42 43 44
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);

	git_remote_free(remote);
}

void test_remote_create__named(void)
{
	git_remote *remote;
	git_config *cfg;
	const char *cfg_val;
45

46
	size_t section_count = count_config_entries_match(_repo, "remote\\.");
47

48 49 50 51
	cl_git_pass(git_remote_create(&remote, _repo, "valid-name", TEST_URL));

	cl_assert_equal_s(git_remote_name(remote), "valid-name");
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
52
	cl_assert_equal_p(git_remote_owner(remote), _repo);
53 54 55 56 57 58 59 60 61

	cl_git_pass(git_repository_config_snapshot(&cfg, _repo));

	cl_git_pass(git_config_get_string(&cfg_val, cfg, "remote.valid-name.fetch"));
	cl_assert_equal_s(cfg_val, "+refs/heads/*:refs/remotes/valid-name/*");

	cl_git_pass(git_config_get_string(&cfg_val, cfg, "remote.valid-name.url"));
	cl_assert_equal_s(cfg_val, TEST_URL);

62
	cl_assert_equal_i(section_count + 2, count_config_entries_match(_repo, "remote\\."));
63

64 65 66 67 68 69
	git_config_free(cfg);
	git_remote_free(remote);
}

void test_remote_create__named_fail_on_invalid_name(void)
{
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	const char *names[] = {
		NULL,
		"Inv@{id",
		"",
		"/",
		"//",
		".lock",
		"a.lock",
	};
	size_t i;

	for (i = 0; i < ARRAY_SIZE(names); i++) {
		git_remote *remote = NULL;
		cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create(&remote, _repo, names[i], TEST_URL));
		cl_assert_equal_p(remote, NULL);
	}
86 87 88 89
}

void test_remote_create__named_fail_on_invalid_url(void)
{
90 91 92 93
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_ERROR, git_remote_create(&remote, _repo, "bad-url", ""));
	cl_assert_equal_p(remote, NULL);
94 95 96 97
}

void test_remote_create__named_fail_on_conflicting_name(void)
{
98 99 100 101
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EEXISTS, git_remote_create(&remote, _repo, "test", TEST_URL));
	cl_assert_equal_p(remote, NULL);
102 103 104 105 106 107
}

void test_remote_create__with_fetchspec(void)
{
	git_remote *remote;
	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 112 113 114
	cl_assert_equal_s(git_remote_name(remote), "test-new");
	cl_assert_equal_s(git_remote_url(remote), "git://github.com/libgit2/libgit2");
	cl_assert_equal_p(git_remote_owner(remote), _repo);

115 116 117
	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
	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 125 126 127

void test_remote_create__with_empty_fetchspec(void)
{
	git_remote *remote;
	git_strarray array;
128
	size_t section_count = count_config_entries_match(_repo, "remote\\.");
129 130 131 132

	cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", NULL));
	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	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 137 138 139 140
	git_remote_free(remote);
}

void test_remote_create__with_fetchspec_invalid_name(void)
{
141 142 143 144
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_with_fetchspec(&remote, _repo, NULL, TEST_URL, NULL));
	cl_assert_equal_p(remote, NULL);
145 146 147 148
}

void test_remote_create__with_fetchspec_invalid_url(void)
{
149 150 151 152
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_with_fetchspec(&remote, _repo, NULL, "", NULL));
	cl_assert_equal_p(remote, NULL);
153
}
154 155 156 157 158

void test_remote_create__anonymous(void)
{
	git_remote *remote;
	git_strarray array;
159
	size_t section_count = count_config_entries_match(_repo, "remote\\.");
160 161 162 163

	cl_git_pass(git_remote_create_anonymous(&remote, _repo, TEST_URL));
	cl_assert_equal_s(git_remote_name(remote), NULL);
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
164
	cl_assert_equal_p(git_remote_owner(remote), _repo);
165 166 167

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	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 172 173 174 175
	git_remote_free(remote);
}

void test_remote_create__anonymous_invalid_url(void)
{
176 177 178 179
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_anonymous(&remote, _repo, ""));
	cl_assert_equal_p(remote, NULL);
180
}
181 182 183 184 185

void test_remote_create__detached(void)
{
	git_remote *remote;
	git_strarray array;
186 187

	size_t section_count = count_config_entries_match(_repo, "remote\\.");
188 189 190 191 192 193 194 195

	cl_git_pass(git_remote_create_detached(&remote, TEST_URL));
	cl_assert_equal_s(git_remote_name(remote), NULL);
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), NULL);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	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 200 201 202 203
	git_remote_free(remote);
}

void test_remote_create__detached_invalid_url(void)
{
204 205 206 207
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_create_detached(&remote, ""));
	cl_assert_equal_p(remote, NULL);
208
}
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

void test_remote_create__with_opts_named(void)
{
	git_remote *remote;
	git_strarray array;
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	opts.name = "test-new";
	opts.repository = _repo;

	cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
	cl_assert_equal_s(git_remote_name(remote), "test-new");
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), _repo);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_i(1, array.count);
	cl_assert_equal_s("+refs/heads/*:refs/remotes/test-new/*", array.strings[0]);

228
	git_strarray_dispose(&array);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	git_remote_free(remote);
}

void test_remote_create__with_opts_named_and_fetchspec(void)
{
	git_remote *remote;
	git_strarray array;
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	opts.name = "test-new";
	opts.repository = _repo;
	opts.fetchspec = "+refs/*:refs/*";

	cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
	cl_assert_equal_s(git_remote_name(remote), "test-new");
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), _repo);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_i(1, array.count);
	cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);

251
	git_strarray_dispose(&array);
252 253 254 255 256 257 258 259 260 261 262
	git_remote_free(remote);
}

void test_remote_create__with_opts_named_no_fetchspec(void)
{
	git_remote *remote;
	git_strarray array;
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	opts.name = "test-new";
	opts.repository = _repo;
263
	opts.flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC;
264 265 266 267 268 269 270 271 272

	cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
	cl_assert_equal_s(git_remote_name(remote), "test-new");
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), _repo);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_i(0, array.count);

273
	git_strarray_dispose(&array);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	git_remote_free(remote);
}

void test_remote_create__with_opts_anonymous(void)
{
	git_remote *remote;
	git_strarray array;
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	opts.repository = _repo;

	cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
	cl_assert_equal_s(git_remote_name(remote), NULL);
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), _repo);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_i(0, array.count);

293
	git_strarray_dispose(&array);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	git_remote_free(remote);
}

void test_remote_create__with_opts_detached(void)
{
	git_remote *remote;
	git_strarray array;
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
	cl_assert_equal_s(git_remote_name(remote), NULL);
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), NULL);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_i(0, array.count);

311
	git_strarray_dispose(&array);
312 313 314 315 316 317 318 319 320 321 322

	git_remote_free(remote);

	cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, NULL));
	cl_assert_equal_s(git_remote_name(remote), NULL);
	cl_assert_equal_s(git_remote_url(remote), TEST_URL);
	cl_assert_equal_p(git_remote_owner(remote), NULL);

	cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
	cl_assert_equal_i(0, array.count);

323
	git_strarray_dispose(&array);
324 325 326 327

	git_remote_free(remote);
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

void test_remote_create__with_opts_insteadof_disabled(void)
{
	git_remote *remote;
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	opts.repository = _repo;
	opts.flags = GIT_REMOTE_CREATE_SKIP_INSTEADOF;

	cl_git_pass(git_remote_create_with_opts(&remote, "http://example.com/libgit2/libgit2", &opts));

	cl_assert_equal_s(git_remote_url(remote), "http://example.com/libgit2/libgit2");
	cl_assert_equal_p(git_remote_pushurl(remote), NULL);

	git_remote_free(remote);
}

345 346 347 348 349 350 351 352 353 354 355 356
static int create_with_name(git_remote **remote, git_repository *repo, const char *name, const char *url)
{
	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;

	opts.repository = repo;
	opts.name = name;

	return git_remote_create_with_opts(remote, url, &opts);
}

void test_remote_create__with_opts_invalid_name(void)
{
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	const char *names[] = {
		"Inv@{id",
		"",
		"/",
		"//",
		".lock",
		"a.lock",
	};
	size_t i;

	for (i = 0; i < ARRAY_SIZE(names); i++) {
		git_remote *remote = NULL;
		cl_git_fail_with(GIT_EINVALIDSPEC, create_with_name(&remote, _repo, names[i], TEST_URL));
		cl_assert_equal_p(remote, NULL);
	}
372 373 374 375
}

void test_remote_create__with_opts_conflicting_name(void)
{
376 377 378 379
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EEXISTS, create_with_name(&remote, _repo, "test", TEST_URL));
	cl_assert_equal_p(remote, NULL);
380 381 382 383
}

void test_remote_create__with_opts_invalid_url(void)
{
384 385 386 387
	git_remote *remote = NULL;

	cl_git_fail_with(GIT_EINVALIDSPEC, create_with_name(&remote, _repo, "test-new", ""));
	cl_assert_equal_p(remote, NULL);
388
}