1import os
2
3import infra.basetest
4
5
6class TestGnuplot(infra.basetest.BRTest):
7    rootfs_overlay = \
8        infra.filepath("tests/package/test_gnuplot/rootfs-overlay")
9    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
10        f"""
11        BR2_PACKAGE_GNUPLOT=y
12        BR2_ROOTFS_OVERLAY="{rootfs_overlay}"
13        BR2_TARGET_ROOTFS_CPIO=y
14        # BR2_TARGET_ROOTFS_TAR is not set
15        """
16
17    def gen_gnuplot_cmd(self, gpcmd):
18        return f"gnuplot -e '{gpcmd}'"
19
20    def test_run(self):
21        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
22        self.emulator.boot(arch="armv5",
23                           kernel="builtin",
24                           options=["-initrd", cpio_file])
25        self.emulator.login()
26
27        # We check the program can run.
28        self.assertRunOk("gnuplot --version")
29
30        # When the locale is C, Gnuplot print the warning:
31        # "line 0: warning: iconv failed to convert degree sign"
32        # We set the locale to avoid this warning.
33        self.assertRunOk('export LC_ALL="en_US.UTF-8"')
34
35        # We check Gnuplot can print a string.
36        string = "Hello Buildroot !"
37        cmd = self.gen_gnuplot_cmd(f'print "{string}"')
38        out, ret = self.emulator.run(cmd)
39        self.assertEqual(ret, 0)
40        self.assertEqual(out[0], string)
41
42        # We check Gnuplot can do a simple arithmetic operation.
43        op1 = 123
44        op2 = 456
45        expected_result = op1 * op2
46        cmd = self.gen_gnuplot_cmd(f"print {op1} * {op2}")
47        out, ret = self.emulator.run(cmd)
48        self.assertEqual(ret, 0)
49        self.assertEqual(int(out[0]), expected_result)
50
51        # We check Gnuplot can return a specific exit code.
52        exit_code = 123
53        cmd = self.gen_gnuplot_cmd(f"exit status {exit_code}")
54        _, ret = self.emulator.run(cmd)
55        self.assertEqual(ret, exit_code)
56
57        # We render a simple plot on the terminal.
58        gpcmd = "set term dumb; set grid; plot [-5:5] x**2;"
59        cmd = self.gen_gnuplot_cmd(gpcmd)
60        self.assertRunOk(cmd)
61
62        # We check a Gnuplot script executes correctly.
63        cmd = "gnuplot /root/gnuplot-test.plot"
64        self.assertRunOk(cmd)
65
66        # Our Gnuplot script is supposed to have generated a text
67        # output of the plot. We check this file contains the plot
68        # title set in the script.
69        exp_str = "Buildroot Test Plot"
70        cmd = f"grep -Fo '{exp_str}' /root/gnuplot-test.txt"
71        out, ret = self.emulator.run(cmd)
72        self.assertEqual(ret, 0)
73        self.assertEqual(out[0], exp_str)
74