]> git.proxmox.com Git - libgit2.git/blob - tests/object/blob/fromstream.c
60ff3991b9ecfca19a3cb42407bfaabc4ab9d4ba
[libgit2.git] / tests / object / blob / fromstream.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 static char textual_content[] = "libgit2\n\r\n\0";
8
9 void test_object_blob_fromstream__initialize(void)
10 {
11 repo = cl_git_sandbox_init("testrepo.git");
12 }
13
14 void test_object_blob_fromstream__cleanup(void)
15 {
16 cl_git_sandbox_cleanup();
17 }
18
19 void test_object_blob_fromstream__multiple_write(void)
20 {
21 git_oid expected_id, id;
22 git_object *blob;
23 git_writestream *stream;
24 int i, howmany = 6;
25
26 cl_git_pass(git_oid_fromstr(&expected_id, "321cbdf08803c744082332332838df6bd160f8f9"));
27
28 cl_git_fail_with(GIT_ENOTFOUND,
29 git_object_lookup(&blob, repo, &expected_id, GIT_OBJECT_ANY));
30
31 cl_git_pass(git_blob_create_from_stream(&stream, repo, NULL));
32
33 for (i = 0; i < howmany; i++)
34 cl_git_pass(stream->write(stream, textual_content, strlen(textual_content)));
35
36 cl_git_pass(git_blob_create_from_stream_commit(&id, stream));
37 cl_assert_equal_oid(&expected_id, &id);
38
39 cl_git_pass(git_object_lookup(&blob, repo, &expected_id, GIT_OBJECT_BLOB));
40
41 git_object_free(blob);
42 }
43
44 #define GITATTR "* text=auto\n" \
45 "*.txt text\n" \
46 "*.data binary\n"
47
48 static void write_attributes(git_repository *repo)
49 {
50 git_str buf = GIT_STR_INIT;
51
52 cl_git_pass(git_str_joinpath(&buf, git_repository_path(repo), "info"));
53 cl_git_pass(git_str_joinpath(&buf, git_str_cstr(&buf), "attributes"));
54
55 cl_git_pass(git_futils_mkpath2file(git_str_cstr(&buf), 0777));
56 cl_git_rewritefile(git_str_cstr(&buf), GITATTR);
57
58 git_str_dispose(&buf);
59 }
60
61 static void assert_named_chunked_blob(const char *expected_sha, const char *fake_name)
62 {
63 git_oid expected_id, id;
64 git_writestream *stream;
65 int i, howmany = 6;
66
67 cl_git_pass(git_oid_fromstr(&expected_id, expected_sha));
68
69 cl_git_pass(git_blob_create_from_stream(&stream, repo, fake_name));
70
71 for (i = 0; i < howmany; i++)
72 cl_git_pass(stream->write(stream, textual_content, strlen(textual_content)));
73
74 cl_git_pass(git_blob_create_from_stream_commit(&id, stream));
75
76 cl_assert_equal_oid(&expected_id, &id);
77 }
78
79 void test_object_blob_fromstream__creating_a_blob_from_chunks_honors_the_attributes_directives(void)
80 {
81 write_attributes(repo);
82
83 assert_named_chunked_blob("321cbdf08803c744082332332838df6bd160f8f9", "dummy.data");
84 assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.txt");
85 assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.dunno");
86 }