]> git.proxmox.com Git - libgit2.git/blame - tests/filter/blob.c
Merge pull request #3288 from ethomson/getenv
[libgit2.git] / tests / filter / blob.c
CommitLineData
0cf77103
RB
1#include "clar_libgit2.h"
2#include "crlf.h"
3
4static git_repository *g_repo = NULL;
5
6void test_filter_blob__initialize(void)
7{
8 g_repo = cl_git_sandbox_init("crlf");
9 cl_git_mkfile("crlf/.gitattributes",
4b11f25a
RB
10 "*.txt text\n*.bin binary\n"
11 "*.crlf text eol=crlf\n"
12 "*.lf text eol=lf\n"
13 "*.ident text ident\n"
14 "*.identcrlf ident text eol=crlf\n"
ad7417d7 15 "*.identlf ident text eol=lf\n");
0cf77103
RB
16}
17
18void test_filter_blob__cleanup(void)
19{
20 cl_git_sandbox_cleanup();
21}
22
23void test_filter_blob__all_crlf(void)
24{
25 git_blob *blob;
a9f51e43 26 git_buf buf = { 0 };
0cf77103
RB
27
28 cl_git_pass(git_revparse_single(
29 (git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */
30
31 cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob));
32
33 cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1));
34
35 cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);
36
37 cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1));
38
39 /* in this case, raw content has crlf in it already */
40 cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
41
42 cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1));
43
2a528bc0
ET
44 /* we never convert CRLF -> LF on platforms that have LF */
45 cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
0cf77103 46
a9f51e43 47 git_buf_free(&buf);
0cf77103
RB
48 git_blob_free(blob);
49}
4b11f25a 50
6adcaab7
ET
51void test_filter_blob__sanitizes(void)
52{
53 git_blob *blob;
54 git_buf buf;
55
56 cl_git_pass(git_revparse_single(
57 (git_object **)&blob, g_repo, "e69de29")); /* zero-byte */
58
59 cl_assert_equal_i(0, git_blob_rawsize(blob));
60 cl_assert_equal_s("", git_blob_rawcontent(blob));
61
62 memset(&buf, 0, sizeof(git_buf));
63 cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1));
64 cl_assert_equal_sz(0, buf.size);
65 cl_assert_equal_s("", buf.ptr);
66 git_buf_free(&buf);
67
68 memset(&buf, 0, sizeof(git_buf));
69 cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1));
70 cl_assert_equal_sz(0, buf.size);
71 cl_assert_equal_s("", buf.ptr);
72 git_buf_free(&buf);
73
74 memset(&buf, 0, sizeof(git_buf));
75 cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1));
76 cl_assert_equal_sz(0, buf.size);
77 cl_assert_equal_s("", buf.ptr);
78 git_buf_free(&buf);
79
80 git_blob_free(blob);
81}
82
4b11f25a
RB
83void test_filter_blob__ident(void)
84{
85 git_oid id;
86 git_blob *blob;
a9f51e43 87 git_buf buf = { 0 };
4b11f25a
RB
88
89 cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
90 cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident"));
91 cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
92 cl_assert_equal_s(
93 "Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
94 git_blob_free(blob);
95
96 cl_git_mkfile("crlf/test.ident", "Some text\n$Id: Any old just you want$\nGoes there\n");
97 cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident"));
98 cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
99 cl_assert_equal_s(
100 "Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
101
102 cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.bin", 1));
103 cl_assert_equal_s(
104 "Some text\n$Id$\nGoes there\n", buf.ptr);
105
106 cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.identcrlf", 1));
107 cl_assert_equal_s(
1ecbcd8e 108 "Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\r\nGoes there\r\n", buf.ptr);
4b11f25a
RB
109
110 cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.identlf", 1));
111 cl_assert_equal_s(
1ecbcd8e 112 "Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\nGoes there\n", buf.ptr);
4b11f25a 113
a9f51e43
RB
114 git_buf_free(&buf);
115 git_blob_free(blob);
116
4b11f25a 117}