]>
Commit | Line | Data |
---|---|---|
3fd1520c | 1 | #include "clar_libgit2.h" |
22a2d3d5 | 2 | #include "futils.h" |
f1558d9b VM |
3 | |
4 | static const char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test"; | |
5 | ||
a5f8c1bd | 6 | void test_core_rmdir__initialize(void) |
f1558d9b | 7 | { |
97769280 | 8 | git_buf path = GIT_BUF_INIT; |
f1558d9b | 9 | |
ce8cd006 | 10 | cl_must_pass(p_mkdir(empty_tmp_dir, 0777)); |
f1558d9b | 11 | |
97769280 RB |
12 | cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one")); |
13 | cl_must_pass(p_mkdir(path.ptr, 0777)); | |
f1558d9b | 14 | |
97769280 RB |
15 | cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_one")); |
16 | cl_must_pass(p_mkdir(path.ptr, 0777)); | |
f1558d9b | 17 | |
97769280 RB |
18 | cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two")); |
19 | cl_must_pass(p_mkdir(path.ptr, 0777)); | |
f1558d9b | 20 | |
97769280 RB |
21 | cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two/three")); |
22 | cl_must_pass(p_mkdir(path.ptr, 0777)); | |
f1558d9b | 23 | |
97769280 RB |
24 | cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/two")); |
25 | cl_must_pass(p_mkdir(path.ptr, 0777)); | |
26 | ||
ac3d33df JK |
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)); | |
f1558d9b VM |
34 | } |
35 | ||
36 | /* make sure empty dir can be deleted recusively */ | |
37 | void test_core_rmdir__delete_recursive(void) | |
38 | { | |
ac3d33df JK |
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 | ||
331e7de9 | 43 | cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); |
ac3d33df JK |
44 | |
45 | cl_assert(!git_path_exists(git_buf_cstr(&path))); | |
46 | ||
47 | git_buf_dispose(&path); | |
f1558d9b VM |
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 | { | |
97769280 | 53 | git_buf file = GIT_BUF_INIT; |
f1558d9b | 54 | |
97769280 | 55 | cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt")); |
f1558d9b | 56 | |
555aa453 | 57 | cl_git_mkfile(git_buf_cstr(&file), "dummy"); |
f1558d9b | 58 | |
331e7de9 | 59 | cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); |
f1558d9b | 60 | |
97769280 | 61 | cl_must_pass(p_unlink(file.ptr)); |
331e7de9 | 62 | cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); |
555aa453 | 63 | |
ac3d33df JK |
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)); | |
555aa453 | 73 | } |
74 | ||
331e7de9 | 75 | void test_core_rmdir__can_skip_non_empty_dir(void) |
555aa453 | 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 | ||
331e7de9 | 83 | cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_NONEMPTY)); |
555aa453 | 84 | cl_assert(git_path_exists(git_buf_cstr(&file)) == true); |
85 | ||
331e7de9 | 86 | cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES)); |
555aa453 | 87 | cl_assert(git_path_exists(empty_tmp_dir) == false); |
97769280 | 88 | |
ac3d33df | 89 | git_buf_dispose(&file); |
f1558d9b | 90 | } |
331e7de9 RB |
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 | ||
ac3d33df | 117 | git_buf_dispose(&file); |
331e7de9 RB |
118 | |
119 | cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); | |
120 | } |