1 #include "clar_libgit2.h"
4 static const char *empty_tmp_dir
= "test_gitfo_rmdir_recurs_test";
6 void test_core_rmdir__initialize(void)
8 git_buf path
= GIT_BUF_INIT
;
10 cl_must_pass(p_mkdir(empty_tmp_dir
, 0777));
12 cl_git_pass(git_buf_joinpath(&path
, empty_tmp_dir
, "/one"));
13 cl_must_pass(p_mkdir(path
.ptr
, 0777));
15 cl_git_pass(git_buf_joinpath(&path
, empty_tmp_dir
, "/one/two_one"));
16 cl_must_pass(p_mkdir(path
.ptr
, 0777));
18 cl_git_pass(git_buf_joinpath(&path
, empty_tmp_dir
, "/one/two_two"));
19 cl_must_pass(p_mkdir(path
.ptr
, 0777));
21 cl_git_pass(git_buf_joinpath(&path
, empty_tmp_dir
, "/one/two_two/three"));
22 cl_must_pass(p_mkdir(path
.ptr
, 0777));
24 cl_git_pass(git_buf_joinpath(&path
, empty_tmp_dir
, "/two"));
25 cl_must_pass(p_mkdir(path
.ptr
, 0777));
27 git_buf_dispose(&path
);
30 void test_core_rmdir__cleanup(void)
32 if (git_path_exists(empty_tmp_dir
))
33 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_REMOVE_FILES
));
36 /* make sure empty dir can be deleted recusively */
37 void test_core_rmdir__delete_recursive(void)
39 git_buf path
= GIT_BUF_INIT
;
40 cl_git_pass(git_buf_joinpath(&path
, empty_tmp_dir
, "/one"));
41 cl_assert(git_path_exists(git_buf_cstr(&path
)));
43 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));
45 cl_assert(!git_path_exists(git_buf_cstr(&path
)));
47 git_buf_dispose(&path
);
50 /* make sure non-empty dir cannot be deleted recusively */
51 void test_core_rmdir__fail_to_delete_non_empty_dir(void)
53 git_buf file
= GIT_BUF_INIT
;
55 cl_git_pass(git_buf_joinpath(&file
, empty_tmp_dir
, "/two/file.txt"));
57 cl_git_mkfile(git_buf_cstr(&file
), "dummy");
59 cl_git_fail(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));
61 cl_must_pass(p_unlink(file
.ptr
));
62 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));
64 cl_assert(!git_path_exists(empty_tmp_dir
));
66 git_buf_dispose(&file
);
69 void test_core_rmdir__keep_base(void)
71 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_SKIP_ROOT
));
72 cl_assert(git_path_exists(empty_tmp_dir
));
75 void test_core_rmdir__can_skip_non_empty_dir(void)
77 git_buf file
= GIT_BUF_INIT
;
79 cl_git_pass(git_buf_joinpath(&file
, empty_tmp_dir
, "/two/file.txt"));
81 cl_git_mkfile(git_buf_cstr(&file
), "dummy");
83 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_SKIP_NONEMPTY
));
84 cl_assert(git_path_exists(git_buf_cstr(&file
)) == true);
86 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_REMOVE_FILES
));
87 cl_assert(git_path_exists(empty_tmp_dir
) == false);
89 git_buf_dispose(&file
);
92 void test_core_rmdir__can_remove_empty_parents(void)
94 git_buf file
= GIT_BUF_INIT
;
97 git_buf_joinpath(&file
, empty_tmp_dir
, "/one/two_two/three/file.txt"));
98 cl_git_mkfile(git_buf_cstr(&file
), "dummy");
99 cl_assert(git_path_isfile(git_buf_cstr(&file
)));
101 cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir
,
102 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_EMPTY_PARENTS
));
104 cl_assert(!git_path_exists(git_buf_cstr(&file
)));
106 git_buf_rtruncate_at_char(&file
, '/'); /* three (only contained file.txt) */
107 cl_assert(!git_path_exists(git_buf_cstr(&file
)));
109 git_buf_rtruncate_at_char(&file
, '/'); /* two_two (only contained three) */
110 cl_assert(!git_path_exists(git_buf_cstr(&file
)));
112 git_buf_rtruncate_at_char(&file
, '/'); /* one (contained two_one also) */
113 cl_assert(git_path_exists(git_buf_cstr(&file
)));
115 cl_assert(git_path_exists(empty_tmp_dir
) == true);
117 git_buf_dispose(&file
);
119 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));