config: add tests These tests are basic, but they should tell us when we've broken something. Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
diff --git a/tests/resources/config/config0 b/tests/resources/config/config0
new file mode 100644
index 0000000..85235c5
--- /dev/null
+++ b/tests/resources/config/config0
@@ -0,0 +1,7 @@
+# This is a test
+; of different comments
+[core]
+ repositoryformatversion = 0
+ filemode = true
+ bare = false
+ logallrefupdates = true
\ No newline at end of file
diff --git a/tests/resources/config/config1 b/tests/resources/config/config1
new file mode 100644
index 0000000..211dc9e
--- /dev/null
+++ b/tests/resources/config/config1
@@ -0,0 +1,5 @@
+# This one checks for case sensitivity
+[this "that"]
+ other = true
+[this "That"]
+ other = yes
diff --git a/tests/resources/config/config2 b/tests/resources/config/config2
new file mode 100644
index 0000000..60a3898
--- /dev/null
+++ b/tests/resources/config/config2
@@ -0,0 +1,5 @@
+; This one tests for multiline values
+[this "That"]
+ and = one one one \
+two two \
+three three
\ No newline at end of file
diff --git a/tests/resources/config/config3 b/tests/resources/config/config3
new file mode 100644
index 0000000..44a5e50
--- /dev/null
+++ b/tests/resources/config/config3
@@ -0,0 +1,3 @@
+# A [section.subsection] header is case-insensitive
+[section.SuBsection]
+ var = hello
diff --git a/tests/t14-config.c b/tests/t14-config.c
new file mode 100644
index 0000000..6428cea
--- /dev/null
+++ b/tests/t14-config.c
@@ -0,0 +1,121 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#include "test_lib.h"
+#include "test_helpers.h"
+
+#include <git2.h>
+
+#define CONFIG_BASE TEST_RESOURCES "/config"
+
+/*
+ * This one is so we know the code isn't completely broken
+ */
+BEGIN_TEST(config0, "read a simple configuration")
+ git_config *cfg;
+ int i;
+
+ must_pass(git_config_open(&cfg, CONFIG_BASE "/config0"));
+ must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &i));
+ must_be_true(i == 0);
+ must_pass(git_config_get_bool(cfg, "core.filemode", &i));
+ must_be_true(i == 1);
+ must_pass(git_config_get_bool(cfg, "core.bare", &i));
+ must_be_true(i == 0);
+ must_pass(git_config_get_bool(cfg, "core.logallrefupdates", &i));
+ must_be_true(i == 1);
+
+ git_config_free(cfg);
+END_TEST
+
+/*
+ * [this "that"] and [this "That] are different namespaces. Make sure
+ * each returns the correct one.
+ */
+BEGIN_TEST(config1, "case sensitivity")
+ git_config *cfg;
+ int i;
+ const char *str;
+
+ must_pass(git_config_open(&cfg, CONFIG_BASE "/config1"));
+
+ must_pass(git_config_get_string(cfg, "this.that.other", &str));
+ must_be_true(!strcmp(str, "true"));
+ must_pass(git_config_get_string(cfg, "this.That.other", &str));
+ must_be_true(!strcmp(str, "yes"));
+
+ must_pass(git_config_get_bool(cfg, "this.that.other", &i));
+ must_be_true(i == 1);
+ must_pass(git_config_get_bool(cfg, "this.That.other", &i));
+ must_be_true(i == 1);
+
+ /* This one doesn't exist */
+ must_fail(git_config_get_bool(cfg, "this.thaT.other", &i));
+
+ git_config_free(cfg);
+END_TEST
+
+/*
+ * If \ is the last non-space character on the line, we read the next
+ * one, separating each line with SP.
+ */
+BEGIN_TEST(config2, "parse a multiline value")
+ git_config *cfg;
+ const char *str;
+
+ must_pass(git_config_open(&cfg, CONFIG_BASE "/config2"));
+
+ must_pass(git_config_get_string(cfg, "this.That.and", &str));
+ must_be_true(!strcmp(str, "one one one two two three three"));
+
+ git_config_free(cfg);
+END_TEST
+
+/*
+ * This kind of subsection declaration is case-insensitive
+ */
+BEGIN_TEST(config3, "parse a [section.subsection] header")
+ git_config *cfg;
+ const char *str;
+
+ must_pass(git_config_open(&cfg, CONFIG_BASE "/config3"));
+
+ must_pass(git_config_get_string(cfg, "section.subsection.var", &str));
+ must_be_true(!strcmp(str, "hello"));
+
+ /* Avoid a false positive */
+ str = "nohello";
+ must_pass(git_config_get_string(cfg, "section.subSectIon.var", &str));
+ must_be_true(!strcmp(str, "hello"));
+
+ git_config_free(cfg);
+END_TEST
+
+
+BEGIN_SUITE(config)
+ ADD_TEST(config0);
+ ADD_TEST(config1);
+ ADD_TEST(config2);
+ ADD_TEST(config3);
+END_SUITE
diff --git a/tests/test_main.c b/tests/test_main.c
index f2a623a..c99722e 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -43,6 +43,7 @@ DECLARE_SUITE(refs);
DECLARE_SUITE(sqlite);
DECLARE_SUITE(repository);
DECLARE_SUITE(threads);
+DECLARE_SUITE(config);
static libgit2_suite suite_methods[]= {
SUITE_NAME(core),
@@ -59,6 +60,7 @@ static libgit2_suite suite_methods[]= {
SUITE_NAME(sqlite),
SUITE_NAME(repository),
SUITE_NAME(threads),
+ SUITE_NAME(config),
};
#define GIT_SUITE_COUNT (ARRAY_SIZE(suite_methods))