Commit 16eaa150 by Vicent Martí

Merge pull request #606 from benstraub/t04_commit_to_clar

Ported t04_commit.c to Clar.
parents 4cba39ac e0799b6c
...@@ -128,11 +128,14 @@ INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} ) ...@@ -128,11 +128,14 @@ INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
# Tests # Tests
IF (BUILD_CLAR) IF (BUILD_CLAR)
FIND_PACKAGE(PythonInterp REQUIRED) FIND_PACKAGE(PythonInterp REQUIRED)
SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/") SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar") SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
INCLUDE_DIRECTORIES(${CLAR_PATH}) INCLUDE_DIRECTORIES(${CLAR_PATH})
FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c) FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
......
#include "clar_libgit2.h"
static int try_build_signature(const char *name, const char *email, git_time_t time, int offset)
{
git_signature *sign;
int error = GIT_SUCCESS;
if ((error = git_signature_new(&sign, name, email, time, offset)) < GIT_SUCCESS)
return error;
git_signature_free((git_signature *)sign);
return error;
}
void test_commit_signature__create_trim(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);
}
void test_commit_signature__create_empties(void)
{
// can not create a signature with empty name or email
cl_git_pass(try_build_signature("nulltoken", "emeric.fermas@gmail.com", 1234567890, 60));
cl_git_fail(try_build_signature("", "emeric.fermas@gmail.com", 1234567890, 60));
cl_git_fail(try_build_signature(" ", "emeric.fermas@gmail.com", 1234567890, 60));
cl_git_fail(try_build_signature("nulltoken", "", 1234567890, 60));
cl_git_fail(try_build_signature("nulltoken", " ", 1234567890, 60));
}
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);
}
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);
}
void test_commit_signature__create_zero_char(void)
{
// creating a zero character signature
git_signature *sign;
cl_git_fail(git_signature_new(&sign, "", "x@y.z", 1234567890, 60));
cl_assert(sign == NULL);
}
#include "clar_libgit2.h"
static const char *committer_name = "Vicent Marti";
static const char *committer_email = "vicent@github.com";
static const char *commit_message = "This commit has been created in memory\n\
This is a commit created in memory and it will be written back to disk\n";
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static const char *root_commit_message = "This is a root commit\n\
This is a root commit and should be the only one in this branch\n";
// Fixture setup
static git_repository *g_repo;
void test_commit_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_commit_write__cleanup(void)
{
cl_git_sandbox_cleanup();
}
// write a new commit object from memory to disk
void test_commit_write__from_memory(void)
{
git_commit *commit;
git_oid tree_id, parent_id, commit_id;
git_signature *author, *committer;
const git_signature *author1, *committer1;
git_commit *parent;
git_tree *tree;
const char *commit_id_str = "8496071c1b46c854b31185ea97743be6a8774479";
git_oid_fromstr(&tree_id, tree_oid);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
git_oid_fromstr(&parent_id, commit_id_str);
cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id));
/* create signatures */
cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
cl_git_pass(git_commit_create_v(
&commit_id, /* out id */
g_repo,
NULL, /* do not update the HEAD */
author,
committer,
NULL,
commit_message,
tree,
1, parent));
git_object_free((git_object *)parent);
git_object_free((git_object *)tree);
git_signature_free(committer);
git_signature_free(author);
cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
/* 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(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(committer1->when.time == 123456789);
cl_assert(committer1->when.offset == 60);
cl_assert(strcmp(git_commit_message(commit), commit_message) == 0);
#ifndef GIT_WIN32
cl_assert((loose_object_mode(REPOSITORY_FOLDER, (git_object *)commit) & 0777) == GIT_OBJECT_FILE_MODE);
#endif
}
// create a root commit
void test_commit_write__root(void)
{
git_commit *commit;
git_oid tree_id, commit_id;
const git_oid *branch_oid;
git_signature *author, *committer;
const char *branch_name = "refs/heads/root-commit-branch";
git_reference *head, *branch;
char *head_old;
git_tree *tree;
git_oid_fromstr(&tree_id, tree_oid);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
/* create signatures */
cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
/* 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));
cl_assert(head_old != NULL);
cl_git_pass(git_reference_set_target(head, branch_name));
cl_git_pass(git_commit_create_v(
&commit_id, /* out id */
g_repo,
"HEAD",
author,
committer,
NULL,
root_commit_message,
tree,
0));
git_object_free((git_object *)tree);
git_signature_free(committer);
git_signature_free(author);
/*
* The fact that creating a commit works has already been
* tested. Here we just make sure it's our commit and that it was
* written as a root commit.
*/
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);
cl_git_pass(git_oid_cmp(branch_oid, &commit_id));
cl_assert(!strcmp(git_commit_message(commit), root_commit_message));
}
[core]
repositoryformatversion = 0
filemode = true
bare = true
logallrefupdates = true
[remote "test"]
url = git://github.com/libgit2/libgit2
fetch = +refs/heads/*:refs/remotes/test/*
# pack-refs with: peeled
41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9 refs/heads/packed
5b5b025afb0b4c913b4c338a42934a3863bf3644 refs/heads/packed-test
a4a7dce85cf63874e984719f4fdd239f5145052f
a65fedf39aefe402d3bb6e24df4d4f5fe4547750
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
763d71aadf09a7951596c9746c024e7eece7c7af
e90810b8df3e80c413d903f631643c716887138d
7b4384978d2493e851f9cca7858815fac9b10980
1385f264afb75a56a5bec74243be9b367ba4ca08
b25fa35b38051e4ae45d4222e795f9df2e43f1d1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment