diff options
| author | Vicent Martà <tanoku@gmail.com> | 2012-01-24 20:35:15 -0800 |
|---|---|---|
| committer | Vicent Martà <tanoku@gmail.com> | 2012-01-24 20:35:15 -0800 |
| commit | 3fd1520cd4d8b4d6b6493a7d3dc393ffd9abf1db (patch) | |
| tree | 51b29f5d8ffeb31ba751ab2a099e4f2a32d4be07 /tests-clar/object/tree | |
| parent | a9fe8ae0ee1ddcc289fad53f1a671f02a3e9a88f (diff) | |
| download | libgit2-3fd1520cd4d8b4d6b6493a7d3dc393ffd9abf1db.tar.gz | |
Rename the Clay test suite to Clar
Clay is the name of a programming language on the makings, and we want
to avoid confusions. Sorry for the huge diff!
Diffstat (limited to 'tests-clar/object/tree')
| -rw-r--r-- | tests-clar/object/tree/diff.c | 168 | ||||
| -rw-r--r-- | tests-clar/object/tree/frompath.c | 75 |
2 files changed, 243 insertions, 0 deletions
diff --git a/tests-clar/object/tree/diff.c b/tests-clar/object/tree/diff.c new file mode 100644 index 000000000..d481b6f2b --- /dev/null +++ b/tests-clar/object/tree/diff.c @@ -0,0 +1,168 @@ +#include "clar_libgit2.h" +#include "tree.h" +#include "repository.h" + +static git_repository *repo; +static git_index *theindex; +static git_tree *atree, *btree; +static git_oid aoid, boid; + +static void diff_cmp(const git_tree_diff_data *a, const git_tree_diff_data *b) +{ + cl_assert(a->old_attr - b->old_attr == 0); + + cl_assert(a->new_attr - b->new_attr == 0); + + cl_assert(git_oid_cmp(&a->old_oid, &b->old_oid) == 0); + cl_assert(git_oid_cmp(&a->new_oid, &b->new_oid) == 0); + + cl_assert(a->status - b->status == 0); + + cl_assert(strcmp(a->path, b->path) == 0); +} + +static int diff_cb(const git_tree_diff_data *diff, void *data) +{ + diff_cmp(diff, data); + return GIT_SUCCESS; +} + +static void test_diff(git_tree *a, git_tree *b, git_tree_diff_cb cb, void *data) +{ + cl_must_pass(git_tree_diff(a, b, cb, data)); + + cl_git_pass(git_index_read_tree(theindex, b)); + cl_git_pass(git_tree_diff_index_recursive(a, theindex, cb, data)); +} + +void test_object_tree_diff__initialize(void) +{ + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); + cl_git_pass(git_repository_index(&theindex, repo)); +} + +void test_object_tree_diff__cleanup(void) +{ + git_tree_free(atree); + git_tree_free(btree); + git_index_free(theindex); + git_repository_free(repo); +} + +void test_object_tree_diff__addition(void) +{ + char *astr = "181037049a54a1eb5fab404658a3a250b44335d7"; + char *bstr = "f60079018b664e4e79329a7ef9559c8d9e0378d1"; + git_tree_diff_data expect; + + memset(&expect, 0x0, sizeof(git_tree_diff_data)); + expect.old_attr = 0; + expect.new_attr = 0100644; + git_oid_fromstr(&expect.new_oid, "fa49b077972391ad58037050f2a75f74e3671e92"); + expect.status = GIT_STATUS_ADDED; + expect.path = "new.txt"; + + cl_must_pass(git_oid_fromstr(&aoid, astr)); + cl_must_pass(git_oid_fromstr(&boid, bstr)); + + cl_must_pass(git_tree_lookup(&atree, repo, &aoid)); + cl_must_pass(git_tree_lookup(&btree, repo, &boid)); + + test_diff(atree, btree, diff_cb, &expect); +} + +void test_object_tree_diff__deletion(void) +{ + char *astr = "f60079018b664e4e79329a7ef9559c8d9e0378d1"; + char *bstr = "181037049a54a1eb5fab404658a3a250b44335d7"; + git_tree_diff_data expect; + + memset(&expect, 0x0, sizeof(git_tree_diff_data)); + expect.old_attr = 0100644; + expect.new_attr = 0; + git_oid_fromstr(&expect.old_oid, "fa49b077972391ad58037050f2a75f74e3671e92"); + expect.status = GIT_STATUS_DELETED; + expect.path = "new.txt"; + cl_must_pass(git_oid_fromstr(&aoid, astr)); + cl_must_pass(git_oid_fromstr(&boid, bstr)); + + cl_must_pass(git_tree_lookup(&atree, repo, &aoid)); + cl_must_pass(git_tree_lookup(&btree, repo, &boid)); + + test_diff(atree, btree, diff_cb, &expect); +} + +void test_object_tree_diff__modification(void) +{ + char *astr = "1810dff58d8a660512d4832e740f692884338ccd"; + char *bstr = "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"; + git_tree_diff_data expect; + + expect.old_attr = 0100644; + expect.new_attr = 0100644; + git_oid_fromstr(&expect.old_oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); + git_oid_fromstr(&expect.new_oid, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); + expect.status = GIT_STATUS_MODIFIED; + expect.path = "branch_file.txt"; + + cl_must_pass(git_oid_fromstr(&aoid, astr)); + cl_must_pass(git_oid_fromstr(&boid, bstr)); + + cl_must_pass(git_tree_lookup(&atree, repo, &aoid)); + cl_must_pass(git_tree_lookup(&btree, repo, &boid)); + + test_diff(atree, btree, diff_cb, &expect); +} + +struct diff_more_data { + git_tree_diff_data expect[3]; + int expect_idx; +}; + +static int diff_more_cb(const git_tree_diff_data *diff, void *data) +{ + struct diff_more_data *more_data = data; + diff_cmp(diff, &more_data->expect[more_data->expect_idx]); + + more_data->expect_idx = (more_data->expect_idx + 1) % ARRAY_SIZE(more_data->expect); + + return GIT_SUCCESS; +} + +void test_object_tree_diff__more(void) +{ + char *astr = "814889a078c031f61ed08ab5fa863aea9314344d"; + char *bstr = "75057dd4114e74cca1d750d0aee1647c903cb60a"; + struct diff_more_data more_data; + git_tree_diff_data *expect = more_data.expect; + + memset(&more_data, 0x0, sizeof(struct diff_more_data)); + /* M README */ + expect[0].old_attr = 0100644; + expect[0].new_attr = 0100644; + git_oid_fromstr(&expect[0].old_oid, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + git_oid_fromstr(&expect[0].new_oid, "1385f264afb75a56a5bec74243be9b367ba4ca08"); + expect[0].status = GIT_STATUS_MODIFIED; + expect[0].path = "README"; + /* A branch_file.txt */ + expect[1].old_attr = 0; + expect[1].new_attr = 0100644; + git_oid_fromstr(&expect[1].new_oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); + expect[1].status = GIT_STATUS_ADDED; + expect[1].path = "branch_file.txt"; + /* M new.txt */ + expect[2].old_attr = 0100644; + expect[2].new_attr = 0100644; + git_oid_fromstr(&expect[2].old_oid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); + git_oid_fromstr(&expect[2].new_oid, "fa49b077972391ad58037050f2a75f74e3671e92"); + expect[2].status = GIT_STATUS_MODIFIED; + expect[2].path = "new.txt"; + + cl_must_pass(git_oid_fromstr(&aoid, astr)); + cl_must_pass(git_oid_fromstr(&boid, bstr)); + + cl_must_pass(git_tree_lookup(&atree, repo, &aoid)); + cl_must_pass(git_tree_lookup(&btree, repo, &boid)); + + test_diff(atree, btree, diff_more_cb, &more_data); +} diff --git a/tests-clar/object/tree/frompath.c b/tests-clar/object/tree/frompath.c new file mode 100644 index 000000000..3857d42a8 --- /dev/null +++ b/tests-clar/object/tree/frompath.c @@ -0,0 +1,75 @@ +#include "clar_libgit2.h" + +static git_repository *repo; +const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12"; +static git_tree *tree; + +void test_object_tree_frompath__initialize(void) +{ + git_oid id; + + cl_fixture_sandbox("testrepo.git"); + cl_git_pass(git_repository_open(&repo, "testrepo.git")); + cl_assert(repo != NULL); + + cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid)); + cl_git_pass(git_tree_lookup(&tree, repo, &id)); + cl_assert(tree != NULL); +} + +void test_object_tree_frompath__cleanup(void) +{ + git_tree_free(tree); + git_repository_free(repo); + cl_fixture_cleanup("testrepo.git"); +} + +static void assert_tree_from_path(git_tree *root, const char *path, git_error expected_result, const char *expected_raw_oid) +{ + git_tree *containing_tree = NULL; + + cl_assert(git_tree_get_subtree(&containing_tree, root, path) == expected_result); + + if (containing_tree == NULL && expected_result != GIT_SUCCESS) + return; + + cl_assert(containing_tree != NULL && expected_result == GIT_SUCCESS); + + cl_assert(git_oid_streq(git_object_id((const git_object *)containing_tree), expected_raw_oid) == GIT_SUCCESS); + + git_tree_free(containing_tree); +} + +void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void) +{ + /* Will return self if given a one path segment... */ + assert_tree_from_path(tree, "README", GIT_SUCCESS, tree_with_subtrees_oid); + + /* ...even one that lead to a non existent tree entry. */ + assert_tree_from_path(tree, "i-do-not-exist.txt", GIT_SUCCESS, tree_with_subtrees_oid); + + /* Will return fgh tree oid given this following path... */ + assert_tree_from_path(tree, "ab/de/fgh/1.txt", GIT_SUCCESS, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54"); + + /* ... and ab tree oid given this one. */ + assert_tree_from_path(tree, "ab/de", GIT_SUCCESS, "f1425cef211cc08caa31e7b545ffb232acb098c3"); + + /* Will succeed if given a valid path which leads to a tree entry which doesn't exist */ + assert_tree_from_path(tree, "ab/de/fgh/i-do-not-exist.txt", GIT_SUCCESS, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54"); +} + +void test_object_tree_frompath__fail_when_processing_an_unknown_tree_segment(void) +{ + assert_tree_from_path(tree, "nope/de/fgh/1.txt", GIT_ENOTFOUND, NULL); + assert_tree_from_path(tree, "ab/me-neither/fgh/2.txt", GIT_ENOTFOUND, NULL); +} + +void test_object_tree_frompath__fail_when_processing_an_invalid_path(void) +{ + assert_tree_from_path(tree, "/", GIT_EINVALIDPATH, NULL); + assert_tree_from_path(tree, "/ab", GIT_EINVALIDPATH, NULL); + assert_tree_from_path(tree, "/ab/de", GIT_EINVALIDPATH, NULL); + assert_tree_from_path(tree, "ab/", GIT_EINVALIDPATH, NULL); + assert_tree_from_path(tree, "ab//de", GIT_EINVALIDPATH, NULL); + assert_tree_from_path(tree, "ab/de/", GIT_EINVALIDPATH, NULL); +} |
