import tempfile import unittest from pathlib import Path from nlprog.patching import PatchError, apply_patch_to_root, summarize_patch class PatchingTests(unittest.TestCase): def test_add_update_delete_file(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) add_patch = """*** Begin Patch *** Add File: notes.txt +hello *** End Patch""" self.assertIn("Add notes.txt", summarize_patch(add_patch)) self.assertEqual(apply_patch_to_root(root, add_patch, set()), ["Added notes.txt"]) self.assertEqual((root / "notes.txt").read_text(encoding="utf-8"), "hello\n") update_patch = """*** Begin Patch *** Update File: notes.txt @@ -hello +hello world *** End Patch""" self.assertEqual(apply_patch_to_root(root, update_patch, set()), ["Updated notes.txt"]) self.assertEqual((root / "notes.txt").read_text(encoding="utf-8"), "hello world\n") delete_patch = """*** Begin Patch *** Delete File: notes.txt *** End Patch""" self.assertEqual(apply_patch_to_root(root, delete_patch, set()), ["Deleted notes.txt"]) self.assertFalse((root / "notes.txt").exists()) def test_protected_path_is_rejected(self): with tempfile.TemporaryDirectory() as tmp: patch = """*** Begin Patch *** Add File: __pycache__/blocked.txt +x *** End Patch""" with self.assertRaises(PatchError): apply_patch_to_root(Path(tmp), patch, {"__pycache__"}) if __name__ == "__main__": unittest.main()