]>
git.proxmox.com Git - libgit2.git/blob - tests/filter/crlf.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/filter.h"
5 static git_repository
*g_repo
= NULL
;
7 void test_filter_crlf__initialize(void)
9 g_repo
= cl_git_sandbox_init("crlf");
11 cl_git_mkfile("crlf/.gitattributes",
12 "*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
14 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
17 void test_filter_crlf__cleanup(void)
19 cl_git_sandbox_cleanup();
22 void test_filter_crlf__to_worktree(void)
26 git_buf in
= { 0 }, out
= { 0 };
28 cl_git_pass(git_filter_list_new(
29 &fl
, g_repo
, GIT_FILTER_TO_WORKTREE
, 0));
31 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
32 cl_assert(crlf
!= NULL
);
34 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
36 in
.ptr
= "Some text\nRight here\n";
37 in
.size
= strlen(in
.ptr
);
39 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
41 cl_assert_equal_s("Some text\r\nRight here\r\n", out
.ptr
);
43 git_filter_list_free(fl
);
47 void test_filter_crlf__to_odb(void)
51 git_buf in
= { 0 }, out
= { 0 };
53 cl_git_pass(git_filter_list_new(
54 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
56 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
57 cl_assert(crlf
!= NULL
);
59 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
61 in
.ptr
= "Some text\r\nRight here\r\n";
62 in
.size
= strlen(in
.ptr
);
64 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
66 cl_assert_equal_s("Some text\nRight here\n", out
.ptr
);
68 git_filter_list_free(fl
);
72 void test_filter_crlf__with_safecrlf(void)
76 git_buf in
= {0}, out
= GIT_BUF_INIT
;
78 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
80 cl_git_pass(git_filter_list_new(
81 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
83 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
84 cl_assert(crlf
!= NULL
);
86 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
88 /* Normalized \r\n succeeds with safecrlf */
89 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
90 in
.size
= strlen(in
.ptr
);
92 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
93 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
95 /* Mix of line endings fails with safecrlf */
96 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
97 in
.size
= strlen(in
.ptr
);
99 cl_git_fail(git_filter_list_apply_to_data(&out
, fl
, &in
));
100 cl_assert_equal_i(giterr_last()->klass
, GITERR_FILTER
);
102 /* Normalized \n is reversible, so does not fail with safecrlf */
103 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
104 in
.size
= strlen(in
.ptr
);
106 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
107 cl_assert_equal_s(in
.ptr
, out
.ptr
);
109 git_filter_list_free(fl
);
113 void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
117 git_buf in
= {0}, out
= GIT_BUF_INIT
;
119 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
121 cl_git_pass(git_filter_list_new(
122 &fl
, g_repo
, GIT_FILTER_TO_ODB
, GIT_FILTER_ALLOW_UNSAFE
));
124 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
125 cl_assert(crlf
!= NULL
);
127 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
129 /* Normalized \r\n succeeds with safecrlf */
130 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
131 in
.size
= strlen(in
.ptr
);
133 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
134 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
136 /* Mix of line endings fails with safecrlf, but allowed to pass */
137 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
138 in
.size
= strlen(in
.ptr
);
140 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
141 /* TODO: check for warning */
142 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
144 /* Normalized \n fails with safecrlf, but allowed to pass */
145 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
146 in
.size
= strlen(in
.ptr
);
148 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
149 /* TODO: check for warning */
150 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
152 git_filter_list_free(fl
);
156 void test_filter_crlf__no_safecrlf(void)
160 git_buf in
= {0}, out
= GIT_BUF_INIT
;
162 cl_git_pass(git_filter_list_new(
163 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
165 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
166 cl_assert(crlf
!= NULL
);
168 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
170 /* Normalized \r\n succeeds with safecrlf */
171 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
172 in
.size
= strlen(in
.ptr
);
174 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
175 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
177 /* Mix of line endings fails with safecrlf */
178 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
179 in
.size
= strlen(in
.ptr
);
181 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
182 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
184 /* Normalized \n fails with safecrlf */
185 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
186 in
.size
= strlen(in
.ptr
);
188 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
189 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
191 git_filter_list_free(fl
);
195 void test_filter_crlf__safecrlf_warn(void)
199 git_buf in
= {0}, out
= GIT_BUF_INIT
;
201 cl_repo_set_string(g_repo
, "core.safecrlf", "warn");
203 cl_git_pass(git_filter_list_new(
204 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
206 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
207 cl_assert(crlf
!= NULL
);
209 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
211 /* Normalized \r\n succeeds with safecrlf=warn */
212 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
213 in
.size
= strlen(in
.ptr
);
215 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
216 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
218 /* Mix of line endings succeeds with safecrlf=warn */
219 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
220 in
.size
= strlen(in
.ptr
);
222 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
223 /* TODO: check for warning */
224 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
226 /* Normalized \n is reversible, so does not fail with safecrlf=warn */
227 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
228 in
.size
= strlen(in
.ptr
);
230 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
231 cl_assert_equal_s(in
.ptr
, out
.ptr
);
233 git_filter_list_free(fl
);