]> git.proxmox.com Git - libgit2.git/blob - tests/submodule/nosubs.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / submodule / nosubs.c
1 /* test the submodule APIs on repositories where there are no submodules */
2
3 #include "clar_libgit2.h"
4 #include "posix.h"
5 #include "futils.h"
6
7 void test_submodule_nosubs__cleanup(void)
8 {
9 cl_git_sandbox_cleanup();
10 }
11
12 void test_submodule_nosubs__lookup(void)
13 {
14 git_repository *repo = cl_git_sandbox_init("status");
15 git_submodule *sm = NULL;
16
17 p_mkdir("status/subrepo", 0777);
18 cl_git_mkfile("status/subrepo/.git", "gitdir: ../.git");
19
20 cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, repo, "subdir"));
21
22 cl_assert_equal_i(GIT_EEXISTS, git_submodule_lookup(&sm, repo, "subrepo"));
23
24 cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, repo, "subdir"));
25
26 cl_assert_equal_i(GIT_EEXISTS, git_submodule_lookup(&sm, repo, "subrepo"));
27 }
28
29 static int fake_submod_cb(git_submodule *sm, const char *n, void *p)
30 {
31 GIT_UNUSED(sm); GIT_UNUSED(n); GIT_UNUSED(p);
32 return 0;
33 }
34
35 void test_submodule_nosubs__foreach(void)
36 {
37 git_repository *repo = cl_git_sandbox_init("status");
38 cl_git_pass(git_submodule_foreach(repo, fake_submod_cb, NULL));
39 }
40
41 void test_submodule_nosubs__add(void)
42 {
43 git_repository *repo = cl_git_sandbox_init("status");
44 git_submodule *sm, *sm2;
45
46 cl_git_pass(git_submodule_add_setup(&sm, repo, "https://github.com/libgit2/libgit2.git", "submodules/libgit2", 1));
47
48 cl_git_pass(git_submodule_lookup(&sm2, repo, "submodules/libgit2"));
49 git_submodule_free(sm2);
50
51 cl_git_pass(git_submodule_foreach(repo, fake_submod_cb, NULL));
52
53 git_submodule_free(sm);
54 }
55
56 void test_submodule_nosubs__bad_gitmodules(void)
57 {
58 git_repository *repo = cl_git_sandbox_init("status");
59
60 cl_git_mkfile("status/.gitmodules", "[submodule \"foobar\"]\tpath=blargle\n\turl=\n\tbranch=\n\tupdate=flooble\n\n");
61
62 cl_git_rewritefile("status/.gitmodules", "[submodule \"foobar\"]\tpath=blargle\n\turl=\n\tbranch=\n\tupdate=rebase\n\n");
63
64 cl_git_pass(git_submodule_lookup(NULL, repo, "foobar"));
65 cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(NULL, repo, "subdir"));
66 }
67
68 void test_submodule_nosubs__add_and_delete(void)
69 {
70 git_repository *repo = cl_git_sandbox_init("status");
71 git_submodule *sm;
72 git_buf buf = GIT_BUF_INIT;
73
74 cl_git_fail(git_submodule_lookup(NULL, repo, "libgit2"));
75 cl_git_fail(git_submodule_lookup(NULL, repo, "submodules/libgit2"));
76
77 /* create */
78
79 cl_git_pass(git_submodule_add_setup(
80 &sm, repo, "https://github.com/libgit2/libgit2.git", "submodules/libgit2", 1));
81 cl_assert_equal_s("submodules/libgit2", git_submodule_name(sm));
82 cl_assert_equal_s("submodules/libgit2", git_submodule_path(sm));
83 git_submodule_free(sm);
84
85 cl_git_pass(git_futils_readbuffer(&buf, "status/.gitmodules"));
86 cl_assert(strstr(buf.ptr, "[submodule \"submodules/libgit2\"]") != NULL);
87 cl_assert(strstr(buf.ptr, "path = submodules/libgit2") != NULL);
88 git_buf_dispose(&buf);
89
90 /* lookup */
91
92 cl_git_fail(git_submodule_lookup(&sm, repo, "libgit2"));
93 cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
94 cl_assert_equal_s("submodules/libgit2", git_submodule_name(sm));
95 cl_assert_equal_s("submodules/libgit2", git_submodule_path(sm));
96 git_submodule_free(sm);
97
98 /* update name */
99
100 cl_git_rewritefile(
101 "status/.gitmodules",
102 "[submodule \"libgit2\"]\n"
103 " path = submodules/libgit2\n"
104 " url = https://github.com/libgit2/libgit2.git\n");
105
106 cl_git_pass(git_submodule_lookup(&sm, repo, "libgit2"));
107 cl_assert_equal_s("libgit2", git_submodule_name(sm));
108 cl_assert_equal_s("submodules/libgit2", git_submodule_path(sm));
109 git_submodule_free(sm);
110 cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
111 git_submodule_free(sm);
112
113 /* revert name update */
114
115 cl_git_rewritefile(
116 "status/.gitmodules",
117 "[submodule \"submodules/libgit2\"]\n"
118 " path = submodules/libgit2\n"
119 " url = https://github.com/libgit2/libgit2.git\n");
120
121 cl_git_fail(git_submodule_lookup(&sm, repo, "libgit2"));
122 cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
123 git_submodule_free(sm);
124
125 /* remove completely */
126
127 cl_must_pass(p_unlink("status/.gitmodules"));
128 cl_git_fail(git_submodule_lookup(&sm, repo, "libgit2"));
129 cl_git_fail(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
130 }