]> git.proxmox.com Git - libgit2.git/blob - tests/object/tree/frompath.c
86ca47e94ec18425df157b5f83972ee2103c56eb
[libgit2.git] / tests / object / tree / frompath.c
1 #include "clar_libgit2.h"
2
3 static git_repository *repo;
4 static git_tree *tree;
5
6 void test_object_tree_frompath__initialize(void)
7 {
8 git_oid id;
9 const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
10
11 cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
12 cl_assert(repo != NULL);
13
14 cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid));
15 cl_git_pass(git_tree_lookup(&tree, repo, &id));
16 cl_assert(tree != NULL);
17 }
18
19 void test_object_tree_frompath__cleanup(void)
20 {
21 git_tree_free(tree);
22 tree = NULL;
23
24 git_repository_free(repo);
25 repo = NULL;
26 }
27
28 static void assert_tree_from_path(
29 git_tree *root,
30 const char *path,
31 const char *expected_entry_name)
32 {
33 git_tree_entry *entry;
34
35 cl_git_pass(git_tree_entry_bypath(&entry, root, path));
36 cl_assert_equal_s(git_tree_entry_name(entry), expected_entry_name);
37 git_tree_entry_free(entry);
38 }
39
40 void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void)
41 {
42 git_tree_entry *e;
43
44 assert_tree_from_path(tree, "README", "README");
45 assert_tree_from_path(tree, "ab/de/fgh/1.txt", "1.txt");
46 assert_tree_from_path(tree, "ab/de/fgh", "fgh");
47 assert_tree_from_path(tree, "ab/de/fgh/", "fgh");
48 assert_tree_from_path(tree, "ab/de", "de");
49 assert_tree_from_path(tree, "ab/", "ab");
50 assert_tree_from_path(tree, "ab/de/", "de");
51
52 cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt"));
53 cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "README/"));
54 cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt"));
55 cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "nope/de/fgh/1.txt"));
56 cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt"));
57 cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt/"));
58 }
59
60 void test_object_tree_frompath__fail_when_processing_an_invalid_path(void)
61 {
62 git_tree_entry *e;
63
64 cl_must_fail(git_tree_entry_bypath(&e, tree, "/"));
65 cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab"));
66 cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab/de"));
67 cl_must_fail(git_tree_entry_bypath(&e, tree, "ab//de"));
68 }