Blame tests/core/rmdir.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
Packit Service 20376f
static const char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test";
Packit Service 20376f
Packit Service 20376f
void test_core_rmdir__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir(empty_tmp_dir, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one"));
Packit Service 20376f
	cl_must_pass(p_mkdir(path.ptr, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_one"));
Packit Service 20376f
	cl_must_pass(p_mkdir(path.ptr, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two"));
Packit Service 20376f
	cl_must_pass(p_mkdir(path.ptr, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two/three"));
Packit Service 20376f
	cl_must_pass(p_mkdir(path.ptr, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, empty_tmp_dir, "/two"));
Packit Service 20376f
	cl_must_pass(p_mkdir(path.ptr, 0777));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* make sure empty dir can be deleted recusively */
Packit Service 20376f
void test_core_rmdir__delete_recursive(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* make sure non-empty dir cannot be deleted recusively */
Packit Service 20376f
void test_core_rmdir__fail_to_delete_non_empty_dir(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf file = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile(git_buf_cstr(&file), "dummy");
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink(file.ptr));
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&file;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_rmdir__can_skip_non_empty_dir(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf file = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile(git_buf_cstr(&file), "dummy");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_NONEMPTY));
Packit Service 20376f
	cl_assert(git_path_exists(git_buf_cstr(&file)) == true);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
	cl_assert(git_path_exists(empty_tmp_dir) == false);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&file;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_rmdir__can_remove_empty_parents(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf file = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(
Packit Service 20376f
		git_buf_joinpath(&file, empty_tmp_dir, "/one/two_two/three/file.txt"));
Packit Service 20376f
	cl_git_mkfile(git_buf_cstr(&file), "dummy");
Packit Service 20376f
	cl_assert(git_path_isfile(git_buf_cstr(&file)));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir,
Packit Service 20376f
		GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS));
Packit Service 20376f
Packit Service 20376f
	cl_assert(!git_path_exists(git_buf_cstr(&file)));
Packit Service 20376f
Packit Service 20376f
	git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */
Packit Service 20376f
	cl_assert(!git_path_exists(git_buf_cstr(&file)));
Packit Service 20376f
Packit Service 20376f
	git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */
Packit Service 20376f
	cl_assert(!git_path_exists(git_buf_cstr(&file)));
Packit Service 20376f
Packit Service 20376f
	git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */
Packit Service 20376f
	cl_assert(git_path_exists(git_buf_cstr(&file)));
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_exists(empty_tmp_dir) == true);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&file;;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY));
Packit Service 20376f
}