Commit 3b32b6d3cc649a7808151dd67d1dd3e45e202f08

Russell Belfer 2013-05-23T09:19:42

More tests of config with various absent files Plus a bit of extra paranoia to ensure config object has valid contents.

diff --git a/src/config.c b/src/config.c
index dc8c702..49b9123 100644
--- a/src/config.c
+++ b/src/config.c
@@ -465,10 +465,13 @@ static int get_string(const char **out, const git_config *cfg, const char *name)
 {
 	file_internal *internal;
 	unsigned int i;
+	int res;
 
 	git_vector_foreach(&cfg->files, i, internal) {
-		int res = get_string_at_file(out, internal->file, name);
+		if (!internal || !internal->file || !internal->file->get)
+			continue;
 
+		res = get_string_at_file(out, internal->file, name);
 		if (res != GIT_ENOTFOUND)
 			return res;
 	}
diff --git a/tests-clar/repo/config.c b/tests-clar/repo/config.c
index 086fb5e..b8971bb 100644
--- a/tests-clar/repo/config.c
+++ b/tests-clar/repo/config.c
@@ -13,14 +13,15 @@ void test_repo_config__initialize(void)
 
 	cl_must_pass(p_mkdir("alternate", 0777));
 	cl_git_pass(git_path_prettify(&path, "alternate", NULL));
-
 }
 
 void test_repo_config__cleanup(void)
 {
+	cl_git_pass(git_path_prettify(&path, "alternate", NULL));
 	cl_git_pass(git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES));
-
 	git_buf_free(&path);
+	cl_assert(!git_path_isdir("alternate"));
+
 	cl_fixture_cleanup("empty_standard_repo");
 }
 
@@ -73,3 +74,127 @@ void test_repo_config__open_missing_global_with_separators(void)
 	git_config_free(config);
 	git_repository_free(repo);
 }
+
+#include "repository.h"
+
+void test_repo_config__read_no_configs(void)
+{
+	git_repository *repo;
+	int val;
+
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
+
+	/* with none */
+
+	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
+	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
+	git_repository_free(repo);
+
+	/* with just system */
+
+	cl_must_pass(p_mkdir("alternate/1", 0777));
+	cl_git_pass(git_buf_joinpath(&path, path.ptr, "1"));
+	cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(10, val);
+	git_repository_free(repo);
+
+	/* with xdg + system */
+
+	cl_must_pass(p_mkdir("alternate/2", 0777));
+	path.ptr[path.size - 1] = '2';
+	cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(20, val);
+	git_repository_free(repo);
+
+	/* with global + xdg + system */
+
+	cl_must_pass(p_mkdir("alternate/3", 0777));
+	path.ptr[path.size - 1] = '3';
+	cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(30, val);
+	git_repository_free(repo);
+
+	/* with all configs */
+
+	cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(40, val);
+	git_repository_free(repo);
+
+	/* with all configs but delete the files ? */
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(40, val);
+
+	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
+	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
+
+	cl_must_pass(p_unlink("alternate/1/gitconfig"));
+	cl_assert(!git_path_isfile("alternate/1/gitconfig"));
+
+	cl_must_pass(p_unlink("alternate/2/config"));
+	cl_assert(!git_path_isfile("alternate/2/config"));
+
+	cl_must_pass(p_unlink("alternate/3/.gitconfig"));
+	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
+
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(40, val);
+	git_repository_free(repo);
+
+	/* reopen */
+
+	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
+	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+	git_repository__cvar_cache_clear(repo);
+	val = -1;
+	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+	cl_assert_equal_i(7, val);
+	git_repository_free(repo);
+
+	cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
+	cl_assert(!git_path_exists("alternate/3/.gitconfig"));
+}