|
6 | 6 | import unittest
|
7 | 7 | import plistlib
|
8 | 8 | import os
|
| 9 | +import sys |
| 10 | +import json |
9 | 11 | import datetime
|
10 | 12 | import codecs
|
| 13 | +import subprocess |
11 | 14 | import binascii
|
12 | 15 | import collections
|
13 | 16 | from test import support
|
@@ -997,6 +1000,77 @@ def test__all__(self):
|
997 | 1000 | not_exported = {"PlistFormat", "PLISTHEADER"}
|
998 | 1001 | support.check__all__(self, plistlib, not_exported=not_exported)
|
999 | 1002 |
|
| 1003 | +@unittest.skipUnless(sys.platform == "darwin", "plutil utility is for Mac os") |
| 1004 | +class TestPlutil(unittest.TestCase): |
| 1005 | + file_name = "plutil_test.plist" |
| 1006 | + properties = { |
| 1007 | + "fname" : "H", |
| 1008 | + "lname":"A", |
| 1009 | + "marks" : {"a":100, "b":0x10} |
| 1010 | + } |
| 1011 | + exptected_properties = { |
| 1012 | + "fname" : "H", |
| 1013 | + "lname": "A", |
| 1014 | + "marks" : {"a":100, "b":16} |
| 1015 | + } |
| 1016 | + pl = { |
| 1017 | + "HexType" : 0x0100000c, |
| 1018 | + "IntType" : 0o123 |
| 1019 | + } |
| 1020 | + |
| 1021 | + @classmethod |
| 1022 | + def setUpClass(cls) -> None: |
| 1023 | + ## Generate plist file with plistlib and parse with plutil |
| 1024 | + with open(cls.file_name,'wb') as f: |
| 1025 | + plistlib.dump(cls.properties, f, fmt=plistlib.FMT_BINARY) |
| 1026 | + |
| 1027 | + @classmethod |
| 1028 | + def tearDownClass(cls) -> None: |
| 1029 | + os.remove(cls.file_name) |
| 1030 | + |
| 1031 | + def get_lint_status(self): |
| 1032 | + return subprocess.run(['plutil', "-lint", self.file_name], capture_output=True, text=True).stdout |
| 1033 | + |
| 1034 | + def convert_to_json(self): |
| 1035 | + """Convert binary file to json using plutil |
| 1036 | + """ |
| 1037 | + subprocess.run(['plutil', "-convert", 'json', self.file_name]) |
| 1038 | + |
| 1039 | + def convert_to_bin(self): |
| 1040 | + """Convert file to binary using plutil |
| 1041 | + """ |
| 1042 | + subprocess.run(['plutil', "-convert", 'binary1', self.file_name]) |
| 1043 | + |
| 1044 | + def write_pl(self): |
| 1045 | + """Write Hex properties to file using writePlist |
| 1046 | + """ |
| 1047 | + with open(self.file_name, 'wb') as f: |
| 1048 | + plistlib.dump(self.pl, f, fmt=plistlib.FMT_BINARY) |
| 1049 | + |
| 1050 | + def test_lint_status(self): |
| 1051 | + # check lint status of file using plutil |
| 1052 | + self.assertEqual(f"{self.file_name}: OK\n", self.get_lint_status()) |
| 1053 | + |
| 1054 | + def check_content(self): |
| 1055 | + # check file content with plutil converting binary to json |
| 1056 | + self.convert_to_json() |
| 1057 | + with open(self.file_name) as f: |
| 1058 | + ff = json.loads(f.read()) |
| 1059 | + self.assertEqual(ff, self.exptected_properties) |
| 1060 | + |
| 1061 | + def check_plistlib_parse(self): |
| 1062 | + # Generate plist files with plutil and parse with plistlib |
| 1063 | + self.convert_to_bin() |
| 1064 | + with open(self.file_name, 'rb') as f: |
| 1065 | + self.assertEqual(plistlib.load(f), self.exptected_properties) |
| 1066 | + |
| 1067 | + def test_octal_and_hex(self): |
| 1068 | + self.write_pl() |
| 1069 | + self.convert_to_json() |
| 1070 | + with open(self.file_name, 'r') as f: |
| 1071 | + p = json.loads(f.read()) |
| 1072 | + self.assertEqual(p.get("HexType"), 16777228) |
| 1073 | + self.assertEqual(p.get("IntType"), 83) |
1000 | 1074 |
|
1001 | 1075 | if __name__ == '__main__':
|
1002 | 1076 | unittest.main()
|
0 commit comments