diff options
Diffstat (limited to 'tests-clar/config')
| -rw-r--r-- | tests-clar/config/add.c | 37 | ||||
| -rw-r--r-- | tests-clar/config/new.c | 36 | ||||
| -rw-r--r-- | tests-clar/config/read.c | 209 | ||||
| -rw-r--r-- | tests-clar/config/stress.c | 39 | ||||
| -rw-r--r-- | tests-clar/config/write.c | 77 |
5 files changed, 398 insertions, 0 deletions
diff --git a/tests-clar/config/add.c b/tests-clar/config/add.c new file mode 100644 index 000000000..b58029951 --- /dev/null +++ b/tests-clar/config/add.c @@ -0,0 +1,37 @@ +#include "clar_libgit2.h" + +void test_config_add__initialize(void) +{ + cl_fixture_sandbox("config/config10"); +} + +void test_config_add__cleanup(void) +{ + cl_fixture_cleanup("config10"); +} + +void test_config_add__to_existing_section(void) +{ + git_config *cfg; + int32_t i; + + cl_git_pass(git_config_open_ondisk(&cfg, "config10")); + cl_git_pass(git_config_set_int32(cfg, "empty.tmp", 5)); + cl_git_pass(git_config_get_int32(cfg, "empty.tmp", &i)); + cl_assert(i == 5); + cl_git_pass(git_config_delete(cfg, "empty.tmp")); + git_config_free(cfg); +} + +void test_config_add__to_new_section(void) +{ + git_config *cfg; + int32_t i; + + cl_git_pass(git_config_open_ondisk(&cfg, "config10")); + cl_git_pass(git_config_set_int32(cfg, "section.tmp", 5)); + cl_git_pass(git_config_get_int32(cfg, "section.tmp", &i)); + cl_assert(i == 5); + cl_git_pass(git_config_delete(cfg, "section.tmp")); + git_config_free(cfg); +} diff --git a/tests-clar/config/new.c b/tests-clar/config/new.c new file mode 100644 index 000000000..96aed2bb3 --- /dev/null +++ b/tests-clar/config/new.c @@ -0,0 +1,36 @@ +#include "clar_libgit2.h" + +#include "filebuf.h" +#include "fileops.h" +#include "posix.h" + +#define TEST_CONFIG "git-new-config" + +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_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_get_string(config, "color.ui", &out)); + cl_assert(strcmp(out, "auto") == 0); + cl_git_pass(git_config_get_string(config, "core.editor", &out)); + cl_assert(strcmp(out, "ed") == 0); + + git_config_free(config); + + p_unlink(TEST_CONFIG); +} diff --git a/tests-clar/config/read.c b/tests-clar/config/read.c new file mode 100644 index 000000000..26e6f4248 --- /dev/null +++ b/tests-clar/config/read.c @@ -0,0 +1,209 @@ +#include "clar_libgit2.h" + +void test_config_read__simple_read(void) +{ + git_config *cfg; + int32_t i; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config0"))); + + cl_git_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &i)); + cl_assert(i == 0); + cl_git_pass(git_config_get_bool(cfg, "core.filemode", &i)); + cl_assert(i == 1); + cl_git_pass(git_config_get_bool(cfg, "core.bare", &i)); + cl_assert(i == 0); + cl_git_pass(git_config_get_bool(cfg, "core.logallrefupdates", &i)); + cl_assert(i == 1); + + git_config_free(cfg); +} + +void test_config_read__case_sensitive(void) +{ + git_config *cfg; + int i; + const char *str; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1"))); + + cl_git_pass(git_config_get_string(cfg, "this.that.other", &str)); + cl_assert(!strcmp(str, "true")); + cl_git_pass(git_config_get_string(cfg, "this.That.other", &str)); + cl_assert(!strcmp(str, "yes")); + + cl_git_pass(git_config_get_bool(cfg, "this.that.other", &i)); + cl_assert(i == 1); + cl_git_pass(git_config_get_bool(cfg, "this.That.other", &i)); + cl_assert(i == 1); + + /* This one doesn't exist */ + cl_must_fail(git_config_get_bool(cfg, "this.thaT.other", &i)); + + git_config_free(cfg); +} + +/* + * If \ is the last non-space character on the line, we read the next + * one, separating each line with SP. + */ +void test_config_read__multiline_value(void) +{ + git_config *cfg; + const char *str; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2"))); + + cl_git_pass(git_config_get_string(cfg, "this.That.and", &str)); + cl_assert(!strcmp(str, "one one one two two three three")); + + git_config_free(cfg); +} + +/* + * This kind of subsection declaration is case-insensitive + */ +void test_config_read__subsection_header(void) +{ + git_config *cfg; + const char *str; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3"))); + + cl_git_pass(git_config_get_string(cfg, "section.subsection.var", &str)); + cl_assert(!strcmp(str, "hello")); + + /* The subsection is transformed to lower-case */ + cl_must_fail(git_config_get_string(cfg, "section.subSectIon.var", &str)); + + git_config_free(cfg); +} + +void test_config_read__lone_variable(void) +{ + git_config *cfg; + const char *str; + int i; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4"))); + + cl_git_pass(git_config_get_string(cfg, "some.section.variable", &str)); + cl_assert(str == NULL); + + cl_git_pass(git_config_get_bool(cfg, "some.section.variable", &i)); + cl_assert(i == 1); + + git_config_free(cfg); +} + +void test_config_read__number_suffixes(void) +{ + git_config *cfg; + int64_t i; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config5"))); + + cl_git_pass(git_config_get_int64(cfg, "number.simple", &i)); + cl_assert(i == 1); + + cl_git_pass(git_config_get_int64(cfg, "number.k", &i)); + cl_assert(i == 1 * 1024); + + cl_git_pass(git_config_get_int64(cfg, "number.kk", &i)); + cl_assert(i == 1 * 1024); + + cl_git_pass(git_config_get_int64(cfg, "number.m", &i)); + cl_assert(i == 1 * 1024 * 1024); + + cl_git_pass(git_config_get_int64(cfg, "number.mm", &i)); + cl_assert(i == 1 * 1024 * 1024); + + cl_git_pass(git_config_get_int64(cfg, "number.g", &i)); + cl_assert(i == 1 * 1024 * 1024 * 1024); + + cl_git_pass(git_config_get_int64(cfg, "number.gg", &i)); + cl_assert(i == 1 * 1024 * 1024 * 1024); + + git_config_free(cfg); +} + +void test_config_read__blank_lines(void) +{ + git_config *cfg; + int i; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config6"))); + + cl_git_pass(git_config_get_bool(cfg, "valid.subsection.something", &i)); + cl_assert(i == 1); + + cl_git_pass(git_config_get_bool(cfg, "something.else.something", &i)); + cl_assert(i == 0); + + git_config_free(cfg); +} + +void test_config_read__invalid_ext_headers(void) +{ + git_config *cfg; + cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7"))); +} + +void test_config_read__empty_files(void) +{ + git_config *cfg; + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8"))); + git_config_free(cfg); +} + +void test_config_read__header_in_last_line(void) +{ + git_config *cfg; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10"))); + git_config_free(cfg); +} + +void test_config_read__prefixes(void) +{ + git_config *cfg; + const char *str; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); + cl_git_pass(git_config_get_string(cfg, "remote.ab.url", &str)); + cl_assert(strcmp(str, "http://example.com/git/ab") == 0); + + cl_git_pass(git_config_get_string(cfg, "remote.abba.url", &str)); + cl_assert(strcmp(str, "http://example.com/git/abba") == 0); + + git_config_free(cfg); +} + +#if 0 + +BEGIN_TEST(config10, "a repo's config overrides the global config") + git_repository *repo; + git_config *cfg; + int32_t version; + + 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; + git_config *cfg; + int32_t num; + + 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); + 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 new file mode 100644 index 000000000..e3b1114f0 --- /dev/null +++ b/tests-clar/config/stress.c @@ -0,0 +1,39 @@ +#include "clar_libgit2.h" + +#include "filebuf.h" +#include "fileops.h" +#include "posix.h" + +void test_config_stress__initialize(void) +{ + git_filebuf file = GIT_FILEBUF_INIT; + + cl_git_pass(git_filebuf_open(&file, "git-test-config", 0)); + + git_filebuf_printf(&file, "[color]\n\tui = auto\n"); + git_filebuf_printf(&file, "[core]\n\teditor = \n"); + + cl_git_pass(git_filebuf_commit(&file, 0666)); +} + +void test_config_stress__cleanup(void) +{ + p_unlink("git-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_git_pass(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_get_string(config, "color.ui", &color)); + cl_git_pass(git_config_get_string(config, "core.editor", &editor)); + + git_config_free(config); +} diff --git a/tests-clar/config/write.c b/tests-clar/config/write.c new file mode 100644 index 000000000..d22c6f2cf --- /dev/null +++ b/tests-clar/config/write.c @@ -0,0 +1,77 @@ +#include "clar_libgit2.h" + +void test_config_write__initialize(void) +{ + cl_fixture_sandbox("config/config9"); +} + +void test_config_write__cleanup(void) +{ + cl_fixture_cleanup("config9"); +} + +void test_config_write__replace_value(void) +{ + git_config *cfg; + int i; + int64_t l, expected = +9223372036854775803; + + /* By freeing the config, we make sure we flush the values */ + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_get_int32(cfg, "core.dummy", &i)); + cl_assert(i == 5); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1)); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_int64(cfg, "core.verylong", expected)); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_get_int64(cfg, "core.verylong", &l)); + cl_assert(l == expected); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_must_fail(git_config_get_int32(cfg, "core.verylong", &i)); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_int64(cfg, "core.verylong", 1)); + git_config_free(cfg); +} + +void test_config_write__delete_value(void) +{ + git_config *cfg; + int32_t i; + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_delete(cfg, "core.dummy")); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_assert(git_config_get_int32(cfg, "core.dummy", &i) == GIT_ENOTFOUND); + cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1)); + git_config_free(cfg); +} + +void test_config_write__delete_inexistent(void) +{ + git_config *cfg; + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_assert(git_config_delete(cfg, "core.imaginary") == GIT_ENOTFOUND); + git_config_free(cfg); +} |
