]> git.proxmox.com Git - libgit2.git/blob - tests/core/rmdir.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / core / rmdir.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3
4 static const char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test";
5
6 void test_core_rmdir__initialize(void)
7 {
8 git_buf path = GIT_BUF_INIT;
9
10 cl_must_pass(p_mkdir(empty_tmp_dir, 0777));
11
12 cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one"));
13 cl_must_pass(p_mkdir(path.ptr, 0777));
14
15 cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_one"));
16 cl_must_pass(p_mkdir(path.ptr, 0777));
17
18 cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two"));
19 cl_must_pass(p_mkdir(path.ptr, 0777));
20
21 cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two/three"));
22 cl_must_pass(p_mkdir(path.ptr, 0777));
23
24 cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/two"));
25 cl_must_pass(p_mkdir(path.ptr, 0777));
26
27 git_buf_dispose(&path);
28 }
29
30 void test_core_rmdir__cleanup(void)
31 {
32 if (git_path_exists(empty_tmp_dir))
33 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES));
34 }
35
36 /* make sure empty dir can be deleted recusively */
37 void test_core_rmdir__delete_recursive(void)
38 {
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)));
42
43 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
44
45 cl_assert(!git_path_exists(git_buf_cstr(&path)));
46
47 git_buf_dispose(&path);
48 }
49
50 /* make sure non-empty dir cannot be deleted recusively */
51 void test_core_rmdir__fail_to_delete_non_empty_dir(void)
52 {
53 git_buf file = GIT_BUF_INIT;
54
55 cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
56
57 cl_git_mkfile(git_buf_cstr(&file), "dummy");
58
59 cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
60
61 cl_must_pass(p_unlink(file.ptr));
62 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
63
64 cl_assert(!git_path_exists(empty_tmp_dir));
65
66 git_buf_dispose(&file);
67 }
68
69 void test_core_rmdir__keep_base(void)
70 {
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));
73 }
74
75 void test_core_rmdir__can_skip_non_empty_dir(void)
76 {
77 git_buf file = GIT_BUF_INIT;
78
79 cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
80
81 cl_git_mkfile(git_buf_cstr(&file), "dummy");
82
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);
85
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);
88
89 git_buf_dispose(&file);
90 }
91
92 void test_core_rmdir__can_remove_empty_parents(void)
93 {
94 git_buf file = GIT_BUF_INIT;
95
96 cl_git_pass(
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)));
100
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));
103
104 cl_assert(!git_path_exists(git_buf_cstr(&file)));
105
106 git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */
107 cl_assert(!git_path_exists(git_buf_cstr(&file)));
108
109 git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */
110 cl_assert(!git_path_exists(git_buf_cstr(&file)));
111
112 git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */
113 cl_assert(git_path_exists(git_buf_cstr(&file)));
114
115 cl_assert(git_path_exists(empty_tmp_dir) == true);
116
117 git_buf_dispose(&file);
118
119 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
120 }