Skip to content

Commit ee19348

Browse files
committed
tests: repo: move template tests into their own suite
There's quite a lot of supporting code for our templates and they are an obvious standalone feature. Thus, let's extract those tests into their own suite to also make refactoring of them easier.
1 parent e86d75f commit ee19348

File tree

2 files changed

+296
-267
lines changed

2 files changed

+296
-267
lines changed

tests/repo/init.c

Lines changed: 2 additions & 267 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@ enum repo_mode {
1313

1414
static git_repository *_repo = NULL;
1515
static git_buf _global_path = GIT_BUF_INIT;
16-
static mode_t g_umask = 0;
1716

1817
void test_repo_init__initialize(void)
1918
{
2019
_repo = NULL;
2120

22-
/* load umask if not already loaded */
23-
if (!g_umask) {
24-
g_umask = p_umask(022);
25-
(void)p_umask(g_umask);
26-
}
27-
28-
git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
29-
&_global_path);
21+
git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
22+
&_global_path);
3023
}
3124

3225
void test_repo_init__cleanup(void)
@@ -380,17 +373,6 @@ void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void)
380373
assert_config_entry_on_init_bytype("core.logallrefupdates", true, false);
381374
}
382375

383-
void test_repo_init__empty_template_path(void)
384-
{
385-
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
386-
opts.template_path = "";
387-
388-
cl_git_pass(git_futils_mkdir("foo", 0755, 0));
389-
cl_git_pass(git_repository_init_ext(&_repo, "foo", &opts));
390-
391-
cleanup_repository("foo");
392-
}
393-
394376
void test_repo_init__extended_0(void)
395377
{
396378
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
@@ -524,253 +506,6 @@ void test_repo_init__relative_gitdir_2(void)
524506
cleanup_repository("root");
525507
}
526508

