Skip to content

Commit f32ec99

Browse files
committed
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.
1 parent 8945438 commit f32ec99

File tree

7 files changed

+98
-36
lines changed

7 files changed

+98
-36
lines changed

tests/clar/clar_libgit2.c

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "clar_libgit2.h"
22
#include "posix.h"
33
#include "fs_path.h"
4+
#include "futils.h"
45
#include "git2/sys/repository.h"
56

67
void cl_git_report_failure(
@@ -548,33 +549,77 @@ void clar__assert_equal_file(
548549
(size_t)expected_bytes, (size_t)total_bytes);
549550
}
550551

551-
static git_buf _cl_restore_home = GIT_BUF_INIT;
552+
#define FAKE_HOMEDIR_NAME "cl_fake_home"
552553

553-
void cl_fake_home_cleanup(void *payload)
554+
static git_buf _cl_restore_homedir = GIT_BUF_INIT;
555+
556+
void cl_fake_homedir_cleanup(void *payload)
554557
{
555558
GIT_UNUSED(payload);
556559

557-
if (_cl_restore_home.ptr) {
558-
cl_git_pass(git_libgit2_opts(
559-
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_home.ptr));
560-
git_buf_dispose(&_cl_restore_home);
560+
if (_cl_restore_homedir.ptr) {
561+
cl_git_pass(git_futils_rmdir_r(FAKE_HOMEDIR_NAME, NULL, GIT_RMDIR_REMOVE_FILES));
562+
563+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, _cl_restore_homedir.ptr));
564+
git_buf_dispose(&_cl_restore_homedir);
561565
}
562566
}
563567

564-
void cl_fake_home(void)
568+
void cl_fake_homedir(git_str *out)
565569
{
566570
git_str path = GIT_STR_INIT;
567571

568572
cl_git_pass(git_libgit2_opts(
569-
GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_home));
573+
GIT_OPT_GET_HOMEDIR, &_cl_restore_homedir));
574+
575+
cl_set_cleanup(cl_fake_homedir_cleanup, NULL);
576+
577+
/* TOC/TOU but merely attempts to prevent accidental cleanup. */
578+
cl_assert(!git_fs_path_exists(FAKE_HOMEDIR_NAME));
579+
cl_must_pass(p_mkdir(FAKE_HOMEDIR_NAME, 0777));
580+
cl_git_pass(git_fs_path_prettify(&path, FAKE_HOMEDIR_NAME, NULL));
581+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, path.ptr));
570582

571-
cl_set_cleanup(cl_fake_home_cleanup, NULL);
583+
if (out)
584+
git_str_swap(out, &path);
585+
586+
git_str_dispose(&path);
587+
}
588+
589+
#define FAKE_GLOBALCONFIG_NAME "cl_fake_global"
590+
591+
static git_buf _cl_restore_globalconfig = GIT_BUF_INIT;
592+
593+
void cl_fake_globalconfig_cleanup(void *payload)
594+
{
595+
GIT_UNUSED(payload);
596+
597+
if (_cl_restore_globalconfig.ptr) {
598+
cl_git_pass(git_futils_rmdir_r(FAKE_GLOBALCONFIG_NAME, NULL, GIT_RMDIR_REMOVE_FILES));
599+
600+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_HOMEDIR, _cl_restore_globalconfig.ptr));
601+
git_buf_dispose(&_cl_restore_globalconfig);
602+
}
603+
}
604+
605+
void cl_fake_globalconfig(git_str *out)
606+
{
607+
git_str path = GIT_STR_INIT;
572608

573-
if (!git_fs_path_exists("home"))
574-
cl_must_pass(p_mkdir("home", 0777));
575-
cl_git_pass(git_fs_path_prettify(&path, "home", NULL));
576609
cl_git_pass(git_libgit2_opts(
577-
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
610+
GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_globalconfig));
611+
612+
cl_set_cleanup(cl_fake_globalconfig_cleanup, NULL);
613+
614+
/* TOC/TOU but merely attempts to prevent accidental cleanup. */
615+
cl_assert(!git_fs_path_exists(FAKE_GLOBALCONFIG_NAME));
616+
cl_must_pass(p_mkdir(FAKE_GLOBALCONFIG_NAME, 0777));
617+
cl_git_pass(git_fs_path_prettify(&path, FAKE_GLOBALCONFIG_NAME, NULL));
618+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
619+
620+
if (out)
621+
git_str_swap(out, &path);
622+
578623
git_str_dispose(&path);
579624
}
580625

tests/clar/clar_libgit2.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,21 @@ int cl_repo_get_bool(git_repository *repo, const char *cfg);
233233

234234
void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
235235

236-
/* set up a fake "home" directory and set libgit2 GLOBAL search path.
237-
*
238-
* automatically configures cleanup function to restore the regular search
239-
* path, although you can call it explicitly if you wish (with NULL).
236+
/*
237+
* set up a fake "home" directory -- automatically configures cleanup
238+
* function to restore the home directory, although you can call it
239+
* explicitly if you wish (with NULL).
240+
*/
241+
void cl_fake_homedir(git_str *);
242+
void cl_fake_homedir_cleanup(void *);
243+
244+
/*
245+
* set up a fake global configuration directory -- automatically
246+
* configures cleanup function to restore the global config
247+
* although you can call it explicitly if you wish (with NULL).
240248
*/
241-
void cl_fake_home(void);
242-
void cl_fake_home_cleanup(void *);
249+
void cl_fake_globalconfig(git_str *);
250+
void cl_fake_globalconfig_cleanup(void *);
243251

244252
void cl_sandbox_set_search_path_defaults(void);
245253
void cl_sandbox_disable_ownership_validation(void);

tests/libgit2/config/include.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ void test_config_include__absolute(void)
4242

