]> git.proxmox.com Git - libgit2.git/blame - tests/filter/blob.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / filter / blob.c
CommitLineData
0cf77103
RB
1#include "clar_libgit2.h"
2#include "crlf.h"
3
4static git_repository *g_repo = NULL;
5
6void test_filter_blob__initialize(void)
7{
8 g_repo = cl_git_sandbox_init("crlf");
9 cl_git_mkfile("crlf/.gitattributes",
4b11f25a
RB
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"
ad7417d7 15 "*.identlf ident text eol=lf\n");
0cf77103
RB
16}
17
18void test_filter_blob__cleanup(void)
19{
20 cl_git_sandbox_cleanup();
21}
22
23void test_filter_blob__all_crlf(void)
24{
25 git_blob *blob;
a9f51e43 26 git_buf buf = { 0 };
0cf77103
RB
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
22a2d3d5 33 cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
0cf77103
RB
34
35 cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);
36
22a2d3d5 37 cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
0cf77103
RB
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
22a2d3d5 42 cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
0cf77103 43
2a528bc0
ET
44 /* we never convert CRLF -> LF on platforms that have LF */
45 cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
0cf77103 46
ac3d33df 47 git_buf_dispose(&buf);
0cf77103
RB
48 git_blob_free(blob);
49}
4b11f25a 50
22a2d3d5
UG
51void 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
6adcaab7
ET
79void 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));
22a2d3d5 91 cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
6adcaab7
ET
92 cl_assert_equal_sz(0, buf.size);
93 cl_assert_equal_s("", buf.ptr);
ac3d33df 94 git_buf_dispose(&buf);
6adcaab7
ET
95
96 memset(&buf, 0, sizeof(git_buf));
22a2d3d5 97 cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
6adcaab7
ET
98 cl_assert_equal_sz(0, buf.size);
99 cl_assert_equal_s("", buf.ptr);
ac3d33df 100 git_buf_dispose(&buf);
6adcaab7
ET
101
102 memset(&buf, 0, sizeof(git_buf));
22a2d3d5 103 cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
6adcaab7
ET
104 cl_assert_equal_sz(0, buf.size);
105 cl_assert_equal_s("", buf.ptr);
ac3d33df 106 git_buf_dispose(&buf);
6adcaab7
ET
107
108 git_blob_free(blob);
109}
110
4b11f25a
RB
111void test_filter_blob__ident(void)
112{
113 git_oid id;
114 git_blob *blob;
a9f51e43 115 git_buf buf = { 0 };
4b11f25a
RB
116
117 cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
22a2d3d5 118 cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
4b11f25a
RB
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");
22a2d3d5 125 cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
4b11f25a
RB
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
22a2d3d5 130 cl_git_pass(git_blob_filter(&buf, blob, "filter.bin", NULL));
4b11f25a
RB
131 cl_assert_equal_s(
132 "Some text\n$Id$\nGoes there\n", buf.ptr);
133
22a2d3d5 134 cl_git_pass(git_blob_filter(&buf, blob, "filter.identcrlf", NULL));
4b11f25a 135 cl_assert_equal_s(
1ecbcd8e 136 "Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\r\nGoes there\r\n", buf.ptr);
4b11f25a 137
22a2d3d5 138 cl_git_pass(git_blob_filter(&buf, blob, "filter.identlf", NULL));
4b11f25a 139 cl_assert_equal_s(
1ecbcd8e 140 "Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\nGoes there\n", buf.ptr);
4b11f25a 141
ac3d33df 142 git_buf_dispose(&buf);
a9f51e43
RB
143 git_blob_free(blob);
144
4b11f25a 145}