test_ed.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. import infra.basetest
  3. class TestEd(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_ED=y
  7. BR2_TARGET_ROOTFS_CPIO=y
  8. # BR2_TARGET_ROOTFS_TAR is not set
  9. """
  10. def run_ed_cmds(self, ed_cmds):
  11. cmd = "ed <<EOF\n"
  12. cmd += "\n".join(ed_cmds)
  13. cmd += "\nEOF"
  14. self.assertRunOk(cmd)
  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. This also check we have the
  22. # actual GNU ed, rather than the Busybox ed, which does not
  23. # recognize the --version option.
  24. self.assertRunOk("ed --version")
  25. test_fname = "test.txt"
  26. input_text_lines = [
  27. "Hello World",
  28. "Embedded Linux is Hard."
  29. ]
  30. output_expected_text = [
  31. "Hello Buildroot",
  32. "---------------",
  33. "Making Embedded Linux Easy."
  34. ]
  35. # We define few "ed" command sequences, creating and editing a
  36. # text file. The final output of this sequence is expected to
  37. # match the expected text previously defined.
  38. create_file = ["a"]
  39. create_file += input_text_lines
  40. create_file += [
  41. ".",
  42. f"w {test_fname}",
  43. "q"
  44. ]
  45. edit_file = [
  46. f"r {test_fname}",
  47. "1",
  48. "s/World/Buildroot/",
  49. "2",
  50. "s/is Hard/Easy/",
  51. "s/^/Making /",
  52. "w",
  53. "q"
  54. ]
  55. insert_txt = [
  56. f"r {test_fname}",
  57. "2",
  58. "i",
  59. "This is a new line",
  60. ".",
  61. "w",
  62. "q"
  63. ]
  64. change_txt = [
  65. f"r {test_fname}",
  66. "2",
  67. "c",
  68. "---------------",
  69. ".",
  70. "w"
  71. "q"
  72. ]
  73. # We execute all "ed" command batches.
  74. ed_cmd_batches = [
  75. create_file,
  76. edit_file,
  77. insert_txt,
  78. change_txt
  79. ]
  80. for ed_cmd_batch in ed_cmd_batches:
  81. self.run_ed_cmds(ed_cmd_batch)
  82. # The final test file should contain the expected text.
  83. out, ret = self.emulator.run(f"cat {test_fname}")
  84. self.assertEqual(ret, 0)
  85. self.assertEqual(out, output_expected_text)