|
| 1 | +#!/usr/bin/python |
| 2 | +"""Tests for the format_xml_plist hook.""" |
| 3 | + |
| 4 | +import plistlib |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from pre_commit_macadmin_hooks import format_xml_plist |
| 10 | + |
| 11 | + |
| 12 | +class TestFormatXMLPlist(unittest.TestCase): |
| 13 | + """Tests for the format_xml_plist hook.""" |
| 14 | + |
| 15 | + def test_format_valid_xml_plist(self): |
| 16 | + """Test formatting a valid XML plist.""" |
| 17 | + test_data = {"key1": "value1", "key2": 123, "key3": ["a", "b", "c"]} |
| 18 | + |
| 19 | + with tempfile.NamedTemporaryFile( |
| 20 | + mode="wb", suffix=".plist", delete=False |
| 21 | + ) as tmp: |
| 22 | + # Write plist in XML format |
| 23 | + plistlib.dump(test_data, tmp, fmt=plistlib.FMT_XML) |
| 24 | + tmp_path = tmp.name |
| 25 | + |
| 26 | + try: |
| 27 | + # Run the formatter |
| 28 | + result = format_xml_plist.main([tmp_path]) |
| 29 | + |
| 30 | + # Should succeed |
| 31 | + self.assertEqual(result, 0) |
| 32 | + |
| 33 | + # Verify the file can still be read and contains the same data |
| 34 | + with open(tmp_path, "rb") as f: |
| 35 | + formatted_data = plistlib.load(f) |
| 36 | + self.assertEqual(formatted_data, test_data) |
| 37 | + finally: |
| 38 | + Path(tmp_path).unlink() |
| 39 | + |
| 40 | + def test_format_binary_plist(self): |
| 41 | + """Test formatting a binary plist converts it to XML.""" |
| 42 | + test_data = {"key1": "value1", "key2": 456} |
| 43 | + |
| 44 | + with tempfile.NamedTemporaryFile( |
| 45 | + mode="wb", suffix=".plist", delete=False |
| 46 | + ) as tmp: |
| 47 | + # Write plist in binary format |
| 48 | + plistlib.dump(test_data, tmp, fmt=plistlib.FMT_BINARY) |
| 49 | + tmp_path = tmp.name |
| 50 | + |
| 51 | + try: |
| 52 | + # Run the formatter |
| 53 | + result = format_xml_plist.main([tmp_path]) |
| 54 | + |
| 55 | + # Should succeed |
| 56 | + self.assertEqual(result, 0) |
| 57 | + |
| 58 | + # Verify the file is now XML format |
| 59 | + with open(tmp_path, "rb") as f: |
| 60 | + content = f.read() |
| 61 | + # XML plists start with <?xml |
| 62 | + self.assertTrue(content.startswith(b"<?xml")) |
| 63 | + |
| 64 | + # Verify data integrity |
| 65 | + with open(tmp_path, "rb") as f: |
| 66 | + formatted_data = plistlib.load(f) |
| 67 | + self.assertEqual(formatted_data, test_data) |
| 68 | + finally: |
| 69 | + Path(tmp_path).unlink() |
| 70 | + |
| 71 | + def test_format_invalid_plist(self): |
| 72 | + """Test that invalid plists return an error.""" |
| 73 | + with tempfile.NamedTemporaryFile( |
| 74 | + mode="w", suffix=".plist", delete=False |
| 75 | + ) as tmp: |
| 76 | + tmp.write("This is not a valid plist") |
| 77 | + tmp_path = tmp.name |
| 78 | + |
| 79 | + try: |
| 80 | + # Run the formatter |
| 81 | + result = format_xml_plist.main([tmp_path]) |
| 82 | + |
| 83 | + # Should fail |
| 84 | + self.assertEqual(result, 1) |
| 85 | + finally: |
| 86 | + Path(tmp_path).unlink() |
| 87 | + |
| 88 | + def test_format_multiple_files(self): |
| 89 | + """Test formatting multiple plist files.""" |
| 90 | + test_data_1 = {"file": "first"} |
| 91 | + test_data_2 = {"file": "second"} |
| 92 | + |
| 93 | + with tempfile.NamedTemporaryFile( |
| 94 | + mode="wb", suffix=".plist", delete=False |
| 95 | + ) as tmp1: |
| 96 | + plistlib.dump(test_data_1, tmp1, fmt=plistlib.FMT_XML) |
| 97 | + tmp1_path = tmp1.name |
| 98 | + |
| 99 | + with tempfile.NamedTemporaryFile( |
| 100 | + mode="wb", suffix=".plist", delete=False |
| 101 | + ) as tmp2: |
| 102 | + plistlib.dump(test_data_2, tmp2, fmt=plistlib.FMT_BINARY) |
| 103 | + tmp2_path = tmp2.name |
| 104 | + |
| 105 | + try: |
| 106 | + # Run the formatter on both files |
| 107 | + result = format_xml_plist.main([tmp1_path, tmp2_path]) |
| 108 | + |
| 109 | + # Should succeed |
| 110 | + self.assertEqual(result, 0) |
| 111 | + |
| 112 | + # Verify both files |
| 113 | + with open(tmp1_path, "rb") as f: |
| 114 | + data1 = plistlib.load(f) |
| 115 | + with open(tmp2_path, "rb") as f: |
| 116 | + data2 = plistlib.load(f) |
| 117 | + |
| 118 | + self.assertEqual(data1, test_data_1) |
| 119 | + self.assertEqual(data2, test_data_2) |
| 120 | + finally: |
| 121 | + Path(tmp1_path).unlink() |
| 122 | + Path(tmp2_path).unlink() |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + unittest.main() |
0 commit comments