Skip to content

Commit 0d12b8d

Browse files
committed
tests: repo: refactor setup of templates and repos
All tests in repo::template have a common pattern of first setting up templates, then settung up the repository that makes use of those templates via several init options. Refactor this pattern into two functions `setup_templates` and `setup_repo` that handle most of that logic to make it easier to spot what a test actually wants to check. Furthermore, this also refactors how we clean up after the tests. Previously, it was a combination of manually calling `cl_fixture_cleanup` and `cl_set_cleanup`, which really is kind of hard to read. This commit refactors this to instead provide the cleaning parameters in the setup functions. All cleanups are then performed in the suite's cleanup function.
1 parent 3b79cea commit 0d12b8d

File tree

1 file changed

+45
-67
lines changed

1 file changed

+45
-67
lines changed

tests/repo/template.c

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ static git_repository *_repo = NULL;
99
static mode_t g_umask = 0;
1010
static git_buf _global_path = GIT_BUF_INIT;
1111

12+
static const char *fixture_repo;
13+
static const char *fixture_templates;
14+
1215
void test_repo_template__initialize(void)
1316
{
1417
_repo = NULL;
@@ -27,14 +30,19 @@ void test_repo_template__cleanup(void)
2730
git_buf_dispose(&_global_path);
2831

2932
cl_fixture_cleanup("tmp_global_path");
30-
}
3133

32-
static void cleanup_repository(void *path)
33-
{
34+
if (fixture_repo) {
35+
cl_fixture_cleanup(fixture_repo);
36+
fixture_repo = NULL;
37+
}
38+
39+
if (fixture_templates) {
40+
cl_fixture_cleanup(fixture_templates);
41+
fixture_templates = NULL;
42+
}
43+
3444
git_repository_free(_repo);
3545
_repo = NULL;
36-
37-
cl_fixture_cleanup((const char *)path);
3846
}
3947

4048
static void assert_hooks_match(
@@ -99,11 +107,21 @@ static void assert_mode_seems_okay(
99107
GIT_MODE_TYPE(expect_mode), GIT_MODE_TYPE(st.st_mode), "%07o");
100108
}
101109

