From 600d8dbf6dccff5a9d86dd5ae01a53de46471796 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 3 Jan 2013 09:10:38 -0800 Subject: [PATCH] Move test cleanup into cleanup functions --- tests-clar/config/read.c | 6 +++- tests-clar/core/env.c | 32 ++++++++++++--------- tests-clar/index/tests.c | 6 +++- tests-clar/network/fetchlocal.c | 6 +++- tests-clar/pack/packbuilder.c | 49 +++++++++++++++++++-------------- tests-clar/stash/save.c | 2 +- tests-clar/status/worktree.c | 16 +++++++---- 7 files changed, 75 insertions(+), 42 deletions(-) diff --git a/tests-clar/config/read.c b/tests-clar/config/read.c index d2ad0de58..b603acb5c 100644 --- a/tests-clar/config/read.c +++ b/tests-clar/config/read.c @@ -1,5 +1,10 @@ #include "clar_libgit2.h" +void test_config_read__cleanup(void) +{ + cl_fixture_cleanup("./empty"); +} + void test_config_read__simple_read(void) { git_config *cfg; @@ -441,7 +446,6 @@ void test_config_read__can_load_and_parse_an_empty_config_file(void) cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither")); git_config_free(cfg); - cl_fixture_cleanup("./empty"); } void test_config_read__cannot_load_a_non_existing_config_file(void) diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c index d1d98885a..27e52ec3b 100644 --- a/tests-clar/core/env.c +++ b/tests-clar/core/env.c @@ -14,6 +14,18 @@ static const char *env_vars[NUM_VARS] = { "HOME" }; static char *env_save[NUM_VARS]; +static char *home_values[] = { + "fake_home", + "fáke_hõme", /* all in latin-1 supplement */ + "fĀke_Ĥome", /* latin extended */ + "fακε_hοmέ", /* having fun with greek */ + "faงe_นome", /* now I have no idea, but thai characters */ + "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */ + "\xe1\xb8\x9fẢke_hoṁe", /* latin extended additional */ + "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */ + NULL +}; + void test_core_env__initialize(void) { int i; @@ -24,6 +36,8 @@ void test_core_env__initialize(void) void test_core_env__cleanup(void) { int i; + char **val; + for (i = 0; i < NUM_VARS; ++i) { cl_setenv(env_vars[i], env_save[i]); #ifdef GIT_WIN32 @@ -31,11 +45,16 @@ void test_core_env__cleanup(void) #endif env_save[i] = NULL; } + + for (val = home_values; *val != NULL; val++) { + cl_fixture_cleanup(*val); + } } static void setenv_and_check(const char *name, const char *value) { char *check; + cl_git_pass(cl_setenv(name, value)); check = cl_getenv(name); cl_assert_equal_s(value, check); @@ -46,17 +65,6 @@ static void setenv_and_check(const char *name, const char *value) void test_core_env__0(void) { - static char *home_values[] = { - "fake_home", - "fáke_hõme", /* all in latin-1 supplement */ - "fĀke_Ĥome", /* latin extended */ - "fακε_hοmέ", /* having fun with greek */ - "faงe_นome", /* now I have no idea, but thai characters */ - "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */ - "\xe1\xb8\x9fẢke_hoṁe", /* latin extended additional */ - "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */ - NULL - }; git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; char testfile[16], tidx = '0'; char **val; @@ -122,8 +130,6 @@ void test_core_env__0(void) } } #endif - - cl_fixture_cleanup(*val); } git_buf_free(&path); diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c index 8359ce027..d2ad71cc6 100644 --- a/tests-clar/index/tests.c +++ b/tests-clar/index/tests.c @@ -24,6 +24,7 @@ static struct test_entry test_entries[] = { {48, "src/revobject.h", 1448, 0x4C3F7FE2} }; +static char *path_to_cleanup = NULL; // Helpers static void copy_file(const char *src, const char *dst) @@ -74,6 +75,9 @@ void test_index_tests__initialize(void) void test_index_tests__cleanup(void) { + if (path_to_cleanup) + cl_fixture_cleanup(path_to_cleanup); + path_to_cleanup = NULL; } @@ -246,7 +250,7 @@ void test_index_tests__add(void) git_index_free(index); git_repository_free(repo); - cl_fixture_cleanup("myrepo"); + path_to_cleanup = "myrepo"; } void test_index_tests__add_from_workdir_to_a_bare_repository_returns_EBAREPO(void) diff --git a/tests-clar/network/fetchlocal.c b/tests-clar/network/fetchlocal.c index 48f30e7c5..9ffbedbfe 100644 --- a/tests-clar/network/fetchlocal.c +++ b/tests-clar/network/fetchlocal.c @@ -11,6 +11,11 @@ static void transfer_cb(const git_transfer_progress *stats, void *payload) (*callcount)++; } +void test_network_fetchlocal__cleanup(void) +{ + cl_fixture_cleanup("foo"); +} + void test_network_fetchlocal__complete(void) { git_repository *repo; @@ -33,7 +38,6 @@ void test_network_fetchlocal__complete(void) git_strarray_free(&refnames); git_remote_free(origin); git_repository_free(repo); - cl_fixture_cleanup("foo"); } void test_network_fetchlocal__partial(void) diff --git a/tests-clar/pack/packbuilder.c b/tests-clar/pack/packbuilder.c index 31823eac1..c36b720e2 100644 --- a/tests-clar/pack/packbuilder.c +++ b/tests-clar/pack/packbuilder.c @@ -9,6 +9,26 @@ static git_packbuilder *_packbuilder; static git_indexer *_indexer; static git_vector _commits; static int _commits_is_initialized; +static char *path_to_cleanup = NULL; +static git_oid oid_to_cleanup = {{0}}; + +static void cleanup_pack(const git_oid *oid) +{ + char *hash, path[1024] = {0}; + + if (git_oid_iszero(&oid_to_cleanup)) return; + + hash = git_oid_allocfmt(oid); + + sprintf(path, "pack-%s.idx", hash); + p_unlink(path); + + sprintf(path, "pack-%s.pack", hash); + p_unlink(path); + + git__free(hash); + git_oid_fromstrn(&oid_to_cleanup, "", 0); +} void test_pack_packbuilder__initialize(void) { @@ -43,6 +63,12 @@ void test_pack_packbuilder__cleanup(void) git_repository_free(_repo); _repo = NULL; + + if (path_to_cleanup) + cl_fixture_cleanup(path_to_cleanup); + path_to_cleanup = NULL; + + cleanup_pack(&oid_to_cleanup); } static void seed_packbuilder(void) @@ -73,24 +99,10 @@ static void seed_packbuilder(void) } } -static void cleanup_pack(const git_oid *oid) -{ - char *hash, path[1024] = {0}; - - hash = git_oid_allocfmt(oid); - - sprintf(path, "pack-%s.idx", hash); - p_unlink(path); - - sprintf(path, "pack-%s.pack", hash); - p_unlink(path); - - git__free(hash); -} - void test_pack_packbuilder__create_pack(void) { git_transfer_progress stats; + path_to_cleanup = "testpack.pack"; seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, "testpack.pack")); @@ -98,9 +110,8 @@ void test_pack_packbuilder__create_pack(void) cl_git_pass(git_indexer_new(&_indexer, "testpack.pack")); cl_git_pass(git_indexer_run(_indexer, &stats)); cl_git_pass(git_indexer_write(_indexer)); + git_oid_cpy(&oid_to_cleanup, git_indexer_hash(_indexer)); - cl_fixture_cleanup("testpack.pack"); - cleanup_pack(git_indexer_hash(_indexer)); } static git_transfer_progress stats; @@ -116,13 +127,11 @@ static int foreach_cb(void *buf, size_t len, void *payload) void test_pack_packbuilder__foreach(void) { git_indexer_stream *idx; - git_oid oid; seed_packbuilder(); cl_git_pass(git_indexer_stream_new(&idx, ".", NULL, NULL)); cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx)); cl_git_pass(git_indexer_stream_finalize(idx, &stats)); - git_oid_cpy(&oid, git_indexer_stream_hash(idx)); + git_oid_cpy(&oid_to_cleanup, git_indexer_stream_hash(idx)); git_indexer_stream_free(idx); - cleanup_pack(&oid); } diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c index 2302ebb4d..e6033e1ef 100644 --- a/tests-clar/stash/save.c +++ b/tests-clar/stash/save.c @@ -32,6 +32,7 @@ void test_stash_save__cleanup(void) repo = NULL; cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_fixture_cleanup("sorry-it-is-a-non-bare-only-party"); } static void assert_object_oid(const char* revision, const char* expected_oid, git_otype type) @@ -211,7 +212,6 @@ void test_stash_save__cannot_stash_against_a_bare_repository(void) git_stash_save(&stash_tip_oid, local, signature, NULL, GIT_STASH_DEFAULT)); git_repository_free(local); - cl_fixture_cleanup("sorry-it-is-a-non-bare-only-party"); } void test_stash_save__can_stash_against_a_detached_head(void) diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c index 70fbca9e8..85363a970 100644 --- a/tests-clar/status/worktree.c +++ b/tests-clar/status/worktree.c @@ -6,6 +6,8 @@ #include "util.h" #include "path.h" +static char *path_to_cleanup = NULL; + /** * Initializer * @@ -25,6 +27,10 @@ void test_status_worktree__initialize(void) void test_status_worktree__cleanup(void) { cl_git_sandbox_cleanup(); + + if (path_to_cleanup) + cl_fixture_cleanup(path_to_cleanup); + path_to_cleanup = NULL; } /** @@ -446,7 +452,7 @@ void test_status_worktree__first_commit_in_progress(void) git_index_free(index); git_repository_free(repo); - cl_fixture_cleanup("getting_started"); + path_to_cleanup = "getting_started"; } @@ -596,7 +602,7 @@ void test_status_worktree__bracket_in_filename(void) git_index_free(index); git_repository_free(repo); - cl_fixture_cleanup("with_bracket"); + path_to_cleanup = "with_bracket"; } void test_status_worktree__space_in_filename(void) @@ -661,7 +667,7 @@ void test_status_worktree__space_in_filename(void) git_index_free(index); git_repository_free(repo); - cl_fixture_cleanup("with_space"); + path_to_cleanup = "with_space"; } static const char *filemode_paths[] = { @@ -772,7 +778,7 @@ void test_status_worktree__disable_pathspec_match(void) ); git_repository_free(repo); - cl_fixture_cleanup("pathspec"); + path_to_cleanup = "pathspec"; } @@ -825,7 +831,7 @@ void test_status_worktree__new_staged_file_must_handle_crlf(void) git_config_free(config); git_index_free(index); git_repository_free(repo); - cl_fixture_cleanup("getting_started"); + path_to_cleanup = "getting_started"; } void test_status_worktree__line_endings_dont_count_as_changes_with_autocrlf(void)