import os import unittest import plistlib import subprocess import json class TestWidgetMock(unittest.TestCase): def test_plutil(self): file_name = "test.plist" properties = { "fname" : "H", "lname":"A", "marks" : {"a":100, "b":0x10} } exptected_properties = { "fname" : "H", "lname": "A", "marks" : {"a":100, "b":16} } ## Generate plist file with plistlib and parse with plutil with open(file_name,'wb') as f: plistlib.dump(properties, f, fmt=plistlib.FMT_BINARY) # check lint status of file using plutil lint_status = subprocess.run(['plutil', "-lint", file_name], capture_output=True, text=True) self.assertEqual(f"{file_name}: OK\n", lint_status.stdout) # check file content with plutil converting binary to json subprocess.run(['plutil', "-convert", 'json', file_name]) with open(file_name) as f: ff = json.loads(f.read()) self.assertEqual(ff, exptected_properties) # Generate plist files with plutil and parse with plistlib with open(file_name, 'rb') as f: subprocess.run(['plutil', "-convert", 'binary1', file_name]) self.assertEqual(plistlib.load(f), exptected_properties) os.remove(file_name) def test_int(self): file_name = "test.plist" pl = { "HexType" : 0x0100000c, "IntType" : 0o123 } plistlib.writePlist(pl, file_name) # check file content with plutil converting xml to json subprocess.run(['plutil', "-convert", 'json', file_name]) with open(file_name, 'r') as f: p = json.loads(f.read()) self.assertEqual(p.get("HexType"), 16777228) self.assertEqual(p.get("IntType"), 83) os.remove(file_name) if __name__ == "__main__": unittest.main()