Hash :
40fe5fbe
Author :
Date :
2011-09-22T22:50:36
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*
* 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>
#include <posix.h>
#include "filebuf.h"
#define CONFIG_BASE TEST_RESOURCES "/config"
#define GLOBAL_CONFIG CONFIG_BASE "/.gitconfig"
/*
* 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_ondisk(&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_ondisk(&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_ondisk(&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_ondisk(&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_TEST(config4, "a variable name on its own is valid")
git_config *cfg;
const char *str;
int i;
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config4"));
must_pass(git_config_get_string(cfg, "some.section.variable", &str));
must_be_true(str == NULL);
must_pass(git_config_get_bool(cfg, "some.section.variable", &i));
must_be_true(i == 1);
git_config_free(cfg);
END_TEST
BEGIN_TEST(config5, "test number suffixes")
git_config *cfg;
long long i;
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config5"));
must_pass(git_config_get_long(cfg, "number.simple", &i));
must_be_true(i == 1);
must_pass(git_config_get_long(cfg, "number.k", &i));
must_be_true(i == 1 * 1024);
must_pass(git_config_get_long(cfg, "number.kk", &i));
must_be_true(i == 1 * 1024);
must_pass(git_config_get_long(cfg, "number.m", &i));
must_be_true(i == 1 * 1024 * 1024);
must_pass(git_config_get_long(cfg, "number.mm", &i));
must_be_true(i == 1 * 1024 * 1024);
must_pass(git_config_get_long(cfg, "number.g", &i));
must_be_true(i == 1 * 1024 * 1024 * 1024);
must_pass(git_config_get_long(cfg, "number.gg", &i));
must_be_true(i == 1 * 1024 * 1024 * 1024);
git_config_free(cfg);
END_TEST
BEGIN_TEST(config6, "test blank lines")
git_config *cfg;
int i;
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config6"));
must_pass(git_config_get_bool(cfg, "valid.subsection.something", &i));
must_be_true(i == 1);
must_pass(git_config_get_bool(cfg, "something.else.something", &i));
must_be_true(i == 0);
git_config_free(cfg);
END_TEST
BEGIN_TEST(config7, "test for invalid ext headers")
git_config *cfg;
must_fail(git_config_open_ondisk(&cfg, CONFIG_BASE "/config7"));
END_TEST
BEGIN_TEST(config8, "don't fail on empty files")
git_config *cfg;
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config8"));
git_config_free(cfg);
END_TEST
BEGIN_TEST(config9, "replace a value")
git_config *cfg;
int i;
long long l, expected = +9223372036854775803;
/* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_int(cfg, "core.dummy", 5));
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_get_int(cfg, "core.dummy", &i));
must_be_true(i == 5);
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_int(cfg, "core.dummy", 1));
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_long(cfg, "core.verylong", expected));
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_get_long(cfg, "core.verylong", &l));
must_be_true(l == expected);
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_fail(git_config_get_int(cfg, "core.verylong", &i));
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_long(cfg, "core.verylong", 1));
git_config_free(cfg);
END_TEST
BEGIN_TEST(config10, "a repo's config overrides the global config")
git_repository *repo;
git_config *cfg;
int version;
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &version));
must_be_true(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;
int num;
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
must_pass(git_config_get_int(cfg, "core.something", &num));
must_be_true(num == 2);
git_config_free(cfg);
git_repository_free(repo);
END_TEST
BEGIN_TEST(config12, "delete a value")
git_config *cfg;
int i;
/* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_int(cfg, "core.dummy", 5));
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_delete(cfg, "core.dummy"));
git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_be_true(git_config_get_int(cfg, "core.dummy", &i) == GIT_ENOTFOUND);
must_pass(git_config_set_int(cfg, "core.dummy", 1));
git_config_free(cfg);
END_TEST
BEGIN_TEST(config13, "can't delete a non-existent value")
git_config *cfg;
/* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_be_true(git_config_delete(cfg, "core.imaginary") == GIT_ENOTFOUND);
git_config_free(cfg);
END_TEST
BEGIN_TEST(config14, "don't fail horribly if a section header is in the last line")
git_config *cfg;
/* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10"));
git_config_free(cfg);
END_TEST
BEGIN_TEST(config15, "add a variable in an existing section")
git_config *cfg;
int i;
/* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10"));
must_pass(git_config_set_int(cfg, "empty.tmp", 5));
must_pass(git_config_get_int(cfg, "empty.tmp", &i));
must_be_true(i == 5);
must_pass(git_config_delete(cfg, "empty.tmp"));
git_config_free(cfg);
END_TEST
BEGIN_TEST(config16, "add a variable in a new section")
git_config *cfg;
int i;
git_filebuf buf;
/* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10"));
must_pass(git_config_set_int(cfg, "section.tmp", 5));
must_pass(git_config_get_int(cfg, "section.tmp", &i));
must_be_true(i == 5);
must_pass(git_config_delete(cfg, "section.tmp"));
git_config_free(cfg);
/* As the section wasn't removed, owerwrite the file */
must_pass(git_filebuf_open(&buf, CONFIG_BASE "/config10", 0));
must_pass(git_filebuf_write(&buf, "[empty]\n", strlen("[empty]\n")));
must_pass(git_filebuf_commit(&buf));
END_TEST
BEGIN_SUITE(config)
ADD_TEST(config0);
ADD_TEST(config1);
ADD_TEST(config2);
ADD_TEST(config3);
ADD_TEST(config4);
ADD_TEST(config5);
ADD_TEST(config6);
ADD_TEST(config7);
ADD_TEST(config8);
ADD_TEST(config9);
ADD_TEST(config10);
ADD_TEST(config11);
ADD_TEST(config12);
ADD_TEST(config13);
ADD_TEST(config14);
ADD_TEST(config15);
ADD_TEST(config16);
END_SUITE