ignore.c 5.34 KB
Newer Older
1
#include "clar_libgit2.h"
2 3
#include "posix.h"
#include "path.h"
4
#include "fileops.h"
5 6 7 8 9

static git_repository *g_repo = NULL;

void test_attr_ignore__initialize(void)
{
10
	g_repo = cl_git_sandbox_init("attr");
11 12 13 14
}

void test_attr_ignore__cleanup(void)
{
15 16
	cl_git_sandbox_cleanup();
	g_repo = NULL;
17 18
}

19
static void assert_is_ignored_(
20
	bool expected, const char *filepath, const char *file, int line)
21
{
22
	int is_ignored = 0;
23

24 25 26 27 28 29
	cl_git_pass_(
		git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), file, line);

	clar__assert_equal(
		file, line, "expected != is_ignored", 1, "%d",
		(int)(expected != 0), (int)(is_ignored != 0));
30
}
31 32
#define assert_is_ignored(expected, filepath) \
	assert_is_ignored_(expected, filepath, __FILE__, __LINE__)
33 34 35

void test_attr_ignore__honor_temporary_rules(void)
{
36
	cl_git_rewritefile("attr/.gitignore", "/NewFolder\n/NewFolder/NewFolder");
37

38 39 40 41 42 43
	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void test_attr_ignore__allow_root(void)
{
	cl_git_rewritefile("attr/.gitignore", "/");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(false, "NewFolder");
	assert_is_ignored(false, "NewFolder/NewFolder");
	assert_is_ignored(false, "NewFolder/NewFolder/File.txt");
}

void test_attr_ignore__ignore_root(void)
{
	cl_git_rewritefile("attr/.gitignore", "/\n\n/NewFolder\n/NewFolder/NewFolder");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
}

64 65 66 67 68 69 70 71 72 73 74
void test_attr_ignore__full_paths(void)
{
	cl_git_rewritefile("attr/.gitignore", "Folder/*/Contained");

	assert_is_ignored(true, "Folder/Middle/Contained");
	assert_is_ignored(false, "Folder/Middle/More/More/Contained");

	cl_git_rewritefile("attr/.gitignore", "Folder/**/Contained");

	assert_is_ignored(true, "Folder/Middle/Contained");
	assert_is_ignored(true, "Folder/Middle/More/More/Contained");
75 76 77 78 79 80 81

	cl_git_rewritefile("attr/.gitignore", "Folder/**/Contained/*/Child");

	assert_is_ignored(true, "Folder/Middle/Contained/Happy/Child");
	assert_is_ignored(false, "Folder/Middle/Contained/Not/Happy/Child");
	assert_is_ignored(true, "Folder/Middle/More/More/Contained/Happy/Child");
	assert_is_ignored(false, "Folder/Middle/More/More/Contained/Not/Happy/Child");
82 83
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
void test_attr_ignore__more_starstar_cases(void)
{
	cl_must_pass(p_unlink("attr/.gitignore"));
	cl_git_mkfile(
		"attr/dir/.gitignore",
		"sub/**/*.html\n");

	assert_is_ignored(false, "aaa.html");
	assert_is_ignored(false, "dir");
	assert_is_ignored(false, "dir/sub");
	assert_is_ignored(true,  "dir/sub/sub2/aaa.html");
	assert_is_ignored(true,  "dir/sub/aaa.html");
	assert_is_ignored(false, "dir/aaa.html");
	assert_is_ignored(false, "sub");
	assert_is_ignored(false, "sub/aaa.html");
	assert_is_ignored(false, "sub/sub2/aaa.html");
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
void test_attr_ignore__leading_stars(void)
{
	cl_git_rewritefile(
		"attr/.gitignore",
		"*/onestar\n"
		"**/twostars\n"
		"*/parent1/kid1/*\n"
		"**/parent2/kid2/*\n");

	assert_is_ignored(true, "dir1/onestar");
	assert_is_ignored(true, "dir1/onestar/child"); /* in ignored dir */
	assert_is_ignored(false, "dir1/dir2/onestar");

	assert_is_ignored(true, "dir1/twostars");
	assert_is_ignored(true, "dir1/twostars/child"); /* in ignored dir */
	assert_is_ignored(true, "dir1/dir2/twostars");
	assert_is_ignored(true, "dir1/dir2/twostars/child"); /* in ignored dir */
	assert_is_ignored(true, "dir1/dir2/dir3/twostars");

	assert_is_ignored(true, "dir1/parent1/kid1/file");
	assert_is_ignored(true, "dir1/parent1/kid1/file/inside/parent");
	assert_is_ignored(false, "dir1/dir2/parent1/kid1/file");
	assert_is_ignored(false, "dir1/parent1/file");
	assert_is_ignored(false, "dir1/kid1/file");

	assert_is_ignored(true, "dir1/parent2/kid2/file");
	assert_is_ignored(true, "dir1/parent2/kid2/file/inside/parent");
	assert_is_ignored(true, "dir1/dir2/parent2/kid2/file");
	assert_is_ignored(true, "dir1/dir2/dir3/parent2/kid2/file");
	assert_is_ignored(false, "dir1/parent2/file");
	assert_is_ignored(false, "dir1/kid2/file");
}
134

135 136 137 138 139 140 141 142 143 144 145 146
void test_attr_ignore__skip_gitignore_directory(void)
{
	cl_git_rewritefile("attr/.git/info/exclude", "/NewFolder\n/NewFolder/NewFolder");
	p_unlink("attr/.gitignore");
	cl_assert(!git_path_exists("attr/.gitignore"));
	p_mkdir("attr/.gitignore", 0777);
	cl_git_mkfile("attr/.gitignore/garbage.txt", "new_file\n");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
147
}
148 149 150 151 152 153 154

void test_attr_ignore__expand_tilde_to_homedir(void)
{
	git_config *cfg;

	assert_is_ignored(false, "example.global_with_tilde");

155
	cl_fake_home();
156

157 158
	/* construct fake home with fake global excludes */
	cl_git_mkfile("home/globalexclude", "# found me\n*.global_with_tilde\n");
159 160

	cl_git_pass(git_repository_config(&cfg, g_repo));
161
	cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude"));
162 163 164 165 166 167 168 169
	git_config_free(cfg);

	git_attr_cache_flush(g_repo); /* must reset to pick up change */

	assert_is_ignored(true, "example.global_with_tilde");

	cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));

170
	cl_fake_home_cleanup(NULL);
171

172 173 174
	git_attr_cache_flush(g_repo); /* must reset to pick up change */

	assert_is_ignored(false, "example.global_with_tilde");
175
}