]>
git.proxmox.com Git - libgit2.git/blob - tests/repo/config.c
37f6b521d820644f3cb548d299ddc2681b88a11d
1 #include "clar_libgit2.h"
6 static git_str path
= GIT_STR_INIT
;
8 void test_repo_config__initialize(void)
10 cl_fixture_sandbox("empty_standard_repo");
11 cl_git_pass(cl_rename(
12 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
16 cl_must_pass(p_mkdir("alternate", 0777));
17 cl_git_pass(git_fs_path_prettify(&path
, "alternate", NULL
));
20 void test_repo_config__cleanup(void)
22 cl_sandbox_set_search_path_defaults();
24 git_str_dispose(&path
);
27 git_futils_rmdir_r("alternate", NULL
, GIT_RMDIR_REMOVE_FILES
));
28 cl_assert(!git_fs_path_isdir("alternate"));
30 cl_fixture_cleanup("empty_standard_repo");
33 void test_repo_config__can_open_global_when_there_is_no_file(void)
36 git_config
*config
, *global
;
38 cl_git_pass(git_libgit2_opts(
39 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
40 cl_git_pass(git_libgit2_opts(
41 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
42 cl_git_pass(git_libgit2_opts(
43 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
45 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
46 cl_git_pass(git_repository_config(&config
, repo
));
47 cl_git_pass(git_config_open_level(
48 &global
, config
, GIT_CONFIG_LEVEL_GLOBAL
));
50 cl_git_pass(git_config_set_string(global
, "test.set", "42"));
52 git_config_free(global
);
53 git_config_free(config
);
54 git_repository_free(repo
);
57 void test_repo_config__can_open_missing_global_with_separators(void)
60 git_config
*config
, *global
;
62 cl_git_pass(git_str_printf(
63 &path
, "%c%s", GIT_PATH_LIST_SEPARATOR
, "dummy"));
65 cl_git_pass(git_libgit2_opts(
66 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
67 cl_git_pass(git_libgit2_opts(
68 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
69 cl_git_pass(git_libgit2_opts(
70 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
72 git_str_dispose(&path
);
74 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
75 cl_git_pass(git_repository_config(&config
, repo
));
76 cl_git_pass(git_config_open_level(
77 &global
, config
, GIT_CONFIG_LEVEL_GLOBAL
));
79 cl_git_pass(git_config_set_string(global
, "test.set", "42"));
81 git_config_free(global
);
82 git_config_free(config
);
83 git_repository_free(repo
);
86 #include "repository.h"
88 void test_repo_config__read_with_no_configs_at_all(void)
93 cl_git_pass(git_libgit2_opts(
94 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
95 cl_git_pass(git_libgit2_opts(
96 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
97 cl_git_pass(git_libgit2_opts(
98 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
102 cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
103 cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
105 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
106 git_repository__configmap_lookup_cache_clear(repo
);
108 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
109 cl_assert_equal_i(GIT_ABBREV_DEFAULT
, val
);
110 git_repository_free(repo
);
112 /* with no local config, just system */
114 cl_sandbox_set_search_path_defaults();
116 cl_must_pass(p_mkdir("alternate/1", 0777));
117 cl_git_pass(git_str_joinpath(&path
, path
.ptr
, "1"));
118 cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
119 cl_git_pass(git_libgit2_opts(
120 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
122 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
123 git_repository__configmap_lookup_cache_clear(repo
);
125 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
126 cl_assert_equal_i(10, val
);
127 git_repository_free(repo
);
129 /* with just xdg + system */
131 cl_must_pass(p_mkdir("alternate/2", 0777));
132 path
.ptr
[path
.size
- 1] = '2';
133 cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
134 cl_git_pass(git_libgit2_opts(
135 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
137 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
138 git_repository__configmap_lookup_cache_clear(repo
);
140 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
141 cl_assert_equal_i(20, val
);
142 git_repository_free(repo
);
144 /* with global + xdg + system */
146 cl_must_pass(p_mkdir("alternate/3", 0777));
147 path
.ptr
[path
.size
- 1] = '3';
148 cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
149 cl_git_pass(git_libgit2_opts(
150 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
152 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
153 git_repository__configmap_lookup_cache_clear(repo
);
155 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
156 cl_assert_equal_i(30, val
);
157 git_repository_free(repo
);
159 /* with all configs */
161 cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
163 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
164 git_repository__configmap_lookup_cache_clear(repo
);
166 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
167 cl_assert_equal_i(40, val
);
168 git_repository_free(repo
);
170 /* with all configs but delete the files ? */
172 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
173 git_repository__configmap_lookup_cache_clear(repo
);
175 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
176 cl_assert_equal_i(40, val
);
178 cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
179 cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
181 cl_must_pass(p_unlink("alternate/1/gitconfig"));
182 cl_assert(!git_fs_path_isfile("alternate/1/gitconfig"));
184 cl_must_pass(p_unlink("alternate/2/config"));
185 cl_assert(!git_fs_path_isfile("alternate/2/config"));
187 cl_must_pass(p_unlink("alternate/3/.gitconfig"));
188 cl_assert(!git_fs_path_isfile("alternate/3/.gitconfig"));
190 git_repository__configmap_lookup_cache_clear(repo
);
192 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
193 cl_assert_equal_i(40, val
);
194 git_repository_free(repo
);
198 cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
199 cl_assert(!git_fs_path_isfile("alternate/3/.gitconfig"));
201 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
202 git_repository__configmap_lookup_cache_clear(repo
);
204 cl_git_pass(git_repository__configmap_lookup(&val
, repo
, GIT_CONFIGMAP_ABBREV
));
205 cl_assert_equal_i(7, val
);
206 git_repository_free(repo
);
208 cl_assert(!git_fs_path_exists("empty_standard_repo/.git/config"));
209 cl_assert(!git_fs_path_exists("alternate/3/.gitconfig"));