diff --git a/tests/GitForDelphi.dpr b/tests/GitForDelphi.dpr index 5a6525a..e12036c 100644 --- a/tests/GitForDelphi.dpr +++ b/tests/GitForDelphi.dpr @@ -29,7 +29,8 @@ uses uTestHelpers in 'uTestHelpers.pas', t10_refs in 'TestsFromLibGit2\t10_refs.pas', uGitForDelphi in '..\uGitForDelphi.pas', - t12_repo in 'TestsFromLibGit2\t12_repo.pas'; + t12_repo in 'TestsFromLibGit2\t12_repo.pas', + t15_config in 'TestsFromLibGit2\t15_config.pas'; {$R *.RES} diff --git a/tests/TestsFromLibGit2/t12_repo.pas b/tests/TestsFromLibGit2/t12_repo.pas index 31a5959..fd8a173 100644 --- a/tests/TestsFromLibGit2/t12_repo.pas +++ b/tests/TestsFromLibGit2/t12_repo.pas @@ -23,8 +23,6 @@ Test12_repo_empty = class(TTestFromLibGit2) procedure test_if_a_repository_is_empty_or_not; end; - // TODO : discover tests - implementation const diff --git a/tests/TestsFromLibGit2/t15_config.pas b/tests/TestsFromLibGit2/t15_config.pas new file mode 100644 index 0000000..5d3ef0b --- /dev/null +++ b/tests/TestsFromLibGit2/t15_config.pas @@ -0,0 +1,234 @@ +unit t15_config; + +interface + +uses + TestFramework, SysUtils, Windows, + uTestsFromLibGit2, uGitForDelphi; + +type + Test15_test_config = class(TTestFromLibGit2) + procedure read_a_simple_configuration; + procedure case_sensitivity; + procedure parse_a_multiline_value; + procedure parse_a__section_subsection__header; + procedure a_variable_name_on_its_own_is_valid; + procedure test_number_suffixes; + procedure test_blank_lines; + procedure test_for_invalid_ext_headers; + procedure don_t_fail_on_empty_files; + procedure replace_a_value; + procedure a_repo_s_config_overrides_the_global_config; + procedure fall_back_to_the_global_config; + end; + +implementation + +const + CONFIG_BASE: PAnsiChar = 'resources/config'; + +procedure Test15_test_config.read_a_simple_configuration; +var + cfg: Pgit_config; + i: Integer; +begin + must_pass(git_config_open_ondisk(&cfg, PAnsiChar(CONFIG_BASE + AnsiString('/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; + +procedure Test15_test_config.case_sensitivity; +var + cfg: Pgit_config; + i: Integer; + s: PAnsiChar; +begin + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config1')))); + + must_pass(git_config_get_string(cfg, 'this.that.other', s)); + must_be_true(StrComp(s, 'true') = 0); + must_pass(git_config_get_string(cfg, 'this.That.other', s)); + must_be_true(StrComp(s, 'yes') = 0); + + 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; + +procedure Test15_test_config.parse_a_multiline_value; +var + cfg: Pgit_config; + s: PAnsiChar; +begin + must_pass(git_config_open_ondisk(&cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config2')))); + + must_pass(git_config_get_string(cfg, 'this.That.and', s)); + must_be_true(StrComp(s, 'one one one two two three three') = 0); + + git_config_free(cfg); +end; + +procedure Test15_test_config.parse_a__section_subsection__header; +var + cfg: Pgit_config; + s: PAnsiChar; +begin + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config3')))); + + must_pass(git_config_get_string(cfg, 'section.subsection.var', s)); + must_be_true(StrComp(s, 'hello') = 0); + + //* Avoid a false positive */ + s := 'nohello'; + must_pass(git_config_get_string(cfg, 'section.subSectIon.var', s)); + must_be_true(StrComp(s, 'hello') = 0); + + git_config_free(cfg); +end; + +procedure Test15_test_config.a_variable_name_on_its_own_is_valid; +var + cfg: Pgit_config; + s: PAnsiChar; + i: Integer; +begin + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config4')))); + + must_pass(git_config_get_string(cfg, 'some.section.variable', s)); + must_be_true(s = nil); + + must_pass(git_config_get_bool(cfg, 'some.section.variable', i)); + must_be_true(i = 1); + + git_config_free(cfg); +end; + +procedure Test15_test_config.test_number_suffixes; +var + cfg: Pgit_config; + i: LongInt; +begin + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/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; + +procedure Test15_test_config.test_blank_lines; +var + cfg: Pgit_config; + i: Integer; +begin + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/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; + +procedure Test15_test_config.test_for_invalid_ext_headers; +var + cfg: Pgit_config; +begin + must_fail(git_config_open_ondisk(&cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config7')))); +end; + +procedure Test15_test_config.don_t_fail_on_empty_files; +var + cfg: Pgit_config; +begin + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config8')))); + + git_config_free(cfg); +end; + +procedure Test15_test_config.replace_a_value; +var + cfg: Pgit_config; + i: Integer; +begin + //* By freeing the config, we make sure we flush the values */ + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/config9')))); + must_pass(git_config_set_int(cfg, 'core.dummy', 5)); + git_config_free(cfg); + + must_pass(git_config_open_ondisk(cfg, PAnsiChar(CONFIG_BASE + AnsiString('/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, PAnsiChar(CONFIG_BASE + AnsiString('/config9')))); + must_pass(git_config_set_int(cfg, 'core.dummy', 1)); + git_config_free(cfg); +end; + +procedure Test15_test_config.a_repo_s_config_overrides_the_global_config; +var + repo: Pgit_repository; + cfg: Pgit_config; + version: Integer; +begin + must_pass(git_repository_open(repo, REPOSITORY_FOLDER)); + must_pass(git_repository_config(cfg, repo, 'resources/config/.gitconfig', nil)); + must_pass(git_config_get_int(cfg, 'core.repositoryformatversion', version)); + must_be_true(version = 0); + git_config_free(cfg); + git_repository_free(repo); +end; + +procedure Test15_test_config.fall_back_to_the_global_config; +var + repo: Pgit_repository; + cfg: Pgit_config; + num: Integer; +begin + must_pass(git_repository_open(repo, REPOSITORY_FOLDER)); + must_pass(git_repository_config(cfg, repo, 'resources/config/.gitconfig', nil)); + must_pass(git_config_get_int(cfg, 'core.something', num)); + must_be_true(num = 2); + git_config_free(cfg); + git_repository_free(repo); +end; + +initialization + RegisterTest('From libgit2.t15-config', Test15_test_config.NamedSuite('config')); + +end. diff --git a/tests/TestsFromLibGit2/uTestsFromLibGit2.pas b/tests/TestsFromLibGit2/uTestsFromLibGit2.pas index 8308f81..fffba2c 100644 --- a/tests/TestsFromLibGit2/uTestsFromLibGit2.pas +++ b/tests/TestsFromLibGit2/uTestsFromLibGit2.pas @@ -141,9 +141,9 @@ function TTestFromLibGit2.GitReturnValue(aResult: Integer): String; errorMessage := git_lasterror; if errorMessage <> '' then - Result := String(errorName) + ': ' + String(errorMessage) + Result := Format('%d %s: %s', [aResult, errorName, errorMessage]) else - Result := String(errorName); + Result := Format('%d %s', [aResult, errorName]); end; procedure TTestFromLibGit2.must_be_true(b: Boolean; const msg: String = ''); @@ -436,5 +436,5 @@ constructor TTestSuiteForLibGit2.Create(TestClass: TTestCaseClass; const aName: initialization InitLibgit2; - // TODO : config tests + end. diff --git a/tests/resources/config/.gitconfig b/tests/resources/config/.gitconfig new file mode 100644 index 0000000..fa72bdd --- /dev/null +++ b/tests/resources/config/.gitconfig @@ -0,0 +1,3 @@ +[core] + repositoryformatversion = 5 + something = 2 \ No newline at end of file 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/resources/config/config4 b/tests/resources/config/config4 new file mode 100644 index 0000000..741fa0f --- /dev/null +++ b/tests/resources/config/config4 @@ -0,0 +1,3 @@ +# A variable name on its own is valid +[some.section] + variable diff --git a/tests/resources/config/config5 b/tests/resources/config/config5 new file mode 100644 index 0000000..8ab60cc --- /dev/null +++ b/tests/resources/config/config5 @@ -0,0 +1,9 @@ +# Test for number suffixes +[number] + simple = 1 + k = 1k + kk = 1K + m = 1m + mm = 1M + g = 1g + gg = 1G diff --git a/tests/resources/config/config6 b/tests/resources/config/config6 new file mode 100644 index 0000000..0f8f90a --- /dev/null +++ b/tests/resources/config/config6 @@ -0,0 +1,5 @@ +[valid "subsection"] + something = true + +[something "else"] + something = false diff --git a/tests/resources/config/config7 b/tests/resources/config/config7 new file mode 100644 index 0000000..6af6fcf --- /dev/null +++ b/tests/resources/config/config7 @@ -0,0 +1,5 @@ +[valid "subsection"] + something = a +; we don't allow anything after closing " +[sec "subsec"x] + bleh = blah diff --git a/tests/resources/config/config8 b/tests/resources/config/config8 new file mode 100644 index 0000000..e69de29 diff --git a/tests/resources/config/config9 b/tests/resources/config/config9 new file mode 100644 index 0000000..4359c78 --- /dev/null +++ b/tests/resources/config/config9 @@ -0,0 +1,2 @@ +[core] + dummy = 1 diff --git a/tests/resources/testrepo.git/config b/tests/resources/testrepo.git/config new file mode 100644 index 0000000..2f89580 --- /dev/null +++ b/tests/resources/testrepo.git/config @@ -0,0 +1,5 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true + logallrefupdates = true