summaryrefslogtreecommitdiff
path: root/tests/submodule/nosubs.c
blob: e343c162064754dbcea2559fedd9805359f48f93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* test the submodule APIs on repositories where there are no submodules */

#include "clar_libgit2.h"
#include "posix.h"
#include "fileops.h"

void test_submodule_nosubs__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_submodule_nosubs__lookup(void)
{
	git_repository *repo = cl_git_sandbox_init("status");
	git_submodule *sm = NULL;

	p_mkdir("status/subrepo", 0777);
	cl_git_mkfile("status/subrepo/.git", "gitdir: ../.git");

	cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, repo, "subdir"));

	cl_assert_equal_i(GIT_EEXISTS, git_submodule_lookup(&sm, repo, "subrepo"));

	cl_git_pass(git_submodule_reload_all(repo, 0));

	cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(&sm, repo, "subdir"));

	cl_assert_equal_i(GIT_EEXISTS, git_submodule_lookup(&sm, repo, "subrepo"));
}

void test_submodule_nosubs__immediate_reload(void)
{
	git_repository *repo = cl_git_sandbox_init("status");
	cl_git_pass(git_submodule_reload_all(repo, 0));
}

static int fake_submod_cb(git_submodule *sm, const char *n, void *p)
{
	GIT_UNUSED(sm); GIT_UNUSED(n); GIT_UNUSED(p);
	return 0;
}

void test_submodule_nosubs__foreach(void)
{
	git_repository *repo = cl_git_sandbox_init("status");
	cl_git_pass(git_submodule_foreach(repo, fake_submod_cb, NULL));
}

void test_submodule_nosubs__add(void)
{
	git_repository *repo = cl_git_sandbox_init("status");
	git_submodule *sm, *sm2;

	cl_git_pass(git_submodule_add_setup(&sm, repo, "https://github.com/libgit2/libgit2.git", "submodules/libgit2", 1));

	cl_git_pass(git_submodule_lookup(&sm2, repo, "submodules/libgit2"));
	git_submodule_free(sm2);

	cl_git_pass(git_submodule_foreach(repo, fake_submod_cb, NULL));
	cl_git_pass(git_submodule_reload_all(repo, 0));

	git_submodule_free(sm);
}

void test_submodule_nosubs__reload_add_reload(void)
{
	git_repository *repo = cl_git_sandbox_init("status");
	git_submodule *sm;

	cl_git_pass(git_submodule_reload_all(repo, 0));

	/* try one add with a reload (to make sure no errors happen) */

	cl_git_pass(git_submodule_add_setup(&sm, repo,
		"https://github.com/libgit2/libgit2.git", "submodules/libgit2", 1));

	cl_git_pass(git_submodule_reload_all(repo, 0));

	cl_assert_equal_s("submodules/libgit2", git_submodule_name(sm));
	git_submodule_free(sm);

	cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
	cl_assert_equal_s("submodules/libgit2", git_submodule_name(sm));
	git_submodule_free(sm);

	/* try one add without a reload (to make sure cache inval works, too) */

	cl_git_pass(git_submodule_add_setup(&sm, repo,
		"https://github.com/libgit2/libgit2.git", "libgit2-again", 1));
	cl_assert_equal_s("libgit2-again", git_submodule_name(sm));
	git_submodule_free(sm);

	cl_git_pass(git_submodule_lookup(&sm, repo, "libgit2-again"));
	cl_assert_equal_s("libgit2-again", git_submodule_name(sm));
	git_submodule_free(sm);
}

void test_submodule_nosubs__bad_gitmodules(void)
{
	git_repository *repo = cl_git_sandbox_init("status");

	cl_git_mkfile("status/.gitmodules", "[submodule \"foobar\"]\tpath=blargle\n\turl=\n\tbranch=\n\tupdate=flooble\n\n");
	cl_git_fail(git_submodule_reload_all(repo, 0));

	cl_git_rewritefile("status/.gitmodules", "[submodule \"foobar\"]\tpath=blargle\n\turl=\n\tbranch=\n\tupdate=rebase\n\n");
	cl_git_pass(git_submodule_reload_all(repo, 0));

	cl_git_pass(git_submodule_lookup(NULL, repo, "foobar"));
	cl_assert_equal_i(GIT_ENOTFOUND, git_submodule_lookup(NULL, repo, "subdir"));
}

void test_submodule_nosubs__add_and_delete(void)
{
	git_repository *repo = cl_git_sandbox_init("status");
	git_submodule *sm;
	git_buf buf = GIT_BUF_INIT;

	/* note lack of calls to git_submodule_reload_all - this *should* work */

	cl_git_fail(git_submodule_lookup(NULL, repo, "libgit2"));
	cl_git_fail(git_submodule_lookup(NULL, repo, "submodules/libgit2"));

	/* create */

	cl_git_pass(git_submodule_add_setup(
		&sm, repo, "https://github.com/libgit2/libgit2.git", "submodules/libgit2", 1));
	cl_assert_equal_s("submodules/libgit2", git_submodule_name(sm));
	cl_assert_equal_s("submodules/libgit2", git_submodule_path(sm));
	git_submodule_free(sm);

	cl_git_pass(git_futils_readbuffer(&buf, "status/.gitmodules"));
	cl_assert(strstr(buf.ptr, "[submodule \"submodules/libgit2\"]") != NULL);
	cl_assert(strstr(buf.ptr, "path = submodules/libgit2") != NULL);
	git_buf_free(&buf);

	/* lookup */

	cl_git_fail(git_submodule_lookup(&sm, repo, "libgit2"));
	cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
	cl_assert_equal_s("submodules/libgit2", git_submodule_name(sm));
	cl_assert_equal_s("submodules/libgit2", git_submodule_path(sm));
	git_submodule_free(sm);

	/* update name */

	cl_git_rewritefile(
		"status/.gitmodules",
		"[submodule \"libgit2\"]\n"
		"  path = submodules/libgit2\n"
		"  url = https://github.com/libgit2/libgit2.git\n");

	cl_git_pass(git_submodule_lookup(&sm, repo, "libgit2"));
	cl_assert_equal_s("libgit2", git_submodule_name(sm));
	cl_assert_equal_s("submodules/libgit2", git_submodule_path(sm));
	git_submodule_free(sm);
	cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
	git_submodule_free(sm);

	/* revert name update */

	cl_git_rewritefile(
		"status/.gitmodules",
		"[submodule \"submodules/libgit2\"]\n"
		"  path = submodules/libgit2\n"
		"  url = https://github.com/libgit2/libgit2.git\n");

	cl_git_fail(git_submodule_lookup(&sm, repo, "libgit2"));
	cl_git_pass(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
	git_submodule_free(sm);

	/* remove completely */

	cl_must_pass(p_unlink("status/.gitmodules"));
	cl_git_fail(git_submodule_lookup(&sm, repo, "libgit2"));
	cl_git_fail(git_submodule_lookup(&sm, repo, "submodules/libgit2"));
}