]> git.ipfire.org Git - pakfire.git/commitdiff
testsuite: Allocate a Pakfire context for each test automatically
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Nov 2017 12:06:33 +0000 (13:06 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Nov 2017 12:06:33 +0000 (13:06 +0100)
This reduces the amount of code that is to be written for each test.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
tests/libpakfire/key.c
tests/libpakfire/main.c
tests/libpakfire/pakfire.h [deleted file]
tests/testsuite.c
tests/testsuite.h

index 7b9b3030bf76c819e7baf7a503b899349afe8503..9e23f7742884ff5657f5e8ab1a7b790952b574c9 100644 (file)
@@ -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)
index 6ee8ef3dfccc3a03b3cd3fb9bce7ed0bb7962c5a..81fda419fef4b88033cec3dc5ea531b7cf491e3d 100644 (file)
 
 #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;
 }
index 0ac36d2b686d53b1f9951cab386a17a64d4fa1f6..018bbc7e58229f62ed8456b2465700dff941fad7 100644 (file)
 
 #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 (file)
index e42d944..0000000
+++ /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 <http://www.gnu.org/licenses/>.       #
-#                                                                             #
-#############################################################################*/
-
-#include <pakfire/pakfire.h>
-
-#include "../testsuite.h"
-
-static Pakfire init_pakfire() {
-    const char* path = TEST_PATH;
-
-    return pakfire_create(path, NULL);
-}
index b427405dc2a42ec2af1fc5193ca734d0402b91cd..031b77b3abc8cba75b37adc05c4f98e2e9f3909c 100644 (file)
@@ -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;
 }
 
index f6dce93e7653b9403a8936bc8d8fba1348804062..1388266c35f65415f5288875cc4d36a1d11cf766 100644 (file)
@@ -24,6 +24,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include <pakfire/pakfire.h>
+
 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 {