]> git.proxmox.com Git - libgit2.git/blame - tests/object/blob/write.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / object / blob / write.c
CommitLineData
6ca9643c 1#include "clar_libgit2.h"
2#include "buffer.h"
3#include "posix.h"
4#include "path.h"
22a2d3d5 5#include "futils.h"
6ca9643c 6
7static git_repository *repo;
8
9#define WORKDIR "empty_standard_repo"
10#define BARE_REPO "testrepo.git"
11#define ELSEWHERE "elsewhere"
12
13typedef int (*blob_creator_fn)(
14 git_oid *,
15 git_repository *,
16 const char *);
17
f0b350eb
CMN
18void test_object_blob_write__cleanup(void)
19{
20 cl_git_sandbox_cleanup();
21}
22
6ca9643c 23static void assert_blob_creation(const char *path_to_file, const char *blob_from_path, blob_creator_fn creator)
24{
25 git_oid oid;
26 cl_git_mkfile(path_to_file, "1..2...3... Can you hear me?\n");
27
28 cl_must_pass(creator(&oid, repo, blob_from_path));
29 cl_assert(git_oid_streq(&oid, "da5e4f20c91c81b44a7e298f3d3fb3fe2f178e32") == 0);
30}
31
32void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_file_located_in_the_working_directory(void)
33{
34 repo = cl_git_sandbox_init(WORKDIR);
35
22a2d3d5 36 assert_blob_creation(WORKDIR "/test.txt", "test.txt", &git_blob_create_from_workdir);
6ca9643c 37}
38
39void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_absolute_filepath_pointing_outside_of_the_working_directory(void)
40{
41 git_buf full_path = GIT_BUF_INIT;
42
43 repo = cl_git_sandbox_init(WORKDIR);
44
45 cl_must_pass(p_mkdir(ELSEWHERE, 0777));
46 cl_must_pass(git_path_prettify_dir(&full_path, ELSEWHERE, NULL));
47 cl_must_pass(git_buf_puts(&full_path, "test.txt"));
48
22a2d3d5 49 assert_blob_creation(ELSEWHERE "/test.txt", git_buf_cstr(&full_path), &git_blob_create_from_disk);
6ca9643c 50
ac3d33df 51 git_buf_dispose(&full_path);
331e7de9 52 cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
6ca9643c 53}
54
55void test_object_blob_write__can_create_a_blob_in_a_bare_repo_from_a_absolute_filepath(void)
56{
57 git_buf full_path = GIT_BUF_INIT;
58
59 repo = cl_git_sandbox_init(BARE_REPO);
60
61 cl_must_pass(p_mkdir(ELSEWHERE, 0777));
62 cl_must_pass(git_path_prettify_dir(&full_path, ELSEWHERE, NULL));
63 cl_must_pass(git_buf_puts(&full_path, "test.txt"));
64
22a2d3d5 65 assert_blob_creation(ELSEWHERE "/test.txt", git_buf_cstr(&full_path), &git_blob_create_from_disk);
6ca9643c 66
ac3d33df 67 git_buf_dispose(&full_path);
331e7de9 68 cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
6ca9643c 69}