Blame tests/commit/commit.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "commit.h"
Packit Service 20376f
#include "git2/commit.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *_repo;
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_fixture_sandbox("testrepo.git");
Packit Service 20376f
	cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository_free(_repo);
Packit Service 20376f
	_repo = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_fixture_cleanup("testrepo.git");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__create_unexisting_update_ref(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_tree *tree;
Packit Service 20376f
	git_commit *commit;
Packit Service 20376f
	git_signature *s;
Packit Service 20376f
	git_reference *ref;
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
Packit Service 20376f
	cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
Packit Service 20376f
	cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
Packit Service 20376f
	cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
Packit Service 20376f
				      NULL, "some msg", tree, 1, (const git_commit **) &commit));
Packit Service 20376f
Packit Service 20376f
	/* fail because the parent isn't the tip of the branch anymore */
Packit Service 20376f
	cl_git_fail(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
Packit Service 20376f
				      NULL, "some msg", tree, 1, (const git_commit **) &commit));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
Packit Service 20376f
	cl_assert_equal_oid(&oid, git_reference_target(ref));
Packit Service 20376f
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
	git_signature_free(s);
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__create_initial_commit(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_tree *tree;
Packit Service 20376f
	git_commit *commit;
Packit Service 20376f
	git_signature *s;
Packit Service 20376f
	git_reference *ref;
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
Packit Service 20376f
	cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
Packit Service 20376f
	cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
Packit Service 20376f
	cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
Packit Service 20376f
				      NULL, "initial commit", tree, 0, NULL));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_oid(&oid, git_reference_target(ref));
