Blame tests/object/blob/write.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *repo;
Packit Service 20376f
Packit Service 20376f
#define WORKDIR "empty_standard_repo"
Packit Service 20376f
#define BARE_REPO "testrepo.git"
Packit Service 20376f
#define ELSEWHERE "elsewhere"
Packit Service 20376f
Packit Service 20376f
typedef int (*blob_creator_fn)(
Packit Service 20376f
	git_oid *,
Packit Service 20376f
	git_repository *,
Packit Service 20376f
	const char *);
Packit Service 20376f
Packit Service 20376f
void test_object_blob_write__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_blob_creation(const char *path_to_file, const char *blob_from_path, blob_creator_fn creator)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	cl_git_mkfile(path_to_file, "1..2...3... Can you hear me?\n");
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(creator(&oid, repo, blob_from_path));
Packit Service 20376f
	cl_assert(git_oid_streq(&oid, "da5e4f20c91c81b44a7e298f3d3fb3fe2f178e32") == 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_file_located_in_the_working_directory(void)
Packit Service 20376f
{
Packit Service 20376f
	repo = cl_git_sandbox_init(WORKDIR);
Packit Service 20376f
Packit Service 20376f
	assert_blob_creation(WORKDIR "/test.txt", "test.txt", &git_blob_create_fromworkdir);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_absolute_filepath_pointing_outside_of_the_working_directory(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf full_path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	repo = cl_git_sandbox_init(WORKDIR);
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir(ELSEWHERE, 0777));
Packit Service 20376f
	cl_must_pass(git_path_prettify_dir(&full_path, ELSEWHERE, NULL));
Packit Service 20376f
	cl_must_pass(git_buf_puts(&full_path, "test.txt"));
Packit Service 20376f
Packit Service 20376f
	assert_blob_creation(ELSEWHERE "/test.txt", git_buf_cstr(&full_path), &git_blob_create_fromdisk);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&full_path);
Packit Service 20376f
	cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_blob_write__can_create_a_blob_in_a_bare_repo_from_a_absolute_filepath(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf full_path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	repo = cl_git_sandbox_init(BARE_REPO);
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir(ELSEWHERE, 0777));
Packit Service 20376f
	cl_must_pass(git_path_prettify_dir(&full_path, ELSEWHERE, NULL));
Packit Service 20376f
	cl_must_pass(git_buf_puts(&full_path, "test.txt"));
Packit Service 20376f
Packit Service 20376f
	assert_blob_creation(ELSEWHERE "/test.txt", git_buf_cstr(&full_path), &git_blob_create_fromdisk);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&full_path);
Packit Service 20376f
	cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
}