1import math
2import os
3
4import infra.basetest
5
6
7class TestTcl(infra.basetest.BRTest):
8    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
9        f"""
10        BR2_PACKAGE_TCL=y
11        # BR2_PACKAGE_TCL_SHLIB_ONLY is not set
12        BR2_ROOTFS_OVERLAY="{infra.filepath("tests/package/test_tcl/rootfs-overlay")}"
13        BR2_TARGET_ROOTFS_CPIO=y
14        # BR2_TARGET_ROOTFS_TAR is not set
15        """
16
17    def test_run(self):
18        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
19        self.emulator.boot(arch="armv5",
20                           kernel="builtin",
21                           options=["-initrd", cpio_file])
22        self.emulator.login()
23
24        # Print tcl the interpreter version and patchlevel.
25        tcl_cmds = "puts \"tcl_version: $tcl_version\";"
26        tcl_cmds += "puts \"patchlevel: [info patchlevel]\";"
27        tcl_cmds += "exit 0"
28        cmd = f"echo '{tcl_cmds}' | tclsh"
29        self.assertRunOk(cmd)
30
31        # We check tclsh correctly print a string.
32        txt = "Hello Buildroot"
33        cmd = f"echo 'puts \"{txt}\"; exit 0' | tclsh"
34        output, exit_code = self.emulator.run(cmd)
35        self.assertEqual(exit_code, 0)
36        self.assertEqual(output[0], txt)
37
38        # We check tclsh can return a non-zero exit code.
39        expected_code = 123
40        cmd = f"echo 'exit {expected_code}' | tclsh"
41        _, exit_code = self.emulator.run(cmd)
42        self.assertEqual(exit_code, expected_code)
43
44        # We check a tcl program computing factorial run correctly.
45        input_value = 12
46        expected_output = str(math.factorial(input_value))
47        cmd = f"/root/factorial.tcl {input_value}"
48        output, exit_code = self.emulator.run(cmd)
49        self.assertEqual(exit_code, 0)
50        self.assertEqual(output[0], expected_output)
51