Commit 10fa2dd61c383e1d920a78cc12aa72f6e0bf7b65

Etienne Samson 2018-06-20T02:26:50

tests: consolidate all remote creation tests in one test suite

diff --git a/tests/network/remote/remotes.c b/tests/network/remote/remotes.c
index 8bbc5c7..ff11a3e 100644
--- a/tests/network/remote/remotes.c
+++ b/tests/network/remote/remotes.c
@@ -312,30 +312,6 @@ void test_network_remote_remotes__add(void)
 	cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2");
 }
 
-void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
-{
-	git_remote *remote;
-
-	cl_assert_equal_i(
-		GIT_EINVALIDSPEC,
-		git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
-}
-
-void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
-{
-	git_remote *remote = NULL;
-
-	cl_assert_equal_i(
-		GIT_EINVALIDSPEC,
-		git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2"));
-	cl_assert_equal_p(remote, NULL);
-
-	cl_assert_equal_i(
-		GIT_EINVALIDSPEC,
-		git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
-	cl_assert_equal_p(remote, NULL);
-}
-
 void test_network_remote_remotes__tagopt(void)
 {
 	const char *name = git_remote_name(_remote);
@@ -389,41 +365,6 @@ void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl
 		git_remote_lookup(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND);
 }
 
-void assert_cannot_create_remote(const char *name, int expected_error)
-{
-	git_remote *remote = NULL;
-
-	cl_git_fail_with(
-		git_remote_create(&remote, _repo, name, "git://github.com/libgit2/libgit2"),
-		expected_error);
-
-	cl_assert_equal_p(remote, NULL);
-}
-
-void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
-{
-	assert_cannot_create_remote("test", GIT_EEXISTS);
-}
-
-void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
-{
-	assert_cannot_create_remote("/", GIT_EINVALIDSPEC);
-	assert_cannot_create_remote("//", GIT_EINVALIDSPEC);
-	assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC);
-	assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC);
-}
-
-void test_network_remote_remote__git_remote_create_with_fetchspec(void)
-{
-	git_remote *remote;
-	git_strarray array;
-
-	cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
-	git_remote_get_fetch_refspecs(&array, remote);
-	cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
-	git_remote_free(remote);
-}
-
 static const char *fetch_refspecs[] = {
 	"+refs/heads/*:refs/remotes/origin/*",
 	"refs/tags/*:refs/tags/*",
diff --git a/tests/remote/create.c b/tests/remote/create.c
index 0aa0f83..79e5d3f 100644
--- a/tests/remote/create.c
+++ b/tests/remote/create.c
@@ -2,7 +2,17 @@
 
 static git_repository *_repo;
 static git_config *_config;
-static char url[] = "http://github.com/libgit2/libgit2.git";
+
+#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);
+
 
 void test_remote_create__initialize(void)
 {
@@ -27,11 +37,68 @@ 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", url));
+	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), url);
+	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;
+	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_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);
+
+	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;
+
+	cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
+	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);
 
+	git_strarray_free(&array);
 	git_remote_free(remote);
 }