diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-02-14 10:08:50 +0000 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-02-14 11:07:24 +0000 |
commit | 08ed0881eaef6592d3553dc96d6102f9f0251507 (patch) | |
tree | 666b457d29293c2efe17d63a56bc866f88f8cda3 | |
parent | b7352a70586aa3b341fdc52c4ec77c5ff1de51f5 (diff) | |
download | libgit2-08ed0881eaef6592d3553dc96d6102f9f0251507.tar.gz |
test: isolate home directory separately from global config
-rw-r--r-- | tests/clar_libgit2.c | 52 | ||||
-rw-r--r-- | tests/clar_libgit2.h | 16 | ||||
-rw-r--r-- | tests/ignore/path.c | 6 | ||||
-rw-r--r-- | tests/ignore/status.c | 4 | ||||
-rw-r--r-- | tests/online/clone.c | 9 | ||||
-rw-r--r-- | tests/remote/httpproxy.c | 2 |
6 files changed, 66 insertions, 23 deletions
diff --git a/tests/clar_libgit2.c b/tests/clar_libgit2.c index 783b457f9..c531b01bc 100644 --- a/tests/clar_libgit2.c +++ b/tests/clar_libgit2.c @@ -548,31 +548,61 @@ void clar__assert_equal_file( (size_t)expected_bytes, (size_t)total_bytes); } -static git_buf _cl_restore_home = GIT_BUF_INIT; +static git_buf _cl_restore_homedir = GIT_BUF_INIT; -void cl_fake_home_cleanup(void *payload) +void cl_fake_homedir_cleanup(void *payload) { GIT_UNUSED(payload); - if (_cl_restore_home.ptr) { + if (_cl_restore_homedir.ptr) { cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_home.ptr)); - git_buf_dispose(&_cl_restore_home); + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_homedir.ptr)); + git_buf_dispose(&_cl_restore_homedir); } } -void cl_fake_home(void) +void cl_fake_homedir(void) { git_str path = GIT_STR_INIT; cl_git_pass(git_libgit2_opts( - GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_home)); + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_homedir)); - cl_set_cleanup(cl_fake_home_cleanup, NULL); + cl_set_cleanup(cl_fake_homedir_cleanup, NULL); - if (!git_fs_path_exists("home")) - cl_must_pass(p_mkdir("home", 0777)); - cl_git_pass(git_fs_path_prettify(&path, "home", NULL)); + if (!git_fs_path_exists("homedir")) + cl_must_pass(p_mkdir("homedir", 0777)); + cl_git_pass(git_fs_path_prettify(&path, "homedir", NULL)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + git_str_dispose(&path); +} + +static git_buf _cl_restore_globalconfig = GIT_BUF_INIT; + +void cl_fake_globalconfig_cleanup(void *payload) +{ + GIT_UNUSED(payload); + + if (_cl_restore_globalconfig.ptr) { + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_globalconfig.ptr)); + git_buf_dispose(&_cl_restore_globalconfig); + } +} + +void cl_fake_globalconfig(void) +{ + git_str path = GIT_STR_INIT; + + cl_git_pass(git_libgit2_opts( + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_globalconfig)); + + cl_set_cleanup(cl_fake_globalconfig_cleanup, NULL); + + if (!git_fs_path_exists("globalconfig")) + cl_must_pass(p_mkdir("globalconfig", 0777)); + cl_git_pass(git_fs_path_prettify(&path, "globalconfig", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); git_str_dispose(&path); diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h index da3f41524..2e06f19ca 100644 --- a/tests/clar_libgit2.h +++ b/tests/clar_libgit2.h @@ -213,13 +213,23 @@ int cl_repo_get_bool(git_repository *repo, const char *cfg); void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value); -/* set up a fake "home" directory and set libgit2 GLOBAL search path. +/* set up a fake "home" directory * * automatically configures cleanup function to restore the regular search * path, although you can call it explicitly if you wish (with NULL). */ -void cl_fake_home(void); -void cl_fake_home_cleanup(void *); +void cl_fake_homedir(void); +void cl_fake_homedir_cleanup(void *); + + +/* + * set up a fake directory for the libgit2 GLOBAL search path. + * + * automatically configures cleanup function to restore the regular search + * path, although you can call it explicitly if you wish (with NULL). + */ +void cl_fake_globalconfig(void); +void cl_fake_globalconfig_cleanup(void *); void cl_sandbox_set_search_path_defaults(void); void cl_sandbox_disable_ownership_validation(void); diff --git a/tests/ignore/path.c b/tests/ignore/path.c index a574d1d79..3b95b88ab 100644 --- a/tests/ignore/path.c +++ b/tests/ignore/path.c @@ -290,10 +290,10 @@ void test_ignore_path__expand_tilde_to_homedir(void) assert_is_ignored(false, "example.global_with_tilde"); - cl_fake_home(); + cl_fake_globalconfig(); /* construct fake home with fake global excludes */ - cl_git_mkfile("home/globalexclude", "# found me\n*.global_with_tilde\n"); + cl_git_mkfile("globalconfig/globalexclude", "# found me\n*.global_with_tilde\n"); cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude")); @@ -305,7 +305,7 @@ void test_ignore_path__expand_tilde_to_homedir(void) cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_fake_home_cleanup(NULL); + cl_fake_globalconfig_cleanup(NULL); git_attr_cache_flush(g_repo); /* must reset to pick up change */ diff --git a/tests/ignore/status.c b/tests/ignore/status.c index deb717590..8214d3847 100644 --- a/tests/ignore/status.c +++ b/tests/ignore/status.c @@ -385,8 +385,8 @@ void test_ignore_status__leading_slash_ignores(void) make_test_data(test_repo_1, test_files_1); - cl_fake_home(); - cl_git_mkfile("home/.gitignore", "/ignore_me\n"); + cl_fake_globalconfig(); + cl_git_mkfile("globalconfig/.gitignore", "/ignore_me\n"); { git_config *cfg; cl_git_pass(git_repository_config(&cfg, g_repo)); diff --git a/tests/online/clone.c b/tests/online/clone.c index 6b11a9a4b..1c5d7cae4 100644 --- a/tests/online/clone.c +++ b/tests/online/clone.c @@ -5,6 +5,7 @@ #include "remote.h" #include "futils.h" #include "refs.h" +#include "sysdir.h" #define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository" #define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository" @@ -606,7 +607,7 @@ void test_online_clone__ssh_github(void) if (!_github_ssh_pubkey || !_github_ssh_privkey) clar__skip(); - cl_fake_homedir(NULL); + cl_fake_homedir(); g_options.fetch_opts.callbacks.credentials = github_credentials; g_options.fetch_opts.callbacks.certificate_check = succeed_certificate_check; @@ -647,7 +648,7 @@ void test_online_clone__ssh_certcheck_accepts_unknown(void) if (!_github_ssh_pubkey || !_github_ssh_privkey) clar__skip(); - cl_fake_homedir(NULL); + cl_fake_homedir(); g_options.fetch_opts.callbacks.credentials = github_credentials; @@ -678,7 +679,9 @@ void test_online_clone__ssh_certcheck_override_knownhosts(void) g_options.fetch_opts.callbacks.credentials = github_credentials; - cl_fake_homedir(&knownhostsfile); + cl_fake_homedir(); + + cl_git_pass(git_sysdir_find_homedir(&knownhostsfile)); cl_git_pass(git_str_joinpath(&knownhostsfile, knownhostsfile.ptr, ".ssh")); cl_git_pass(p_mkdir(knownhostsfile.ptr, 0777)); diff --git a/tests/remote/httpproxy.c b/tests/remote/httpproxy.c index f62a2545b..199b6f0b6 100644 --- a/tests/remote/httpproxy.c +++ b/tests/remote/httpproxy.c @@ -132,7 +132,7 @@ static void assert_global_config_match(const char *config, const char *expected) void test_remote_httpproxy__config_overrides_detached_remote(void) { - cl_fake_home(); + cl_fake_globalconfig(); assert_global_config_match(NULL, NULL); assert_global_config_match("http.proxy", "http://localhost:1/"); |