Skip to content

Commit 3b79cea

Browse files
committed
tests: repo: refactor template path handling
The repo::template test suite makes use of quite a few local variables that could be consolidated. Do so to make the code easier to read.
1 parent ee19348 commit 3b79cea

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

tests/repo/template.c

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,29 @@ static void assert_mode_seems_okay(
101101

102102
static const char *template_sandbox(const char *name)
103103
{
104-
git_buf hooks_path = GIT_BUF_INIT, link_path = GIT_BUF_INIT,
105-
dotfile_path = GIT_BUF_INIT;
106-
const char *path = cl_fixture(name);
104+
git_buf path = GIT_BUF_INIT;
107105

108106
cl_fixture_sandbox(name);
109107

110-
/* create a symlink from link.sample to update.sample if the filesystem
108+
/*
109+
* Create a symlink from link.sample to update.sample if the filesystem
111110
* supports it.
112111
*/
113-
114-
cl_git_pass(git_buf_joinpath(&hooks_path, name, "hooks"));
115-
cl_git_pass(git_buf_joinpath(&link_path, hooks_path.ptr, "link.sample"));
116-
112+
cl_git_pass(git_buf_join3(&path, '/', name, "hooks", "link.sample"));
117113
#ifdef GIT_WIN32
118-
cl_git_mkfile(link_path.ptr, "#!/bin/sh\necho hello, world\n");
114+
cl_git_mkfile(path.ptr, "#!/bin/sh\necho hello, world\n");
119115
#else
120-
cl_must_pass(symlink("update.sample", link_path.ptr));
116+
cl_must_pass(p_symlink("update.sample", path.ptr));
121117
#endif
122118

123-
/* create a file starting with a dot */
124-
cl_git_pass(git_buf_joinpath(&dotfile_path, hooks_path.ptr, ".dotfile"));
125-
cl_git_mkfile(dotfile_path.ptr, "something\n");
126-
git_buf_dispose(&dotfile_path);
119+
git_buf_clear(&path);
127120

128-
git_buf_dispose(&dotfile_path);
129-
git_buf_dispose(&link_path);
130-
git_buf_dispose(&hooks_path);
121+
/* Create a file starting with a dot */
122+
cl_git_pass(git_buf_join3(&path, '/', name, "hooks", ".dotfile"));
123+
cl_git_mkfile(path.ptr, "something\n");
131124

132-
return path;
125+
git_buf_dispose(&path);
126+
return cl_fixture(name);
133127
}
134128

135129
static void configure_templatedir(const char *template_path)
@@ -139,19 +133,16 @@ static void configure_templatedir(const char *template_path)
139133

140134
static void validate_templates(git_repository *repo, const char *template_path)
141135
{
142-
git_buf template_description = GIT_BUF_INIT;
143-
git_buf repo_description = GIT_BUF_INIT;
144-
git_buf expected = GIT_BUF_INIT;
145-
git_buf actual = GIT_BUF_INIT;
136+
git_buf path = GIT_BUF_INIT, expected = GIT_BUF_INIT, actual = GIT_BUF_INIT;
146137
int filemode;
147138

148-
cl_git_pass(git_buf_joinpath(&template_description, template_path,
149-
"description"));
150-
cl_git_pass(git_buf_joinpath(&repo_description, git_repository_path(repo),
151-
"description"));
139+
cl_git_pass(git_buf_joinpath(&path, template_path, "description"));
140+
cl_git_pass(git_futils_readbuffer(&expected, path.ptr));
152141

153-
cl_git_pass(git_futils_readbuffer(&expected, template_description.ptr));
154-
cl_git_pass(git_futils_readbuffer(&actual, repo_description.ptr));
142+
git_buf_clear(&path);
143+
144+
cl_git_pass(git_buf_joinpath(&path, git_repository_path(repo), "description"));
145+
cl_git_pass(git_futils_readbuffer(&actual, path.ptr));
155146

156147
cl_assert_equal_s(expected.ptr, actual.ptr);
157148

@@ -160,19 +151,16 @@ static void validate_templates(git_repository *repo, const char *template_path)
160151
assert_hooks_match(
161152
template_path, git_repository_path(repo),
162153
"hooks/update.sample", filemode);
163-
164154
assert_hooks_match(
165155
template_path, git_repository_path(repo),
166156
"hooks/link.sample", filemode);
167-
168157
assert_hooks_match(
169158
template_path, git_repository_path(repo),
170159
"hooks/.dotfile", filemode);
171160

172161
git_buf_dispose(&expected);
173162
git_buf_dispose(&actual);
174-
git_buf_dispose(&repo_description);
175-
git_buf_dispose(&template_description);
163+
git_buf_dispose(&path);
176164
}
177165

178166
void test_repo_template__external_templates_specified_in_options(void)

0 commit comments

Comments
 (0)