Skip to content

Commit 6be5787

Browse files
committed
Add tests for config
1 parent fe6ef73 commit 6be5787

File tree

16 files changed

+291
-6
lines changed

16 files changed

+291
-6
lines changed

tests/GitForDelphi.dpr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ uses
2929
uTestHelpers in 'uTestHelpers.pas',
3030
t10_refs in 'TestsFromLibGit2\t10_refs.pas',
3131
uGitForDelphi in '..\uGitForDelphi.pas',
32-
t12_repo in 'TestsFromLibGit2\t12_repo.pas';
32+
t12_repo in 'TestsFromLibGit2\t12_repo.pas',
33+
t15_config in 'TestsFromLibGit2\t15_config.pas';
3334

3435
{$R *.RES}
3536

tests/TestsFromLibGit2/t12_repo.pas

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Test12_repo_empty = class(TTestFromLibGit2)
2323
procedure test_if_a_repository_is_empty_or_not;
2424
end;
2525

26-
// TODO : discover tests
27-
2826
implementation
2927

3028
const
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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.

tests/TestsFromLibGit2/uTestsFromLibGit2.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ function TTestFromLibGit2.GitReturnValue(aResult: Integer): String;
141141
errorMessage := git_lasterror;
142142

143143
if errorMessage <> '' then
144-
Result := String(errorName) + ': ' + String(errorMessage)
144+
Result := Format('%d %s: %s', [aResult, errorName, errorMessage])
145145
else
146-
Result := String(errorName);
146+
Result := Format('%d %s', [aResult, errorName]);
147147
end;
148148

149149
procedure TTestFromLibGit2.must_be_true(b: Boolean; const msg: String = '');
@@ -436,5 +436,5 @@ constructor TTestSuiteForLibGit2.Create(TestClass: TTestCaseClass; const aName:
436436

437437
initialization
438438
InitLibgit2;
439-
// TODO : config tests
439+
440440
end.

tests/resources/config/.gitconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[core]
2+
repositoryformatversion = 5
3+
something = 2

tests/resources/config/config0

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is a test
2+
; of different comments
3+
[core]
4+
repositoryformatversion = 0
5+
filemode = true
6+
bare = false
7+
logallrefupdates = true

tests/resources/config/config1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This one checks for case sensitivity
2+
[this "that"]
3+
other = true
4+
[this "That"]
5+
other = yes

tests/resources/config/config2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; This one tests for multiline values
2+
[this "That"]
3+
and = one one one \
4+
two two \
5+
three three

tests/resources/config/config3

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# A [section.subsection] header is case-insensitive
2+
[section.SuBsection]
3+
var = hello

tests/resources/config/config4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# A variable name on its own is valid
2+
[some.section]
3+
variable

0 commit comments

Comments
 (0)