|
| 1 | +unit t15_config; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + TestFramework, SysUtils, Windows, |
| 7 | + uTestsFromLibGit2, uGitForDelphi; |
| 8 | + |
| 9 | +type |
| 10 | + Test15_test_config = class(TTestFromLibGit2) |
| 11 | + procedure read_a_simple_configuration; |
| 12 | + procedure case_sensitivity; |
| 13 | + procedure parse_a_multiline_value; |
| 14 | + procedure parse_a__section_subsection__header; |
| 15 | + procedure a_variable_name_on_its_own_is_valid; |
| 16 | + procedure test_number_suffixes; |
| 17 | + procedure test_blank_lines; |
| 18 | + procedure test_for_invalid_ext_headers; |
| 19 | + procedure don_t_fail_on_empty_files; |
| 20 | + procedure replace_a_value; |
| 21 | + procedure a_repo_s_config_overrides_the_global_config; |
| 22 | + procedure fall_back_to_the_global_config; |
| 23 | + end; |
| 24 | + |
| 25 | +implementation |
| 26 | + |
| 27 | +const |
| 28 | + CONFIG_BASE: PAnsiChar = 'resources/config'; |
| 29 | + |
| 30 | +procedure Test15_test_config.read_a_simple_configuration; |
| 31 | +var |
| 32 | + cfg: Pgit_config; |
| 33 | + i: Integer; |
| 34 | +begin |
| 35 | + must_pass(git_config_open_ondisk(&cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config0')))); |
| 36 | + must_pass(git_config_get_int(cfg, 'core.repositoryformatversion', i)); |
| 37 | + must_be_true(i = 0); |
| 38 | + must_pass(git_config_get_bool(cfg, 'core.filemode', i)); |
| 39 | + must_be_true(i = 1); |
| 40 | + must_pass(git_config_get_bool(cfg, 'core.bare', i)); |
| 41 | + must_be_true(i = 0); |
| 42 | + must_pass(git_config_get_bool(cfg, 'core.logallrefupdates', i)); |
| 43 | + must_be_true(i = 1); |
| 44 | + |
| 45 | + git_config_free(cfg); |
| 46 | +end; |
| 47 | + |
| 48 | +procedure Test15_test_config.case_sensitivity; |
| 49 | +var |
| 50 | + cfg: Pgit_config; |
| 51 | + i: Integer; |
| 52 | + s: PAnsiChar; |
| 53 | +begin |
| 54 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config1')))); |
| 55 | + |
| 56 | + must_pass(git_config_get_string(cfg, 'this.that.other', s)); |
| 57 | + must_be_true(StrComp(s, 'true') = 0); |
| 58 | + must_pass(git_config_get_string(cfg, 'this.That.other', s)); |
| 59 | + must_be_true(StrComp(s, 'yes') = 0); |
| 60 | + |
| 61 | + must_pass(git_config_get_bool(cfg, 'this.that.other', i)); |
| 62 | + must_be_true(i = 1); |
| 63 | + must_pass(git_config_get_bool(cfg, 'this.That.other', i)); |
| 64 | + must_be_true(i = 1); |
| 65 | + |
| 66 | + //* This one doesn't exist */ |
| 67 | + must_fail(git_config_get_bool(cfg, 'this.thaT.other', i)); |
| 68 | + |
| 69 | + git_config_free(cfg); |
| 70 | +end; |
| 71 | + |
| 72 | +procedure Test15_test_config.parse_a_multiline_value; |
| 73 | +var |
| 74 | + cfg: Pgit_config; |
| 75 | + s: PAnsiChar; |
| 76 | +begin |
| 77 | + must_pass(git_config_open_ondisk(&cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config2')))); |
| 78 | + |
| 79 | + must_pass(git_config_get_string(cfg, 'this.That.and', s)); |
| 80 | + must_be_true(StrComp(s, 'one one one two two three three') = 0); |
| 81 | + |
| 82 | + git_config_free(cfg); |
| 83 | +end; |
| 84 | + |
| 85 | +procedure Test15_test_config.parse_a__section_subsection__header; |
| 86 | +var |
| 87 | + cfg: Pgit_config; |
| 88 | + s: PAnsiChar; |
| 89 | +begin |
| 90 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config3')))); |
| 91 | + |
| 92 | + must_pass(git_config_get_string(cfg, 'section.subsection.var', s)); |
| 93 | + must_be_true(StrComp(s, 'hello') = 0); |
| 94 | + |
| 95 | + //* Avoid a false positive */ |
| 96 | + s := 'nohello'; |
| 97 | + must_pass(git_config_get_string(cfg, 'section.subSectIon.var', s)); |
| 98 | + must_be_true(StrComp(s, 'hello') = 0); |
| 99 | + |
| 100 | + git_config_free(cfg); |
| 101 | +end; |
| 102 | + |
| 103 | +procedure Test15_test_config.a_variable_name_on_its_own_is_valid; |
| 104 | +var |
| 105 | + cfg: Pgit_config; |
| 106 | + s: PAnsiChar; |
| 107 | + i: Integer; |
| 108 | +begin |
| 109 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config4')))); |
| 110 | + |
| 111 | + must_pass(git_config_get_string(cfg, 'some.section.variable', s)); |
| 112 | + must_be_true(s = nil); |
| 113 | + |
| 114 | + must_pass(git_config_get_bool(cfg, 'some.section.variable', i)); |
| 115 | + must_be_true(i = 1); |
| 116 | + |
| 117 | + git_config_free(cfg); |
| 118 | +end; |
| 119 | + |
| 120 | +procedure Test15_test_config.test_number_suffixes; |
| 121 | +var |
| 122 | + cfg: Pgit_config; |
| 123 | + i: LongInt; |
| 124 | +begin |
| 125 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config5')))); |
| 126 | + |
| 127 | + must_pass(git_config_get_long(cfg, 'number.simple', i)); |
| 128 | + must_be_true(i = 1); |
| 129 | + |
| 130 | + must_pass(git_config_get_long(cfg, 'number.k', i)); |
| 131 | + must_be_true(i = 1 * 1024); |
| 132 | + |
| 133 | + must_pass(git_config_get_long(cfg, 'number.kk', i)); |
| 134 | + must_be_true(i = 1 * 1024); |
| 135 | + |
| 136 | + must_pass(git_config_get_long(cfg, 'number.m', i)); |
| 137 | + must_be_true(i = 1 * 1024 * 1024); |
| 138 | + |
| 139 | + must_pass(git_config_get_long(cfg, 'number.mm', i)); |
| 140 | + must_be_true(i = 1 * 1024 * 1024); |
| 141 | + |
| 142 | + must_pass(git_config_get_long(cfg, 'number.g', i)); |
| 143 | + must_be_true(i = 1 * 1024 * 1024 * 1024); |
| 144 | + |
| 145 | + must_pass(git_config_get_long(cfg, 'number.gg', i)); |
| 146 | + must_be_true(i = 1 * 1024 * 1024 * 1024); |
| 147 | + |
| 148 | + git_config_free(cfg); |
| 149 | +end; |
| 150 | + |
| 151 | +procedure Test15_test_config.test_blank_lines; |
| 152 | +var |
| 153 | + cfg: Pgit_config; |
| 154 | + i: Integer; |
| 155 | +begin |
| 156 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config6')))); |
| 157 | + |
| 158 | + must_pass(git_config_get_bool(cfg, 'valid.subsection.something', i)); |
| 159 | + must_be_true(i = 1); |
| 160 | + |
| 161 | + must_pass(git_config_get_bool(cfg, 'something.else.something', i)); |
| 162 | + must_be_true(i = 0); |
| 163 | + |
| 164 | + git_config_free(cfg); |
| 165 | +end; |
| 166 | + |
| 167 | +procedure Test15_test_config.test_for_invalid_ext_headers; |
| 168 | +var |
| 169 | + cfg: Pgit_config; |
| 170 | +begin |
| 171 | + must_fail(git_config_open_ondisk(&cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config7')))); |
| 172 | +end; |
| 173 | + |
| 174 | +procedure Test15_test_config.don_t_fail_on_empty_files; |
| 175 | +var |
| 176 | + cfg: Pgit_config; |
| 177 | +begin |
| 178 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config8')))); |
| 179 | + |
| 180 | + git_config_free(cfg); |
| 181 | +end; |
| 182 | + |
| 183 | +procedure Test15_test_config.replace_a_value; |
| 184 | +var |
| 185 | + cfg: Pgit_config; |
| 186 | + i: Integer; |
| 187 | +begin |
| 188 | + //* By freeing the config, we make sure we flush the values */ |
| 189 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config9')))); |
| 190 | + must_pass(git_config_set_int(cfg, 'core.dummy', 5)); |
| 191 | + git_config_free(cfg); |
| 192 | + |
| 193 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config9')))); |
| 194 | + must_pass(git_config_get_int(cfg, 'core.dummy', i)); |
| 195 | + must_be_true(i = 5); |
| 196 | + git_config_free(cfg); |
| 197 | + |
| 198 | + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config9')))); |
| 199 | + must_pass(git_config_set_int(cfg, 'core.dummy', 1)); |
| 200 | + git_config_free(cfg); |
| 201 | +end; |
| 202 | + |
| 203 | +procedure Test15_test_config.a_repo_s_config_overrides_the_global_config; |
| 204 | +var |
| 205 | + repo: Pgit_repository; |
| 206 | + cfg: Pgit_config; |
| 207 | + version: Integer; |
| 208 | +begin |
| 209 | + must_pass(git_repository_open(repo, REPOSITORY_FOLDER)); |
| 210 | + must_pass(git_repository_config(cfg, repo, 'resources/config/.gitconfig', nil)); |
| 211 | + must_pass(git_config_get_int(cfg, 'core.repositoryformatversion', version)); |
| 212 | + must_be_true(version = 0); |
| 213 | + git_config_free(cfg); |
| 214 | + git_repository_free(repo); |
| 215 | +end; |
| 216 | + |
| 217 | +procedure Test15_test_config.fall_back_to_the_global_config; |
| 218 | +var |
| 219 | + repo: Pgit_repository; |
| 220 | + cfg: Pgit_config; |
| 221 | + num: Integer; |
| 222 | +begin |
| 223 | + must_pass(git_repository_open(repo, REPOSITORY_FOLDER)); |
| 224 | + must_pass(git_repository_config(cfg, repo, 'resources/config/.gitconfig', nil)); |
| 225 | + must_pass(git_config_get_int(cfg, 'core.something', num)); |
| 226 | + must_be_true(num = 2); |
| 227 | + git_config_free(cfg); |
| 228 | + git_repository_free(repo); |
| 229 | +end; |
| 230 | + |
| 231 | +initialization |
| 232 | + RegisterTest('From libgit2.t15-config', Test15_test_config.NamedSuite('config')); |
| 233 | + |
| 234 | +end. |
0 commit comments