diff options
Diffstat (limited to 'tests-clar/commit')
-rw-r--r-- | tests-clar/commit/commit.c | 4 | ||||
-rw-r--r-- | tests-clar/commit/parent.c | 60 | ||||
-rw-r--r-- | tests-clar/commit/parse.c | 117 | ||||
-rw-r--r-- | tests-clar/commit/signature.c | 48 | ||||
-rw-r--r-- | tests-clar/commit/write.c | 37 |
5 files changed, 197 insertions, 69 deletions
diff --git a/tests-clar/commit/commit.c b/tests-clar/commit/commit.c index 1205e5285..8f071ff94 100644 --- a/tests-clar/commit/commit.c +++ b/tests-clar/commit/commit.c @@ -11,6 +11,8 @@ void test_commit_commit__initialize(void) void test_commit_commit__cleanup(void) { git_repository_free(_repo); + _repo = NULL; + cl_fixture_cleanup("testrepo.git"); } @@ -35,7 +37,7 @@ void test_commit_commit__create_unexisting_update_ref(void) NULL, "some msg", tree, 1, (const git_commit **) &commit)); cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar")); - cl_assert(!git_oid_cmp(&oid, git_reference_oid(ref))); + cl_assert(!git_oid_cmp(&oid, git_reference_target(ref))); git_tree_free(tree); git_commit_free(commit); diff --git a/tests-clar/commit/parent.c b/tests-clar/commit/parent.c new file mode 100644 index 000000000..18ce0bba6 --- /dev/null +++ b/tests-clar/commit/parent.c @@ -0,0 +1,60 @@ +#include "clar_libgit2.h" + +static git_repository *_repo; +static git_commit *commit; + +void test_commit_parent__initialize(void) +{ + git_oid oid; + + cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); + + git_oid_fromstr(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); +} + +void test_commit_parent__cleanup(void) +{ + git_commit_free(commit); + commit = NULL; + + git_repository_free(_repo); + _repo = NULL; +} + +static void assert_nth_gen_parent(unsigned int gen, const char *expected_oid) +{ + git_commit *parent = NULL; + int error; + + error = git_commit_nth_gen_ancestor(&parent, commit, gen); + + if (expected_oid != NULL) { + cl_assert_equal_i(0, error); + cl_assert_equal_i(0, git_oid_streq(git_commit_id(parent), expected_oid)); + } else + cl_assert_equal_i(GIT_ENOTFOUND, error); + + git_commit_free(parent); +} + +/* + * $ git show be35~0 + * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644 + * + * $ git show be35~1 + * commit 9fd738e8f7967c078dceed8190330fc8648ee56a + * + * $ git show be35~3 + * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644 + * + * $ git show be35~42 + * fatal: ambiguous argument 'be35~42': unknown revision or path not in the working tree. + */ +void test_commit_parent__can_retrieve_nth_generation_parent(void) +{ + assert_nth_gen_parent(0, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + assert_nth_gen_parent(1, "9fd738e8f7967c078dceed8190330fc8648ee56a"); + assert_nth_gen_parent(3, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + assert_nth_gen_parent(42, NULL); +} diff --git a/tests-clar/commit/parse.c b/tests-clar/commit/parse.c index bbb502cb5..b99d27991 100644 --- a/tests-clar/commit/parse.c +++ b/tests-clar/commit/parse.c @@ -108,19 +108,20 @@ passing_signature_test_case passing_signature_cases[] = { // Parse an obviously invalid signature {"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60}, // Parse an obviously invalid signature - {"committer foo<@bar>123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60}, + {"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60}, // Parse an obviously invalid signature {"committer <>\n", "committer ", "", "", 0, 0}, - {"committer Vicent Marti <tanoku@gmail.com> 123456 -1500 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"committer Vicent Marti <tanoku@gmail.com> 123456 +0163 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti <tanoku@gmail.com> notime \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti <tanoku@gmail.com> 123456 notimezone \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author Vicent Marti <tanoku@gmail.com> notime +0100\n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, + {"committer Vicent Marti <tanoku@gmail.com> 123456 -1500 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0}, + {"committer Vicent Marti <tanoku@gmail.com> 123456 +0163 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0}, {"author Vicent Marti <tanoku@gmail.com>\n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, - {"author A U Thor <author@example.com>, C O. Miter <comiter@example.com> 1234567890 -0700\n", "author ", "A U Thor", "author@example.com", 1234567890, -420}, - {"author A U Thor <author@example.com> and others 1234567890 -0700\n", "author ", "A U Thor", "author@example.com", 1234567890, -420}, - {"author A U Thor <author@example.com> and others 1234567890\n", "author ", "A U Thor", "author@example.com", 1234567890, 0}, - {"author A U Thor> <author@example.com> and others 1234567890\n", "author ", "A U Thor>", "author@example.com", 1234567890, 0}, + /* a variety of dates */ + {"author Vicent Marti <tanoku@gmail.com> 0 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0}, + {"author Vicent Marti <tanoku@gmail.com> 1234567890 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 1234567890, 0}, + {"author Vicent Marti <tanoku@gmail.com> 2147483647 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0x7fffffff, 0}, + {"author Vicent Marti <tanoku@gmail.com> 4294967295 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0xffffffff, 0}, + {"author Vicent Marti <tanoku@gmail.com> 4294967296 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 4294967296, 0}, + {"author Vicent Marti <tanoku@gmail.com> 8589934592 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 8589934592, 0}, + {NULL,NULL,NULL,NULL,0,0} }; @@ -137,7 +138,7 @@ failing_signature_test_case failing_signature_cases[] = { {"author Vicent Marti <broken@email 12345 \n", "author "}, {"committer Vicent Marti ><\n", "committer "}, {"author ", "author "}, - {NULL,NULL,} + {NULL, NULL,} }; void test_commit_parse__signature(void) @@ -149,12 +150,13 @@ void test_commit_parse__signature(void) { const char *str = passcase->string; size_t len = strlen(passcase->string); - struct git_signature person = {NULL, NULL, {0, 0}}; + struct git_signature person = {0}; + cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n')); - cl_assert(strcmp(passcase->name, person.name) == 0); - cl_assert(strcmp(passcase->email, person.email) == 0); - cl_assert(passcase->time == person.when.time); - cl_assert(passcase->offset == person.when.offset); + cl_assert_equal_s(passcase->name, person.name); + cl_assert_equal_s(passcase->email, person.email); + cl_assert_equal_i((int)passcase->time, (int)person.when.time); + cl_assert_equal_i(passcase->offset, person.when.offset); git__free(person.name); git__free(person.email); } @@ -162,7 +164,7 @@ void test_commit_parse__signature(void) { const char *str = failcase->string; size_t len = strlen(failcase->string); - git_signature person = {NULL, NULL, {0, 0}}; + git_signature person = {0}; cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n')); git__free(person.name); git__free(person.email); } @@ -236,6 +238,30 @@ author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\ committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\ \n\ a simple commit which works\n", +/* simple commit with GPG signature */ +"tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\ +parent 34734e478d6cf50c27c9d69026d93974d052c454\n\ +author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\ +committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\ +gpgsig -----BEGIN PGP SIGNATURE-----\n\ + Version: GnuPG v1.4.12 (Darwin)\n\ + \n\ + iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\ + o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\ + JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\ + AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\ + SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\ + who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\ + 6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\ + cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\ + c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\ + ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\ + 7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\ + cpxtDQQMGYFpXK/71stq\n\ + =ozeK\n\ + -----END PGP SIGNATURE-----\n\ +\n\ +a simple commit which works\n", }; void test_commit_parse__entire_commit(void) @@ -251,10 +277,8 @@ void test_commit_parse__entire_commit(void) commit->object.repo = g_repo; cl_git_fail(git_commit__parse_buffer( - commit, - failing_commit_cases[i], - strlen(failing_commit_cases[i])) - ); + commit, failing_commit_cases[i], strlen(failing_commit_cases[i])) + ); git_commit__free(commit); } @@ -272,17 +296,11 @@ void test_commit_parse__entire_commit(void) strlen(passing_commit_cases[i])) ); - git_commit__free(commit); - - commit = (git_commit*)git__malloc(sizeof(git_commit)); - memset(commit, 0x0, sizeof(git_commit)); - commit->object.repo = g_repo; - - cl_git_pass(git_commit__parse_buffer( - commit, - passing_commit_cases[i], - strlen(passing_commit_cases[i])) - ); + if (!i) + cl_assert_equal_s("", git_commit_message(commit)); + else + cl_assert(git__prefixcmp( + git_commit_message(commit), "a simple commit which works") == 0); git_commit__free(commit); } @@ -323,10 +341,10 @@ void test_commit_parse__details0(void) { commit_time = git_commit_time(commit); parents = git_commit_parentcount(commit); - cl_assert(strcmp(author->name, "Scott Chacon") == 0); - cl_assert(strcmp(author->email, "schacon@gmail.com") == 0); - cl_assert(strcmp(committer->name, "Scott Chacon") == 0); - cl_assert(strcmp(committer->email, "schacon@gmail.com") == 0); + cl_assert_equal_s("Scott Chacon", author->name); + cl_assert_equal_s("schacon@gmail.com", author->email); + cl_assert_equal_s("Scott Chacon", committer->name); + cl_assert_equal_s("schacon@gmail.com", committer->email); cl_assert(message != NULL); cl_assert(strchr(message, '\n') != NULL); cl_assert(commit_time > 0); @@ -348,3 +366,30 @@ void test_commit_parse__details0(void) { } } +void test_commit_parse__leading_lf(void) +{ + git_commit *commit; + const char *buffer = +"tree 1810dff58d8a660512d4832e740f692884338ccd\n\ +parent e90810b8df3e80c413d903f631643c716887138d\n\ +author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\ +committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\ +\n\ +\n\ +\n\ +This commit has a few LF at the start of the commit message"; + const char *message = +"\n\ +\n\ +This commit has a few LF at the start of the commit message"; + + commit = (git_commit*)git__malloc(sizeof(git_commit)); + memset(commit, 0x0, sizeof(git_commit)); + commit->object.repo = g_repo; + + cl_git_pass(git_commit__parse_buffer(commit, buffer, strlen(buffer))); + + cl_assert_equal_s(message, git_commit_message(commit)); + + git_commit__free(commit); +} diff --git a/tests-clar/commit/signature.c b/tests-clar/commit/signature.c index 290b11fa3..9364efb10 100644 --- a/tests-clar/commit/signature.c +++ b/tests-clar/commit/signature.c @@ -13,17 +13,39 @@ static int try_build_signature(const char *name, const char *email, git_time_t t return error; } +static void assert_name_and_email( + const char *expected_name, + const char *expected_email, + const char *name, + const char *email) +{ + git_signature *sign; + + cl_git_pass(git_signature_new(&sign, name, email, 1234567890, 60)); + cl_assert_equal_s(expected_name, sign->name); + cl_assert_equal_s(expected_email, sign->email); + + git_signature_free(sign); +} -void test_commit_signature__create_trim(void) +void test_commit_signature__leading_and_trailing_spaces_are_trimmed(void) { - // creating a signature trims leading and trailing spaces - git_signature *sign; - cl_git_pass(git_signature_new(&sign, " nulltoken ", " emeric.fermas@gmail.com ", 1234567890, 60)); - cl_assert(strcmp(sign->name, "nulltoken") == 0); - cl_assert(strcmp(sign->email, "emeric.fermas@gmail.com") == 0); - git_signature_free((git_signature *)sign); + assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com "); } +void test_commit_signature__angle_brackets_in_names_are_not_supported(void) +{ + cl_git_fail(try_build_signature("<Phil Haack", "phil@haack", 1234567890, 60)); + cl_git_fail(try_build_signature("Phil>Haack", "phil@haack", 1234567890, 60)); + cl_git_fail(try_build_signature("<Phil Haack>", "phil@haack", 1234567890, 60)); +} + +void test_commit_signature__angle_brackets_in_email_are_not_supported(void) +{ + cl_git_fail(try_build_signature("Phil Haack", ">phil@haack", 1234567890, 60)); + cl_git_fail(try_build_signature("Phil Haack", "phil@>haack", 1234567890, 60)); + cl_git_fail(try_build_signature("Phil Haack", "<phil@haack>", 1234567890, 60)); +} void test_commit_signature__create_empties(void) { @@ -39,21 +61,13 @@ void test_commit_signature__create_empties(void) void test_commit_signature__create_one_char(void) { // creating a one character signature - git_signature *sign; - cl_git_pass(git_signature_new(&sign, "x", "foo@bar.baz", 1234567890, 60)); - cl_assert(strcmp(sign->name, "x") == 0); - cl_assert(strcmp(sign->email, "foo@bar.baz") == 0); - git_signature_free((git_signature *)sign); + assert_name_and_email("x", "foo@bar.baz", "x", "foo@bar.baz"); } void test_commit_signature__create_two_char(void) { // creating a two character signature - git_signature *sign; - cl_git_pass(git_signature_new(&sign, "xx", "x@y.z", 1234567890, 60)); - cl_assert(strcmp(sign->name, "xx") == 0); - cl_assert(strcmp(sign->email, "x@y.z") == 0); - git_signature_free((git_signature *)sign); + assert_name_and_email("xx", "foo@bar.baz", "xx", "foo@bar.baz"); } void test_commit_signature__create_zero_char(void) diff --git a/tests-clar/commit/write.c b/tests-clar/commit/write.c index 9c4d077a6..e9946af89 100644 --- a/tests-clar/commit/write.c +++ b/tests-clar/commit/write.c @@ -17,16 +17,22 @@ void test_commit_write__initialize(void) { g_repo = cl_git_sandbox_init("testrepo"); } + void test_commit_write__cleanup(void) { - git_reference_free(head); - git_reference_free(branch); + git_reference_free(head); + head = NULL; + + git_reference_free(branch); + branch = NULL; - git_commit_free(commit); + git_commit_free(commit); + commit = NULL; - git__free(head_old); + git__free(head_old); + head_old = NULL; - cl_git_sandbox_cleanup(); + cl_git_sandbox_cleanup(); } @@ -72,19 +78,19 @@ void test_commit_write__from_memory(void) /* Check attributes were set correctly */ author1 = git_commit_author(commit); cl_assert(author1 != NULL); - cl_assert(strcmp(author1->name, committer_name) == 0); - cl_assert(strcmp(author1->email, committer_email) == 0); + cl_assert_equal_s(committer_name, author1->name); + cl_assert_equal_s(committer_email, author1->email); cl_assert(author1->when.time == 987654321); cl_assert(author1->when.offset == 90); committer1 = git_commit_committer(commit); cl_assert(committer1 != NULL); - cl_assert(strcmp(committer1->name, committer_name) == 0); - cl_assert(strcmp(committer1->email, committer_email) == 0); + cl_assert_equal_s(committer_name, committer1->name); + cl_assert_equal_s(committer_email, committer1->email); cl_assert(committer1->when.time == 123456789); cl_assert(committer1->when.offset == 60); - cl_assert(strcmp(git_commit_message(commit), commit_message) == 0); + cl_assert_equal_s(commit_message, git_commit_message(commit)); } // create a root commit @@ -106,10 +112,11 @@ void test_commit_write__root(void) /* First we need to update HEAD so it points to our non-existant branch */ cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC); - head_old = git__strdup(git_reference_target(head)); + head_old = git__strdup(git_reference_symbolic_target(head)); cl_assert(head_old != NULL); - - cl_git_pass(git_reference_set_target(head, branch_name)); + git_reference_free(head); + + cl_git_pass(git_reference_symbolic_create(&head, g_repo, "HEAD", branch_name, 1)); cl_git_pass(git_commit_create_v( &commit_id, /* out id */ @@ -134,7 +141,7 @@ void test_commit_write__root(void) cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); cl_assert(git_commit_parentcount(commit) == 0); cl_git_pass(git_reference_lookup(&branch, g_repo, branch_name)); - branch_oid = git_reference_oid(branch); + branch_oid = git_reference_target(branch); cl_git_pass(git_oid_cmp(branch_oid, &commit_id)); - cl_assert(!strcmp(git_commit_message(commit), root_commit_message)); + cl_assert_equal_s(root_commit_message, git_commit_message(commit)); } |