Commit f32ec999 by Edward Thomson

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.
parent 89454382
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "posix.h" #include "posix.h"
#include "fs_path.h" #include "fs_path.h"
#include "futils.h"
#include "git2/sys/repository.h" #include "git2/sys/repository.h"
void cl_git_report_failure( void cl_git_report_failure(
...@@ -548,33 +549,77 @@ void clar__assert_equal_file( ...@@ -548,33 +549,77 @@ void clar__assert_equal_file(
(size_t)expected_bytes, (size_t)total_bytes); (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); GIT_UNUSED(payload);
if (_cl_restore_home.ptr) { if (_cl_restore_homedir.ptr) {
cl_git_pass(git_libgit2_opts( cl_git_pass(git_futils_rmdir_r(FAKE_HOMEDIR_NAME, NULL, GIT_RMDIR_REMOVE_FILES));
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_home.ptr));
git_buf_dispose(&_cl_restore_home); 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; git_str path = GIT_STR_INIT;
cl_git_pass(git_libgit2_opts( 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));
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);
cl_set_cleanup(cl_fake_home_cleanup, NULL); 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( 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); git_str_dispose(&path);
} }
......
...@@ -233,13 +233,21 @@ int cl_repo_get_bool(git_repository *repo, const char *cfg); ...@@ -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); 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
* automatically configures cleanup function to restore the regular search * function to restore the home directory, although you can call it
* path, although you can call it explicitly if you wish (with NULL). * 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_globalconfig(git_str *);
void cl_fake_home_cleanup(void *); void cl_fake_globalconfig_cleanup(void *);
void cl_sandbox_set_search_path_defaults(void); void cl_sandbox_set_search_path_defaults(void);
void cl_sandbox_disable_ownership_validation(void); void cl_sandbox_disable_ownership_validation(void);
......
...@@ -42,8 +42,13 @@ void test_config_include__absolute(void) ...@@ -42,8 +42,13 @@ void test_config_include__absolute(void)
void test_config_include__homedir(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("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")); cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir"));
...@@ -53,6 +58,8 @@ void test_config_include__homedir(void) ...@@ -53,6 +58,8 @@ void test_config_include__homedir(void)
cl_sandbox_set_search_path_defaults(); cl_sandbox_set_search_path_defaults();
cl_git_pass(p_unlink("config-include-homedir")); 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 */ /* We need to pretend that the variables were defined where the file was included */
...@@ -113,7 +120,8 @@ void test_config_include__missing(void) ...@@ -113,7 +120,8 @@ void test_config_include__missing(void)
void test_config_include__missing_homedir(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"); cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
git_error_clear(); git_error_clear();
......
...@@ -728,14 +728,11 @@ void test_config_read__path(void) ...@@ -728,14 +728,11 @@ void test_config_read__path(void)
{ {
git_config *cfg; git_config *cfg;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
git_buf old_path = GIT_BUF_INIT;
git_str home_path = GIT_STR_INIT; git_str home_path = GIT_STR_INIT;
git_str expected_path = GIT_STR_INIT; git_str expected_path = GIT_STR_INIT;
cl_git_pass(p_mkdir("fakehome", 0777)); cl_fake_homedir(&home_path);
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_git_mkfile("./testconfig", "[some]\n path = ~/somefile"); cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile");
cl_git_pass(git_fs_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL)); 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) ...@@ -761,8 +758,6 @@ void test_config_read__path(void)
cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo"); cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo");
cl_git_fail(git_config_get_path(&path, cfg, "some.path")); 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(&home_path);
git_str_dispose(&expected_path); git_str_dispose(&expected_path);
git_config_free(cfg); git_config_free(cfg);
......
...@@ -286,14 +286,16 @@ void test_ignore_path__subdirectory_gitignore(void) ...@@ -286,14 +286,16 @@ void test_ignore_path__subdirectory_gitignore(void)
void test_ignore_path__expand_tilde_to_homedir(void) void test_ignore_path__expand_tilde_to_homedir(void)
{ {
git_str homefile = GIT_STR_INIT;
git_config *cfg; git_config *cfg;
assert_is_ignored(false, "example.global_with_tilde"); 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 */ /* 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_repository_config(&cfg, g_repo));
cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude")); cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude"));
...@@ -305,11 +307,13 @@ void test_ignore_path__expand_tilde_to_homedir(void) ...@@ -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_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 */ git_attr_cache_flush(g_repo); /* must reset to pick up change */
assert_is_ignored(false, "example.global_with_tilde"); assert_is_ignored(false, "example.global_with_tilde");
git_str_dispose(&homefile);
} }
/* Ensure that the .gitignore in the subdirectory only affects /* Ensure that the .gitignore in the subdirectory only affects
......
...@@ -363,6 +363,7 @@ void test_ignore_status__subdirectories_not_at_root(void) ...@@ -363,6 +363,7 @@ void test_ignore_status__subdirectories_not_at_root(void)
void test_ignore_status__leading_slash_ignores(void) void test_ignore_status__leading_slash_ignores(void)
{ {
git_str homedir = GIT_STR_INIT;
git_status_options opts = GIT_STATUS_OPTIONS_INIT; git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts; status_entry_counts counts;
static const char *paths_2[] = { static const char *paths_2[] = {
...@@ -385,8 +386,9 @@ void test_ignore_status__leading_slash_ignores(void) ...@@ -385,8 +386,9 @@ void test_ignore_status__leading_slash_ignores(void)
make_test_data(test_repo_1, test_files_1); make_test_data(test_repo_1, test_files_1);
cl_fake_home(); cl_fake_homedir(&homedir);
cl_git_mkfile("home/.gitignore", "/ignore_me\n"); cl_git_pass(git_str_joinpath(&homedir, homedir.ptr, ".gitignore"));
cl_git_mkfile(homedir.ptr, "/ignore_me\n");
{ {
git_config *cfg; git_config *cfg;
cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_repository_config(&cfg, g_repo));
...@@ -412,6 +414,8 @@ void test_ignore_status__leading_slash_ignores(void) ...@@ -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(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path); cl_assert_equal_i(0, counts.wrong_sorted_path);
git_str_dispose(&homedir);
} }
void test_ignore_status__multiple_leading_slash(void) void test_ignore_status__multiple_leading_slash(void)
......
...@@ -132,7 +132,7 @@ static void assert_global_config_match(const char *config, const char *expected) ...@@ -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) 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(NULL, NULL);
assert_global_config_match("http.proxy", "http://localhost:1/"); assert_global_config_match("http.proxy", "http://localhost:1/");
...@@ -141,8 +141,6 @@ void test_remote_httpproxy__config_overrides_detached_remote(void) ...@@ -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:4/");
assert_global_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/"); 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/"); 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) void test_remote_httpproxy__env(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment