]>
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(&fl
, g_repo
, GIT_FILTER_TO_WORKTREE
));
30 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
31 cl_assert(crlf
!= NULL
);
33 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
35 in
.ptr
= "Some text\nRight here\n";
36 in
.size
= strlen(in
.ptr
);
38 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 cl_assert_equal_s("Some text\nRight here\n", out
.ptr
);
46 git_filter_list_free(fl
);
50 void test_filter_crlf__to_odb(void)
54 git_buf in
= { 0 }, out
= { 0 };
56 cl_git_pass(git_filter_list_new(&fl
, g_repo
, GIT_FILTER_TO_ODB
));
58 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
59 cl_assert(crlf
!= NULL
);
61 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
63 in
.ptr
= "Some text\r\nRight here\r\n";
64 in
.size
= strlen(in
.ptr
);
66 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
68 cl_assert_equal_s("Some text\nRight here\n", out
.ptr
);
70 git_filter_list_free(fl
);
74 void test_filter_crlf__with_safecrlf(void)
78 git_buf in
= {0}, out
= GIT_BUF_INIT
;
80 cl_repo_set_bool(g_repo
, "core.safecrlf", true);
82 cl_git_pass(git_filter_list_new(&fl
, g_repo
, GIT_FILTER_TO_ODB
));
84 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
85 cl_assert(crlf
!= NULL
);
87 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
89 /* Normalized \r\n succeeds with safecrlf */
90 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
91 in
.size
= strlen(in
.ptr
);
93 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
94 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
96 /* Mix of line endings fails with safecrlf */
97 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
98 in
.size
= strlen(in
.ptr
);
100 cl_git_fail(git_filter_list_apply_to_data(&out
, fl
, &in
));
101 cl_assert_equal_i(giterr_last()->klass
, GITERR_FILTER
);
103 /* Normalized \n fails with safecrlf */
104 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
105 in
.size
= strlen(in
.ptr
);
107 cl_git_fail(git_filter_list_apply_to_data(&out
, fl
, &in
));
108 cl_assert_equal_i(giterr_last()->klass
, GITERR_FILTER
);
110 git_filter_list_free(fl
);
114 void test_filter_crlf__no_safecrlf(void)
118 git_buf in
= {0}, out
= GIT_BUF_INIT
;
120 cl_git_pass(git_filter_list_new(&fl
, g_repo
, GIT_FILTER_TO_ODB
));
122 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
123 cl_assert(crlf
!= NULL
);
125 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
127 /* Normalized \r\n succeeds with safecrlf */
128 in
.ptr
= "Normal\r\nCRLF\r\nline-endings.\r\n";
129 in
.size
= strlen(in
.ptr
);
131 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
132 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out
.ptr
);
134 /* Mix of line endings fails with safecrlf */
135 in
.ptr
= "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
136 in
.size
= strlen(in
.ptr
);
138 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
139 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out
.ptr
);
141 /* Normalized \n fails with safecrlf */
142 in
.ptr
= "Normal\nLF\nonly\nline-endings.\n";
143 in
.size
= strlen(in
.ptr
);
145 cl_git_pass(git_filter_list_apply_to_data(&out
, fl
, &in
));
146 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out
.ptr
);
148 git_filter_list_free(fl
);