]> git.proxmox.com Git - libgit2.git/blob - tests/filter/blob.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / filter / blob.c
1 #include "clar_libgit2.h"
2 #include "crlf.h"
3
4 static git_repository *g_repo = NULL;
5
6 void test_filter_blob__initialize(void)
7 {
8 g_repo = cl_git_sandbox_init("crlf");
9 cl_git_mkfile("crlf/.gitattributes",
10 "*.txt text\n*.bin binary\n"
11 "*.crlf text eol=crlf\n"
12 "*.lf text eol=lf\n"
13 "*.ident text ident\n"
14 "*.identcrlf ident text eol=crlf\n"
15 "*.identlf ident text eol=lf\n");
16 }
17
18 void test_filter_blob__cleanup(void)
19 {
20 cl_git_sandbox_cleanup();
21 }
22
23 void test_filter_blob__all_crlf(void)
24 {
25 git_blob *blob;
26 git_buf buf = { 0 };
27
28 cl_git_pass(git_revparse_single(
29 (git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */
30
31 cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob));
32
33 cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
34
35 cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);
36
37 cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
38
39 /* in this case, raw content has crlf in it already */
40 cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
41
42 cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
43
44 /* we never convert CRLF -> LF on platforms that have LF */
45 cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
46
47 git_buf_dispose(&buf);
48 git_blob_free(blob);
49 }
50
51 void test_filter_blob__from_lf(void)
52 {
53 git_blob *blob;
54 git_buf buf = { 0 };
55
56 cl_git_pass(git_revparse_single(
57 (git_object **)&blob, g_repo, "799770d")); /* all-lf */
58
59 cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
60
61 cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
62
63 cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
64
65 cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
66
67 /* in this case, raw content has crlf in it already */
68 cl_assert_equal_s(ALL_LF_TEXT_AS_CRLF, buf.ptr);
69
70 cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
71
72 /* we never convert CRLF -> LF on platforms that have LF */
73 cl_assert_equal_s(ALL_LF_TEXT_AS_LF, buf.ptr);
74
75 git_buf_dispose(&buf);
76 git_blob_free(blob);
77 }
78
79 void test_filter_blob__sanitizes(void)
80 {
81 git_blob *blob;
82 git_buf buf;
83
84 cl_git_pass(git_revparse_single(
85 (git_object **)&blob, g_repo, "e69de29")); /* zero-byte */
86
87 cl_assert_equal_i(0, git_blob_rawsize(blob));
88 cl_assert_equal_s("", git_blob_rawcontent(blob));
89
90 memset(&buf, 0, sizeof(git_buf));
91 cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
92 cl_assert_equal_sz(0, buf.size);
93 cl_assert_equal_s("", buf.ptr);
94 git_buf_dispose(&buf);
95
96 memset(&buf, 0, sizeof(git_buf));
97 cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
98 cl_assert_equal_sz(0, buf.size);
99 cl_assert_equal_s("", buf.ptr);
100 git_buf_dispose(&buf);
101
102 memset(&buf, 0, sizeof(git_buf));
103 cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
104 cl_assert_equal_sz(0, buf.size);
105 cl_assert_equal_s("", buf.ptr);
106 git_buf_dispose(&buf);
107
108 git_blob_free(blob);
109 }
110
111 void test_filter_blob__ident(void)
112 {
113 git_oid id;
114 git_blob *blob;
115 git_buf buf = { 0 };
116
117 cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
118 cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
119 cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
120 cl_assert_equal_s(
121 "Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
122 git_blob_free(blob);
123
124 cl_git_mkfile("crlf/test.ident", "Some text\n$Id: Any old just you want$\nGoes there\n");
125 cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
126 cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
127 cl_assert_equal_s(
128 "Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
129
130 cl_git_pass(git_blob_filter(&buf, blob, "filter.bin", NULL));
131 cl_assert_equal_s(
132 "Some text\n$Id$\nGoes there\n", buf.ptr);
133
134 cl_git_pass(git_blob_filter(&buf, blob, "filter.identcrlf", NULL));
135 cl_assert_equal_s(
136 "Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\r\nGoes there\r\n", buf.ptr);
137
138 cl_git_pass(git_blob_filter(&buf, blob, "filter.identlf", NULL));
139 cl_assert_equal_s(
140 "Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\nGoes there\n", buf.ptr);
141
142 git_buf_dispose(&buf);
143 git_blob_free(blob);
144
145 }