test_sed.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import os
  2. import infra.basetest
  3. class TestSed(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
  7. BR2_PACKAGE_SED=y
  8. BR2_TARGET_ROOTFS_CPIO=y
  9. # BR2_TARGET_ROOTFS_TAR is not set
  10. """
  11. def check_gnu_sed(self):
  12. in_file = "testfile.txt"
  13. # We create a test file for this test.
  14. self.assertRunOk(f"echo 'This is a test' > {in_file}")
  15. # Check we have the GNU sed by testing a GNU extension likely
  16. # not present in other implementation. See:
  17. # https://www.gnu.org/software/sed/manual/sed.html#Extended-Commands
  18. # Note: we cannot search for "GNU sed" in sed --version,
  19. # because busybox sed --version outputs: "This is not GNU sed
  20. # version 4.0". The 'F' and 'Q' sed commands are known to be
  21. # unimplemented in BusyBox 1.36.1.
  22. expected_code = 123
  23. sed_script = f"F;Q {expected_code}"
  24. cmd = f"sed '{sed_script}' {in_file}"
  25. output, exit_code = self.emulator.run(cmd)
  26. self.assertEqual(exit_code, expected_code)
  27. self.assertEqual(output, [in_file])
  28. def check_sed_substitute(self):
  29. testfile_num = 5
  30. # We create few different test files for this test.
  31. cmd = f'for i in $(seq {testfile_num}) ; do '
  32. cmd += 'echo "=== $i Hello ===" > file$i.txt ; '
  33. cmd += 'done'
  34. self.assertRunOk(cmd)
  35. # We reformat file content, in-place.
  36. sed_script = "s/^=== \\([0-9]*\\) \\(Hello\\) ===$/\\2 \\1/"
  37. cmd = f"sed -i '{sed_script}' file[0-9]*.txt"
  38. self.assertRunOk(cmd)
  39. # We substitute numbers with the string "Buildroot". We use an
  40. # extended regular expression (with the '+'), so we test with
  41. # the '-r' option.
  42. sed_script = "s/[0-9]+/Buildroot/g"
  43. cmd = f"sed -r -i '{sed_script}' file[0-9]*.txt"
  44. self.assertRunOk(cmd)
  45. # Our previous text manipulations are expected to end up with
  46. # the "Hello Buildroot" string in all files.
  47. cmd = "cat file[0-9]*.txt"
  48. output, exit_code = self.emulator.run(cmd)
  49. self.assertEqual(exit_code, 0)
  50. self.assertEqual(output, ["Hello Buildroot"] * testfile_num)
  51. def check_sed_line_count(self):
  52. # We use the '=' command to count lines.
  53. line_count = 1234
  54. cmd = f"seq {line_count} | sed -n '$='"
  55. output, exit_code = self.emulator.run(cmd)
  56. self.assertEqual(exit_code, 0)
  57. self.assertEqual(int(output[0]), line_count)
  58. def check_sed_line_address(self):
  59. input_file = "strings.txt"
  60. expected_file = "expected.txt"
  61. # We create simple data for this test.
  62. strings = ["one", "two", "three", "four", "five"]
  63. content = '\\n'.join(strings)
  64. cmd = f"echo -e \"{content}\" > {input_file}"
  65. self.assertRunOk(cmd)
  66. # The manipulation in this tests are expected to extract the
  67. # first and last of the input. We create the expected data for
  68. # comparison.
  69. expected_output = [strings[0], strings[-1]]
  70. content = '\\n'.join(expected_output)
  71. cmd = f"echo -e \"{content}\" > {expected_file}"
  72. self.assertRunOk(cmd)
  73. # We remove lines between strings "two" and "four" included.
  74. cmd = f"sed '/two/,/four/d' {input_file} > output1.txt"
  75. self.assertRunOk(cmd)
  76. # We check output is the same as the expected data.
  77. cmd = f"cmp {expected_file} output1.txt"
  78. self.assertRunOk(cmd)
  79. # We redo the same manipulation using line number addresses.
  80. cmd = f"sed -n '1p;5p' {input_file} > output2.txt"
  81. self.assertRunOk(cmd)
  82. # We check again output is correct.
  83. cmd = f"cmp {expected_file} output2.txt"
  84. self.assertRunOk(cmd)
  85. def test_run(self):
  86. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  87. self.emulator.boot(arch="armv5",
  88. kernel="builtin",
  89. options=["-initrd", cpio_file])
  90. self.emulator.login()
  91. # Check the program can execute
  92. self.assertRunOk("sed --version")
  93. self.check_gnu_sed()
  94. self.check_sed_substitute()
  95. self.check_sed_line_count()
  96. self.check_sed_line_address()