Packit Service 20376f
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
	git_signature_free(s);
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__create_initial_commit_parent_not_current(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_oid original_oid;
Packit Service 20376f
	git_tree *tree;
Packit Service 20376f
	git_commit *commit;
Packit Service 20376f
	git_signature *s;
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
Packit Service 20376f
	cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
Packit Service 20376f
	cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_name_to_id(&original_oid, _repo, "HEAD"));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_commit_create(&oid, _repo, "HEAD", s, s,
Packit Service 20376f
				      NULL, "initial commit", tree, 0, NULL));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_name_to_id(&oid, _repo, "HEAD"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_oid(&oid, &original_oid);
Packit Service 20376f
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
	git_signature_free(s);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void assert_commit_summary(const char *expected, const char *given)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *dummy;
Packit Service 20376f
Packit Service 20376f
	cl_assert(dummy = git__calloc(1, sizeof(struct git_commit)));
Packit Service 20376f
Packit Service 20376f
	dummy->raw_message = git__strdup(given);
Packit Service 20376f
	cl_assert_equal_s(expected, git_commit_summary(dummy));
Packit Service 20376f
Packit Service 20376f
	git_commit__free(dummy);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void assert_commit_body(const char *expected, const char *given)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *dummy;
Packit Service 20376f
Packit Service 20376f
	cl_assert(dummy = git__calloc(1, sizeof(struct git_commit)));
Packit Service 20376f
Packit Service 20376f
	dummy->raw_message = git__strdup(given);
Packit Service 20376f
	cl_assert_equal_s(expected, git_commit_body(dummy));
Packit Service 20376f
Packit Service 20376f
	git_commit__free(dummy);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__summary(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_commit_summary("One-liner with no trailing newline", "One-liner with no trailing newline");
Packit Service 20376f
	assert_commit_summary("One-liner with trailing newline", "One-liner with trailing newline\n");
Packit Service 20376f
	assert_commit_summary("Trimmed leading&trailing newlines", "\n\nTrimmed leading&trailing newlines\n\n");
Packit Service 20376f
	assert_commit_summary("First paragraph only", "\nFirst paragraph only\n\n(There are more!)");
Packit Service 20376f
	assert_commit_summary("First paragraph with  unwrapped trailing\tlines", "\nFirst paragraph\nwith  unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
Packit Service 20376f
	assert_commit_summary("\tLeading tabs", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
Packit Service 20376f
	assert_commit_summary(" Leading Spaces", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */
Packit Service 20376f
	assert_commit_summary("Trailing tabs\tare removed", "Trailing tabs\tare removed\t\t");
Packit Service 20376f
	assert_commit_summary("Trailing spaces  are removed", "Trailing spaces  are removed  ");
Packit Service 20376f
	assert_commit_summary("Trailing tabs", "Trailing tabs\t\n\nare removed");
Packit Service 20376f
	assert_commit_summary("Trailing spaces", "Trailing spaces \n\nare removed");
Packit Service 20376f
	assert_commit_summary("Newlines are replaced by spaces", "Newlines\nare\nreplaced by spaces\n");
Packit Service 20376f
	assert_commit_summary("  Spaces after newlines are collapsed", "\n  Spaces after newlines\n  are\n  collapsed\n  "); /* newlines at the very beginning are ignored and not collapsed */
Packit Service 20376f
	assert_commit_summary(" Spaces before newlines are collapsed", "  \nSpaces before newlines  \nare  \ncollapsed  \n");
Packit Service 20376f
	assert_commit_summary(" Spaces around newlines are collapsed", "  \n  Spaces around newlines  \n  are  \n  collapsed  \n  ");
Packit Service 20376f
	assert_commit_summary(" Trailing newlines are" , "  \n  Trailing newlines  \n  are  \n\n  collapsed  \n  ");
Packit Service 20376f
	assert_commit_summary(" Trailing spaces are stripped", "  \n  Trailing spaces \n  are stripped \n\n  \n \t ");
Packit Service 20376f
	assert_commit_summary("", "");
Packit Service 20376f
	assert_commit_summary("", " ");
Packit Service 20376f
	assert_commit_summary("", "\n");
Packit Service 20376f
	assert_commit_summary("", "\n \n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_commit_commit__body(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_commit_body(NULL, "One-liner with no trailing newline");
Packit Service 20376f
	assert_commit_body(NULL, "One-liner with trailing newline\n");
Packit Service 20376f
	assert_commit_body(NULL, "\n\nTrimmed leading&trailing newlines\n\n");
Packit Service 20376f
	assert_commit_body("(There are more!)", "\nFirst paragraph only\n\n(There are more!)");
Packit Service 20376f
	assert_commit_body("(Yes, unwrapped!)", "\nFirst paragraph\nwith  unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
Packit Service 20376f
	assert_commit_body("are preserved", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
Packit Service 20376f
	assert_commit_body("are preserved", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */
Packit Service 20376f
	assert_commit_body(NULL, "Trailing tabs\tare removed\t\t");
Packit Service 20376f
	assert_commit_body(NULL, "Trailing spaces  are removed  ");
Packit Service 20376f
	assert_commit_body("are removed", "Trailing tabs\t\n\nare removed");
Packit Service 20376f
	assert_commit_body("are removed", "Trailing spaces \n\nare removed");
Packit Service 20376f
	assert_commit_body(NULL,"Newlines\nare\nreplaced by spaces\n");
Packit Service 20376f
	assert_commit_body(NULL , "\n  Spaces after newlines\n  are\n  collapsed\n  "); /* newlines at the very beginning are ignored and not collapsed */
Packit Service 20376f
	assert_commit_body(NULL , "  \nSpaces before newlines  \nare  \ncollapsed  \n");
Packit Service 20376f
	assert_commit_body(NULL , "  \n  Spaces around newlines  \n  are  \n  collapsed  \n  ");
Packit Service 20376f
	assert_commit_body("collapsed" , "  \n  Trailing newlines  \n  are  \n\n  collapsed  \n  ");
Packit Service 20376f
	assert_commit_body(NULL, "  \n  Trailing spaces \n  are stripped \n\n  \n \t ");
Packit Service 20376f
	assert_commit_body(NULL , "");
Packit Service 20376f
	assert_commit_body(NULL , " ");
Packit Service 20376f
	assert_commit_body(NULL , "\n");
Packit Service 20376f
	assert_commit_body(NULL , "\n \n");
Packit Service 20376f
}