]> git.proxmox.com Git - libgit2.git/blob - tests/object/blob/write.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / object / blob / write.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "path.h"
4 #include "futils.h"
5
6 static git_repository *repo;
7
8 #define WORKDIR "empty_standard_repo"
9 #define BARE_REPO "testrepo.git"
10 #define ELSEWHERE "elsewhere"
11
12 typedef int (*blob_creator_fn)(
13 git_oid *,
14 git_repository *,
15 const char *);
16
17 void test_object_blob_write__cleanup(void)
18 {
19 cl_git_sandbox_cleanup();
20 }
21
22 static 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
31 void 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
35 assert_blob_creation(WORKDIR "/test.txt", "test.txt", &git_blob_create_from_workdir);
36 }
37
38 void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_absolute_filepath_pointing_outside_of_the_working_directory(void)
39 {
40 git_str full_path = GIT_STR_INIT;
41
42 repo = cl_git_sandbox_init(WORKDIR);
43
44 cl_must_pass(p_mkdir(ELSEWHERE, 0777));
45 cl_must_pass(git_fs_path_prettify_dir(&full_path, ELSEWHERE, NULL));
46 cl_must_pass(git_str_puts(&full_path, "test.txt"));
47
48 assert_blob_creation(ELSEWHERE "/test.txt", git_str_cstr(&full_path), &git_blob_create_from_disk);
49
50 git_str_dispose(&full_path);
51 cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
52 }
53
54 void test_object_blob_write__can_create_a_blob_in_a_bare_repo_from_a_absolute_filepath(void)
55 {
56 git_str full_path = GIT_STR_INIT;
57
58 repo = cl_git_sandbox_init(BARE_REPO);
59
60 cl_must_pass(p_mkdir(ELSEWHERE, 0777));
61 cl_must_pass(git_fs_path_prettify_dir(&full_path, ELSEWHERE, NULL));
62 cl_must_pass(git_str_puts(&full_path, "test.txt"));
63
64 assert_blob_creation(ELSEWHERE "/test.txt", git_str_cstr(&full_path), &git_blob_create_from_disk);
65
66 git_str_dispose(&full_path);
67 cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
68 }