Skip to content

Commit 689b05c

Browse files
AliyevHambv
andauthored
bpo-42095: plistlib: Add tests that compare with plutil(1) (#27173)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent d0b2b00 commit 689b05c

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Lib/test/test_plistlib.py

+74
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import unittest
77
import plistlib
88
import os
9+
import sys
10+
import json
911
import datetime
1012
import codecs
13+
import subprocess
1114
import binascii
1215
import collections
1316
from test import support
@@ -997,6 +1000,77 @@ def test__all__(self):
9971000
not_exported = {"PlistFormat", "PLISTHEADER"}
9981001
support.check__all__(self, plistlib, not_exported=not_exported)
9991002

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)
10001074

10011075
if __name__ == '__main__':
10021076
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added interop tests for Apple plists: generate plist files with Python
2+
plistlib and parse with Apple plutil; and the other way round.

0 commit comments

Comments
 (0)