102-
static const char *template_sandbox(const char *name)
110+
static void setup_repo(const char *name, git_repository_init_options *opts)
111+
{
112+
cl_git_pass(git_repository_init_ext(&_repo, name, opts));
113+
fixture_repo = name;
114+
}
115+
116+
static void setup_templates(const char *name, bool setup_globally)
103117
{
104118
git_buf path = GIT_BUF_INIT;
105119

106-
cl_fixture_sandbox(name);
120+
cl_fixture_sandbox("template");
121+
if (strcmp(name, "template"))
122+
cl_must_pass(p_rename("template", name));
123+
124+
fixture_templates = name;
107125

108126
/*
109127
* Create a symlink from link.sample to update.sample if the filesystem
@@ -122,13 +140,14 @@ static const char *template_sandbox(const char *name)
122140
cl_git_pass(git_buf_join3(&path, '/', name, "hooks", ".dotfile"));
123141
cl_git_mkfile(path.ptr, "something\n");
124142

125-
git_buf_dispose(&path);
126-
return cl_fixture(name);
127-
}
143+
git_buf_clear(&path);
128144

129-
static void configure_templatedir(const char *template_path)
130-
{
131-
create_tmp_global_config("tmp_global_path", "init.templatedir", template_path);
145+
if (setup_globally) {
146+
cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), name));
147+
create_tmp_global_config("tmp_global_path", "init.templatedir", path.ptr);
148+
}
149+
150+
git_buf_dispose(&path);
132151
}
133152

134153
static void validate_templates(git_repository *repo, const char *template_path)
@@ -167,93 +186,55 @@ void test_repo_template__external_templates_specified_in_options(void)
167186
{
168187
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
169188

170-
cl_set_cleanup(&cleanup_repository, "templated.git");
171-
template_sandbox("template");
172-
173189
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
174190
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
175191
opts.template_path = "template";
176192

177-
cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
178-
179-
cl_assert(git_repository_is_bare(_repo));
180-
181-
cl_assert(!git__suffixcmp(git_repository_path(_repo), "/templated.git/"));
193+
setup_templates("template", false);
194+
setup_repo("templated.git", &opts);
182195

183196
validate_templates(_repo, "template");
184-
cl_fixture_cleanup("template");
185197
}
186198

187199
void test_repo_template__external_templates_specified_in_config(void)
188200
{
189-
git_buf template_path = GIT_BUF_INIT;
190-
191201
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
192202

193-
cl_set_cleanup(&cleanup_repository, "templated.git");
194-
template_sandbox("template");
195-
196-
cl_git_pass(git_buf_joinpath(&template_path, clar_sandbox_path(),
197-
"template"));
198-
199-
configure_templatedir(template_path.ptr);
200-
201203
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
202204
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
203205

204-
cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
206+
setup_templates("template", true);
207+
setup_repo("templated.git", &opts);
205208

206209
validate_templates(_repo, "template");
207-
cl_fixture_cleanup("template");
208-
209-
git_buf_dispose(&template_path);
210210
}
211211

212212
void test_repo_template__external_templates_with_leading_dot(void)
213213
{
214-
git_buf template_path = GIT_BUF_INIT;
215-
216214
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
217215

218-
cl_set_cleanup(&cleanup_repository, "templated.git");
219-
template_sandbox("template");
220-
221-
cl_must_pass(p_rename("template", ".template_with_leading_dot"));
222-
223-
cl_git_pass(git_buf_joinpath(&template_path, clar_sandbox_path(),
224-
".template_with_leading_dot"));
225-
226-
configure_templatedir(template_path.ptr);
227-
228216
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
229217
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
230218

231-
cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
219+
setup_templates(".template_with_leading_dot", true);
220+
setup_repo("templated.git", &opts);
232221

233222
validate_templates(_repo, ".template_with_leading_dot");
234-
cl_fixture_cleanup(".template_with_leading_dot");
235-
236-
git_buf_dispose(&template_path);
237223
}
238224

239225
void test_repo_template__extended_with_template_and_shared_mode(void)
240226
{
241227
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
242-
int filemode = true;
243-
const char *repo_path = NULL;
244-
245-
cl_set_cleanup(&cleanup_repository, "init_shared_from_tpl");
246-
template_sandbox("template");
228+
const char *repo_path;
229+
int filemode;
247230

248231
opts.flags = GIT_REPOSITORY_INIT_MKPATH |
249232
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
250233
opts.template_path = "template";
251234
opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP;
252235

253-
cl_git_pass(git_repository_init_ext(&_repo, "init_shared_from_tpl", &opts));
254-
255-
cl_assert(!git_repository_is_bare(_repo));
256-
cl_assert(!git__suffixcmp(git_repository_path(_repo), "/init_shared_from_tpl/.git/"));
236+
setup_templates("template", false);
237+
setup_repo("init_shared_from_tpl", &opts);
257238

258239
filemode = cl_repo_get_bool(_repo, "core.filemode");
259240

@@ -266,17 +247,14 @@ void test_repo_template__extended_with_template_and_shared_mode(void)
266247
GIT_FILEMODE_BLOB, false, filemode);
267248

268249
validate_templates(_repo, "template");
269-
270-
cl_fixture_cleanup("template");
271250
}
272251

273252
void test_repo_template__empty_template_path(void)
274253
{
275254
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
276-
opts.template_path = "";
277255

278-
cl_git_pass(git_futils_mkdir("foo", 0755, 0));
279-
cl_git_pass(git_repository_init_ext(&_repo, "foo", &opts));
256+
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
257+
opts.template_path = "";
280258

281-
cleanup_repository("foo");
259+
setup_repo("foo", &opts);
282260
}

0 commit comments

Comments
 (0)