1import os
2
3import infra.basetest
4
5
6class TestGhostscript(infra.basetest.BRTest):
7    rootfs_overlay = \
8        infra.filepath("tests/package/test_ghostscript/rootfs-overlay")
9    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
10        f"""
11        BR2_PACKAGE_GHOSTSCRIPT=y
12        BR2_PACKAGE_TESSERACT_OCR=y
13        BR2_PACKAGE_TESSERACT_OCR_LANG_ENG=y
14        BR2_ROOTFS_OVERLAY="{rootfs_overlay}"
15        BR2_TARGET_ROOTFS_CPIO=y
16        # BR2_TARGET_ROOTFS_TAR is not set
17        """
18
19    def test_run(self):
20        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
21        self.emulator.boot(arch="armv5",
22                           kernel="builtin",
23                           options=["-initrd", cpio_file])
24        self.emulator.login()
25
26        # Check the program can execute.
27        self.assertRunOk("gs --version")
28
29        doc_basename = "document"
30        ps_file = doc_basename + ".ps"
31        pgm_file = doc_basename + ".pgm"
32        txt_file = doc_basename + ".txt"
33
34        # Render a basic PostScript file to an image file.
35        cmd = "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pgmraw -r150"
36        cmd += f" -dTextAlphaBits=4 -sOutputFile='{pgm_file}' {ps_file}"
37        self.assertRunOk(cmd)
38
39        # Run text recognition on the image file.
40        cmd = f"tesseract {pgm_file} {doc_basename}"
41        self.assertRunOk(cmd, timeout=30)
42
43        # Check we extracted the expected string from the input
44        # PostScript file.
45        cmd = f"cat {txt_file}"
46        out, ret = self.emulator.run(cmd)
47        self.assertEqual(ret, 0)
48        self.assertEqual(out[0], "Hello Buildroot!")
49