All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julien Olivain <ju.o@free.fr>
To: buildroot@buildroot.org
Cc: Julien Olivain <ju.o@free.fr>
Subject: [Buildroot] [PATCH 1/1] support/testing: add sed runtime test
Date: Tue, 16 Jan 2024 19:35:12 +0100	[thread overview]
Message-ID: <20240116183512.320951-1-ju.o@free.fr> (raw)

Signed-off-by: Julien Olivain <ju.o@free.fr>
---
Tested on branch master at commit e07402a with commands:

    make check-package
    ...
    0 warnings generated

    support/testing/run-tests \
        -d dl -o output_folder \
        tests.package.test_sed
    ...
    OK
---
 DEVELOPERS                                |   1 +
 support/testing/tests/package/test_sed.py | 118 ++++++++++++++++++++++
 2 files changed, 119 insertions(+)
 create mode 100644 support/testing/tests/package/test_sed.py

diff --git a/DEVELOPERS b/DEVELOPERS
index f5b04937b6..24a80b7b36 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1811,6 +1811,7 @@ F:	support/testing/tests/package/test_python_spake2.py
 F:	support/testing/tests/package/test_rdma_core.py
 F:	support/testing/tests/package/test_rdma_core/
 F:	support/testing/tests/package/test_screen.py
+F:	support/testing/tests/package/test_sed.py
 F:	support/testing/tests/package/test_stress_ng.py
 F:	support/testing/tests/package/test_tcl.py
 F:	support/testing/tests/package/test_tcl/
diff --git a/support/testing/tests/package/test_sed.py b/support/testing/tests/package/test_sed.py
new file mode 100644
index 0000000000..0a0fdb890a
--- /dev/null
+++ b/support/testing/tests/package/test_sed.py
@@ -0,0 +1,118 @@
+import os
+
+import infra.basetest
+
+
+class TestSed(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
+        BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
+        BR2_PACKAGE_SED=y
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    def check_gnu_sed(self):
+        in_file = "testfile.txt"
+
+        # We create a test file for this test.
+        self.assertRunOk(f"echo 'This is a test' > {in_file}")
+
+        # Check we have the GNU sed by testing a GNU extension likely
+        # not present in other implementation. See:
+        # https://www.gnu.org/software/sed/manual/sed.html#Extended-Commands
+        # Note: we cannot search for "GNU sed" in sed --version,
+        # because busybox sed --version outputs: "This is not GNU sed
+        # version 4.0". The 'F' and 'Q' sed commands are known to be
+        # unimplemented in BusyBox 1.36.1.
+        expected_code = 123
+        sed_script = f"F;Q {expected_code}"
+        cmd = f"sed '{sed_script}' {in_file}"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, expected_code)
+        self.assertEqual(output, [in_file])
+
+    def check_sed_substitute(self):
+        testfile_num = 5
+
+        # We create few different test files for this test.
+        cmd = f'for i in $(seq {testfile_num}) ; do '
+        cmd += 'echo "=== $i Hello ===" > file$i.txt ; '
+        cmd += 'done'
+        self.assertRunOk(cmd)
+
+        # We reformat file content, in-place.
+        sed_script = "s/^=== \\([0-9]*\\) \\(Hello\\) ===$/\\2 \\1/"
+        cmd = f"sed -i '{sed_script}' file[0-9]*.txt"
+        self.assertRunOk(cmd)
+
+        # We substitute numbers with the string "Buildroot". We use an
+        # extended regular expression (with the '+'), so we test with
+        # the '-r' option.
+        sed_script = "s/[0-9]+/Buildroot/g"
+        cmd = f"sed -r -i '{sed_script}' file[0-9]*.txt"
+        self.assertRunOk(cmd)
+
+        # Our previous text manipulations are expected to end up with
+        # the "Hello Buildroot" string in all files.
+        cmd = "cat file[0-9]*.txt"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(output, ["Hello Buildroot"] * testfile_num)
+
+    def check_sed_line_count(self):
+        # We use the '=' command to count lines.
+        line_count = 1234
+        cmd = f"seq {line_count} | sed -n '$='"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(int(output[0]), line_count)
+
+    def check_sed_line_address(self):
+        input_file = "strings.txt"
+        expected_file = "expected.txt"
+
+        # We create simple data for this test.
+        strings = ["one", "two", "three", "four", "five"]
+        content = '\\n'.join(strings)
+        cmd = f"echo -e \"{content}\" > {input_file}"
+        self.assertRunOk(cmd)
+
+        # The manipulation in this tests are expected to extract the
+        # first and last of the input. We create the expected data for
+        # comparison.
+        expected_output = [strings[0], strings[-1]]
+        content = '\\n'.join(expected_output)
+        cmd = f"echo -e \"{content}\" > {expected_file}"
+        self.assertRunOk(cmd)
+
+        # We remove lines between strings "two" and "four" included.
+        cmd = f"sed '/two/,/four/d' {input_file} > output1.txt"
+        self.assertRunOk(cmd)
+
+        # We check output is the same as the expected data.
+        cmd = f"cmp {expected_file} output1.txt"
+        self.assertRunOk(cmd)
+
+        # We redo the same manipulation using line number addresses.
+        cmd = f"sed -n '1p;5p' {input_file} > output2.txt"
+        self.assertRunOk(cmd)
+
+        # We check again output is correct.
+        cmd = f"cmp {expected_file} output2.txt"
+        self.assertRunOk(cmd)
+
+    def test_run(self):
+        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+        self.emulator.boot(arch="armv5",
+                           kernel="builtin",
+                           options=["-initrd", cpio_file])
+        self.emulator.login()
+
+        # Check the program can execute
+        self.assertRunOk("sed --version")
+
+        self.check_gnu_sed()
+        self.check_sed_substitute()
+        self.check_sed_line_count()
+        self.check_sed_line_address()
-- 
2.43.0

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

             reply	other threads:[~2024-01-16 18:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-16 18:35 Julien Olivain [this message]
2024-01-21 16:01 ` [Buildroot] [PATCH 1/1] support/testing: add sed runtime test Yann E. MORIN

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240116183512.320951-1-ju.o@free.fr \
    --to=ju.o@free.fr \
    --cc=buildroot@buildroot.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.