Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryorah <yoram.harmelin@gmail.com>2012-09-10 14:11:02 +0400
committeryorah <yoram.harmelin@gmail.com>2012-10-23 14:48:38 +0400
commita1abe66aca3625eec1cabb2e93cf8df0be1b63f0 (patch)
tree17de8c31afbdb3398a391e1fcede5010394dec85 /tests-clar/config
parentf8ede94808348ac12db1d5dd91e5f66624d8b40d (diff)
Add config level support in the config API
Added `struct git_config_entry`: a git_config_entry contains the key, the value, and the config file level from which a config element was found. Added `git_config_open_level`: build a single-level focused config object from a multi-level one. We are now storing `git_config_entry`s in the khash of the config_file
Diffstat (limited to 'tests-clar/config')
-rw-r--r--tests-clar/config/configlevel.c59
-rw-r--r--tests-clar/config/multivar.c10
-rw-r--r--tests-clar/config/new.c9
-rw-r--r--tests-clar/config/read.c174
-rw-r--r--tests-clar/config/stress.c27
-rw-r--r--tests-clar/config/write.c76
6 files changed, 292 insertions, 63 deletions
diff --git a/tests-clar/config/configlevel.c b/tests-clar/config/configlevel.c
new file mode 100644
index 000000000..d947856fa
--- /dev/null
+++ b/tests-clar/config/configlevel.c
@@ -0,0 +1,59 @@
+#include "clar_libgit2.h"
+
+void test_config_configlevel__adding_the_same_level_twice_returns_EEXISTS(void)
+{
+ int error;
+ git_config *cfg;
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
+ GIT_CONFIG_LEVEL_LOCAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+ error = git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0);
+
+ cl_git_fail(error);
+ cl_assert_equal_i(GIT_EEXISTS, error);
+
+ git_config_free(cfg);
+}
+
+void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(void)
+{
+ git_config *cfg;
+ const char *s;
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
+ GIT_CONFIG_LEVEL_LOCAL, 1));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
+ GIT_CONFIG_LEVEL_LOCAL, 1));
+
+ cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal"));
+ cl_assert_equal_s("don't find me!", s);
+
+ git_config_free(cfg);
+}
+
+void test_config_configlevel__can_read_from_a_single_level_focused_file_after_parent_config_has_been_freed(void)
+{
+ git_config *cfg;
+ git_config *single_level_cfg;
+ const char *s;
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
+ GIT_CONFIG_LEVEL_LOCAL, 0));
+
+ cl_git_pass(git_config_open_level(&single_level_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL));
+
+ git_config_free(cfg);
+
+ cl_git_pass(git_config_get_string(&s, single_level_cfg, "core.stringglobal"));
+ cl_assert_equal_s("don't find me!", s);
+
+ git_config_free(single_level_cfg);
+}
diff --git a/tests-clar/config/multivar.c b/tests-clar/config/multivar.c
index 3b40cd09a..26537e20a 100644
--- a/tests-clar/config/multivar.c
+++ b/tests-clar/config/multivar.c
@@ -12,13 +12,11 @@ void test_config_multivar__cleanup(void)
cl_fixture_cleanup("config");
}
-static int mv_read_cb(const char *name, const char *value, void *data)
+static int mv_read_cb(const git_config_entry *entry, void *data)
{
int *n = (int *) data;
- GIT_UNUSED(value);
-
- if (!strcmp(name, _name))
+ if (!strcmp(entry->name, _name))
(*n)++;
return 0;
@@ -37,11 +35,11 @@ void test_config_multivar__foreach(void)
git_config_free(cfg);
}
-static int cb(const char *val, void *data)
+static int cb(const git_config_entry *entry, void *data)
{
int *n = (int *) data;
- GIT_UNUSED(val);
+ GIT_UNUSED(entry);
(*n)++;
diff --git a/tests-clar/config/new.c b/tests-clar/config/new.c
index fae3ce941..6bd719fba 100644
--- a/tests-clar/config/new.c
+++ b/tests-clar/config/new.c
@@ -9,21 +9,16 @@
void test_config_new__write_new_config(void)
{
const char *out;
- struct git_config_file *file;
git_config *config;
- cl_git_pass(git_config_file__ondisk(&file, TEST_CONFIG));
- cl_git_pass(git_config_new(&config));
- cl_git_pass(git_config_add_file(config, file, 0));
+ cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_set_string(config, "color.ui", "auto"));
cl_git_pass(git_config_set_string(config, "core.editor", "ed"));
git_config_free(config);
- cl_git_pass(git_config_file__ondisk(&file, TEST_CONFIG));
- cl_git_pass(git_config_new(&config));
- cl_git_pass(git_config_add_file(config, file, 0));
+ cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&out, config, "color.ui"));
cl_assert_equal_s(out, "auto");
diff --git a/tests-clar/config/read.c b/tests-clar/config/read.c
index fcd22463d..cf781e6bf 100644
--- a/tests-clar/config/read.c
+++ b/tests-clar/config/read.c
@@ -191,22 +191,24 @@ void test_config_read__escaping_quotes(void)
git_config_free(cfg);
}
-static int count_cfg_entries(
- const char *var_name, const char *value, void *payload)
+static int count_cfg_entries_and_compare_levels(
+ const git_config_entry *entry, void *payload)
{
int *count = payload;
- GIT_UNUSED(var_name);
- GIT_UNUSED(value);
+
+ if (!strcmp(entry->value, "7") || !strcmp(entry->value, "17"))
+ cl_assert(entry->level == GIT_CONFIG_LEVEL_GLOBAL);
+ else
+ cl_assert(entry->level == GIT_CONFIG_LEVEL_SYSTEM);
+
(*count)++;
return 0;
}
-static int cfg_callback_countdown(
- const char *var_name, const char *value, void *payload)
+static int cfg_callback_countdown(const git_config_entry *entry, void *payload)
{
int *count = payload;
- GIT_UNUSED(var_name);
- GIT_UNUSED(value);
+ GIT_UNUSED(entry);
(*count)--;
if (*count == 0)
return -100;
@@ -218,11 +220,15 @@ void test_config_read__foreach(void)
git_config *cfg;
int count, ret;
- cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
+ GIT_CONFIG_LEVEL_SYSTEM, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
count = 0;
- cl_git_pass(git_config_foreach(cfg, count_cfg_entries, &count));
- cl_assert_equal_i(5, count);
+ cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
+ cl_assert_equal_i(7, count);
count = 3;
cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
@@ -231,6 +237,14 @@ void test_config_read__foreach(void)
git_config_free(cfg);
}
+static int count_cfg_entries(const git_config_entry *entry, void *payload)
+{
+ int *count = payload;
+ GIT_UNUSED(entry);
+ (*count)++;
+ return 0;
+}
+
void test_config_read__foreach_match(void)
{
git_config *cfg;
@@ -282,31 +296,129 @@ void test_config_read__whitespace_not_required_around_assignment(void)
git_config_free(cfg);
}
-#if 0
+void test_config_read__read_git_config_entry(void)
+{
+ git_config *cfg;
+ const git_config_entry *entry;
-BEGIN_TEST(config10, "a repo's config overrides the global config")
- git_repository *repo;
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
+ GIT_CONFIG_LEVEL_SYSTEM, 0));
+
+ cl_git_pass(git_config_get_config_entry(&entry, cfg, "core.dummy2"));
+ cl_assert_equal_s("core.dummy2", entry->name);
+ cl_assert_equal_s("42", entry->value);
+ cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level);
+
+ git_config_free(cfg);
+}
+
+/*
+ * At the beginning of the test:
+ * - config9 has: core.dummy2=42
+ * - config15 has: core.dummy2=7
+ * - config16 has: core.dummy2=28
+ */
+void test_config_read__local_config_overrides_global_config_overrides_system_config(void)
+{
git_config *cfg;
- int32_t version;
+ int32_t i;
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
+ GIT_CONFIG_LEVEL_SYSTEM, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
+ GIT_CONFIG_LEVEL_LOCAL, 0));
+
+ cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
+ cl_assert_equal_i(28, i);
+
+ git_config_free(cfg);
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
+ GIT_CONFIG_LEVEL_SYSTEM, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+
+ cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
+ cl_assert_equal_i(7, i);
- cl_git_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
- cl_git_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
- cl_git_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &version));
- cl_assert(version == 0);
git_config_free(cfg);
- git_repository_free(repo);
-END_TEST
+}
-BEGIN_TEST(config11, "fall back to the global config")
- git_repository *repo;
+/*
+ * At the beginning of the test:
+ * - config9 has: core.global does not exist
+ * - config15 has: core.global=17
+ * - config16 has: core.global=29
+ *
+ * And also:
+ * - config9 has: core.system does not exist
+ * - config15 has: core.system does not exist
+ * - config16 has: core.system=11
+ */
+void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void)
+{
git_config *cfg;
- int32_t num;
+ int32_t i;
- cl_git_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
- cl_git_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
- cl_git_pass(git_config_get_int32(cfg, "core.something", &num));
- cl_assert(num == 2);
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
+ GIT_CONFIG_LEVEL_SYSTEM, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
+ GIT_CONFIG_LEVEL_LOCAL, 0));
+
+ cl_git_pass(git_config_get_int32(&i, cfg, "core.global"));
+ cl_assert_equal_i(17, i);
+ cl_git_pass(git_config_get_int32(&i, cfg, "core.system"));
+ cl_assert_equal_i(11, i);
+
+ git_config_free(cfg);
+}
+
+/*
+ * At the beginning of the test, config18 has:
+ * int32global = 28
+ * int64global = 9223372036854775803
+ * boolglobal = true
+ * stringglobal = I'm a global config value!
+ *
+ * And config19 has:
+ * int32global = -1
+ * int64global = -2
+ * boolglobal = false
+ * stringglobal = don't find me!
+ *
+ */
+void test_config_read__simple_read_from_specific_level(void)
+{
+ git_config *cfg, *cfg_specific;
+ int i;
+ int64_t l, expected = +9223372036854775803;
+ const char *s;
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
+ GIT_CONFIG_LEVEL_SYSTEM, 0));
+
+ cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
+
+ cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global"));
+ cl_assert_equal_i(28, i);
+ cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global"));
+ cl_assert(l == expected);
+ cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal"));
+ cl_assert_equal_b(true, i);
+ cl_git_pass(git_config_get_string(&s, cfg_specific, "core.stringglobal"));
+ cl_assert_equal_s("I'm a global config value!", s);
+
+ git_config_free(cfg_specific);
git_config_free(cfg);
- git_repository_free(repo);
-END_TEST
-#endif
+}
diff --git a/tests-clar/config/stress.c b/tests-clar/config/stress.c
index 6e7db6e8f..317e877f7 100644
--- a/tests-clar/config/stress.c
+++ b/tests-clar/config/stress.c
@@ -4,11 +4,13 @@
#include "fileops.h"
#include "posix.h"
+#define TEST_CONFIG "git-test-config"
+
void test_config_stress__initialize(void)
{
git_filebuf file = GIT_FILEBUF_INIT;
- cl_git_pass(git_filebuf_open(&file, "git-test-config", 0));
+ cl_git_pass(git_filebuf_open(&file, TEST_CONFIG, 0));
git_filebuf_printf(&file, "[color]\n\tui = auto\n");
git_filebuf_printf(&file, "[core]\n\teditor = \n");
@@ -18,19 +20,16 @@ void test_config_stress__initialize(void)
void test_config_stress__cleanup(void)
{
- p_unlink("git-test-config");
+ p_unlink(TEST_CONFIG);
}
void test_config_stress__dont_break_on_invalid_input(void)
{
const char *editor, *color;
- struct git_config_file *file;
git_config *config;
- cl_assert(git_path_exists("git-test-config"));
- cl_git_pass(git_config_file__ondisk(&file, "git-test-config"));
- cl_git_pass(git_config_new(&config));
- cl_git_pass(git_config_add_file(config, file, 0));
+ cl_assert(git_path_exists(TEST_CONFIG));
+ cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&color, config, "color.ui"));
cl_git_pass(git_config_get_string(&editor, config, "core.editor"));
@@ -40,13 +39,10 @@ void test_config_stress__dont_break_on_invalid_input(void)
void test_config_stress__comments(void)
{
- struct git_config_file *file;
git_config *config;
const char *str;
- cl_git_pass(git_config_file__ondisk(&file, cl_fixture("config/config12")));
- cl_git_pass(git_config_new(&config));
- cl_git_pass(git_config_add_file(config, file, 0));
+ cl_git_pass(git_config_open_ondisk(&config, cl_fixture("config/config12")));
cl_git_pass(git_config_get_string(&str, config, "some.section.other"));
cl_assert(!strcmp(str, "hello! \" ; ; ; "));
@@ -62,21 +58,16 @@ void test_config_stress__comments(void)
void test_config_stress__escape_subsection_names(void)
{
- struct git_config_file *file;
git_config *config;
const char *str;
cl_assert(git_path_exists("git-test-config"));
- cl_git_pass(git_config_file__ondisk(&file, "git-test-config"));
- cl_git_pass(git_config_new(&config));
- cl_git_pass(git_config_add_file(config, file, 0));
+ cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_set_string(config, "some.sec\\tion.other", "foo"));
git_config_free(config);
- cl_git_pass(git_config_file__ondisk(&file, "git-test-config"));
- cl_git_pass(git_config_new(&config));
- cl_git_pass(git_config_add_file(config, file, 0));
+ cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&str, config, "some.sec\\tion.other"));
cl_assert(!strcmp("foo", str));
diff --git a/tests-clar/config/write.c b/tests-clar/config/write.c
index eeda4d66a..d98d1dd53 100644
--- a/tests-clar/config/write.c
+++ b/tests-clar/config/write.c
@@ -3,12 +3,14 @@
void test_config_write__initialize(void)
{
cl_fixture_sandbox("config/config9");
+ cl_fixture_sandbox("config/config15");
cl_fixture_sandbox("config/config17");
}
void test_config_write__cleanup(void)
{
cl_fixture_cleanup("config9");
+ cl_fixture_cleanup("config15");
cl_fixture_cleanup("config17");
}
@@ -69,6 +71,40 @@ void test_config_write__delete_value(void)
git_config_free(cfg);
}
+/*
+ * At the beginning of the test:
+ * - config9 has: core.dummy2=42
+ * - config15 has: core.dummy2=7
+ */
+void test_config_write__delete_value_at_specific_level(void)
+{
+ git_config *cfg, *cfg_specific;
+ int32_t i;
+
+ cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
+ cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
+ cl_assert(i == 7);
+ git_config_free(cfg);
+
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, "config9",
+ GIT_CONFIG_LEVEL_LOCAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, "config15",
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+
+ cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
+
+ cl_git_pass(git_config_delete(cfg_specific, "core.dummy2"));
+ git_config_free(cfg);
+
+ cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
+ cl_assert(git_config_get_int32(&i, cfg, "core.dummy2") == GIT_ENOTFOUND);
+ cl_git_pass(git_config_set_int32(cfg, "core.dummy2", 7));
+
+ git_config_free(cfg_specific);
+ git_config_free(cfg);
+}
+
void test_config_write__write_subsection(void)
{
git_config *cfg;
@@ -139,7 +175,45 @@ void test_config_write__escape_value(void)
git_config_free(cfg);
}
-void test_config_write__add_value_in_file_with_no_clrf_at_the_end(void)
+void test_config_write__add_value_at_specific_level(void)
+{
+ git_config *cfg, *cfg_specific;
+ int i;
+ int64_t l, expected = +9223372036854775803;
+ const char *s;
+
+ // open config15 as global level config file
+ cl_git_pass(git_config_new(&cfg));
+ cl_git_pass(git_config_add_file_ondisk(cfg, "config9",
+ GIT_CONFIG_LEVEL_LOCAL, 0));
+ cl_git_pass(git_config_add_file_ondisk(cfg, "config15",
+ GIT_CONFIG_LEVEL_GLOBAL, 0));
+
+ cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
+
+ cl_git_pass(git_config_set_int32(cfg_specific, "core.int32global", 28));
+ cl_git_pass(git_config_set_int64(cfg_specific, "core.int64global", expected));
+ cl_git_pass(git_config_set_bool(cfg_specific, "core.boolglobal", true));
+ cl_git_pass(git_config_set_string(cfg_specific, "core.stringglobal", "I'm a global config value!"));
+ git_config_free(cfg_specific);
+ git_config_free(cfg);
+
+ // open config15 as local level config file
+ cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
+
+ cl_git_pass(git_config_get_int32(&i, cfg, "core.int32global"));
+ cl_assert_equal_i(28, i);
+ cl_git_pass(git_config_get_int64(&l, cfg, "core.int64global"));
+ cl_assert(l == expected);
+ cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal"));
+ cl_assert_equal_b(true, i);
+ cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal"));
+ cl_assert_equal_s("I'm a global config value!", s);
+
+ git_config_free(cfg);
+}
+
+void test_config_write__add_value_at_file_with_no_clrf_at_the_end(void)
{
git_config *cfg;
int i;