rmdir.c 3.5 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
Vicent Marti committed
3 4 5

static const char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test";

6
void test_rmdir__initialize(void)
Vicent Marti committed
7
{
8
	git_str path = GIT_STR_INIT;
Vicent Marti committed
9

10
	cl_must_pass(p_mkdir(empty_tmp_dir, 0777));
Vicent Marti committed
11

12
	cl_git_pass(git_str_joinpath(&path, empty_tmp_dir, "/one"));
13
	cl_must_pass(p_mkdir(path.ptr, 0777));
Vicent Marti committed
14

15
	cl_git_pass(git_str_joinpath(&path, empty_tmp_dir, "/one/two_one"));
16
	cl_must_pass(p_mkdir(path.ptr, 0777));
Vicent Marti committed
17

18
	cl_git_pass(git_str_joinpath(&path, empty_tmp_dir, "/one/two_two"));
19
	cl_must_pass(p_mkdir(path.ptr, 0777));
Vicent Marti committed
20

21
	cl_git_pass(git_str_joinpath(&path, empty_tmp_dir, "/one/two_two/three"));
22
	cl_must_pass(p_mkdir(path.ptr, 0777));
Vicent Marti committed
23

24
	cl_git_pass(git_str_joinpath(&path, empty_tmp_dir, "/two"));
25 26
	cl_must_pass(p_mkdir(path.ptr, 0777));

27
	git_str_dispose(&path);
Vicent Marti committed
28 29
}

30
void test_rmdir__cleanup(void)
31
{
32
	if (git_fs_path_exists(empty_tmp_dir))
33 34 35
		cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES));
}

Dimitris Apostolou committed
36
/* make sure empty dir can be deleted recursively */
37
void test_rmdir__delete_recursive(void)
Vicent Marti committed
38
{
39 40
	git_str path = GIT_STR_INIT;
	cl_git_pass(git_str_joinpath(&path, empty_tmp_dir, "/one"));
41
	cl_assert(git_fs_path_exists(git_str_cstr(&path)));
42

43
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
44

45
	cl_assert(!git_fs_path_exists(git_str_cstr(&path)));
46

47
	git_str_dispose(&path);
Vicent Marti committed
48 49
}

Dimitris Apostolou committed
50
/* make sure non-empty dir cannot be deleted recursively */
51
void test_rmdir__fail_to_delete_non_empty_dir(void)
Vicent Marti committed
52
{
53
	git_str file = GIT_STR_INIT;
Vicent Marti committed
54

55
	cl_git_pass(git_str_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
Vicent Marti committed
56

57
	cl_git_mkfile(git_str_cstr(&file), "dummy");
Vicent Marti committed
58

59
	cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
Vicent Marti committed
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_fs_path_exists(empty_tmp_dir));
65

66
	git_str_dispose(&file);
67 68
}

69
void test_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_fs_path_exists(empty_tmp_dir));
73 74
}

75
void test_rmdir__can_skip_non_empty_dir(void)
76
{
77
	git_str file = GIT_STR_INIT;
78

79
	cl_git_pass(git_str_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
80

81
	cl_git_mkfile(git_str_cstr(&file), "dummy");
82

83
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_NONEMPTY));
84
	cl_assert(git_fs_path_exists(git_str_cstr(&file)) == true);
85

86
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES));
87
	cl_assert(git_fs_path_exists(empty_tmp_dir) == false);
88

89
	git_str_dispose(&file);
Vicent Marti committed
90
}
91

92
void test_rmdir__can_remove_empty_parents(void)
93
{
94
	git_str file = GIT_STR_INIT;
95 96

	cl_git_pass(
97 98
		git_str_joinpath(&file, empty_tmp_dir, "/one/two_two/three/file.txt"));
	cl_git_mkfile(git_str_cstr(&file), "dummy");
99
	cl_assert(git_fs_path_isfile(git_str_cstr(&file)));
100 101 102 103

	cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir,
		GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS));

104
	cl_assert(!git_fs_path_exists(git_str_cstr(&file)));
105

106
	git_str_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */
107
	cl_assert(!git_fs_path_exists(git_str_cstr(&file)));
108

109
	git_str_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */
110
	cl_assert(!git_fs_path_exists(git_str_cstr(&file)));
111

112
	git_str_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */
113
	cl_assert(git_fs_path_exists(git_str_cstr(&file)));
114

115
	cl_assert(git_fs_path_exists(empty_tmp_dir) == true);
116

117
	git_str_dispose(&file);
118 119 120

	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
}