4343
void test_config_include__homedir(void)
4444
{
45-
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
45+
git_str homefile = GIT_STR_INIT;
46+
47+
cl_fake_homedir(&homefile);
48+
cl_git_pass(git_str_joinpath(&homefile, homefile.ptr, "config-included"));
49+
4650
cl_git_mkfile("config-include-homedir", "[include]\npath = ~/config-included");
51+
cl_git_mkfile(homefile.ptr, "[foo \"bar\"]\n\tbaz = huzzah\n");
4752

4853
cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir"));
4954

@@ -53,6 +58,8 @@ void test_config_include__homedir(void)
5358
cl_sandbox_set_search_path_defaults();
5459

5560
cl_git_pass(p_unlink("config-include-homedir"));
61+
62+
git_str_dispose(&homefile);
5663
}
5764

5865
/* We need to pretend that the variables were defined where the file was included */
@@ -113,7 +120,8 @@ void test_config_include__missing(void)
113120

114121
void test_config_include__missing_homedir(void)
115122
{
116-
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
123+
cl_fake_homedir(NULL);
124+
117125
cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
118126

119127
git_error_clear();

tests/libgit2/config/read.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -728,14 +728,11 @@ void test_config_read__path(void)
728728
{
729729
git_config *cfg;
730730
git_buf path = GIT_BUF_INIT;
731-
git_buf old_path = GIT_BUF_INIT;
732731
git_str home_path = GIT_STR_INIT;
733732
git_str expected_path = GIT_STR_INIT;
734733

735-
cl_git_pass(p_mkdir("fakehome", 0777));
736-
cl_git_pass(git_fs_path_prettify(&home_path, "fakehome", NULL));
737-
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &old_path));
738-
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, home_path.ptr));
734+
cl_fake_homedir(&home_path);
735+
739736
cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile");
740737
cl_git_pass(git_fs_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL));
741738

@@ -761,8 +758,6 @@ void test_config_read__path(void)
761758
cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo");
762759
cl_git_fail(git_config_get_path(&path, cfg, "some.path"));
763760

764-
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, old_path.ptr));
765-
git_buf_dispose(&old_path);
766761
git_str_dispose(&home_path);
767762
git_str_dispose(&expected_path);
768763
git_config_free(cfg);

tests/libgit2/ignore/path.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,16 @@ void test_ignore_path__subdirectory_gitignore(void)
286286

287287
void test_ignore_path__expand_tilde_to_homedir(void)
288288
{
289+
git_str homefile = GIT_STR_INIT;
289290
git_config *cfg;
290291

291292
assert_is_ignored(false, "example.global_with_tilde");
292293

293-
cl_fake_home();
294+
cl_fake_homedir(&homefile);
295+
cl_git_pass(git_str_joinpath(&homefile, homefile.ptr, "globalexclude"));
294296

295297
/* construct fake home with fake global excludes */
296-
cl_git_mkfile("home/globalexclude", "# found me\n*.global_with_tilde\n");
298+
cl_git_mkfile(homefile.ptr, "# found me\n*.global_with_tilde\n");
297299

298300
cl_git_pass(git_repository_config(&cfg, g_repo));
299301
cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude"));
@@ -305,11 +307,13 @@ void test_ignore_path__expand_tilde_to_homedir(void)
305307

306308
cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
307309

308-
cl_fake_home_cleanup(NULL);
310+
cl_fake_homedir_cleanup(NULL);
309311

310312
git_attr_cache_flush(g_repo); /* must reset to pick up change */
311313

312314
assert_is_ignored(false, "example.global_with_tilde");
315+
316+
git_str_dispose(&homefile);
313317
}
314318

315319
/* Ensure that the .gitignore in the subdirectory only affects

tests/libgit2/ignore/status.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ void test_ignore_status__subdirectories_not_at_root(void)
363363

364364
void test_ignore_status__leading_slash_ignores(void)
365365
{
366+
git_str homedir = GIT_STR_INIT;
366367
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
367368
status_entry_counts counts;
368369
static const char *paths_2[] = {
@@ -385,8 +386,9 @@ void test_ignore_status__leading_slash_ignores(void)
385386

386387
make_test_data(test_repo_1, test_files_1);
387388

388-
cl_fake_home();
389-
cl_git_mkfile("home/.gitignore", "/ignore_me\n");
389+
cl_fake_homedir(&homedir);
390+
cl_git_pass(git_str_joinpath(&homedir, homedir.ptr, ".gitignore"));
391+
cl_git_mkfile(homedir.ptr, "/ignore_me\n");
390392
{
391393
git_config *cfg;
392394
cl_git_pass(git_repository_config(&cfg, g_repo));
@@ -412,6 +414,8 @@ void test_ignore_status__leading_slash_ignores(void)
412414
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
413415
cl_assert_equal_i(0, counts.wrong_status_flags_count);
414416
cl_assert_equal_i(0, counts.wrong_sorted_path);
417+
418+
git_str_dispose(&homedir);
415419
}
416420

417421
void test_ignore_status__multiple_leading_slash(void)

tests/libgit2/remote/httpproxy.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void assert_global_config_match(const char *config, const char *expected)
132132

133133
void test_remote_httpproxy__config_overrides_detached_remote(void)
134134
{
135-
cl_fake_home();
135+
cl_fake_globalconfig(NULL);
136136

137137
assert_global_config_match(NULL, NULL);
138138
assert_global_config_match("http.proxy", "http://localhost:1/");
@@ -141,8 +141,6 @@ void test_remote_httpproxy__config_overrides_detached_remote(void)
141141
assert_global_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/");
142142
assert_global_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/");
143143
assert_global_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/");
144-
145-
cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
146144
}
147145

148146
void test_remote_httpproxy__env(void)

0 commit comments

Comments
 (0)