test_gnuplot.py 2.5 KB

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