diff options
Diffstat (limited to 'tests/config')
-rw-r--r-- | tests/config/config_helpers.c | 28 | ||||
-rw-r--r-- | tests/config/config_helpers.h | 4 | ||||
-rw-r--r-- | tests/config/global.c | 15 | ||||
-rw-r--r-- | tests/config/include.c | 2 | ||||
-rw-r--r-- | tests/config/multivar.c | 6 | ||||
-rw-r--r-- | tests/config/read.c | 2 | ||||
-rw-r--r-- | tests/config/refresh.c | 9 | ||||
-rw-r--r-- | tests/config/rename.c | 85 | ||||
-rw-r--r-- | tests/config/snapshot.c | 64 | ||||
-rw-r--r-- | tests/config/validkeyname.c | 20 | ||||
-rw-r--r-- | tests/config/write.c | 28 |
11 files changed, 222 insertions, 41 deletions
diff --git a/tests/config/config_helpers.c b/tests/config/config_helpers.c index 53bd945a0..35da720e0 100644 --- a/tests/config/config_helpers.c +++ b/tests/config/config_helpers.c @@ -35,3 +35,31 @@ void assert_config_entry_value( cl_assert_equal_s(expected_value, out); } + +static int count_config_entries_cb( + const git_config_entry *entry, + void *payload) +{ + int *how_many = (int *)payload; + + GIT_UNUSED(entry); + + (*how_many)++; + + return 0; +} + +int count_config_entries_match(git_repository *repo, const char *pattern) +{ + git_config *config; + int how_many = 0; + + cl_git_pass(git_repository_config(&config, repo)); + + cl_assert_equal_i(0, git_config_foreach_match( + config, pattern, count_config_entries_cb, &how_many)); + + git_config_free(config); + + return how_many; +} diff --git a/tests/config/config_helpers.h b/tests/config/config_helpers.h index b887b3d38..440645730 100644 --- a/tests/config/config_helpers.h +++ b/tests/config/config_helpers.h @@ -7,3 +7,7 @@ extern void assert_config_entry_value( git_repository *repo, const char *name, const char *expected_value); + +extern int count_config_entries_match( + git_repository *repo, + const char *pattern); diff --git a/tests/config/global.c b/tests/config/global.c index d5f95f504..fc471f90d 100644 --- a/tests/config/global.c +++ b/tests/config/global.c @@ -6,18 +6,17 @@ void test_config_global__initialize(void) { git_buf path = GIT_BUF_INIT; - cl_assert_equal_i(0, p_mkdir("home", 0777)); + cl_git_pass(git_futils_mkdir_r("home", NULL, 0777)); cl_git_pass(git_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); - cl_assert_equal_i(0, p_mkdir("xdg", 0777)); - cl_assert_equal_i(0, p_mkdir("xdg/git", 0777)); + cl_git_pass(git_futils_mkdir_r("xdg/git", NULL, 0777)); cl_git_pass(git_path_prettify(&path, "xdg/git", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); - cl_assert_equal_i(0, p_mkdir("etc", 0777)); + cl_git_pass(git_futils_mkdir_r("etc", NULL, 0777)); cl_git_pass(git_path_prettify(&path, "etc", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); @@ -27,13 +26,7 @@ void test_config_global__initialize(void) void test_config_global__cleanup(void) { - cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_git_pass(git_futils_rmdir_r("xdg", NULL, GIT_RMDIR_REMOVE_FILES)); - cl_git_pass(git_futils_rmdir_r("etc", NULL, GIT_RMDIR_REMOVE_FILES)); - - git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, NULL); - git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, NULL); - git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL); + cl_sandbox_set_search_path_defaults(); } void test_config_global__open_global(void) diff --git a/tests/config/include.c b/tests/config/include.c index 535573808..58bc690ff 100644 --- a/tests/config/include.c +++ b/tests/config/include.c @@ -47,6 +47,8 @@ void test_config_include__homedir(void) cl_assert_equal_s(str, "huzzah"); git_config_free(cfg); + + cl_sandbox_set_search_path_defaults(); } void test_config_include__refresh(void) diff --git a/tests/config/multivar.c b/tests/config/multivar.c index afdb1e5f4..015008992 100644 --- a/tests/config/multivar.c +++ b/tests/config/multivar.c @@ -231,13 +231,13 @@ void test_config_multivar__delete(void) n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); - cl_assert(n == 2); + cl_assert_equal_i(2, n); cl_git_pass(git_config_delete_multivar(cfg, _name, "github")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); - cl_assert(n == 1); + cl_assert_equal_i(1, n); git_config_free(cfg); @@ -245,7 +245,7 @@ void test_config_multivar__delete(void) n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); - cl_assert(n == 1); + cl_assert_equal_i(1, n); git_config_free(cfg); } diff --git a/tests/config/read.c b/tests/config/read.c index abc088d59..25672729f 100644 --- a/tests/config/read.c +++ b/tests/config/read.c @@ -247,7 +247,7 @@ void test_config_read__foreach(void) count = 3; cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count)); - cl_assert_equal_i(GIT_EUSER, ret); + cl_assert_equal_i(-100, ret); git_config_free(cfg); } diff --git a/tests/config/refresh.c b/tests/config/refresh.c index 99d677f0e..08cd45b95 100644 --- a/tests/config/refresh.c +++ b/tests/config/refresh.c @@ -26,9 +26,6 @@ void test_config_refresh__update_value(void) cl_git_rewritefile(TEST_FILE, "[section]\n\tvalue = 10\n\n"); - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(1, v); - cl_git_pass(git_config_refresh(cfg)); cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); @@ -53,9 +50,9 @@ void test_config_refresh__delete_value(void) cl_git_rewritefile(TEST_FILE, "[section]\n\tnewval = 10\n\n"); - cl_git_pass(git_config_get_int32(&v, cfg, "section.value")); - cl_assert_equal_i(1, v); - cl_git_fail(git_config_get_int32(&v, cfg, "section.newval")); + cl_git_fail_with(GIT_ENOTFOUND, git_config_get_int32(&v, cfg, "section.value")); + + cl_git_pass(git_config_get_int32(&v, cfg, "section.newval")); cl_git_pass(git_config_refresh(cfg)); diff --git a/tests/config/rename.c b/tests/config/rename.c new file mode 100644 index 000000000..db07c798f --- /dev/null +++ b/tests/config/rename.c @@ -0,0 +1,85 @@ +#include "clar_libgit2.h" +#include "config.h" + +static git_repository *g_repo = NULL; +static git_config *g_config = NULL; + +void test_config_rename__initialize(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_git_pass(git_repository_config(&g_config, g_repo)); +} + +void test_config_rename__cleanup(void) +{ + git_config_free(g_config); + g_config = NULL; + + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +void test_config_rename__can_rename(void) +{ + const git_config_entry *ce; + + cl_git_pass(git_config_get_entry( + &ce, g_config, "branch.track-local.remote")); + cl_assert_equal_s(".", ce->value); + + cl_git_fail(git_config_get_entry( + &ce, g_config, "branch.local-track.remote")); + + cl_git_pass(git_config_rename_section( + g_repo, "branch.track-local", "branch.local-track")); + + cl_git_pass(git_config_get_entry( + &ce, g_config, "branch.local-track.remote")); + cl_assert_equal_s(".", ce->value); + + cl_git_fail(git_config_get_entry( + &ce, g_config, "branch.track-local.remote")); +} + +void test_config_rename__prevent_overwrite(void) +{ + const git_config_entry *ce; + + cl_git_pass(git_config_set_string( + g_config, "branch.local-track.remote", "yellow")); + + cl_git_pass(git_config_get_entry( + &ce, g_config, "branch.local-track.remote")); + cl_assert_equal_s("yellow", ce->value); + + cl_git_pass(git_config_rename_section( + g_repo, "branch.track-local", "branch.local-track")); + + cl_git_pass(git_config_get_entry( + &ce, g_config, "branch.local-track.remote")); + cl_assert_equal_s(".", ce->value); + + /* so, we don't currently prevent overwrite... */ + /* { + const git_error *err; + cl_assert((err = giterr_last()) != NULL); + cl_assert(err->message != NULL); + } */ +} + +static void assert_invalid_config_section_name( + git_repository *repo, const char *name) +{ + cl_git_fail_with( + git_config_rename_section(repo, "branch.remoteless", name), + GIT_EINVALIDSPEC); +} + +void test_config_rename__require_a_valid_new_name(void) +{ + assert_invalid_config_section_name(g_repo, ""); + assert_invalid_config_section_name(g_repo, "bra\nch"); + assert_invalid_config_section_name(g_repo, "branc#"); + assert_invalid_config_section_name(g_repo, "bra\nch.duh"); + assert_invalid_config_section_name(g_repo, "branc#.duh"); +} diff --git a/tests/config/snapshot.c b/tests/config/snapshot.c new file mode 100644 index 000000000..c9f15921a --- /dev/null +++ b/tests/config/snapshot.c @@ -0,0 +1,64 @@ +#include "clar_libgit2.h" + +void test_config_snapshot__create_snapshot(void) +{ + int32_t tmp; + git_config *cfg, *snapshot; + const char *filename = "config-ext-change"; + + cl_git_mkfile(filename, "[old]\nvalue = 5\n"); + + cl_git_pass(git_config_open_ondisk(&cfg, filename)); + + cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); + cl_assert_equal_i(5, tmp); + + cl_git_pass(git_config_snapshot(&snapshot, cfg)); + + /* Change the value on the file itself (simulate external process) */ + cl_git_mkfile(filename, "[old]\nvalue = 56\n"); + + cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); + cl_assert_equal_i(56, tmp); + + cl_git_pass(git_config_get_int32(&tmp, snapshot, "old.value")); + cl_assert_equal_i(5, tmp); + + git_config_free(snapshot); + git_config_free(cfg); +} + +static int count_me(const git_config_entry *entry, void *payload) +{ + int *n = (int *) payload; + + GIT_UNUSED(entry); + + (*n)++; + + return 0; +} + +void test_config_snapshot__multivar(void) +{ + int count = 0; + git_config *cfg, *snapshot; + const char *filename = "config-file"; + + cl_git_mkfile(filename, "[old]\nvalue = 5\nvalue = 6\n"); + + cl_git_pass(git_config_open_ondisk(&cfg, filename)); + cl_git_pass(git_config_get_multivar_foreach(cfg, "old.value", NULL, count_me, &count)); + + cl_assert_equal_i(2, count); + + cl_git_pass(git_config_snapshot(&snapshot, cfg)); + git_config_free(cfg); + + count = 0; + cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count)); + + cl_assert_equal_i(2, count); + + git_config_free(snapshot); +} diff --git a/tests/config/validkeyname.c b/tests/config/validkeyname.c index 33699737b..0ef4a9ae3 100644 --- a/tests/config/validkeyname.c +++ b/tests/config/validkeyname.c @@ -46,23 +46,3 @@ void test_config_validkeyname__accessing_requires_a_valid_name(void) assert_invalid_config_key_name("dif.dir\nstat.lines"); assert_invalid_config_key_name("dif.dirstat.li\nes"); } - -static void assert_invalid_config_section_name(git_repository *repo, const char *name) -{ - cl_git_fail_with(git_config_rename_section(repo, "branch.remoteless", name), GIT_EINVALIDSPEC); -} - -void test_config_validkeyname__renaming_a_section_requires_a_valid_name(void) -{ - git_repository *repo; - - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - - assert_invalid_config_section_name(repo, ""); - assert_invalid_config_section_name(repo, "bra\nch"); - assert_invalid_config_section_name(repo, "branc#"); - assert_invalid_config_section_name(repo, "bra\nch.duh"); - assert_invalid_config_section_name(repo, "branc#.duh"); - - git_repository_free(repo); -} diff --git a/tests/config/write.c b/tests/config/write.c index 15f750dc0..402be9317 100644 --- a/tests/config/write.c +++ b/tests/config/write.c @@ -303,3 +303,31 @@ void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void) git_config_free(cfg); } + +void test_config_write__outside_change(void) +{ + int32_t tmp; + git_config *cfg; + const char *filename = "config-ext-change"; + + cl_git_mkfile(filename, "[old]\nvalue = 5\n"); + + cl_git_pass(git_config_open_ondisk(&cfg, filename)); + + cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); + + /* Change the value on the file itself (simulate external process) */ + cl_git_mkfile(filename, "[old]\nvalue = 6\n"); + + cl_git_pass(git_config_set_int32(cfg, "new.value", 7)); + + cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); + cl_assert_equal_i(6, tmp); + + cl_git_pass(git_config_refresh(cfg)); + + cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); + cl_assert_equal_i(6, tmp); + + git_config_free(cfg); +} |