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));
30 /* make sure empty dir can be deleted recusively */
31 void test_core_rmdir__delete_recursive(void)
33 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));
36 /* make sure non-empty dir cannot be deleted recusively */
37 void test_core_rmdir__fail_to_delete_non_empty_dir(void)
39 git_buf file
= GIT_BUF_INIT
;
41 cl_git_pass(git_buf_joinpath(&file
, empty_tmp_dir
, "/two/file.txt"));
43 cl_git_mkfile(git_buf_cstr(&file
), "dummy");
45 cl_git_fail(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));
47 cl_must_pass(p_unlink(file
.ptr
));
48 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));
53 void test_core_rmdir__can_skip_non_empty_dir(void)
55 git_buf file
= GIT_BUF_INIT
;
57 cl_git_pass(git_buf_joinpath(&file
, empty_tmp_dir
, "/two/file.txt"));
59 cl_git_mkfile(git_buf_cstr(&file
), "dummy");
61 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_SKIP_NONEMPTY
));
62 cl_assert(git_path_exists(git_buf_cstr(&file
)) == true);
64 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_REMOVE_FILES
));
65 cl_assert(git_path_exists(empty_tmp_dir
) == false);
70 void test_core_rmdir__can_remove_empty_parents(void)
72 git_buf file
= GIT_BUF_INIT
;
75 git_buf_joinpath(&file
, empty_tmp_dir
, "/one/two_two/three/file.txt"));
76 cl_git_mkfile(git_buf_cstr(&file
), "dummy");
77 cl_assert(git_path_isfile(git_buf_cstr(&file
)));
79 cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir
,
80 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_EMPTY_PARENTS
));
82 cl_assert(!git_path_exists(git_buf_cstr(&file
)));
84 git_buf_rtruncate_at_char(&file
, '/'); /* three (only contained file.txt) */
85 cl_assert(!git_path_exists(git_buf_cstr(&file
)));
87 git_buf_rtruncate_at_char(&file
, '/'); /* two_two (only contained three) */
88 cl_assert(!git_path_exists(git_buf_cstr(&file
)));
90 git_buf_rtruncate_at_char(&file
, '/'); /* one (contained two_one also) */
91 cl_assert(git_path_exists(git_buf_cstr(&file
)));
93 cl_assert(git_path_exists(empty_tmp_dir
) == true);
97 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir
, NULL
, GIT_RMDIR_EMPTY_HIERARCHY
));