]>
Commit | Line | Data |
---|---|---|
1 | #include "clar_libgit2.h" | |
2 | #include "git2/sys/filter.h" | |
3 | ||
4 | static git_repository *g_repo = NULL; | |
5 | ||
6 | void test_filter_crlf__initialize(void) | |
7 | { | |
8 | g_repo = cl_git_sandbox_init("crlf"); | |
9 | ||
10 | cl_git_mkfile("crlf/.gitattributes", | |
11 | "*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n"); | |
12 | ||
13 | cl_repo_set_bool(g_repo, "core.autocrlf", true); | |
14 | } | |
15 | ||
16 | void test_filter_crlf__cleanup(void) | |
17 | { | |
18 | cl_git_sandbox_cleanup(); | |
19 | } | |
20 | ||
21 | void test_filter_crlf__to_worktree(void) | |
22 | { | |
23 | git_filter_list *fl; | |
24 | git_filter *crlf; | |
25 | git_buf out = GIT_BUF_INIT; | |
26 | const char *in; | |
27 | size_t in_len; | |
28 | ||
29 | cl_git_pass(git_filter_list_new( | |
30 | &fl, g_repo, GIT_FILTER_TO_WORKTREE, 0)); | |
31 | ||
32 | crlf = git_filter_lookup(GIT_FILTER_CRLF); | |
33 | cl_assert(crlf != NULL); | |
34 | ||
35 | cl_git_pass(git_filter_list_push(fl, crlf, NULL)); | |
36 | ||
37 | in = "Some text\nRight here\n"; | |
38 | in_len = strlen(in); | |
39 | ||
40 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
41 | ||
42 | cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr); | |
43 | ||
44 | git_filter_list_free(fl); | |
45 | git_buf_dispose(&out); | |
46 | } | |
47 | ||
48 | void test_filter_crlf__to_odb(void) | |
49 | { | |
50 | git_filter_list *fl; | |
51 | git_filter *crlf; | |
52 | git_buf out = GIT_BUF_INIT; | |
53 | const char *in; | |
54 | size_t in_len; | |
55 | ||
56 | cl_git_pass(git_filter_list_new( | |
57 | &fl, g_repo, GIT_FILTER_TO_ODB, 0)); | |
58 | ||
59 | crlf = git_filter_lookup(GIT_FILTER_CRLF); | |
60 | cl_assert(crlf != NULL); | |
61 | ||
62 | cl_git_pass(git_filter_list_push(fl, crlf, NULL)); | |
63 | ||
64 | in = "Some text\r\nRight here\r\n"; | |
65 | in_len = strlen(in); | |
66 | ||
67 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
68 | ||
69 | cl_assert_equal_s("Some text\nRight here\n", out.ptr); | |
70 | ||
71 | git_filter_list_free(fl); | |
72 | git_buf_dispose(&out); | |
73 | } | |
74 | ||
75 | void test_filter_crlf__with_safecrlf(void) | |
76 | { | |
77 | git_filter_list *fl; | |
78 | git_filter *crlf; | |
79 | git_buf out = GIT_BUF_INIT; | |
80 | const char *in; | |
81 | size_t in_len; | |
82 | ||
83 | cl_repo_set_bool(g_repo, "core.safecrlf", true); | |
84 | ||
85 | cl_git_pass(git_filter_list_new( | |
86 | &fl, g_repo, GIT_FILTER_TO_ODB, 0)); | |
87 | ||
88 | crlf = git_filter_lookup(GIT_FILTER_CRLF); | |
89 | cl_assert(crlf != NULL); | |
90 | ||
91 | cl_git_pass(git_filter_list_push(fl, crlf, NULL)); | |
92 | ||
93 | /* Normalized \r\n succeeds with safecrlf */ | |
94 | in = "Normal\r\nCRLF\r\nline-endings.\r\n"; | |
95 | in_len = strlen(in); | |
96 | ||
97 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
98 | cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); | |
99 | ||
100 | /* Mix of line endings fails with safecrlf */ | |
101 | in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; | |
102 | in_len = strlen(in); | |
103 | ||
104 | cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
105 | cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER); | |
106 | ||
107 | /* Normalized \n fails for autocrlf=true when safecrlf=true */ | |
108 | in = "Normal\nLF\nonly\nline-endings.\n"; | |
109 | in_len = strlen(in); | |
110 | ||
111 | cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
112 | cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER); | |
113 | ||
114 | /* String with \r but without \r\n does not fail with safecrlf */ | |
115 | in = "Normal\nCR only\rand some more\nline-endings.\n"; | |
116 | in_len = strlen(in); | |
117 | ||
118 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
119 | cl_assert_equal_s("Normal\nCR only\rand some more\nline-endings.\n", out.ptr); | |
120 | ||
121 | git_filter_list_free(fl); | |
122 | git_buf_dispose(&out); | |
123 | } | |
124 | ||
125 | void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void) | |
126 | { | |
127 | git_filter_list *fl; | |
128 | git_filter *crlf; | |
129 | git_buf out = GIT_BUF_INIT; | |
130 | const char *in; | |
131 | size_t in_len; | |
132 | ||
133 | cl_repo_set_bool(g_repo, "core.safecrlf", true); | |
134 | ||
135 | cl_git_pass(git_filter_list_new( | |
136 | &fl, g_repo, GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)); | |
137 | ||
138 | crlf = git_filter_lookup(GIT_FILTER_CRLF); | |
139 | cl_assert(crlf != NULL); | |
140 | ||
141 | cl_git_pass(git_filter_list_push(fl, crlf, NULL)); | |
142 | ||
143 | /* Normalized \r\n succeeds with safecrlf */ | |
144 | in = "Normal\r\nCRLF\r\nline-endings.\r\n"; | |
145 | in_len = strlen(in); | |
146 | ||
147 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
148 | cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); | |
149 | ||
150 | /* Mix of line endings fails with safecrlf, but allowed to pass */ | |
151 | in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; | |
152 | in_len = strlen(in); | |
153 | ||
154 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
155 | /* TODO: check for warning */ | |
156 | cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr); | |
157 | ||
158 | /* Normalized \n fails with safecrlf, but allowed to pass */ | |
159 | in = "Normal\nLF\nonly\nline-endings.\n"; | |
160 | in_len = strlen(in); | |
161 | ||
162 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
163 | /* TODO: check for warning */ | |
164 | cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr); | |
165 | ||
166 | git_filter_list_free(fl); | |
167 | git_buf_dispose(&out); | |
168 | } | |
169 | ||
170 | void test_filter_crlf__no_safecrlf(void) | |
171 | { | |
172 | git_filter_list *fl; | |
173 | git_filter *crlf; | |
174 | git_buf out = GIT_BUF_INIT; | |
175 | const char *in; | |
176 | size_t in_len; | |
177 | ||
178 | cl_git_pass(git_filter_list_new( | |
179 | &fl, g_repo, GIT_FILTER_TO_ODB, 0)); | |
180 | ||
181 | crlf = git_filter_lookup(GIT_FILTER_CRLF); | |
182 | cl_assert(crlf != NULL); | |
183 | ||
184 | cl_git_pass(git_filter_list_push(fl, crlf, NULL)); | |
185 | ||
186 | /* Normalized \r\n succeeds with safecrlf */ | |
187 | in = "Normal\r\nCRLF\r\nline-endings.\r\n"; | |
188 | in_len = strlen(in); | |
189 | ||
190 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
191 | cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); | |
192 | ||
193 | /* Mix of line endings fails with safecrlf */ | |
194 | in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; | |
195 | in_len = strlen(in); | |
196 | ||
197 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
198 | cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr); | |
199 | ||
200 | /* Normalized \n fails with safecrlf */ | |
201 | in = "Normal\nLF\nonly\nline-endings.\n"; | |
202 | in_len = strlen(in); | |
203 | ||
204 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
205 | cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr); | |
206 | ||
207 | git_filter_list_free(fl); | |
208 | git_buf_dispose(&out); | |
209 | } | |
210 | ||
211 | void test_filter_crlf__safecrlf_warn(void) | |
212 | { | |
213 | git_filter_list *fl; | |
214 | git_filter *crlf; | |
215 | git_buf out = GIT_BUF_INIT; | |
216 | const char *in; | |
217 | size_t in_len; | |
218 | ||
219 | cl_repo_set_string(g_repo, "core.safecrlf", "warn"); | |
220 | ||
221 | cl_git_pass(git_filter_list_new( | |
222 | &fl, g_repo, GIT_FILTER_TO_ODB, 0)); | |
223 | ||
224 | crlf = git_filter_lookup(GIT_FILTER_CRLF); | |
225 | cl_assert(crlf != NULL); | |
226 | ||
227 | cl_git_pass(git_filter_list_push(fl, crlf, NULL)); | |
228 | ||
229 | /* Normalized \r\n succeeds with safecrlf=warn */ | |
230 | in = "Normal\r\nCRLF\r\nline-endings.\r\n"; | |
231 | in_len = strlen(in); | |
232 | ||
233 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
234 | cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); | |
235 | ||
236 | /* Mix of line endings succeeds with safecrlf=warn */ | |
237 | in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; | |
238 | in_len = strlen(in); | |
239 | ||
240 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
241 | /* TODO: check for warning */ | |
242 | cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr); | |
243 | ||
244 | /* Normalized \n is reversible, so does not fail with safecrlf=warn */ | |
245 | in = "Normal\nLF\nonly\nline-endings.\n"; | |
246 | in_len = strlen(in); | |
247 | ||
248 | cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); | |
249 | cl_assert_equal_s(in, out.ptr); | |
250 | ||
251 | git_filter_list_free(fl); | |
252 | git_buf_dispose(&out); | |
253 | } |