summaryrefslogtreecommitdiff
path: root/tests-clar/checkout/crlf.c
blob: 856d32bd1ba781f0fe253cb453cebb52744bb098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "clar_libgit2.h"
#include "checkout_helpers.h"

#include "git2/checkout.h"
#include "repository.h"

#define UTF8_BOM "\xEF\xBB\xBF"
#define ALL_CRLF_TEXT_RAW	"crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n"
#define ALL_LF_TEXT_RAW		"lf\nlf\nlf\nlf\nlf\n"
#define MORE_CRLF_TEXT_RAW	"crlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf\r\n"
#define MORE_LF_TEXT_RAW	"lf\nlf\ncrlf\r\nlf\nlf\n"

#define ALL_LF_TEXT_AS_CRLF	"lf\r\nlf\r\nlf\r\nlf\r\nlf\r\n"

static git_repository *g_repo;

void test_checkout_crlf__initialize(void)
{
	git_tree *tree;

	g_repo = cl_git_sandbox_init("crlf");

	cl_git_pass(git_repository_head_tree(&tree, g_repo));
	git_tree_free(tree);
}

void test_checkout_crlf__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

#ifdef GIT_WIN32
static void set_config_entry_to(const char *entry_name, bool value)
{
	git_config *cfg;

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_set_bool(cfg, entry_name, value));

	git_config_free(cfg);
}

static void set_core_autocrlf_to(bool value)
{
	set_config_entry_to("core.autocrlf", value);
}
#endif

void test_checkout_crlf__detect_crlf_autocrlf_false(void)
{
#ifdef GIT_WIN32
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

	set_core_autocrlf_to(false);

	git_checkout_head(g_repo, &opts);

	test_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
#endif
}

void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
#ifdef GIT_WIN32
	git_index *index;
	const git_index_entry *entry;
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

	set_core_autocrlf_to(false);

	git_checkout_head(g_repo, &opts);

	git_repository_index(&index, g_repo);

	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
	cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));

	git_index_free(index);
#endif
}

void test_checkout_crlf__detect_crlf_autocrlf_true(void)
{
#ifdef GIT_WIN32
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

	set_core_autocrlf_to(true);

	git_checkout_head(g_repo, &opts);

	test_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
#endif
}

void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
#ifdef GIT_WIN32
	git_index *index;
	const git_index_entry *entry;
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

	set_core_autocrlf_to(true);

	git_checkout_head(g_repo, &opts);

	git_repository_index(&index, g_repo);

	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
	cl_assert(entry->file_size == strlen(ALL_LF_TEXT_AS_CRLF));

	git_index_free(index);
#endif
}