From 418051aeb2c0d91c40137efdf178f59e31c2b9dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20M=C3=A9lotte?= Date: Thu, 26 Jun 2025 13:19:18 +0200 Subject: [PATCH] support/testing: new runtime test for gumbo-parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new test requires a br2-external directory because we compile a small test program on the host and install it on the target, but it's not useful to have it in the main Buildroot package tree. The test program loads and parses a sample HTML document. Taking inspiration from 'examples/get_title.c' in gumbo-parser, it also searches for the title of the document just to check that we can do more than the parsing. Signed-off-by: Raphaël Mélotte Signed-off-by: Julien Olivain (cherry picked from commit da23be6338c1e82f39475d166407f97511e0b666) Signed-off-by: Thomas Perale --- .../br2-external/gumbo-parser/Config.in | 1 + .../br2-external/gumbo-parser/external.desc | 1 + .../br2-external/gumbo-parser/external.mk | 1 + .../package/gumbo-parser-test/Config.in | 7 ++++ .../gumbo-parser-test/gumbo-parser-test.mk | 19 +++++++++ .../package/gumbo-parser-test/gumbo_test.c | 40 +++++++++++++++++++ .../tests/package/test_gumbo_parser.py | 27 +++++++++++++ 7 files changed, 96 insertions(+) create mode 100644 support/testing/tests/package/br2-external/gumbo-parser/Config.in create mode 100644 support/testing/tests/package/br2-external/gumbo-parser/external.desc create mode 100644 support/testing/tests/package/br2-external/gumbo-parser/external.mk create mode 100644 support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/Config.in create mode 100644 support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo-parser-test.mk create mode 100644 support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo_test.c create mode 100644 support/testing/tests/package/test_gumbo_parser.py diff --git a/support/testing/tests/package/br2-external/gumbo-parser/Config.in b/support/testing/tests/package/br2-external/gumbo-parser/Config.in new file mode 100644 index 0000000000..28a97c1dfe --- /dev/null +++ b/support/testing/tests/package/br2-external/gumbo-parser/Config.in @@ -0,0 +1 @@ +source "$BR2_EXTERNAL_GUMBO_PARSER_PATH/package/gumbo-parser-test/Config.in" diff --git a/support/testing/tests/package/br2-external/gumbo-parser/external.desc b/support/testing/tests/package/br2-external/gumbo-parser/external.desc new file mode 100644 index 0000000000..4bda9627e9 --- /dev/null +++ b/support/testing/tests/package/br2-external/gumbo-parser/external.desc @@ -0,0 +1 @@ +name: GUMBO_PARSER diff --git a/support/testing/tests/package/br2-external/gumbo-parser/external.mk b/support/testing/tests/package/br2-external/gumbo-parser/external.mk new file mode 100644 index 0000000000..bdc8b2fc79 --- /dev/null +++ b/support/testing/tests/package/br2-external/gumbo-parser/external.mk @@ -0,0 +1 @@ +include $(sort $(wildcard $(BR2_EXTERNAL_GUMBO_PARSER_PATH)/package/*/*.mk)) diff --git a/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/Config.in b/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/Config.in new file mode 100644 index 0000000000..714d3e0198 --- /dev/null +++ b/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/Config.in @@ -0,0 +1,7 @@ +config BR2_PACKAGE_GUMBO_PARSER_TEST + bool "gumbo-parser-test" + depends on BR2_PACKAGE_GUMBO_PARSER + help + Test program for gumbo-parser library. + This package builds a simple test program that demonstrates + basic HTML parsing functionality using gumbo-parser. diff --git a/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo-parser-test.mk b/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo-parser-test.mk new file mode 100644 index 0000000000..111f1977bf --- /dev/null +++ b/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo-parser-test.mk @@ -0,0 +1,19 @@ +################################################################################ +# +# gumbo-parser-test +# +################################################################################ + +GUMBO_PARSER_TEST_DEPENDENCIES = gumbo-parser + +define GUMBO_PARSER_TEST_BUILD_CMDS + $(TARGET_CC) $(TARGET_CFLAGS) -o $(@D)/gumbo_test \ + $(GUMBO_PARSER_TEST_PKGDIR)/gumbo_test.c \ + $(TARGET_LDFLAGS) -lgumbo +endef + +define GUMBO_PARSER_TEST_INSTALL_TARGET_CMDS + $(INSTALL) -D -m 0755 $(@D)/gumbo_test $(TARGET_DIR)/usr/bin/gumbo_test +endef + +$(eval $(generic-package)) diff --git a/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo_test.c b/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo_test.c new file mode 100644 index 0000000000..f9c9ca4a91 --- /dev/null +++ b/support/testing/tests/package/br2-external/gumbo-parser/package/gumbo-parser-test/gumbo_test.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +static void search_for_title(GumboNode* node) { + if (node->type != GUMBO_NODE_ELEMENT) { + return; + } + + if (node->v.element.tag == GUMBO_TAG_TITLE) { + GumboNode* text = node->v.element.children.data[0]; + if (text->type == GUMBO_NODE_TEXT) { + printf("Found title: %s\n", text->v.text.text); + } + return; + } + + GumboVector* children = &node->v.element.children; + for (unsigned int i = 0; i < children->length; ++i) { + search_for_title(children->data[i]); + } +} + +int main() { + const char* html = "Test HTML

Hello World

"; + + GumboOutput* output = gumbo_parse(html); + + if (output == NULL) { + printf("HTML parsing failed\n"); + return 1; + } + + printf("HTML parsing successful\n"); + search_for_title(output->root); + + gumbo_destroy_output(&kGumboDefaultOptions, output); + return 0; +} diff --git a/support/testing/tests/package/test_gumbo_parser.py b/support/testing/tests/package/test_gumbo_parser.py new file mode 100644 index 0000000000..efa8ec91a7 --- /dev/null +++ b/support/testing/tests/package/test_gumbo_parser.py @@ -0,0 +1,27 @@ +import os + +import infra.basetest + + +class TestGumboParser(infra.basetest.BRTest): + br2_external = [infra.filepath("tests/package/br2-external/gumbo-parser")] + config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \ + """ + BR2_PACKAGE_GUMBO_PARSER=y + BR2_PACKAGE_GUMBO_PARSER_TEST=y + BR2_TARGET_ROOTFS_CPIO=y + # BR2_TARGET_ROOTFS_TAR is not set + """ + + def test_run(self): + cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") + self.emulator.boot(arch="armv5", + kernel="builtin", + options=["-initrd", cpio_file]) + self.emulator.login() + + # Run the test program and check output + out, ret = self.emulator.run("/usr/bin/gumbo_test") + self.assertEqual(ret, 0) + self.assertIn("HTML parsing successful", "\n".join(out)) + self.assertIn("Found title: Test HTML", "\n".join(out))