]>
Commit | Line | Data |
---|---|---|
a4b75dcf | 1 | #include "clar_libgit2.h" |
83634d38 | 2 | #include "sysdir.h" |
22a2d3d5 | 3 | #include "futils.h" |
a4b75dcf CMN |
4 | #include <ctype.h> |
5 | ||
e579e0f7 | 6 | static git_str path = GIT_STR_INIT; |
a4b75dcf CMN |
7 | |
8 | void test_repo_config__initialize(void) | |
9 | { | |
10 | cl_fixture_sandbox("empty_standard_repo"); | |
8487e237 RB |
11 | cl_git_pass(cl_rename( |
12 | "empty_standard_repo/.gitted", "empty_standard_repo/.git")); | |
a4b75dcf | 13 | |
e579e0f7 | 14 | git_str_clear(&path); |
a4b75dcf CMN |
15 | |
16 | cl_must_pass(p_mkdir("alternate", 0777)); | |
e579e0f7 | 17 | cl_git_pass(git_fs_path_prettify(&path, "alternate", NULL)); |
a4b75dcf CMN |
18 | } |
19 | ||
20 | void test_repo_config__cleanup(void) | |
21 | { | |
8487e237 RB |
22 | cl_sandbox_set_search_path_defaults(); |
23 | ||
e579e0f7 | 24 | git_str_dispose(&path); |
8487e237 RB |
25 | |
26 | cl_git_pass( | |
27 | git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES)); | |
e579e0f7 | 28 | cl_assert(!git_fs_path_isdir("alternate")); |
3b32b6d3 | 29 | |
a4b75dcf CMN |
30 | cl_fixture_cleanup("empty_standard_repo"); |
31 | } | |
32 | ||
8487e237 | 33 | void test_repo_config__can_open_global_when_there_is_no_file(void) |
a4b75dcf CMN |
34 | { |
35 | git_repository *repo; | |
36 | git_config *config, *global; | |
37 | ||
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)); | |
44 | ||
45 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
46 | cl_git_pass(git_repository_config(&config, repo)); | |
8487e237 RB |
47 | cl_git_pass(git_config_open_level( |
48 | &global, config, GIT_CONFIG_LEVEL_GLOBAL)); | |
a4b75dcf CMN |
49 | |
50 | cl_git_pass(git_config_set_string(global, "test.set", "42")); | |
51 | ||
52 | git_config_free(global); | |
53 | git_config_free(config); | |
54 | git_repository_free(repo); | |
55 | } | |
56 | ||
8487e237 | 57 | void test_repo_config__can_open_missing_global_with_separators(void) |
a4b75dcf CMN |
58 | { |
59 | git_repository *repo; | |
60 | git_config *config, *global; | |
61 | ||
e579e0f7 | 62 | cl_git_pass(git_str_printf( |
8487e237 | 63 | &path, "%c%s", GIT_PATH_LIST_SEPARATOR, "dummy")); |
a4b75dcf CMN |
64 | |
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)); | |
71 | ||
e579e0f7 | 72 | git_str_dispose(&path); |
a4b75dcf CMN |
73 | |
74 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
75 | cl_git_pass(git_repository_config(&config, repo)); | |
8487e237 RB |
76 | cl_git_pass(git_config_open_level( |
77 | &global, config, GIT_CONFIG_LEVEL_GLOBAL)); | |
a4b75dcf CMN |
78 | |
79 | cl_git_pass(git_config_set_string(global, "test.set", "42")); | |
80 | ||
81 | git_config_free(global); | |
82 | git_config_free(config); | |
83 | git_repository_free(repo); | |
84 | } | |
3b32b6d3 RB |
85 | |
86 | #include "repository.h" | |
87 | ||
8487e237 | 88 | void test_repo_config__read_with_no_configs_at_all(void) |
3b32b6d3 RB |
89 | { |
90 | git_repository *repo; | |
91 | int val; | |
92 | ||
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)); | |
99 | ||
100 | /* with none */ | |
101 | ||
102 | cl_must_pass(p_unlink("empty_standard_repo/.git/config")); | |
e579e0f7 | 103 | cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config")); |
3b32b6d3 RB |
104 | |
105 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 106 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 107 | val = -1; |
22a2d3d5 | 108 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
109 | cl_assert_equal_i(GIT_ABBREV_DEFAULT, val); |
110 | git_repository_free(repo); | |
111 | ||
8487e237 | 112 | /* with no local config, just system */ |
39b1ad7f | 113 | |
8487e237 | 114 | cl_sandbox_set_search_path_defaults(); |
3b32b6d3 RB |
115 | |
116 | cl_must_pass(p_mkdir("alternate/1", 0777)); | |
e579e0f7 | 117 | cl_git_pass(git_str_joinpath(&path, path.ptr, "1")); |
3b32b6d3 RB |
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)); | |
121 | ||
122 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 123 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 124 | val = -1; |
22a2d3d5 | 125 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
126 | cl_assert_equal_i(10, val); |
127 | git_repository_free(repo); | |
128 | ||
8487e237 | 129 | /* with just xdg + system */ |
3b32b6d3 RB |
130 | |
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)); | |
136 | ||
137 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 138 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 139 | val = -1; |
22a2d3d5 | 140 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
141 | cl_assert_equal_i(20, val); |
142 | git_repository_free(repo); | |
143 | ||
144 | /* with global + xdg + system */ | |
145 | ||
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)); | |
151 | ||
152 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 153 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 154 | val = -1; |
22a2d3d5 | 155 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
156 | cl_assert_equal_i(30, val); |
157 | git_repository_free(repo); | |
158 | ||
159 | /* with all configs */ | |
160 | ||
161 | cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n"); | |
162 | ||
163 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 164 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 165 | val = -1; |
22a2d3d5 | 166 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
167 | cl_assert_equal_i(40, val); |
168 | git_repository_free(repo); | |
169 | ||
170 | /* with all configs but delete the files ? */ | |
171 | ||
172 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 173 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 174 | val = -1; |
22a2d3d5 | 175 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
176 | cl_assert_equal_i(40, val); |
177 | ||
178 | cl_must_pass(p_unlink("empty_standard_repo/.git/config")); | |
e579e0f7 | 179 | cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config")); |
3b32b6d3 RB |
180 | |
181 | cl_must_pass(p_unlink("alternate/1/gitconfig")); | |
e579e0f7 | 182 | cl_assert(!git_fs_path_isfile("alternate/1/gitconfig")); |
3b32b6d3 RB |
183 | |
184 | cl_must_pass(p_unlink("alternate/2/config")); | |
e579e0f7 | 185 | cl_assert(!git_fs_path_isfile("alternate/2/config")); |
3b32b6d3 RB |
186 | |
187 | cl_must_pass(p_unlink("alternate/3/.gitconfig")); | |
e579e0f7 | 188 | cl_assert(!git_fs_path_isfile("alternate/3/.gitconfig")); |
3b32b6d3 | 189 | |
22a2d3d5 | 190 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 191 | val = -1; |
22a2d3d5 | 192 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
193 | cl_assert_equal_i(40, val); |
194 | git_repository_free(repo); | |
195 | ||
196 | /* reopen */ | |
197 | ||
e579e0f7 MB |
198 | cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config")); |
199 | cl_assert(!git_fs_path_isfile("alternate/3/.gitconfig")); | |
3b32b6d3 RB |
200 | |
201 | cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); | |
22a2d3d5 | 202 | git_repository__configmap_lookup_cache_clear(repo); |
3b32b6d3 | 203 | val = -1; |
22a2d3d5 | 204 | cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV)); |
3b32b6d3 RB |
205 | cl_assert_equal_i(7, val); |
206 | git_repository_free(repo); | |
207 | ||
e579e0f7 MB |
208 | cl_assert(!git_fs_path_exists("empty_standard_repo/.git/config")); |
209 | cl_assert(!git_fs_path_exists("alternate/3/.gitconfig")); | |
3b32b6d3 | 210 | } |