test_less.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import infra.basetest
  3. class TestLess(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
  7. BR2_PACKAGE_LESS=y
  8. BR2_TARGET_ROOTFS_CPIO=y
  9. # BR2_TARGET_ROOTFS_TAR is not set
  10. """
  11. def test_run(self):
  12. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  13. self.emulator.boot(arch="armv5",
  14. kernel="builtin",
  15. options=["-initrd", cpio_file])
  16. self.emulator.login()
  17. # Check the program can execute. This command also checks that
  18. # the "less" program is from the actual "less" package, rather
  19. # than the Busybox implementation (the Busybox "less" applet
  20. # does not recognize the "--version" option and would fail).
  21. self.assertRunOk("less --version")
  22. # We create a test file.
  23. ref_txt = "Hello Buildroot!"
  24. input_fname = "input.txt"
  25. self.assertRunOk(f"echo \'{ref_txt}\' > {input_fname}")
  26. # "less" is mainly an interactive user program and uses
  27. # terminal control characters. This test checks a basic "less"
  28. # invocation in which there is no user interaction. The
  29. # program is expected to give back the input data.
  30. output, exit_code = self.emulator.run(f"less -F {input_fname}")
  31. self.assertEqual(exit_code, 0)
  32. # "less" might insert a carriage-return ^M control character,
  33. # which will be converted to a new-line (by the
  34. # str.splitlines() in Emulator.run()). We check that our
  35. # reference text line is in the output (rather than only
  36. # testing output[0]).
  37. self.assertIn(ref_txt, output)
  38. # We redo about the same test, with "less" reading stdin this
  39. # time. We also use the "less -o log" option to log the output
  40. # into a file. We expect to see our reference text on stdout.
  41. output_fname = "output.txt"
  42. cmd = f"cat {input_fname} | less -F -o {output_fname}"
  43. output, exit_code = self.emulator.run(cmd)
  44. self.assertEqual(exit_code, 0)
  45. self.assertIn(ref_txt, output)
  46. # The output file content which logged the output is also
  47. # expected to be the same as the input.
  48. self.assertRunOk(f"cmp {input_fname} {output_fname}")