1 #include "clar_libgit2.h"
5 void test_config_global__initialize(void)
7 git_buf path
= GIT_BUF_INIT
;
9 cl_git_pass(git_futils_mkdir_r("home", 0777));
10 cl_git_pass(git_path_prettify(&path
, "home", NULL
));
11 cl_git_pass(git_libgit2_opts(
12 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
14 cl_git_pass(git_futils_mkdir_r("xdg/git", 0777));
15 cl_git_pass(git_path_prettify(&path
, "xdg/git", NULL
));
16 cl_git_pass(git_libgit2_opts(
17 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
19 cl_git_pass(git_futils_mkdir_r("etc", 0777));
20 cl_git_pass(git_path_prettify(&path
, "etc", NULL
));
21 cl_git_pass(git_libgit2_opts(
22 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
24 git_buf_dispose(&path
);
27 void test_config_global__cleanup(void)
29 cl_sandbox_set_search_path_defaults();
30 cl_git_pass(git_futils_rmdir_r("home", NULL
, GIT_RMDIR_REMOVE_FILES
));
31 cl_git_pass(git_futils_rmdir_r("xdg", NULL
, GIT_RMDIR_REMOVE_FILES
));
32 cl_git_pass(git_futils_rmdir_r("etc", NULL
, GIT_RMDIR_REMOVE_FILES
));
35 void test_config_global__open_global(void)
37 git_config
*cfg
, *global
, *selected
, *dummy
;
40 cl_git_mkfile("home/.gitconfig", "[global]\n test = 4567\n");
42 cl_git_pass(git_config_open_default(&cfg
));
43 cl_git_pass(git_config_get_int32(&value
, cfg
, "global.test"));
44 cl_assert_equal_i(4567, value
);
46 cl_git_pass(git_config_open_level(&global
, cfg
, GIT_CONFIG_LEVEL_GLOBAL
));
47 cl_git_pass(git_config_get_int32(&value
, global
, "global.test"));
48 cl_assert_equal_i(4567, value
);
50 cl_git_fail(git_config_open_level(&dummy
, cfg
, GIT_CONFIG_LEVEL_XDG
));
52 cl_git_pass(git_config_open_global(&selected
, cfg
));
53 cl_git_pass(git_config_get_int32(&value
, selected
, "global.test"));
54 cl_assert_equal_i(4567, value
);
56 git_config_free(selected
);
57 git_config_free(global
);
61 void test_config_global__open_symlinked_global(void)
67 cl_git_mkfile("home/.gitconfig.linked", "[global]\n test = 4567\n");
68 cl_must_pass(symlink(".gitconfig.linked", "home/.gitconfig"));
70 cl_git_pass(git_config_open_default(&cfg
));
71 cl_git_pass(git_config_get_int32(&value
, cfg
, "global.test"));
72 cl_assert_equal_i(4567, value
);
78 void test_config_global__lock_missing_global_config(void)
81 git_config_entry
*entry
;
82 git_transaction
*transaction
;
84 (void)p_unlink("home/.gitconfig"); /* No global config */
86 cl_git_pass(git_config_open_default(&cfg
));
87 cl_git_pass(git_config_lock(&transaction
, cfg
));
88 cl_git_pass(git_config_set_string(cfg
, "assertion.fail", "boom"));
89 cl_git_pass(git_transaction_commit(transaction
));
90 git_transaction_free(transaction
);
93 cl_git_pass(git_config_get_entry(&entry
, cfg
, "assertion.fail"));
94 cl_assert_equal_s("boom", entry
->value
);
96 git_config_entry_free(entry
);
99 /* We can reread the new value from the global config */
100 cl_git_pass(git_config_open_default(&cfg
));
101 cl_git_pass(git_config_get_entry(&entry
, cfg
, "assertion.fail"));
102 cl_assert_equal_s("boom", entry
->value
);
104 git_config_entry_free(entry
);
105 git_config_free(cfg
);
108 void test_config_global__open_xdg(void)
110 git_config
*cfg
, *xdg
, *selected
;
111 const char *str
= "teststring";
112 const char *key
= "this.variable";
115 cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n");
117 cl_git_pass(git_config_open_default(&cfg
));
118 cl_git_pass(git_config_open_level(&xdg
, cfg
, GIT_CONFIG_LEVEL_XDG
));
119 cl_git_pass(git_config_open_global(&selected
, cfg
));
121 cl_git_pass(git_config_set_string(xdg
, key
, str
));
122 cl_git_pass(git_config_get_string_buf(&buf
, selected
, key
));
123 cl_assert_equal_s(str
, buf
.ptr
);
125 git_buf_dispose(&buf
);
126 git_config_free(selected
);
127 git_config_free(xdg
);
128 git_config_free(cfg
);
131 void test_config_global__open_programdata(void)
134 git_repository
*repo
;
135 git_buf config_path
= GIT_BUF_INIT
;
136 git_buf var_contents
= GIT_BUF_INIT
;
138 if (cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE"))
141 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH
,
142 GIT_CONFIG_LEVEL_PROGRAMDATA
, &config_path
));
144 if (!git_path_isdir(config_path
.ptr
))
145 cl_git_pass(p_mkdir(config_path
.ptr
, 0777));
147 cl_git_pass(git_buf_puts(&config_path
, "/config"));
149 cl_git_pass(git_config_open_ondisk(&cfg
, config_path
.ptr
));
150 cl_git_pass(git_config_set_string(cfg
, "programdata.var", "even higher level"));
152 git_buf_dispose(&config_path
);
153 git_config_free(cfg
);
155 git_config_open_default(&cfg
);
156 cl_git_pass(git_config_get_string_buf(&var_contents
, cfg
, "programdata.var"));
157 cl_assert_equal_s("even higher level", var_contents
.ptr
);
159 git_config_free(cfg
);
160 git_buf_dispose(&var_contents
);
162 cl_git_pass(git_repository_init(&repo
, "./foo.git", true));
163 cl_git_pass(git_repository_config(&cfg
, repo
));
164 cl_git_pass(git_config_get_string_buf(&var_contents
, cfg
, "programdata.var"));
165 cl_assert_equal_s("even higher level", var_contents
.ptr
);
167 git_config_free(cfg
);
168 git_buf_dispose(&var_contents
);
169 git_repository_free(repo
);
170 cl_fixture_cleanup("./foo.git");