diff options
Diffstat (limited to 'tests-clar')
| -rw-r--r-- | tests-clar/attr/repo.c | 8 | ||||
| -rw-r--r-- | tests-clar/clar_helpers.c | 49 | ||||
| -rw-r--r-- | tests-clar/clar_libgit2.h | 5 | ||||
| -rw-r--r-- | tests-clar/config/multivar.c | 8 | ||||
| -rw-r--r-- | tests-clar/core/dirent.c | 6 | ||||
| -rw-r--r-- | tests-clar/diff/blob.c | 9 | ||||
| -rw-r--r-- | tests-clar/diff/index.c | 8 | ||||
| -rw-r--r-- | tests-clar/diff/iterator.c | 50 | ||||
| -rw-r--r-- | tests-clar/diff/tree.c | 9 | ||||
| -rw-r--r-- | tests-clar/diff/workdir.c | 8 | ||||
| -rw-r--r-- | tests-clar/status/ignore.c | 13 | ||||
| -rw-r--r-- | tests-clar/status/worktree.c | 67 |
12 files changed, 109 insertions, 131 deletions
diff --git a/tests-clar/attr/repo.c b/tests-clar/attr/repo.c index 9f6a49bf4..2afea23d6 100644 --- a/tests-clar/attr/repo.c +++ b/tests-clar/attr/repo.c @@ -104,12 +104,12 @@ void test_attr_repo__get_many(void) } static int count_attrs( - const char *GIT_UNUSED(name), - const char *GIT_UNUSED(value), + const char *name, + const char *value, void *payload) { - GIT_UNUSED_ARG(name); - GIT_UNUSED_ARG(value); + GIT_UNUSED(name); + GIT_UNUSED(value); *((int *)payload) += 1; diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c index 1ef5a9bf2..22db56f0c 100644 --- a/tests-clar/clar_helpers.c +++ b/tests-clar/clar_helpers.c @@ -39,3 +39,52 @@ void cl_git_append2file(const char *filename, const char *new_content) cl_must_pass(p_chmod(filename, 0644)); } +static const char *_cl_sandbox = NULL; +static git_repository *_cl_repo = NULL; + +git_repository *cl_git_sandbox_init(const char *sandbox) +{ + /* Copy the whole sandbox folder from our fixtures to our test sandbox + * area. After this it can be accessed with `./sandbox` + */ + cl_fixture_sandbox(sandbox); + _cl_sandbox = sandbox; + + p_chdir(sandbox); + + /* Rename `sandbox/.gitted` to `sandbox/.git` which must be done since + * we cannot store a folder named `.git` inside the fixtures folder of + * our libgit2 repo. + */ + cl_git_pass(p_rename(".gitted", ".git")); + + /* If we have `gitattributes`, rename to `.gitattributes`. This may + * be necessary if we don't want the attributes to be applied in the + * libgit2 repo, but just during testing. + */ + if (p_access("gitattributes", F_OK) == 0) + cl_git_pass(p_rename("gitattributes", ".gitattributes")); + + /* As with `gitattributes`, we may need `gitignore` just for testing. */ + if (p_access("gitignore", F_OK) == 0) + cl_git_pass(p_rename("gitignore", ".gitignore")); + + p_chdir(".."); + + /* Now open the sandbox repository and make it available for tests */ + cl_git_pass(git_repository_open(&_cl_repo, sandbox)); + + return _cl_repo; +} + +void cl_git_sandbox_cleanup(void) +{ + if (_cl_repo) { + git_repository_free(_cl_repo); + _cl_repo = NULL; + } + if (_cl_sandbox) { + cl_fixture_cleanup(_cl_sandbox); + _cl_sandbox = NULL; + } +} diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h index 43dc4e846..5c034a385 100644 --- a/tests-clar/clar_libgit2.h +++ b/tests-clar/clar_libgit2.h @@ -58,4 +58,9 @@ GIT_INLINE(void) cl_assert_strequal_internal( void cl_git_mkfile(const char *filename, const char *content); void cl_git_append2file(const char *filename, const char *new_content); +/* Git sandbox setup helpers */ + +git_repository *cl_git_sandbox_init(const char *sandbox); +void cl_git_sandbox_cleanup(void); + #endif diff --git a/tests-clar/config/multivar.c b/tests-clar/config/multivar.c index bccdc1289..a8451aca2 100644 --- a/tests-clar/config/multivar.c +++ b/tests-clar/config/multivar.c @@ -12,10 +12,12 @@ void test_config_multivar__cleanup(void) cl_fixture_cleanup("config"); } -static int mv_read_cb(const char *name, const char *GIT_UNUSED(value), void *data) +static int mv_read_cb(const char *name, const char *value, void *data) { int *n = (int *) data; + GIT_UNUSED(value); + if (!strcmp(name, _name)) (*n)++; @@ -35,10 +37,12 @@ void test_config_multivar__foreach(void) git_config_free(cfg); } -static int cb(const char *GIT_UNUSED(val), void *data) +static int cb(const char *val, void *data) { int *n = (int *) data; + GIT_UNUSED(val); + (*n)++; return GIT_SUCCESS; diff --git a/tests-clar/core/dirent.c b/tests-clar/core/dirent.c index 782370969..9c366bf97 100644 --- a/tests-clar/core/dirent.c +++ b/tests-clar/core/dirent.c @@ -88,10 +88,10 @@ static int one_entry(void *state, git_buf *path) return GIT_ERROR; } -static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path)) +static int dont_call_me(void *state, git_buf *path) { - GIT_UNUSED_ARG(state); - GIT_UNUSED_ARG(path); + GIT_UNUSED(state); + GIT_UNUSED(path); return GIT_ERROR; } diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c index 7aa8ceb22..bdfe8baaf 100644 --- a/tests-clar/diff/blob.c +++ b/tests-clar/diff/blob.c @@ -5,17 +5,12 @@ static git_repository *g_repo = NULL; void test_diff_blob__initialize(void) { - cl_fixture_sandbox("attr"); - cl_git_pass(p_rename("attr/.gitted", "attr/.git")); - cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes")); - cl_git_pass(git_repository_open(&g_repo, "attr/.git")); + g_repo = cl_git_sandbox_init("attr"); } void test_diff_blob__cleanup(void) { - git_repository_free(g_repo); - g_repo = NULL; - cl_fixture_cleanup("attr"); + cl_git_sandbox_cleanup(); } void test_diff_blob__0(void) diff --git a/tests-clar/diff/index.c b/tests-clar/diff/index.c index 0941c7c21..171815df5 100644 --- a/tests-clar/diff/index.c +++ b/tests-clar/diff/index.c @@ -5,16 +5,12 @@ static git_repository *g_repo = NULL; void test_diff_index__initialize(void) { - cl_fixture_sandbox("status"); - cl_git_pass(p_rename("status/.gitted", "status/.git")); - cl_git_pass(git_repository_open(&g_repo, "status/.git")); + g_repo = cl_git_sandbox_init("status"); } void test_diff_index__cleanup(void) { - git_repository_free(g_repo); - g_repo = NULL; - cl_fixture_cleanup("status"); + cl_git_sandbox_cleanup(); } void test_diff_index__0(void) diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c index 1ad126ca8..3953fd83f 100644 --- a/tests-clar/diff/iterator.c +++ b/tests-clar/diff/iterator.c @@ -2,37 +2,6 @@ #include "diff_helpers.h" #include "iterator.h" -static git_repository *g_repo = NULL; -static const char *g_sandbox = NULL; - -static void setup_sandbox(const char *sandbox) -{ - cl_fixture_sandbox(sandbox); - g_sandbox = sandbox; - - p_chdir(sandbox); - cl_git_pass(p_rename(".gitted", ".git")); - if (p_access("gitattributes", F_OK) == 0) - cl_git_pass(p_rename("gitattributes", ".gitattributes")); - if (p_access("gitignore", F_OK) == 0) - cl_git_pass(p_rename("gitignore", ".gitignore")); - p_chdir(".."); - - cl_git_pass(git_repository_open(&g_repo, sandbox)); -} - -static void cleanup_sandbox(void) -{ - if (g_repo) { - git_repository_free(g_repo); - g_repo = NULL; - } - if (g_sandbox) { - cl_fixture_cleanup(g_sandbox); - g_sandbox = NULL; - } -} - void test_diff_iterator__initialize(void) { /* since we are doing tests with different sandboxes, defer setup @@ -44,7 +13,7 @@ void test_diff_iterator__initialize(void) void test_diff_iterator__cleanup(void) { - cleanup_sandbox(); + cl_git_sandbox_cleanup(); } @@ -60,11 +29,10 @@ static void tree_iterator_test( git_iterator *i; const git_index_entry *entry; int count = 0; + git_repository *repo = cl_git_sandbox_init(sandbox); - setup_sandbox(sandbox); - - cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish)); - cl_git_pass(git_iterator_for_tree(g_repo, t, &i)); + cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); + cl_git_pass(git_iterator_for_tree(repo, t, &i)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { @@ -183,10 +151,9 @@ static void index_iterator_test( git_iterator *i; const git_index_entry *entry; int count = 0; + git_repository *repo = cl_git_sandbox_init(sandbox); - setup_sandbox(sandbox); - - cl_git_pass(git_iterator_for_index(g_repo, &i)); + cl_git_pass(git_iterator_for_index(repo, &i)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { @@ -303,10 +270,9 @@ static void workdir_iterator_test( git_iterator *i; const git_index_entry *entry; int count = 0, count_all = 0; + git_repository *repo = cl_git_sandbox_init(sandbox); - setup_sandbox(sandbox); - - cl_git_pass(git_iterator_for_workdir(g_repo, &i)); + cl_git_pass(git_iterator_for_workdir(repo, &i)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index f5fdfba16..91e1343cc 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -5,17 +5,12 @@ static git_repository *g_repo = NULL; void test_diff_tree__initialize(void) { - cl_fixture_sandbox("attr"); - cl_git_pass(p_rename("attr/.gitted", "attr/.git")); - cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes")); - cl_git_pass(git_repository_open(&g_repo, "attr/.git")); + g_repo = cl_git_sandbox_init("attr"); } void test_diff_tree__cleanup(void) { - git_repository_free(g_repo); - g_repo = NULL; - cl_fixture_cleanup("attr"); + cl_git_sandbox_cleanup(); } void test_diff_tree__0(void) diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 7312a72a7..28cfa23e2 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -5,16 +5,12 @@ static git_repository *g_repo = NULL; void test_diff_workdir__initialize(void) { - cl_fixture_sandbox("status"); - cl_git_pass(p_rename("status/.gitted", "status/.git")); - cl_git_pass(git_repository_open(&g_repo, "status/.git")); + g_repo = cl_git_sandbox_init("status"); } void test_diff_workdir__cleanup(void) { - git_repository_free(g_repo); - g_repo = NULL; - cl_fixture_cleanup("status"); + cl_git_sandbox_cleanup(); } void test_diff_workdir__to_index(void) diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c index 67aecba31..99cb9e8b8 100644 --- a/tests-clar/status/ignore.c +++ b/tests-clar/status/ignore.c @@ -7,21 +7,12 @@ static git_repository *g_repo = NULL; void test_status_ignore__initialize(void) { - /* Before each test, instantiate the attr repo from the fixtures and - * rename the .gitted to .git so it is a repo with a working dir. Also - * rename gitignore to .gitignore. - */ - cl_fixture_sandbox("attr"); - cl_git_pass(p_rename("attr/.gitted", "attr/.git")); - cl_git_pass(p_rename("attr/gitignore", "attr/.gitignore")); - cl_git_pass(git_repository_open(&g_repo, "attr/.git")); + g_repo = cl_git_sandbox_init("attr"); } void test_status_ignore__cleanup(void) { - git_repository_free(g_repo); - g_repo = NULL; - cl_fixture_cleanup("attr"); + cl_git_sandbox_cleanup(); } void test_status_ignore__0(void) diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c index 7d730bb9b..f654b8a94 100644 --- a/tests-clar/status/worktree.c +++ b/tests-clar/status/worktree.c @@ -5,12 +5,6 @@ /** - * Test fixtures - */ -static git_repository *_repository = NULL; - - -/** * Auxiliary methods */ static int @@ -37,48 +31,27 @@ exit: } static int -cb_status__count(const char *GIT_UNUSED(p), unsigned int GIT_UNUSED(s), void *payload) +cb_status__count(const char *p, unsigned int s, void *payload) { volatile int *count = (int *)payload; - GIT_UNUSED_ARG(p); - GIT_UNUSED_ARG(s); + GIT_UNUSED(p); + GIT_UNUSED(s); - *count++; + (*count)++; return GIT_SUCCESS; } - /** * Initializer * - * This method is called once before starting each - * test, and will load the required fixtures + * Not all of the tests in this file use the same fixtures, so we allow each + * test to load their fixture at the top of the test function. */ void test_status_worktree__initialize(void) { - /* - * Sandbox the `status/` repository from our Fixtures. - * This will copy the whole folder to our sandbox, - * so now it can be accessed with `./status` - */ - cl_fixture_sandbox("status"); - - /* - * Rename `status/.gitted` to `status/.git` - * We do this because we cannot store a folder named `.git` - * inside the fixtures folder in our libgit2 repo. - */ - cl_git_pass( - p_rename("status/.gitted", "status/.git") - ); - - /* - * Open the sandboxed "status" repository - */ - cl_git_pass(git_repository_open(&_repository, "status/.git")); } /** @@ -89,10 +62,7 @@ void test_status_worktree__initialize(void) */ void test_status_worktree__cleanup(void) { - git_repository_free(_repository); - _repository = NULL; - - cl_fixture_cleanup("status"); + cl_git_sandbox_cleanup(); } /** @@ -101,6 +71,7 @@ void test_status_worktree__cleanup(void) void test_status_worktree__whole_repository(void) { struct status_entry_counts counts; + git_repository *repo = cl_git_sandbox_init("status"); memset(&counts, 0x0, sizeof(struct status_entry_counts)); counts.expected_entry_count = entry_count0; @@ -108,7 +79,7 @@ void test_status_worktree__whole_repository(void) counts.expected_statuses = entry_statuses0; cl_git_pass( - git_status_foreach(_repository, cb_status__normal, &counts) + git_status_foreach(repo, cb_status__normal, &counts) ); cl_assert(counts.entry_count == counts.expected_entry_count); @@ -119,8 +90,10 @@ void test_status_worktree__whole_repository(void) void test_status_worktree__empty_repository(void) { int count = 0; + git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); + + cl_git_pass(git_status_foreach(repo, cb_status__count, &count)); - git_status_foreach(_repository, cb_status__count, &count); cl_assert(count == 0); } @@ -128,10 +101,11 @@ void test_status_worktree__single_file(void) { int i; unsigned int status_flags; + git_repository *repo = cl_git_sandbox_init("status"); for (i = 0; i < (int)entry_count0; i++) { cl_git_pass( - git_status_file(&status_flags, _repository, entry_paths0[i]) + git_status_file(&status_flags, repo, entry_paths0[i]) ); cl_assert(entry_statuses0[i] == status_flags); } @@ -140,15 +114,22 @@ void test_status_worktree__single_file(void) void test_status_worktree__ignores(void) { int i, ignored; + git_repository *repo = cl_git_sandbox_init("status"); for (i = 0; i < (int)entry_count0; i++) { - cl_git_pass(git_status_should_ignore(_repository, entry_paths0[i], &ignored)); + cl_git_pass( + git_status_should_ignore(repo, entry_paths0[i], &ignored) + ); cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_WT_IGNORED)); } - cl_git_pass(git_status_should_ignore(_repository, "nonexistent_file", &ignored)); + cl_git_pass( + git_status_should_ignore(repo, "nonexistent_file", &ignored) + ); cl_assert(!ignored); - cl_git_pass(git_status_should_ignore(_repository, "ignored_nonexistent_file", &ignored)); + cl_git_pass( + git_status_should_ignore(repo, "ignored_nonexistent_file", &ignored) + ); cl_assert(ignored); } |
