]> git.proxmox.com Git - libgit2.git/blob - tests-clar/attr/ignore.c
8df0eb9de0e82629a92b7a88ba85f2ff01466aa7
[libgit2.git] / tests-clar / attr / ignore.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "path.h"
4 #include "fileops.h"
5
6 static git_repository *g_repo = NULL;
7
8 void test_attr_ignore__initialize(void)
9 {
10 g_repo = cl_git_sandbox_init("attr");
11 }
12
13 void test_attr_ignore__cleanup(void)
14 {
15 cl_git_sandbox_cleanup();
16 g_repo = NULL;
17 }
18
19 void assert_is_ignored(bool expected, const char *filepath)
20 {
21 int is_ignored;
22
23 cl_git_pass(git_ignore_path_is_ignored(&is_ignored, g_repo, filepath));
24 cl_assert_equal_b(expected, is_ignored);
25 }
26
27 void test_attr_ignore__honor_temporary_rules(void)
28 {
29 cl_git_rewritefile("attr/.gitignore", "/NewFolder\n/NewFolder/NewFolder");
30
31 assert_is_ignored(false, "File.txt");
32 assert_is_ignored(true, "NewFolder");
33 assert_is_ignored(true, "NewFolder/NewFolder");
34 assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
35 }
36
37 void test_attr_ignore__skip_gitignore_directory(void)
38 {
39 cl_git_rewritefile("attr/.git/info/exclude", "/NewFolder\n/NewFolder/NewFolder");
40 p_unlink("attr/.gitignore");
41 cl_assert(!git_path_exists("attr/.gitignore"));
42 p_mkdir("attr/.gitignore", 0777);
43 cl_git_mkfile("attr/.gitignore/garbage.txt", "new_file\n");
44
45 assert_is_ignored(false, "File.txt");
46 assert_is_ignored(true, "NewFolder");
47 assert_is_ignored(true, "NewFolder/NewFolder");
48 assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
49 }
50
51 void test_attr_ignore__expand_tilde_to_homedir(void)
52 {
53 git_buf path = GIT_BUF_INIT;
54 git_config *cfg;
55
56 assert_is_ignored(false, "example.global_with_tilde");
57
58 /* construct fake home with fake global excludes */
59
60 cl_must_pass(p_mkdir("home", 0777));
61 cl_git_pass(git_path_prettify(&path, "home", NULL));
62 cl_git_pass(git_libgit2_opts(
63 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
64
65 cl_git_mkfile("home/globalexcludes", "# found me\n*.global_with_tilde\n");
66
67 cl_git_pass(git_repository_config(&cfg, g_repo));
68 cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexcludes"));
69 git_config_free(cfg);
70
71 git_attr_cache_flush(g_repo); /* must reset to pick up change */
72
73 assert_is_ignored(true, "example.global_with_tilde");
74
75 cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
76
77 cl_git_pass(git_libgit2_opts(
78 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
79
80 git_buf_free(&path);
81 }