]>
Commit | Line | Data |
---|---|---|
22df47cb RB |
1 | /* test the submodule APIs on repositories where there are no submodules */ |
2 | ||
3 | #include "clar_libgit2.h" | |
4 | #include "posix.h" | |
22a2d3d5 | 5 | #include "futils.h" |
22df47cb RB |
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 | ||
22df47cb RB |
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 | ||
22df47cb RB |
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)); | |
8061d519 | 52 | |
8061d519 | 53 | git_submodule_free(sm); |
22df47cb RB |
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"); | |
22df47cb RB |
61 | |
62 | cl_git_rewritefile("status/.gitmodules", "[submodule \"foobar\"]\tpath=blargle\n\turl=\n\tbranch=\n\tupdate=rebase\n\n"); | |
22df47cb RB |
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 | } | |
4ece3e22 RB |
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 | ||
4ece3e22 RB |
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); | |
ac3d33df | 88 | git_buf_dispose(&buf); |
4ece3e22 RB |
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 | } |