Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d92b9a

Browse files
ethomsonEdward Thomson
authored and
Edward Thomson
committed
crlf tests: use known-good data produced by git
Given a variety of combinations of core.autocrlf settings and attributes settings, test that we check out data into the working directory the same as a known-good test resource created by git.git.
1 parent bf8c0a9 commit 3d92b9a

File tree

1 file changed

+156
-36
lines changed

1 file changed

+156
-36
lines changed

tests/checkout/crlf.c

Lines changed: 156 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "clar_libgit2.h"
22
#include "checkout_helpers.h"
33
#include "../filter/crlf.h"
4+
#include "fileops.h"
45

56
#include "git2/checkout.h"
67
#include "repository.h"
@@ -9,14 +10,169 @@
910

1011
static git_repository *g_repo;
1112

13+
static const char *systype;
14+
static git_buf expected_fixture = GIT_BUF_INIT;
15+
1216
void test_checkout_crlf__initialize(void)
1317
{
1418
g_repo = cl_git_sandbox_init("crlf");
19+
20+
if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
21+
systype = "windows";
22+
else
23+
systype = "posix";
1524
}
1625

1726
void test_checkout_crlf__cleanup(void)
1827
{
1928
cl_git_sandbox_cleanup();
29+
30+
if (expected_fixture.size) {
31+
cl_fixture_cleanup(expected_fixture.ptr);
32+
git_buf_free(&expected_fixture);
33+
}
34+
}
35+
36+
struct compare_data
37+
{
38+
const char *dirname;
39+
const char *autocrlf;
40+
const char *attrs;
41+
};
42+
43+
static int compare_file(void *payload, git_buf *actual_path)
44+
{
45+
git_buf expected_path = GIT_BUF_INIT;
46+
git_buf actual_contents = GIT_BUF_INIT;
47+
git_buf expected_contents = GIT_BUF_INIT;
48+
struct compare_data *cd = payload;
49+
bool failed = true;
50+
51+
if (strcmp(git_path_basename(actual_path->ptr), ".git") == 0 ||
52+
strcmp(git_path_basename(actual_path->ptr), ".gitattributes") == 0) {
53+
failed = false;
54+
goto done;
55+
}
56+
57+
cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname,
58+
git_path_basename(actual_path->ptr)));
59+
60+
if (!git_path_isfile(expected_path.ptr) ||
61+
!git_path_isfile(actual_path->ptr))
62+
goto done;
63+
64+
if (git_futils_readbuffer(&actual_contents, actual_path->ptr) < 0 ||
65+
git_futils_readbuffer(&expected_contents, expected_path.ptr) < 0)
66+
goto done;
67+
68+
if (actual_contents.size != expected_contents.size)
69+
goto done;
70+
71+
if (memcmp(actual_contents.ptr, expected_contents.ptr, expected_contents.size) != 0)
72+
goto done;
73+
74+
failed = false;
75+
76+
done:
77+
if (failed) {
78+
git_buf details = GIT_BUF_INIT;
79+
git_buf_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}",
80+
git_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs);
81+
clar__fail(__FILE__, __LINE__,
82+
"checked out contents did not match expected", details.ptr, 0);
83+
git_buf_free(&details);
84+
}
85+
86+
git_buf_free(&expected_contents);
87+
git_buf_free(&actual_contents);
88+
git_buf_free(&expected_path);
89+
90+
return 0;
91+
}
92+
93+
static void test_checkout(const char *autocrlf, const char *attrs)
94+
{
95+
git_buf attrbuf = GIT_BUF_INIT;
96+
git_buf expected_dirname = GIT_BUF_INIT;
97+
git_buf sandboxname = GIT_BUF_INIT;
98+
git_buf reponame = GIT_BUF_INIT;
99+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
100+
struct compare_data compare_data = { NULL, autocrlf, attrs };
101+
const char *c;
102+
103+
git_buf_puts(&reponame, "crlf");
104+
105+
git_buf_puts(&sandboxname, "autocrlf_");
106+
git_buf_puts(&sandboxname, autocrlf);
107+
108+
if (*attrs) {
109+
git_buf_puts(&sandboxname, ",");
110+
111+
for (c = attrs; *c; c++) {
112+
if (*c == ' ')
113+
git_buf_putc(&sandboxname, ',');
114+
else if (*c == '=')
115+
git_buf_putc(&sandboxname, '_');
116+
else
117+
git_buf_putc(&sandboxname, *c);
118+
}
119+
120+
git_buf_printf(&attrbuf, "* %s\n", attrs);
121+
cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
122+
}
123+
124+
cl_repo_set_string(g_repo, "core.autocrlf", autocrlf);
125+
126+
git_buf_joinpath(&expected_dirname, systype, sandboxname.ptr);
127+
git_buf_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr);
128+
cl_fixture_sandbox(expected_fixture.ptr);
129+
130+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
131+
git_checkout_head(g_repo, &opts);
132+
133+
compare_data.dirname = sandboxname.ptr;
134+
cl_git_pass(git_path_direach(&reponame, 0, compare_file, &compare_data));
135+
136+
cl_fixture_cleanup(expected_fixture.ptr);
137+
git_buf_free(&expected_fixture);
138+
139+
git_buf_free(&attrbuf);
140+
git_buf_free(&expected_fixture);
141+
git_buf_free(&expected_dirname);
142+
git_buf_free(&sandboxname);
143+
git_buf_free(&reponame);
144+
}
145+
146+
static void empty_workdir(const char *name)
147+
{
148+
git_vector contents = GIT_VECTOR_INIT;
149+
size_t i;
150+
const char *fn;
151+
152+
git_path_dirload(&contents, name, 0, 0);
153+
git_vector_foreach(&contents, i, fn) {
154+
if (strncasecmp(git_path_basename(fn), ".git", 4) == 0)
155+
continue;
156+
p_unlink(fn);
157+
}
158+
git_vector_free_deep(&contents);
159+
}
160+
161+
void test_checkout_crlf__matches_core_git(void)
162+
{
163+
const char *autocrlf[] = { "true", "false", "input", NULL };
164+
const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
165+
"text", "text eol=crlf", "text eol=lf",
166+
"text=auto", "text=auto eol=crlf", "text=auto eol=lf",
167+
NULL };
168+
const char **a, **b;
169+
170+
for (a = autocrlf; *a; a++) {
171+
for (b = attrs; *b; b++) {
172+
empty_workdir("crlf");
173+
test_checkout(*a, *b);
174+
}
175+
}
20176
}
21177

22178
void test_checkout_crlf__detect_crlf_autocrlf_false(void)
@@ -72,42 +228,6 @@ void test_checkout_crlf__detect_crlf_autocrlf_true(void)
72228
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
73229
}
74230

75-
void test_checkout_crlf__more_lf_autocrlf_true(void)
76-
{
77-
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
78-
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
79-
80-
cl_repo_set_bool(g_repo, "core.autocrlf", true);
81-
82-
git_checkout_head(g_repo, &opts);
83-
84-
check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
85-
}
86-
87-
void test_checkout_crlf__more_crlf_autocrlf_true(void)
88-
{
89-
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
90-
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
91-
92-
cl_repo_set_bool(g_repo, "core.autocrlf", true);
93-
94-
git_checkout_head(g_repo, &opts);
95-
96-
check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
97-
}
98-
99-
void test_checkout_crlf__all_crlf_autocrlf_true(void)
100-
{
101-
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
102-
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
103-
104-
cl_repo_set_bool(g_repo, "core.autocrlf", true);
105-
106-
git_checkout_head(g_repo, &opts);
107-
108-
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
109-
}
110-
111231
void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
112232
{
113233
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

0 commit comments

Comments
 (0)