]> git.proxmox.com Git - libgit2.git/blob - tests/core/rmdir.c
Rename tests-clar to tests
[libgit2.git] / tests / core / rmdir.c
1 #include "clar_libgit2.h"
2 #include "fileops.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_free(&path);
28 }
29
30 /* make sure empty dir can be deleted recusively */
31 void test_core_rmdir__delete_recursive(void)
32 {
33 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
34 }
35
36 /* make sure non-empty dir cannot be deleted recusively */
37 void test_core_rmdir__fail_to_delete_non_empty_dir(void)
38 {
39 git_buf file = GIT_BUF_INIT;
40
41 cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
42
43 cl_git_mkfile(git_buf_cstr(&file), "dummy");
44
45 cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
46
47 cl_must_pass(p_unlink(file.ptr));
48 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
49
50 git_buf_free(&file);
51 }
52
53 void test_core_rmdir__can_skip_non_empty_dir(void)
54 {
55 git_buf file = GIT_BUF_INIT;
56
57 cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
58
59 cl_git_mkfile(git_buf_cstr(&file), "dummy");
60
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);
63
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);
66
67 git_buf_free(&file);
68 }
69
70 void test_core_rmdir__can_remove_empty_parents(void)
71 {
72 git_buf file = GIT_BUF_INIT;
73
74 cl_git_pass(
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)));
78
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));
81
82 cl_assert(!git_path_exists(git_buf_cstr(&file)));
83
84 git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */
85 cl_assert(!git_path_exists(git_buf_cstr(&file)));
86
87 git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */
88 cl_assert(!git_path_exists(git_buf_cstr(&file)));
89
90 git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */
91 cl_assert(git_path_exists(git_buf_cstr(&file)));
92
93 cl_assert(git_path_exists(empty_tmp_dir) == true);
94
95 git_buf_free(&file);
96
97 cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
98 }