test_tcl.py 1.8 KB

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