]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
test: Add a simple test program for the parser
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 30 Nov 2022 13:55:14 +0000 (13:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 30 Nov 2022 13:55:14 +0000 (13:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/parser/test.c [new file with mode: 0644]

index 2444915b1c0dd3b128d55613ffdf7acf556390b0..19423e405d622a751878fb83fadd24a1773c98fc 100644 (file)
@@ -37,6 +37,7 @@
 /tests/libpakfire/snapshot
 /tests/libpakfire/string
 /tests/libpakfire/util
+/tests/parser/test
 /tests/stub/root
 /tmp
 *.py[co]
index d672c652d849c2de2bd883f63f7adf8c14349896..af977f8cfecd9391a675e2bb664eea692c5db093 100644 (file)
@@ -693,6 +693,31 @@ TEST_STUB_ROOT = $(abs_top_builddir)/tests/stub/root
 
 # ------------------------------------------------------------------------------
 
+tests_parserdir = $(testsdir)/parser
+
+tests_parser_PROGRAMS = \
+       tests/parser/test
+
+dist_tests_parser_test_SOURCES = \
+       tests/parser/test.c
+
+tests_parser_test_CPPFLAGS = \
+       $(AM_CPPFLAGS) \
+       $(PAKFIRE_CPPFLAGS) \
+       -DPAKFIRE_PRIVATE
+
+tests_parser_test_CFLAGS = \
+       $(JSON_C_CFLAGS)
+
+tests_parser_test_LDFLAGS = \
+       $(AM_LDFLAGS)
+
+tests_parser_test_LDADD = \
+       libpakfire.la \
+       libpakfire-internal.la
+
+# ------------------------------------------------------------------------------
+
 dist_scripts_SCRIPTS = \
        src/scripts/check-buildroot \
        src/scripts/check-fhs \
diff --git a/tests/parser/test.c b/tests/parser/test.c
new file mode 100644 (file)
index 0000000..9e83f30
--- /dev/null
@@ -0,0 +1,103 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2022 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 <stdio.h>
+
+#include <pakfire/logging.h>
+#include <pakfire/pakfire.h>
+#include <pakfire/parser.h>
+#include <pakfire/util.h>
+
+int main(int argc, const char* argv[]) {
+       struct pakfire* pakfire = NULL;
+       struct pakfire_parser* parser = NULL;
+       struct pakfire_parser_error* error = NULL;
+       const char* message = NULL;
+       FILE* f = NULL;
+       int r = 1;
+
+       char root[PATH_MAX] = "/tmp/pakfire-parser-test.XXXXXX";
+
+       // Create test root directory
+       char* tmp = pakfire_mkdtemp(root);
+       if (!tmp) {
+               fprintf(stderr, "Could not create temporary directory %s: %m\n", root);
+               goto ERROR;
+       }
+
+       // Create a pakfire instance
+       r = pakfire_create(&pakfire, root, NULL, NULL,
+               0, LOG_DEBUG, pakfire_log_stderr, NULL);
+       if (r) {
+               fprintf(stderr, "Could not create Pakfire: %m\n");
+               goto ERROR;
+       }
+
+       // Create a new parser
+       parser = pakfire_parser_create(pakfire, NULL, NULL, 0);
+       if (!parser) {
+               fprintf(stderr, "Could not create a parser: %m\n");
+               goto ERROR;
+       }
+
+       // Try parsing all files that have been passed on the command line
+       for (unsigned int i = 1; i < argc; i++) {
+               printf("Parsing %s...\n", argv[i]);
+
+               // Try opening the file
+               f = fopen(argv[i], "r");
+               if (!f) {
+                       fprintf(stderr, "Could not open %s: %m\n", argv[i]);
+                       r = 1;
+                       goto ERROR;
+               }
+
+               // Try parsing the file
+               r = pakfire_parser_read(parser, f, &error);
+               if (r) {
+                       if (error) {
+                               message = pakfire_parser_error_get_message(error);
+                               fprintf(stderr, "PARSER ERROR: %s\n", message);
+
+                               fclose(f);
+                               goto ERROR;
+                       }
+               }
+
+               fclose(f);
+       }
+
+       // Dump the entire parser
+       char* dump = pakfire_parser_dump(parser);
+       if (dump) {
+               printf("%s\n", dump);
+               free(dump);
+       }
+
+ERROR:
+       if (error)
+               pakfire_parser_error_unref(error);
+       if (parser)
+               pakfire_parser_unref(parser);
+       if (pakfire)
+               pakfire_unref(pakfire);
+
+       return r;
+}