From: Michael Tremer Date: Thu, 30 Nov 2017 12:06:33 +0000 (+0100) Subject: testsuite: Allocate a Pakfire context for each test automatically X-Git-Tag: 0.9.28~1285^2~1237 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f907e1a27ddcdc27c8710430fa00ad9a55cb7208;p=pakfire.git testsuite: Allocate a Pakfire context for each test automatically This reduces the amount of code that is to be written for each test. Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 7b9b3030b..9e23f7742 100644 --- a/Makefile.am +++ b/Makefile.am @@ -492,9 +492,6 @@ tests_libtestsuite_la_CPPFLAGS = \ $(TESTSUITE_CPPFLAGS) \ -DABS_TOP_BUILDDIR=\"$(abs_top_builddir)\" -EXTRA_DIST += \ - tests/libpakfire/pakfire.h - TESTSUITE_CPPFLAGS = \ $(AM_CPPFLAGS) \ $(PAKFIRE_CPPFLAGS) diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 6ee8ef3df..81fda419f 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -25,15 +25,10 @@ #include "../testsuite.h" #include "key.h" -#include "pakfire.h" int test_init(const test_t* t) { - Pakfire pakfire = init_pakfire(); - if (!pakfire) - return EXIT_FAILURE; - // Try loading any keys & delete them all - PakfireKey* keys = pakfire_key_list(pakfire); + PakfireKey* keys = pakfire_key_list(t->pakfire); while (keys && *keys) { PakfireKey key = *keys++; @@ -42,7 +37,7 @@ int test_init(const test_t* t) { } // Load list of keys again - keys = pakfire_key_list(pakfire); + keys = pakfire_key_list(t->pakfire); // Must be empty now assert_return(keys == NULL, EXIT_FAILURE); @@ -51,20 +46,16 @@ int test_init(const test_t* t) { } int test_import(const test_t* t) { - Pakfire pakfire = init_pakfire(); - if (!pakfire) - return EXIT_FAILURE; - // Try to delete the key just in case it // has been imported before - PakfireKey key = pakfire_key_get(pakfire, TEST_KEY_FINGERPRINT); + PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT); if (key) { pakfire_key_delete(key); pakfire_key_unref(key); } // Import a key - PakfireKey* keys = pakfire_key_import(pakfire, TEST_KEY_DATA); + PakfireKey* keys = pakfire_key_import(t->pakfire, TEST_KEY_DATA); // We should have a list with precisely one key object assert_return(keys, EXIT_FAILURE); @@ -78,24 +69,19 @@ int test_import(const test_t* t) { const char* fingerprint = pakfire_key_get_fingerprint(key); assert_return(strcmp(fingerprint, TEST_KEY_FINGERPRINT) == 0, EXIT_FAILURE); - pakfire_unref(pakfire); - return EXIT_SUCCESS; } int test_export(const test_t* t) { - Pakfire pakfire = init_pakfire(); - if (!pakfire) - return EXIT_FAILURE; - - PakfireKey key = pakfire_key_get(pakfire, TEST_KEY_FINGERPRINT); + PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT); assert_return(key, EXIT_FAILURE); char* data = pakfire_key_export(key, 0); assert_return(data, EXIT_FAILURE); - pakfire_free(data); - pakfire_unref(pakfire); + LOG("Exported key:\n%s\n", data); + + pakfire_free(data); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/main.c b/tests/libpakfire/main.c index 0ac36d2b6..018bbc7e5 100644 --- a/tests/libpakfire/main.c +++ b/tests/libpakfire/main.c @@ -24,30 +24,16 @@ #include "../testsuite.h" -#include "pakfire.h" - static int test_init(const test_t* t) { - Pakfire pakfire = init_pakfire(); - if (!pakfire) - return EXIT_FAILURE; - - LOG("Allocated at %p\n", pakfire); - - pakfire_unref(pakfire); + LOG("Allocated at %p\n", t->pakfire); return EXIT_SUCCESS; } static int test_path(const test_t* t) { - Pakfire pakfire = init_pakfire(); - if (!pakfire) - return EXIT_FAILURE; - - const char* path = pakfire_get_path(pakfire); + const char* path = pakfire_get_path(t->pakfire); assert_return(strcmp(path, TEST_PATH) == 0, EXIT_FAILURE); - pakfire_unref(pakfire); - return EXIT_SUCCESS; } diff --git a/tests/libpakfire/pakfire.h b/tests/libpakfire/pakfire.h deleted file mode 100644 index e42d944f4..000000000 --- a/tests/libpakfire/pakfire.h +++ /dev/null @@ -1,29 +0,0 @@ -/*############################################################################# -# # -# Pakfire - The IPFire package management system # -# Copyright (C) 2017 Pakfire development team # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see . # -# # -#############################################################################*/ - -#include - -#include "../testsuite.h" - -static Pakfire init_pakfire() { - const char* path = TEST_PATH; - - return pakfire_create(path, NULL); -} diff --git a/tests/testsuite.c b/tests/testsuite.c index b427405dc..031b77b3a 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -37,13 +37,19 @@ int testsuite_init() { return 0; } -static int test_run(const test_t* t) { +static int test_run(test_t* t) { LOG("running %s\n", t->name); + t->pakfire = pakfire_create(TEST_PATH, NULL); + assert_return(t->pakfire, EXIT_FAILURE); + int r = t->func(t); if (r) LOG("Test failed with error code: %d\n", r); + // Release pakfire + pakfire_unref(t->pakfire); + return r; } diff --git a/tests/testsuite.h b/tests/testsuite.h index f6dce93e7..1388266c3 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -24,6 +24,8 @@ #include #include +#include + extern const char* TEST_PATH; // Forward declaration @@ -34,6 +36,7 @@ typedef int (*test_function_t)(const struct test* t); typedef struct test { const char* name; test_function_t func; + Pakfire pakfire; } test_t; typedef struct testsuite {