diff options
Diffstat (limited to 'tests/checkout/crlf.c')
| -rw-r--r-- | tests/checkout/crlf.c | 265 |
1 files changed, 173 insertions, 92 deletions
diff --git a/tests/checkout/crlf.c b/tests/checkout/crlf.c index 61459b3a4..2cf3af364 100644 --- a/tests/checkout/crlf.c +++ b/tests/checkout/crlf.c @@ -1,6 +1,7 @@ #include "clar_libgit2.h" #include "checkout_helpers.h" #include "../filter/crlf.h" +#include "fileops.h" #include "git2/checkout.h" #include "repository.h" @@ -9,94 +10,208 @@ static git_repository *g_repo; +static const char *systype; +static git_buf expected_fixture = GIT_BUF_INIT; + void test_checkout_crlf__initialize(void) { g_repo = cl_git_sandbox_init("crlf"); + + if (GIT_EOL_NATIVE == GIT_EOL_CRLF) + systype = "windows"; + else + systype = "posix"; } void test_checkout_crlf__cleanup(void) { cl_git_sandbox_cleanup(); + + if (expected_fixture.size) { + cl_fixture_cleanup(expected_fixture.ptr); + git_buf_free(&expected_fixture); + } } -void test_checkout_crlf__detect_crlf_autocrlf_false(void) +struct compare_data { - git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_FORCE; + const char *dirname; + const char *autocrlf; + const char *attrs; +}; - cl_repo_set_bool(g_repo, "core.autocrlf", false); +static int compare_file(void *payload, git_buf *actual_path) +{ + git_buf expected_path = GIT_BUF_INIT; + git_buf actual_contents = GIT_BUF_INIT; + git_buf expected_contents = GIT_BUF_INIT; + struct compare_data *cd = payload; + bool failed = true; + + if (strcmp(git_path_basename(actual_path->ptr), ".git") == 0 || + strcmp(git_path_basename(actual_path->ptr), ".gitattributes") == 0) { + failed = false; + goto done; + } - git_checkout_head(g_repo, &opts); + cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname, + git_path_basename(actual_path->ptr))); - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); -} + if (!git_path_isfile(expected_path.ptr) || + !git_path_isfile(actual_path->ptr)) + goto done; -void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void) -{ - git_index *index; - const git_index_entry *entry; - git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_FORCE; + if (git_futils_readbuffer(&actual_contents, actual_path->ptr) < 0 || + git_futils_readbuffer(&expected_contents, expected_path.ptr) < 0) + goto done; - cl_repo_set_bool(g_repo, "core.autocrlf", false); + if (actual_contents.size != expected_contents.size) + goto done; - git_repository_index(&index, g_repo); - tick_index(index); + if (memcmp(actual_contents.ptr, expected_contents.ptr, expected_contents.size) != 0) + goto done; - git_checkout_head(g_repo, &opts); + failed = false; - cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL); - cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW)); +done: + if (failed) { + git_buf details = GIT_BUF_INIT; + git_buf_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}", + git_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs); + clar__fail(__FILE__, __LINE__, + "checked out contents did not match expected", details.ptr, 0); + git_buf_free(&details); + } - cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL); - cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW)); + git_buf_free(&expected_contents); + git_buf_free(&actual_contents); + git_buf_free(&expected_path); - git_index_free(index); + return 0; } -void test_checkout_crlf__detect_crlf_autocrlf_true(void) +static void test_checkout(const char *autocrlf, const char *attrs) { + git_buf attrbuf = GIT_BUF_INIT; + git_buf expected_dirname = GIT_BUF_INIT; + git_buf sandboxname = GIT_BUF_INIT; + git_buf reponame = GIT_BUF_INIT; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_FORCE; + struct compare_data compare_data = { NULL, autocrlf, attrs }; + const char *c; - cl_repo_set_bool(g_repo, "core.autocrlf", true); + git_buf_puts(&reponame, "crlf"); + + git_buf_puts(&sandboxname, "autocrlf_"); + git_buf_puts(&sandboxname, autocrlf); + if (*attrs) { + git_buf_puts(&sandboxname, ","); + + for (c = attrs; *c; c++) { + if (*c == ' ') + git_buf_putc(&sandboxname, ','); + else if (*c == '=') + git_buf_putc(&sandboxname, '_'); + else + git_buf_putc(&sandboxname, *c); + } + + git_buf_printf(&attrbuf, "* %s\n", attrs); + cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr); + } + + cl_repo_set_string(g_repo, "core.autocrlf", autocrlf); + + git_buf_joinpath(&expected_dirname, systype, sandboxname.ptr); + git_buf_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr); + cl_fixture_sandbox(expected_fixture.ptr); + + opts.checkout_strategy = GIT_CHECKOUT_FORCE; git_checkout_head(g_repo, &opts); - if (GIT_EOL_NATIVE == GIT_EOL_LF) - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); - else - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); + compare_data.dirname = sandboxname.ptr; + cl_git_pass(git_path_direach(&reponame, 0, compare_file, &compare_data)); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); + cl_fixture_cleanup(expected_fixture.ptr); + git_buf_free(&expected_fixture); + + git_buf_free(&attrbuf); + git_buf_free(&expected_fixture); + git_buf_free(&expected_dirname); + git_buf_free(&sandboxname); + git_buf_free(&reponame); } -void test_checkout_crlf__more_lf_autocrlf_true(void) +static void empty_workdir(const char *name) +{ + git_vector contents = GIT_VECTOR_INIT; + size_t i; + const char *fn; + + git_path_dirload(&contents, name, 0, 0); + git_vector_foreach(&contents, i, fn) { + if (strncasecmp(git_path_basename(fn), ".git", 4) == 0) + continue; + p_unlink(fn); + } + git_vector_free_deep(&contents); +} + +void test_checkout_crlf__matches_core_git(void) +{ + const char *autocrlf[] = { "true", "false", "input", NULL }; + const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf", + "text", "text eol=crlf", "text eol=lf", + "text=auto", "text=auto eol=crlf", "text=auto eol=lf", + NULL }; + const char **a, **b; + + for (a = autocrlf; *a; a++) { + for (b = attrs; *b; b++) { + empty_workdir("crlf"); + test_checkout(*a, *b); + } + } +} + +void test_checkout_crlf__detect_crlf_autocrlf_false(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_repo_set_bool(g_repo, "core.autocrlf", true); + cl_repo_set_bool(g_repo, "core.autocrlf", false); git_checkout_head(g_repo, &opts); - check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW); + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); } -void test_checkout_crlf__more_crlf_autocrlf_true(void) +void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void) { + git_index *index; + const git_index_entry *entry; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; - cl_repo_set_bool(g_repo, "core.autocrlf", true); + cl_repo_set_bool(g_repo, "core.autocrlf", false); + + git_repository_index(&index, g_repo); + tick_index(index); git_checkout_head(g_repo, &opts); - check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW); + cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL); + cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW)); + + cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL); + cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW)); + + git_index_free(index); } -void test_checkout_crlf__all_crlf_autocrlf_true(void) +void test_checkout_crlf__detect_crlf_autocrlf_true(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; @@ -105,6 +220,7 @@ void test_checkout_crlf__all_crlf_autocrlf_true(void) git_checkout_head(g_repo, &opts); + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); } @@ -115,22 +231,14 @@ void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void) cl_repo_set_bool(g_repo, "core.autocrlf", true); - git_repository_set_head(g_repo, "refs/heads/utf8"); + git_repository_set_head(g_repo, "refs/heads/master"); git_checkout_head(g_repo, &opts); - if (GIT_EOL_NATIVE == GIT_EOL_LF) - { - check_file_contents("./crlf/few-utf8-chars-lf.txt", FEW_UTF8_LF_RAW); - check_file_contents("./crlf/many-utf8-chars-lf.txt", MANY_UTF8_LF_RAW); - } - else - { - check_file_contents("./crlf/few-utf8-chars-lf.txt", FEW_UTF8_CRLF_RAW); - check_file_contents("./crlf/many-utf8-chars-lf.txt", MANY_UTF8_CRLF_RAW); - } + check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_CRLF_RAW); + check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_CRLF_RAW); - check_file_contents("./crlf/few-utf8-chars-crlf.txt", FEW_UTF8_CRLF_RAW); - check_file_contents("./crlf/many-utf8-chars-crlf.txt", MANY_UTF8_CRLF_RAW); + check_file_contents("./crlf/few-utf8-chars-crlf", FEW_UTF8_CRLF_RAW); + check_file_contents("./crlf/many-utf8-chars-crlf", MANY_UTF8_CRLF_RAW); } void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void) @@ -149,10 +257,7 @@ void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void) cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL); - if (GIT_EOL_NATIVE == GIT_EOL_LF) - cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW), entry->file_size); - else - cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size); + cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size); cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL); cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size); @@ -221,25 +326,14 @@ void test_checkout_crlf__with_ident(void) git_checkout_head(g_repo, &opts); - if (GIT_EOL_NATIVE == GIT_EOL_LF) { - cl_assert_equal_file( - ALL_LF_TEXT_RAW - "\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\n", - 0, "crlf/lf.ident"); - cl_assert_equal_file( - ALL_CRLF_TEXT_AS_LF - "\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\n\n", - 0, "crlf/crlf.ident"); - } else { - cl_assert_equal_file( - ALL_LF_TEXT_AS_CRLF - "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n", - 0, "crlf/lf.ident"); - cl_assert_equal_file( - ALL_CRLF_TEXT_RAW - "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n", - 0, "crlf/crlf.ident"); - } + cl_assert_equal_file( + ALL_LF_TEXT_AS_CRLF + "\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n", + 0, "crlf/lf.ident"); + cl_assert_equal_file( + ALL_CRLF_TEXT_RAW + "\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n", + 0, "crlf/crlf.ident"); cl_assert_equal_file( "$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n" @@ -274,13 +368,8 @@ void test_checkout_crlf__autocrlf_true_no_attrs(void) git_checkout_head(g_repo, &opts); - if (GIT_EOL_NATIVE == GIT_EOL_CRLF) { - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF); - } else { - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); - } + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF); } void test_checkout_crlf__autocrlf_input_no_attrs(void) @@ -327,13 +416,8 @@ void test_checkout_crlf__autocrlf_true_text_auto_attr(void) git_checkout_head(g_repo, &opts); - if (GIT_EOL_NATIVE == GIT_EOL_CRLF) { - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF); - } else { - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); - } + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF); } void test_checkout_crlf__autocrlf_input_text_auto_attr(void) @@ -363,10 +447,7 @@ void test_checkout_crlf__can_write_empty_file(void) check_file_contents("./crlf/test1.txt", ""); - if (GIT_EOL_NATIVE == GIT_EOL_LF) - check_file_contents("./crlf/test2.txt", "test2.txt's content\n"); - else - check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n"); + check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n"); check_file_contents("./crlf/test3.txt", ""); } |
