1import json 2import os 3 4import infra.basetest 5 6 7class TestJq(infra.basetest.BRTest): 8 rootfs_overlay = \ 9 infra.filepath("tests/package/test_jq/rootfs-overlay") 10 config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \ 11 f""" 12 BR2_PACKAGE_JQ=y 13 BR2_ROOTFS_OVERLAY="{rootfs_overlay}" 14 BR2_TARGET_ROOTFS_CPIO=y 15 # BR2_TARGET_ROOTFS_TAR is not set 16 """ 17 18 def test_run(self): 19 cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") 20 self.emulator.boot(arch="armv5", 21 kernel="builtin", 22 options=["-initrd", cpio_file]) 23 self.emulator.login() 24 25 # Check the program can execute. 26 self.assertRunOk("jq --version") 27 28 # Run jq on examples extracted from JSON RFC: 29 # https://www.rfc-editor.org/rfc/rfc8259.txt 30 for i in range(1, 6): 31 fname = f"ex13-{i}.json" 32 cmd = f"jq -M '.' {fname}" 33 self.assertRunOk(cmd) 34 35 # Check the execution fails on a non JSON file. 36 cmd = "jq -M '.' broken.json" 37 _, ret = self.emulator.run(cmd) 38 self.assertNotEqual(ret, 0) 39 40 # Check an execution of a simple query. Note that output is a 41 # JSON (quoted) string. 42 cmd = "jq -M '.[1].City' ex13-2.json" 43 out, ret = self.emulator.run(cmd) 44 self.assertEqual(ret, 0) 45 self.assertEqual(out[0], '"SUNNYVALE"') 46 47 # Run the same query with the -r option, to output raw text 48 # (i.e. strings without quotes). 49 cmd = "jq -r -M '.[1].City' ex13-2.json" 50 out, ret = self.emulator.run(cmd) 51 self.assertEqual(ret, 0) 52 self.assertEqual(out[0], "SUNNYVALE") 53 54 # Print the ex13-2.json file as compact JSON (with option -c). 55 cmd = "jq -c -M '.' ex13-2.json" 56 out, ret = self.emulator.run(cmd) 57 self.assertEqual(ret, 0) 58 # We reload this compact string using the Python json parser, 59 # to test interoperability. We check the same element as in 60 # previous queries in the Python object. 61 json_data = json.loads(out[0]) 62 self.assertEqual(json_data[1]["City"], "SUNNYVALE") 63