[Python-checkins] bpo-42095: plistlib: Add tests that compare with plutil(1) (#27173)

ambv webhook-mailer at python.org
Sat Jul 17 06:11:09 EDT 2021


https://github.com/python/cpython/commit/689b05c6281ee6bafb9a0c0bf291260efa130f64
commit: 689b05c6281ee6bafb9a0c0bf291260efa130f64
branch: main
author: Hasan <aliyevH at hotmail.com>
committer: ambv <lukasz at langa.pl>
date: 2021-07-17T12:11:04+02:00
summary:

bpo-42095: plistlib: Add tests that compare with plutil(1) (#27173)

Co-authored-by: Łukasz Langa <lukasz at langa.pl>

files:
A Misc/NEWS.d/next/Tests/2021-07-17-11-41-20.bpo-42095.kCB7oj.rst
M Lib/test/test_plistlib.py

diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py
index ef96c6ceda21a..6b457440be543 100644
--- a/Lib/test/test_plistlib.py
+++ b/Lib/test/test_plistlib.py
@@ -6,8 +6,11 @@
 import unittest
 import plistlib
 import os
+import sys
+import json
 import datetime
 import codecs
+import subprocess
 import binascii
 import collections
 from test import support
@@ -997,6 +1000,77 @@ def test__all__(self):
         not_exported = {"PlistFormat", "PLISTHEADER"}
         support.check__all__(self, plistlib, not_exported=not_exported)
 
+ at unittest.skipUnless(sys.platform == "darwin", "plutil utility is for Mac os")
+class TestPlutil(unittest.TestCase):
+    file_name = "plutil_test.plist"
+    properties = {
+            "fname" : "H",
+            "lname":"A",
+            "marks" : {"a":100, "b":0x10}
+        }
+    exptected_properties = {
+        "fname" : "H",
+        "lname": "A",
+        "marks" : {"a":100, "b":16}
+    }
+    pl = {
+            "HexType" : 0x0100000c,
+            "IntType" : 0o123
+        }
+
+    @classmethod
+    def setUpClass(cls) -> None:
+        ## Generate plist file with plistlib and parse with plutil
+        with open(cls.file_name,'wb') as f:
+            plistlib.dump(cls.properties, f, fmt=plistlib.FMT_BINARY)
+
+    @classmethod
+    def tearDownClass(cls) -> None:
+        os.remove(cls.file_name)
+
+    def get_lint_status(self):
+        return subprocess.run(['plutil', "-lint", self.file_name], capture_output=True, text=True).stdout
+
+    def convert_to_json(self):
+        """Convert binary file to json using plutil
+        """
+        subprocess.run(['plutil', "-convert", 'json', self.file_name])
+
+    def convert_to_bin(self):
+        """Convert file to binary using plutil
+        """
+        subprocess.run(['plutil', "-convert", 'binary1', self.file_name])
+
+    def write_pl(self):
+        """Write Hex properties to file using writePlist
+        """
+        with open(self.file_name, 'wb') as f:
+            plistlib.dump(self.pl, f, fmt=plistlib.FMT_BINARY)
+
+    def test_lint_status(self):
+        # check lint status of file using plutil
+        self.assertEqual(f"{self.file_name}: OK\n", self.get_lint_status())
+
+    def check_content(self):
+        # check file content with plutil converting binary to json
+        self.convert_to_json()
+        with open(self.file_name) as f:
+            ff = json.loads(f.read())
+            self.assertEqual(ff, self.exptected_properties)
+
+    def check_plistlib_parse(self):
+        # Generate plist files with plutil and parse with plistlib
+        self.convert_to_bin()
+        with open(self.file_name, 'rb') as f:
+            self.assertEqual(plistlib.load(f), self.exptected_properties)
+
+    def test_octal_and_hex(self):
+        self.write_pl()
+        self.convert_to_json()
+        with open(self.file_name, 'r') as f:
+            p = json.loads(f.read())
+            self.assertEqual(p.get("HexType"), 16777228)
+            self.assertEqual(p.get("IntType"), 83)
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Tests/2021-07-17-11-41-20.bpo-42095.kCB7oj.rst b/Misc/NEWS.d/next/Tests/2021-07-17-11-41-20.bpo-42095.kCB7oj.rst
new file mode 100644
index 0000000000000..bf7bc5b2ff449
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-07-17-11-41-20.bpo-42095.kCB7oj.rst
@@ -0,0 +1,2 @@
+Added interop tests for Apple plists: generate plist files with Python
+plistlib and parse with Apple plutil; and the other way round.



More information about the Python-checkins mailing list