blob.c 3.92 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include "clar_libgit2.h"
#include "crlf.h"

static git_repository *g_repo = NULL;

void test_filter_blob__initialize(void)
{
	g_repo = cl_git_sandbox_init("crlf");
	cl_git_mkfile("crlf/.gitattributes",
Russell Belfer committed
10 11 12 13 14
		"*.txt text\n*.bin binary\n"
		"*.crlf text eol=crlf\n"
		"*.lf text eol=lf\n"
		"*.ident text ident\n"
		"*.identcrlf ident text eol=crlf\n"
15
		"*.identlf ident text eol=lf\n");
16 17 18 19 20 21 22 23 24 25
}

void test_filter_blob__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_filter_blob__all_crlf(void)
{
	git_blob *blob;
26
	git_buf buf = { 0 };
27 28 29 30 31 32

	cl_git_pass(git_revparse_single(
		(git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */

	cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob));

33
	cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
34 35 36

	cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);

37
	cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
38 39 40 41

	/* in this case, raw content has crlf in it already */
	cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);

42
	cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
43

44 45
	/* we never convert CRLF -> LF on platforms that have LF */
	cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
46

47
	git_buf_dispose(&buf);
48 49
	git_blob_free(blob);
}
Russell Belfer committed
50

51 52 53 54 55 56 57 58 59 60
void test_filter_blob__from_lf(void)
{
	git_blob *blob;
	git_buf buf = { 0 };

	cl_git_pass(git_revparse_single(
		(git_object **)&blob, g_repo, "799770d")); /* all-lf */

	cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));

61
	cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
62 63 64

	cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);

65
	cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
66 67 68 69

	/* in this case, raw content has crlf in it already */
	cl_assert_equal_s(ALL_LF_TEXT_AS_CRLF, buf.ptr);

70
	cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
71 72 73 74 75 76 77 78

	/* we never convert CRLF -> LF on platforms that have LF */
	cl_assert_equal_s(ALL_LF_TEXT_AS_LF, buf.ptr);

	git_buf_dispose(&buf);
	git_blob_free(blob);
}

79 80 81 82 83 84 85 86 87 88 89 90
void test_filter_blob__sanitizes(void)
{
	git_blob *blob;
	git_buf buf;

	cl_git_pass(git_revparse_single(
		(git_object **)&blob, g_repo, "e69de29")); /* zero-byte */

	cl_assert_equal_i(0, git_blob_rawsize(blob));
	cl_assert_equal_s("", git_blob_rawcontent(blob));

	memset(&buf, 0, sizeof(git_buf));
91
	cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
92 93
	cl_assert_equal_sz(0, buf.size);
	cl_assert_equal_s("", buf.ptr);
94
	git_buf_dispose(&buf);
95 96

	memset(&buf, 0, sizeof(git_buf));
97
	cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
98 99
	cl_assert_equal_sz(0, buf.size);
	cl_assert_equal_s("", buf.ptr);
100
	git_buf_dispose(&buf);
101 102

	memset(&buf, 0, sizeof(git_buf));
103
	cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
104 105
	cl_assert_equal_sz(0, buf.size);
	cl_assert_equal_s("", buf.ptr);
106
	git_buf_dispose(&buf);
107 108 109 110

	git_blob_free(blob);
}

Russell Belfer committed
111 112 113 114
void test_filter_blob__ident(void)
{
	git_oid id;
	git_blob *blob;
115
	git_buf buf = { 0 };
Russell Belfer committed
116 117

	cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
118
	cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
Russell Belfer committed
119 120 121 122 123 124
	cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
	cl_assert_equal_s(
		"Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
	git_blob_free(blob);

	cl_git_mkfile("crlf/test.ident", "Some text\n$Id: Any old just you want$\nGoes there\n");
125
	cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
Russell Belfer committed
126 127 128 129
	cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
	cl_assert_equal_s(
		"Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));

130
	cl_git_pass(git_blob_filter(&buf, blob, "filter.bin", NULL));
Russell Belfer committed
131 132 133
	cl_assert_equal_s(
		"Some text\n$Id$\nGoes there\n", buf.ptr);

134
	cl_git_pass(git_blob_filter(&buf, blob, "filter.identcrlf", NULL));
Russell Belfer committed
135
	cl_assert_equal_s(
136
		"Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\r\nGoes there\r\n", buf.ptr);
Russell Belfer committed
137

138
	cl_git_pass(git_blob_filter(&buf, blob, "filter.identlf", NULL));
Russell Belfer committed
139
	cl_assert_equal_s(
140
		"Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\nGoes there\n", buf.ptr);
Russell Belfer committed
141

142
	git_buf_dispose(&buf);
143 144
	git_blob_free(blob);

Russell Belfer committed
145
}