1import os
2
3import infra.basetest
4
5
6class TestFile(infra.basetest.BRTest):
7    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
8        f"""
9        BR2_PACKAGE_FILE=y
10        BR2_ROOTFS_OVERLAY="{infra.filepath("tests/package/test_file/rootfs-overlay")}"
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        self.assertRunOk("file --version")
23
24        tests = [
25            ("", "plain-text.txt", "ASCII text"),
26            ("-i", "plain-text.txt", "text/plain"),
27            ("", "plain-text.txt.gz", "gzip compressed data"),
28            ("-i", "plain-text.txt.gz", "application/gzip"),
29            ("-z", "plain-text.txt.gz", "ASCII text"),
30            ("", "random-data.bin", "data"),
31            ("-i", "random-data.bin", "application/octet-stream"),
32            ("", "code.c", "C source"),
33            ("-i", "code.c", "text/x-c"),
34            ("", "script.sh", "POSIX shell script"),
35            ("-i", "script.sh", "text/x-shellscript"),
36            ("", "script.py", "Python script"),
37            ("", "/usr/share/misc/magic.mgc", "magic binary file for file"),
38            ("", "/usr/bin/file", "ELF"),
39            ("", "/dev/zero", "character special"),
40            ("", "/", "directory"),
41            ("-h", "symlink-to-plain-text.txt", "symbolic link"),
42            ("-L", "symlink-to-plain-text.txt", "ASCII text")
43        ]
44        for opt_str, path, pattern in tests:
45            cmd = f"file {opt_str} '{path}'"
46            out, ret = self.emulator.run(cmd)
47            self.assertEqual(ret, 0, f"Failed to run '{cmd}'")
48            self.assertIn(pattern, "\n".join(out))
49