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