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
));
42 cl_assert_equal_s("Some text\r\nRight here\r\n", out
.ptr
);
44 cl_assert_equal_s("Some text\nRight here\n", out
.ptr
);
47 git_filter_list_free(fl
);
51 void test_filter_crlf__to_odb(void)
55 git_buf in
= { 0 }, out
= { 0 };
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
.ptr
= "Some text\r\nRight here\r\n";
66 in
.size
= strlen(in
.ptr
);
68 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
70 cl_assert_equal_s("Some text\nRight here\n", out
.ptr
);
72 git_filter_list_free(fl
);
76 void test_filter_crlf__with_safecrlf(void)
80 git_buf in
= {0}, out
= GIT_BUF_INIT
;
82 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
84 cl_git_pass(git_filter_list_new(
85 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
87 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
88 cl_assert(crlf
!= NULL
);
90 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
92 /* Normalized \r\n succeeds with safecrlf */
93 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
94 in
.size
= strlen(in
.ptr
);
96 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
97 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
99 /* Mix of line endings fails with safecrlf */
100 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
101 in
.size
= strlen(in
.ptr
);
103 cl_git_fail(git_filter_list_apply_to_data(&out
, fl
, &in
));
104 cl_assert_equal_i(giterr_last()->klass
, GITERR_FILTER
);
106 /* Normalized \n is reversible, so does not fail with safecrlf */
107 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
108 in
.size
= strlen(in
.ptr
);
110 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
111 cl_assert_equal_s(in
.ptr
, out
.ptr
);
113 git_filter_list_free(fl
);
117 void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
121 git_buf in
= {0}, out
= GIT_BUF_INIT
;
123 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
125 cl_git_pass(git_filter_list_new(
126 &fl
, g_repo
, GIT_FILTER_TO_ODB
, GIT_FILTER_OPT_ALLOW_UNSAFE
));
128 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
129 cl_assert(crlf
!= NULL
);
131 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
133 /* Normalized \r\n succeeds with safecrlf */
134 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
135 in
.size
= strlen(in
.ptr
);
137 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
138 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
140 /* Mix of line endings fails with safecrlf, but allowed to pass */
141 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
142 in
.size
= strlen(in
.ptr
);
144 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
145 /* TODO: check for warning */
146 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
148 /* Normalized \n fails with safecrlf, but allowed to pass */
149 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
150 in
.size
= strlen(in
.ptr
);
152 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
153 /* TODO: check for warning */
154 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
156 git_filter_list_free(fl
);
160 void test_filter_crlf__no_safecrlf(void)
164 git_buf in
= {0}, out
= GIT_BUF_INIT
;
166 cl_git_pass(git_filter_list_new(
167 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
169 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
170 cl_assert(crlf
!= NULL
);
172 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
174 /* Normalized \r\n succeeds with safecrlf */
175 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
176 in
.size
= strlen(in
.ptr
);
178 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
179 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
181 /* Mix of line endings fails with safecrlf */
182 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
183 in
.size
= strlen(in
.ptr
);
185 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
186 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
188 /* Normalized \n fails with safecrlf */
189 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
190 in
.size
= strlen(in
.ptr
);
192 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
193 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
195 git_filter_list_free(fl
);