]>
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 out
= GIT_BUF_INIT
;
30 cl_git_pass(git_filter_list_new(
31 &fl
, g_repo
, GIT_FILTER_TO_WORKTREE
, 0));
33 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
34 cl_assert(crlf
!= NULL
);
36 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
38 in
= "Some text\nRight here\n";
41 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
43 cl_assert_equal_s("Some text\r\nRight here\r\n", out
.ptr
);
45 git_filter_list_free(fl
);
46 git_buf_dispose(&out
);
49 void test_filter_crlf__to_odb(void)
53 git_buf out
= GIT_BUF_INIT
;
57 cl_git_pass(git_filter_list_new(
58 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
60 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
61 cl_assert(crlf
!= NULL
);
63 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
65 in
= "Some text\r\nRight here\r\n";
68 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
70 cl_assert_equal_s("Some text\nRight here\n", out
.ptr
);
72 git_filter_list_free(fl
);
73 git_buf_dispose(&out
);
76 void test_filter_crlf__with_safecrlf(void)
80 git_buf out
= GIT_BUF_INIT
;
84 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
86 cl_git_pass(git_filter_list_new(
87 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
89 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
90 cl_assert(crlf
!= NULL
);
92 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
94 /* Normalized \r\n succeeds with safecrlf */
95 in
= "Normal\r\nCRLF\r\nline-endings.\r\n";
98 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
99 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
101 /* Mix of line endings fails with safecrlf */
102 in
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
105 cl_git_fail(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
106 cl_assert_equal_i(git_error_last()->klass
, GIT_ERROR_FILTER
);
108 /* Normalized \n fails for autocrlf=true when safecrlf=true */
109 in
= "Normal\nLF\nonly\nline-endings.\n";
112 cl_git_fail(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
113 cl_assert_equal_i(git_error_last()->klass
, GIT_ERROR_FILTER
);
115 /* String with \r but without \r\n does not fail with safecrlf */
116 in
= "Normal\nCR only\rand some more\nline-endings.\n";
119 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
120 cl_assert_equal_s("Normal\nCR only\rand some more\nline-endings.\n", out
.ptr
);
122 git_filter_list_free(fl
);
123 git_buf_dispose(&out
);
126 void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
130 git_buf out
= GIT_BUF_INIT
;
134 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
136 cl_git_pass(git_filter_list_new(
137 &fl
, g_repo
, GIT_FILTER_TO_ODB
, GIT_FILTER_ALLOW_UNSAFE
));
139 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
140 cl_assert(crlf
!= NULL
);
142 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
144 /* Normalized \r\n succeeds with safecrlf */
145 in
= "Normal\r\nCRLF\r\nline-endings.\r\n";
148 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
149 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
151 /* Mix of line endings fails with safecrlf, but allowed to pass */
152 in
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
155 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
156 /* TODO: check for warning */
157 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
159 /* Normalized \n fails with safecrlf, but allowed to pass */
160 in
= "Normal\nLF\nonly\nline-endings.\n";
163 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
164 /* TODO: check for warning */
165 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
167 git_filter_list_free(fl
);
168 git_buf_dispose(&out
);
171 void test_filter_crlf__no_safecrlf(void)
175 git_buf out
= GIT_BUF_INIT
;
179 cl_git_pass(git_filter_list_new(
180 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
182 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
183 cl_assert(crlf
!= NULL
);
185 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
187 /* Normalized \r\n succeeds with safecrlf */
188 in
= "Normal\r\nCRLF\r\nline-endings.\r\n";
191 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
192 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
194 /* Mix of line endings fails with safecrlf */
195 in
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
198 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
199 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
201 /* Normalized \n fails with safecrlf */
202 in
= "Normal\nLF\nonly\nline-endings.\n";
205 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
206 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
208 git_filter_list_free(fl
);
209 git_buf_dispose(&out
);
212 void test_filter_crlf__safecrlf_warn(void)
216 git_buf out
= GIT_BUF_INIT
;
220 cl_repo_set_string(g_repo
, "core.safecrlf", "warn");
222 cl_git_pass(git_filter_list_new(
223 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
225 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
226 cl_assert(crlf
!= NULL
);
228 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
230 /* Normalized \r\n succeeds with safecrlf=warn */
231 in
= "Normal\r\nCRLF\r\nline-endings.\r\n";
234 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
235 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
237 /* Mix of line endings succeeds with safecrlf=warn */
238 in
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
241 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
242 /* TODO: check for warning */
243 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
245 /* Normalized \n is reversible, so does not fail with safecrlf=warn */
246 in
= "Normal\nLF\nonly\nline-endings.\n";
249 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, in
, in_len
));
250 cl_assert_equal_s(in
, out
.ptr
);
252 git_filter_list_free(fl
);
253 git_buf_dispose(&out
);