summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2023-01-23 15:49:09 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2023-01-23 15:49:09 +0000
commit1faa4ca2a4458d59f0de058c203d969e95e81e93 (patch)
treef548171dfe7594dcb439a0911f253d81e48064e3
parent82b5df82ddb47076ea50d142d07f76330f0283b4 (diff)
downloadlibgit2-1faa4ca2a4458d59f0de058c203d969e95e81e93.tar.gz
tests: configure temporary homedirs correctly
Now that we've split the notion of the home directory from the global configuration store, our tests should use the appropriate one based on what they're doing.
-rw-r--r--tests/clar/clar_libgit2.c71
-rw-r--r--tests/clar/clar_libgit2.h20
-rw-r--r--tests/libgit2/config/include.c12
-rw-r--r--tests/libgit2/config/read.c9
-rw-r--r--tests/libgit2/ignore/path.c10
-rw-r--r--tests/libgit2/ignore/status.c8
-rw-r--r--tests/libgit2/remote/httpproxy.c4
7 files changed, 98 insertions, 36 deletions
diff --git a/tests/clar/clar_libgit2.c b/tests/clar/clar_libgit2.c
index 783b457f9..cc953915d 100644
--- a/tests/clar/clar_libgit2.c
+++ b/tests/clar/clar_libgit2.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "fs_path.h"
+#include "futils.h"
#include "git2/sys/repository.h"
void cl_git_report_failure(
@@ -548,33 +549,77 @@ void clar__assert_equal_file(
(size_t)expected_bytes, (size_t)total_bytes);
}
-static git_buf _cl_restore_home = GIT_BUF_INIT;
+#define FAKE_HOMEDIR_NAME "cl_fake_home"
-void cl_fake_home_cleanup(void *payload)
+static git_buf _cl_restore_homedir = GIT_BUF_INIT;
+
+void cl_fake_homedir_cleanup(void *payload)
{
GIT_UNUSED(payload);
- if (_cl_restore_home.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);
+ if (_cl_restore_homedir.ptr) {
+ cl_git_pass(git_futils_rmdir_r(FAKE_HOMEDIR_NAME, NULL, GIT_RMDIR_REMOVE_FILES));
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, _cl_restore_homedir.ptr));
+ git_buf_dispose(&_cl_restore_homedir);
}
}
-void cl_fake_home(void)
+void cl_fake_homedir(git_str *out)
{
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_HOMEDIR, &_cl_restore_homedir));
+
+ cl_set_cleanup(cl_fake_homedir_cleanup, NULL);
+
+ /* TOC/TOU but merely attempts to prevent accidental cleanup. */
+ cl_assert(!git_fs_path_exists(FAKE_HOMEDIR_NAME));
+ cl_must_pass(p_mkdir(FAKE_HOMEDIR_NAME, 0777));
+ cl_git_pass(git_fs_path_prettify(&path, FAKE_HOMEDIR_NAME, NULL));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, path.ptr));
- cl_set_cleanup(cl_fake_home_cleanup, NULL);
+ if (out)
+ git_str_swap(out, &path);
+
+ git_str_dispose(&path);
+}
+
+#define FAKE_GLOBALCONFIG_NAME "cl_fake_global"
+
+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_futils_rmdir_r(FAKE_GLOBALCONFIG_NAME, NULL, GIT_RMDIR_REMOVE_FILES));
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, _cl_restore_globalconfig.ptr));
+ git_buf_dispose(&_cl_restore_globalconfig);
+ }
+}
+
+void cl_fake_globalconfig(git_str *out)
+{
+ git_str path = GIT_STR_INIT;
- if (!git_fs_path_exists("home"))
- cl_must_pass(p_mkdir("home", 0777));
- cl_git_pass(git_fs_path_prettify(&path, "home", NULL));
cl_git_pass(git_libgit2_opts(
- GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+ GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_globalconfig));
+
+ cl_set_cleanup(cl_fake_globalconfig_cleanup, NULL);
+
+ /* TOC/TOU but merely attempts to prevent accidental cleanup. */
+ cl_assert(!git_fs_path_exists(FAKE_GLOBALCONFIG_NAME));
+ cl_must_pass(p_mkdir(FAKE_GLOBALCONFIG_NAME, 0777));
+ cl_git_pass(git_fs_path_prettify(&path, FAKE_GLOBALCONFIG_NAME, NULL));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+
+ if (out)
+ git_str_swap(out, &path);
+
git_str_dispose(&path);
}
diff --git a/tests/clar/clar_libgit2.h b/tests/clar/clar_libgit2.h
index d2d9da0aa..87f1f9bd9 100644
--- a/tests/clar/clar_libgit2.h
+++ b/tests/clar/clar_libgit2.h
@@ -233,13 +233,21 @@ 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.
- *
- * automatically configures cleanup function to restore the regular search
- * path, although you can call it explicitly if you wish (with NULL).
+/*
+ * set up a fake "home" directory -- automatically configures cleanup
+ * function to restore the home directory, although you can call it
+ * explicitly if you wish (with NULL).
+ */
+void cl_fake_homedir(git_str *);
+void cl_fake_homedir_cleanup(void *);
+
+/*
+ * set up a fake global configuration directory -- automatically
+ * configures cleanup function to restore the global config
+ * 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_globalconfig(git_str *);
+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/libgit2/config/include.c b/tests/libgit2/config/include.c
index 9328f3cf6..1b55fdc86 100644
--- a/tests/libgit2/config/include.c
+++ b/tests/libgit2/config/include.c
@@ -42,8 +42,13 @@ void test_config_include__absolute(void)
void test_config_include__homedir(void)
{
- cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
+ git_str homefile = GIT_STR_INIT;
+
+ cl_fake_homedir(&homefile);
+ cl_git_pass(git_str_joinpath(&homefile, homefile.ptr, "config-included"));
+
cl_git_mkfile("config-include-homedir", "[include]\npath = ~/config-included");
+ cl_git_mkfile(homefile.ptr, "[foo \"bar\"]\n\tbaz = huzzah\n");
cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir"));
@@ -53,6 +58,8 @@ void test_config_include__homedir(void)
cl_sandbox_set_search_path_defaults();
cl_git_pass(p_unlink("config-include-homedir"));
+
+ git_str_dispose(&homefile);
}
/* We need to pretend that the variables were defined where the file was included */
@@ -113,7 +120,8 @@ void test_config_include__missing(void)
void test_config_include__missing_homedir(void)
{
- cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
+ cl_fake_homedir(NULL);
+
cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
git_error_clear();
diff --git a/tests/libgit2/config/read.c b/tests/libgit2/config/read.c
index a2e668c20..ac6459b9e 100644
--- a/tests/libgit2/config/read.c
+++ b/tests/libgit2/config/read.c
@@ -728,14 +728,11 @@ void test_config_read__path(void)
{
git_config *cfg;
git_buf path = GIT_BUF_INIT;
- git_buf old_path = GIT_BUF_INIT;
git_str home_path = GIT_STR_INIT;
git_str expected_path = GIT_STR_INIT;
- cl_git_pass(p_mkdir("fakehome", 0777));
- cl_git_pass(git_fs_path_prettify(&home_path, "fakehome", NULL));
- cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &old_path));
- cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, home_path.ptr));
+ cl_fake_homedir(&home_path);
+
cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile");
cl_git_pass(git_fs_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL));
@@ -761,8 +758,6 @@ void test_config_read__path(void)
cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo");
cl_git_fail(git_config_get_path(&path, cfg, "some.path"));
- cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, old_path.ptr));
- git_buf_dispose(&old_path);
git_str_dispose(&home_path);
git_str_dispose(&expected_path);
git_config_free(cfg);
diff --git a/tests/libgit2/ignore/path.c b/tests/libgit2/ignore/path.c
index a574d1d79..17f28bc5d 100644
--- a/tests/libgit2/ignore/path.c
+++ b/tests/libgit2/ignore/path.c
@@ -286,14 +286,16 @@ void test_ignore_path__subdirectory_gitignore(void)
void test_ignore_path__expand_tilde_to_homedir(void)
{
+ git_str homefile = GIT_STR_INIT;
git_config *cfg;
assert_is_ignored(false, "example.global_with_tilde");
- cl_fake_home();
+ cl_fake_homedir(&homefile);
+ cl_git_pass(git_str_joinpath(&homefile, homefile.ptr, "globalexclude"));
/* construct fake home with fake global excludes */
- cl_git_mkfile("home/globalexclude", "# found me\n*.global_with_tilde\n");
+ cl_git_mkfile(homefile.ptr, "# 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,11 +307,13 @@ 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_homedir_cleanup(NULL);
git_attr_cache_flush(g_repo); /* must reset to pick up change */
assert_is_ignored(false, "example.global_with_tilde");
+
+ git_str_dispose(&homefile);
}
/* Ensure that the .gitignore in the subdirectory only affects
diff --git a/tests/libgit2/ignore/status.c b/tests/libgit2/ignore/status.c
index deb717590..a007774d7 100644
--- a/tests/libgit2/ignore/status.c
+++ b/tests/libgit2/ignore/status.c
@@ -363,6 +363,7 @@ void test_ignore_status__subdirectories_not_at_root(void)
void test_ignore_status__leading_slash_ignores(void)
{
+ git_str homedir = GIT_STR_INIT;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts;
static const char *paths_2[] = {
@@ -385,8 +386,9 @@ 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_homedir(&homedir);
+ cl_git_pass(git_str_joinpath(&homedir, homedir.ptr, ".gitignore"));
+ cl_git_mkfile(homedir.ptr, "/ignore_me\n");
{
git_config *cfg;
cl_git_pass(git_repository_config(&cfg, g_repo));
@@ -412,6 +414,8 @@ void test_ignore_status__leading_slash_ignores(void)
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
+
+ git_str_dispose(&homedir);
}
void test_ignore_status__multiple_leading_slash(void)
diff --git a/tests/libgit2/remote/httpproxy.c b/tests/libgit2/remote/httpproxy.c
index f62a2545b..6ba00b7c9 100644
--- a/tests/libgit2/remote/httpproxy.c
+++ b/tests/libgit2/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(NULL);
assert_global_config_match(NULL, NULL);
assert_global_config_match("http.proxy", "http://localhost:1/");
@@ -141,8 +141,6 @@ void test_remote_httpproxy__config_overrides_detached_remote(void)
assert_global_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/");
assert_global_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/");
assert_global_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/");
-
- cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
}
void test_remote_httpproxy__env(void)