[Buildroot] [git commit branch/2025.02.x] support/testing: guile: new runtime test

Titouan Christophe via buildroot <[email protected]>
Newsgroups net.busybox.buildroot
Message-ID <[email protected]>
commit: https://gitlab.com/buildroot.org/buildroot/-/commit/e6e34723304093d51817defd78635408707e802d
branch: https://gitlab.com/buildroot.org/buildroot/-/tree/2025.02.x

Signed-off-by: Julien Olivain <[email protected]>
(cherry picked from commit 29c988c7fc4f4916f023e08e939e1326056ebf2f)
Signed-off-by: Titouan Christophe <[email protected]>
---
 DEVELOPERS                                         |  2 +
 support/testing/tests/package/test_guile.py        | 76 ++++++++++++++++++++++
 .../test_guile/rootfs-overlay/root/fact.scm        | 13 ++++
 3 files changed, 91 insertions(+)

diff --git a/DEVELOPERS b/DEVELOPERS
index a0111b631f..fbe573ebed 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1852,6 +1852,8 @@ F:	support/testing/tests/package/test_gnuradio/
 F:	support/testing/tests/package/test_gpsd.py
 F:	support/testing/tests/package/test_gpsd/
 F:	support/testing/tests/package/test_gstreamer1.py
+F:	support/testing/tests/package/test_guile.py
+F:	support/testing/tests/package/test_guile/
 F:	support/testing/tests/package/test_gzip.py
 F:	support/testing/tests/package/test_highway.py
 F:	support/testing/tests/package/test_hwloc.py
diff --git a/support/testing/tests/package/test_guile.py b/support/testing/tests/package/test_guile.py
new file mode 100644
index 0000000000..0af4a3f65c
--- /dev/null
+++ b/support/testing/tests/package/test_guile.py
@@ -0,0 +1,76 @@
+import os
+from math import factorial
+
+import infra.basetest
+
+
+class TestGuile(infra.basetest.BRTest):
+    rootfs_overlay = \
+        infra.filepath("tests/package/test_guile/rootfs-overlay")
+    config = \
+        f"""
+        BR2_aarch64=y
+        BR2_TOOLCHAIN_EXTERNAL=y
+        BR2_GENERATE_LOCALE="en_US.UTF-8"
+        BR2_LINUX_KERNEL=y
+        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
+        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.18.40"
+        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
+        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
+        BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
+        BR2_SYSTEM_ENABLE_NLS=y
+        BR2_TARGET_ROOTFS_CPIO=y
+        BR2_TARGET_ROOTFS_CPIO_GZIP=y
+        BR2_ROOTFS_OVERLAY="{rootfs_overlay}"
+        # BR2_TARGET_ROOTFS_TAR is not set
+        BR2_PACKAGE_GUILE=y
+        """
+
+    def guile_cmd(self, guile_expr):
+        return f"guile -q -c '{guile_expr}'"
+
+    def test_run(self):
+        img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
+        kern = os.path.join(self.builddir, "images", "Image")
+        self.emulator.boot(arch="aarch64",
+                           kernel=kern,
+                           options=["-M", "virt", "-cpu", "cortex-a57", "-m", "512M", "-initrd", img])
+        self.emulator.login()
+
+        # Guile needs a locale configured to work properly.
+        self.assertRunOk("export LANG=en_US.UTF-8")
+
+        # Check the program works.
+        self.assertRunOk("guile --version")
+
+        # Check a string output.
+        string = "Hello Buildroot!"
+        cmd = self.guile_cmd(f'(display "{string}")(newline)')
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(output[0], string)
+
+        # Check the exit status works.
+        code = 123
+        cmd = self.guile_cmd(f"(exit {code})")
+        _, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, code)
+
+        # Check basic artihmetic works.
+        val1 = 123
+        val2 = 456
+        guile_expr = f'(display (* {val1} {val2}))(newline)'
+        cmd = self.guile_cmd(guile_expr)
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        expected_result = val1 * val2
+        self.assertEqual(int(output[0]), expected_result)
+
+        # Check we can call a Guile script. We disable auto
+        # compilation to suppress compilation and cache messages.
+        val = 12
+        cmd = f"GUILE_AUTO_COMPILE=0 /root/fact.scm {val}"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        expected_result = factorial(val)
+        self.assertEqual(int(output[0]), expected_result)
diff --git a/support/testing/tests/package/test_guile/rootfs-overlay/root/fact.scm b/support/testing/tests/package/test_guile/rootfs-overlay/root/fact.scm
new file mode 100755
index 0000000000..a937ff4041
--- /dev/null
+++ b/support/testing/tests/package/test_guile/rootfs-overlay/root/fact.scm
@@ -0,0 +1,13 @@
+#! /usr/bin/env guile
+!#
+
+; Adapted from "The Guile Reference Manual"
+; Section 4.3.4 Scripting Examples
+; https://doc.guix.gnu.org/guile/latest/en/html_node/Scripting-Examples.html
+
+(define (fact n)
+  (if (zero? n) 1
+    (* n (fact (- n 1)))))
+
+(display (fact (string->number (cadr (command-line)))))
+(newline)
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.