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