Hash :
f8fc987c
Author :
Date :
2018-06-20T02:26:56
tests: git_remote_create_detached
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
#include "clar_libgit2.h"
static git_repository *_repo;
static git_config *_config;
#define TEST_URL "http://github.com/libgit2/libgit2.git"
#define cl_git_assert_cannot_create_remote(expected, creation_expr) \
do { \
git_remote *r = NULL; \
int res = ((creation_expr)); \
cl_git_fail_with(expected, res); \
cl_assert_equal_p(r, NULL); \
} while (0);
static int cl_git_config_section_count__foreach(const git_config_entry *entry, void *payload)
{
size_t *count = payload;
GIT_UNUSED(entry);
(*count)++;
return 0;
}
static size_t cl_git_config_sections_count(git_config *cfg, const char *regex)
{
size_t count = 0;
cl_git_pass(git_config_foreach_match(cfg, regex, cl_git_config_section_count__foreach, &count));
return count;
}
void test_remote_create__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
cl_git_pass(git_repository_config(&_config, _repo));
}
void test_remote_create__cleanup(void)
{
git_config_free(_config);
git_repository_free(_repo);
_repo = NULL;
cl_fixture_cleanup("testrepo.git");
}
void test_remote_create__manual(void)
{
git_remote *remote;
cl_git_pass(git_config_set_string(_config, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"));
cl_git_pass(git_config_set_string(_config, "remote.origin.url", TEST_URL));
cl_git_pass(git_remote_lookup(&remote, _repo, "origin"));
cl_assert_equal_s(git_remote_name(remote), "origin");
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;
size_t section_count = cl_git_config_sections_count(_config, "remote.");
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);
cl_assert_equal_p(git_remote_owner(remote), _repo);
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);
cl_assert_equal_i(section_count + 2, cl_git_config_sections_count(_config, "remote."));
git_config_free(cfg);
git_remote_free(remote);
}
void test_remote_create__named_fail_on_invalid_name(void)
{
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, NULL, TEST_URL));
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, "Inv@{id", TEST_URL));
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, "", TEST_URL));
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, "/", TEST_URL));
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, "//", TEST_URL));
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, ".lock", TEST_URL));
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create(&r, _repo, "a.lock", TEST_URL));
}
void test_remote_create__named_fail_on_invalid_url(void)
{
cl_git_assert_cannot_create_remote(GIT_ERROR, git_remote_create(&r, _repo, "bad-url", ""));
}
void test_remote_create__named_fail_on_conflicting_name(void)
{
cl_git_assert_cannot_create_remote(GIT_EEXISTS, git_remote_create(&r, _repo, "test", TEST_URL));
}
void test_remote_create__with_fetchspec(void)
{
git_remote *remote;
git_strarray array;
size_t section_count = cl_git_config_sections_count(_config, "remote.");
cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
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);
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);
cl_assert_equal_i(section_count + 2, cl_git_config_sections_count(_config, "remote."));
git_strarray_free(&array);
git_remote_free(remote);
}
void test_remote_create__with_empty_fetchspec(void)
{
git_remote *remote;
git_strarray array;
size_t section_count = cl_git_config_sections_count(_config, "remote.");
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);
cl_assert_equal_i(section_count + 1, cl_git_config_sections_count(_config, "remote."));
git_strarray_free(&array);
git_remote_free(remote);
}
void test_remote_create__with_fetchspec_invalid_name(void)
{
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create_with_fetchspec(&r, _repo, NULL, TEST_URL, NULL));
}
void test_remote_create__with_fetchspec_invalid_url(void)
{
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create_with_fetchspec(&r, _repo, NULL, "", NULL));
}
void test_remote_create__anonymous(void)
{
git_remote *remote;
git_strarray array;
size_t section_count = cl_git_config_sections_count(_config, "remote.");
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);
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);
cl_assert_equal_i(section_count, cl_git_config_sections_count(_config, "remote."));
git_strarray_free(&array);
git_remote_free(remote);
}
void test_remote_create__anonymous_invalid_url(void)
{
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create_anonymous(&r, _repo, ""));
}
void test_remote_create__detached(void)
{
git_remote *remote;
git_strarray array;
size_t section_count = cl_git_config_sections_count(_config, "remote.");
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);
cl_assert_equal_i(section_count, cl_git_config_sections_count(_config, "remote."));
git_strarray_free(&array);
git_remote_free(remote);
}
void test_remote_create__detached_invalid_url(void)
{
cl_git_assert_cannot_create_remote(GIT_EINVALIDSPEC, git_remote_create_detached(&r, ""));
}