527-
#define CLEAR_FOR_CORE_FILEMODE(M) ((M) &= ~0177)
528-
529-
static void assert_hooks_match(
530-
const char *template_dir,
531-
const char *repo_dir,
532-
const char *hook_path,
533-
bool core_filemode)
534-
{
535-
git_buf expected = GIT_BUF_INIT;
536-
git_buf actual = GIT_BUF_INIT;
537-
struct stat expected_st, st;
538-
539-
cl_git_pass(git_buf_joinpath(&expected, template_dir, hook_path));
540-
cl_git_pass(git_path_lstat(expected.ptr, &expected_st));
541-
542-
cl_git_pass(git_buf_joinpath(&actual, repo_dir, hook_path));
543-
cl_git_pass(git_path_lstat(actual.ptr, &st));
544-
545-
cl_assert(expected_st.st_size == st.st_size);
546-
547-
if (GIT_MODE_TYPE(expected_st.st_mode) != GIT_FILEMODE_LINK) {
548-
mode_t expected_mode =
549-
GIT_MODE_TYPE(expected_st.st_mode) |
550-
(GIT_PERMS_FOR_WRITE(expected_st.st_mode) & ~g_umask);
551-
552-
if (!core_filemode) {
553-
CLEAR_FOR_CORE_FILEMODE(expected_mode);
554-
CLEAR_FOR_CORE_FILEMODE(st.st_mode);
555-
}
556-
557-
cl_assert_equal_i_fmt(expected_mode, st.st_mode, "%07o");
558-
}
559-
560-
git_buf_dispose(&expected);
561-
git_buf_dispose(&actual);
562-
}
563-
564-
static void assert_mode_seems_okay(
565-
const char *base, const char *path,
566-
git_filemode_t expect_mode, bool expect_setgid, bool core_filemode)
567-
{
568-
git_buf full = GIT_BUF_INIT;
569-
struct stat st;
570-
571-
cl_git_pass(git_buf_joinpath(&full, base, path));
572-
cl_git_pass(git_path_lstat(full.ptr, &st));
573-
git_buf_dispose(&full);
574-
575-
if (!core_filemode) {
576-
CLEAR_FOR_CORE_FILEMODE(expect_mode);
577-
CLEAR_FOR_CORE_FILEMODE(st.st_mode);
578-
expect_setgid = false;
579-
}
580-
581-
if (S_ISGID != 0)
582-
cl_assert_equal_b(expect_setgid, (st.st_mode & S_ISGID) != 0);
583-
584-
cl_assert_equal_b(
585-
GIT_PERMS_IS_EXEC(expect_mode), GIT_PERMS_IS_EXEC(st.st_mode));
586-
587-
cl_assert_equal_i_fmt(
588-
GIT_MODE_TYPE(expect_mode), GIT_MODE_TYPE(st.st_mode), "%07o");
589-
}
590-
591-
static const char *template_sandbox(const char *name)
592-
{
593-
git_buf hooks_path = GIT_BUF_INIT, link_path = GIT_BUF_INIT,
594-
dotfile_path = GIT_BUF_INIT;
595-
const char *path = cl_fixture(name);
596-
597-
cl_fixture_sandbox(name);
598-
599-
/* create a symlink from link.sample to update.sample if the filesystem
600-
* supports it.
601-
*/
602-
603-
cl_git_pass(git_buf_joinpath(&hooks_path, name, "hooks"));
604-
cl_git_pass(git_buf_joinpath(&link_path, hooks_path.ptr, "link.sample"));
605-
606-
#ifdef GIT_WIN32
607-
cl_git_mkfile(link_path.ptr, "#!/bin/sh\necho hello, world\n");
608-
#else
609-
cl_must_pass(symlink("update.sample", link_path.ptr));
610-
#endif
611-
612-
/* create a file starting with a dot */
613-
cl_git_pass(git_buf_joinpath(&dotfile_path, hooks_path.ptr, ".dotfile"));
614-
cl_git_mkfile(dotfile_path.ptr, "something\n");
615-
git_buf_dispose(&dotfile_path);
616-
617-
git_buf_dispose(&dotfile_path);
618-
git_buf_dispose(&link_path);
619-
git_buf_dispose(&hooks_path);
620-
621-
return path;
622-
}
623-
624-
static void configure_templatedir(const char *template_path)
625-
{
626-
create_tmp_global_config("tmp_global_path", "init.templatedir", template_path);
627-
}
628-
629-
static void validate_templates(git_repository *repo, const char *template_path)
630-
{
631-
git_buf template_description = GIT_BUF_INIT;
632-
git_buf repo_description = GIT_BUF_INIT;
633-
git_buf expected = GIT_BUF_INIT;
634-
git_buf actual = GIT_BUF_INIT;
635-
int filemode;
636-
637-
cl_git_pass(git_buf_joinpath(&template_description, template_path,
638-
"description"));
639-
cl_git_pass(git_buf_joinpath(&repo_description, git_repository_path(repo),
640-
"description"));
641-
642-
cl_git_pass(git_futils_readbuffer(&expected, template_description.ptr));
643-
cl_git_pass(git_futils_readbuffer(&actual, repo_description.ptr));
644-
645-
cl_assert_equal_s(expected.ptr, actual.ptr);
646-
647-
filemode = cl_repo_get_bool(repo, "core.filemode");
648-
649-
assert_hooks_match(
650-
template_path, git_repository_path(repo),
651-
"hooks/update.sample", filemode);
652-
653-
assert_hooks_match(
654-
template_path, git_repository_path(repo),
655-
"hooks/link.sample", filemode);
656-
657-
assert_hooks_match(
658-
template_path, git_repository_path(repo),
659-
"hooks/.dotfile", filemode);
660-
661-
git_buf_dispose(&expected);
662-
git_buf_dispose(&actual);
663-
git_buf_dispose(&repo_description);
664-
git_buf_dispose(&template_description);
665-
}
666-
667-
void test_repo_init__external_templates_specified_in_options(void)
668-
{
669-
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
670-
671-
cl_set_cleanup(&cleanup_repository, "templated.git");
672-
template_sandbox("template");
673-
674-
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
675-
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
676-
opts.template_path = "template";
677-
678-
cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
679-
680-
cl_assert(git_repository_is_bare(_repo));
681-
682-
cl_assert(!git__suffixcmp(git_repository_path(_repo), "/templated.git/"));
683-
684-
validate_templates(_repo, "template");
685-
cl_fixture_cleanup("template");
686-
}
687-
688-
void test_repo_init__external_templates_specified_in_config(void)
689-
{
690-
git_buf template_path = GIT_BUF_INIT;
691-
692-
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
693-
694-
cl_set_cleanup(&cleanup_repository, "templated.git");
695-
template_sandbox("template");
696-
697-
cl_git_pass(git_buf_joinpath(&template_path, clar_sandbox_path(),
698-
"template"));
699-
700-
configure_templatedir(template_path.ptr);
701-
702-
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
703-
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
704-
705-
cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
706-
707-
validate_templates(_repo, "template");
708-
cl_fixture_cleanup("template");
709-
710-
git_buf_dispose(&template_path);
711-
}
712-
713-
void test_repo_init__external_templates_with_leading_dot(void)
714-
{
715-
git_buf template_path = GIT_BUF_INIT;
716-
717-
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
718-
719-
cl_set_cleanup(&cleanup_repository, "templated.git");
720-
template_sandbox("template");
721-
722-
cl_must_pass(p_rename("template", ".template_with_leading_dot"));
723-
724-
cl_git_pass(git_buf_joinpath(&template_path, clar_sandbox_path(),
725-
".template_with_leading_dot"));
726-
727-
configure_templatedir(template_path.ptr);
728-
729-
opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
730-
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
731-
732-
cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
733-
734-
validate_templates(_repo, ".template_with_leading_dot");
735-
cl_fixture_cleanup(".template_with_leading_dot");
736-
737-
git_buf_dispose(&template_path);
738-
}
739-
740-
void test_repo_init__extended_with_template_and_shared_mode(void)
741-
{
742-
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
743-
int filemode = true;
744-
const char *repo_path = NULL;
745-
746-
cl_set_cleanup(&cleanup_repository, "init_shared_from_tpl");
747-
template_sandbox("template");
748-
749-
opts.flags = GIT_REPOSITORY_INIT_MKPATH |
750-
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
751-
opts.template_path = "template";
752-
opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP;
753-
754-
cl_git_pass(git_repository_init_ext(&_repo, "init_shared_from_tpl", &opts));
755-
756-
cl_assert(!git_repository_is_bare(_repo));
757-
cl_assert(!git__suffixcmp(git_repository_path(_repo), "/init_shared_from_tpl/.git/"));
758-
759-
filemode = cl_repo_get_bool(_repo, "core.filemode");
760-
761-
repo_path = git_repository_path(_repo);
762-
assert_mode_seems_okay(repo_path, "hooks",
763-
GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode);
764-
assert_mode_seems_okay(repo_path, "info",
765-
GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode);
766-
assert_mode_seems_okay(repo_path, "description",
767-
GIT_FILEMODE_BLOB, false, filemode);
768-
769-
validate_templates(_repo, "template");
770-
771-
cl_fixture_cleanup("template");
772-
}
773-
774509
void test_repo_init__can_reinit_an_initialized_repository(void)
775510
{
776511
git_repository *reinit;

0 commit comments

Comments
 (0)