]> git.proxmox.com Git - libgit2.git/blob - tests/config/global.c
Merge pull request #3480 from ethomson/nsecs
[libgit2.git] / tests / config / global.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 #include "fileops.h"
4
5 void test_config_global__initialize(void)
6 {
7 git_buf path = GIT_BUF_INIT;
8
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));
13
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));
18
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));
23
24 git_buf_free(&path);
25 }
26
27 void test_config_global__cleanup(void)
28 {
29 cl_sandbox_set_search_path_defaults();
30 }
31
32 void test_config_global__open_global(void)
33 {
34 git_config *cfg, *global, *selected, *dummy;
35
36 cl_git_pass(git_config_open_default(&cfg));
37 cl_git_pass(git_config_open_level(&global, cfg, GIT_CONFIG_LEVEL_GLOBAL));
38 cl_git_fail(git_config_open_level(&dummy, cfg, GIT_CONFIG_LEVEL_XDG));
39 cl_git_pass(git_config_open_global(&selected, cfg));
40
41 git_config_free(selected);
42 git_config_free(global);
43 git_config_free(cfg);
44 }
45
46 void test_config_global__open_xdg(void)
47 {
48 git_config *cfg, *xdg, *selected;
49 const char *str = "teststring";
50 const char *key = "this.variable";
51 git_buf buf = {0};
52
53 cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n");
54
55 cl_git_pass(git_config_open_default(&cfg));
56 cl_git_pass(git_config_open_level(&xdg, cfg, GIT_CONFIG_LEVEL_XDG));
57 cl_git_pass(git_config_open_global(&selected, cfg));
58
59 cl_git_pass(git_config_set_string(xdg, key, str));
60 cl_git_pass(git_config_get_string_buf(&buf, selected, key));
61 cl_assert_equal_s(str, buf.ptr);
62
63 git_buf_free(&buf);
64 git_config_free(selected);
65 git_config_free(xdg);
66 git_config_free(cfg);
67 }
68
69 void test_config_global__open_programdata(void)
70 {
71 char *programdata;
72 git_config *cfg;
73 git_repository *repo;
74 git_buf config_path = GIT_BUF_INIT;
75 git_buf var_contents = GIT_BUF_INIT;
76
77 if (!cl_getenv("GITTEST_INVASIVE_FS_STRUCTURE"))
78 cl_skip();
79
80 programdata = cl_getenv("PROGRAMDATA");
81 cl_git_pass(git_buf_printf(&config_path, "%s/Git", programdata));
82 cl_git_pass(p_mkdir(config_path.ptr, 0777));
83 cl_git_pass(git_buf_puts(&config_path, "/config"));
84
85 cl_git_pass(git_config_open_ondisk(&cfg, config_path.ptr));
86 cl_git_pass(git_config_set_string(cfg, "programdata.var", "even higher level"));
87
88 git_buf_free(&config_path);
89 git_config_free(cfg);
90
91 git_config_open_default(&cfg);
92 cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
93 cl_assert_equal_s("even higher level", var_contents.ptr);
94
95 git_config_free(cfg);
96 git_buf_free(&var_contents);
97
98 cl_git_pass(git_repository_init(&repo, "./foo.git", true));
99 cl_git_pass(git_repository_config(&cfg, repo));
100 cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
101 cl_assert_equal_s("even higher level", var_contents.ptr);
102
103 git_config_free(cfg);
104 git_buf_free(&var_contents);
105 git_repository_free(repo);
106 cl_fixture_cleanup("./foo.git");
107 }