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

static const char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test";

Vicent Marti committed
6
void test_core_rmdir__initialize(void)
Vicent Marti committed
7
{
8
	git_buf path = GIT_BUF_INIT;
Vicent Marti committed
9

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

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

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

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

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

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

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

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

Vicent Marti committed
36 37 38
/* make sure empty dir can be deleted recusively */
void test_core_rmdir__delete_recursive(void)
{
39 40 41 42
	git_buf path = GIT_BUF_INIT;
	cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one"));
	cl_assert(git_path_exists(git_buf_cstr(&path)));

43
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
44 45 46 47

	cl_assert(!git_path_exists(git_buf_cstr(&path)));

	git_buf_dispose(&path);
Vicent Marti committed
48 49 50 51 52
}

/* make sure non-empty dir cannot be deleted recusively */
void test_core_rmdir__fail_to_delete_non_empty_dir(void)
{
53
	git_buf file = GIT_BUF_INIT;
Vicent Marti committed
54

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

57
	cl_git_mkfile(git_buf_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 65
	cl_assert(!git_path_exists(empty_tmp_dir));

66
	git_buf_dispose(&file);
67 68
}

69 70 71 72 73 74
void test_core_rmdir__keep_base(void)
{
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_ROOT));
	cl_assert(git_path_exists(empty_tmp_dir));
}

75
void test_core_rmdir__can_skip_non_empty_dir(void)
76 77 78 79 80 81 82
{
	git_buf file = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));

	cl_git_mkfile(git_buf_cstr(&file), "dummy");

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

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);
Vicent Marti committed
90
}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

void test_core_rmdir__can_remove_empty_parents(void)
{
	git_buf file = GIT_BUF_INIT;

	cl_git_pass(
		git_buf_joinpath(&file, empty_tmp_dir, "/one/two_two/three/file.txt"));
	cl_git_mkfile(git_buf_cstr(&file), "dummy");
	cl_assert(git_path_isfile(git_buf_cstr(&file)));

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

	cl_assert(!git_path_exists(git_buf_cstr(&file)));

	git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */
	cl_assert(!git_path_exists(git_buf_cstr(&file)));

	git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */
	cl_assert(!git_path_exists(git_buf_cstr(&file)));

	git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */
	cl_assert(git_path_exists(git_buf_cstr(&file)));

	cl_assert(git_path_exists(empty_tmp_dir) == true);

117
	git_buf_dispose(&file);
118 119 120

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