From 535365fde2ce65e932595a9b86e8d37652afbdb8 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Tue, 7 Nov 2023 16:29:22 -0600 Subject: [PATCH] add more patch log tests for `should_patch` --- pykotor/tslpatcher/config.py | 8 ++-- tests/tslpatcher/test_config.py | 84 +++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/pykotor/tslpatcher/config.py b/pykotor/tslpatcher/config.py index 757aaeec8..57b4d2e7b 100644 --- a/pykotor/tslpatcher/config.py +++ b/pykotor/tslpatcher/config.py @@ -288,7 +288,7 @@ def should_patch( container_type = "folder" if capsule is None else "archive" if patch.replace_file and exists: - saveas_str = f"'{patch.saveas}' to" if patch.saveas != patch.sourcefile else "in" + saveas_str = f"'{patch.saveas}' in" if patch.saveas != patch.sourcefile else "in" self.log.add_note(f"{patch.action[:-1]}ing '{patch.sourcefile}' and replacing existing file {saveas_str} the '{local_folder}' {container_type}") return True @@ -306,9 +306,9 @@ def should_patch( # In capsules, I haven't seen any TSLPatcher mods reach this point. I know TSLPatcher at least supports this portion for non-capsules. # Most mods will use an [InstallList] to ensure the files exist before patching anyways, but not all. - save_type: str = "adding" if capsule is not None else "saving" - saving_as_str = f"as '{patch.saveas}' " if patch.saveas != patch.sourcefile else "" - self.log.add_note(f"{patch.action[:-1]}ing '{patch.sourcefile}' and {save_type} {saving_as_str}to the '{local_folder}' {container_type}") + save_type: str = "adding" if capsule is not None and patch.saveas == patch.sourcefile else "saving" + saving_as_str = f"as '{patch.saveas}' in" if patch.saveas != patch.sourcefile else "to" + self.log.add_note(f"{patch.action[:-1]}ing '{patch.sourcefile}' and {save_type} {saving_as_str} the '{local_folder}' {container_type}") return True def install(self) -> None: diff --git a/tests/tslpatcher/test_config.py b/tests/tslpatcher/test_config.py index a9d24de15..ecb2e07a1 100644 --- a/tests/tslpatcher/test_config.py +++ b/tests/tslpatcher/test_config.py @@ -94,12 +94,7 @@ def test_lookup_resource_capsule_exists_true_no_file(self): # Act & Assert self.assertIsNone( - self.config.lookup_resource( - self.patch, - self.output_container_path, - exists_at_output_location=True, - capsule=capsule - ) + self.config.lookup_resource(self.patch, self.output_container_path, exists_at_output_location=True, capsule=capsule) ) def test_lookup_resource_no_capsule_exists_true_no_file(self): @@ -110,14 +105,7 @@ def test_lookup_resource_no_capsule_exists_true_no_file(self): mock_load_file.side_effect = FileNotFoundError # Act & Assert - self.assertIsNone( - self.config.lookup_resource( - self.patch, - self.output_container_path, - exists_at_output_location=True, - capsule=None - ) - ) + self.assertIsNone(self.config.lookup_resource(self.patch, self.output_container_path, exists_at_output_location=True, capsule=None)) def test_lookup_resource_no_capsule_exists_false_no_file(self): # Arrange @@ -127,14 +115,7 @@ def test_lookup_resource_no_capsule_exists_false_no_file(self): mock_load_file.side_effect = FileNotFoundError # Act & Assert - self.assertIsNone( - self.config.lookup_resource( - self.patch, - self.output_container_path, - exists_at_output_location=False, - capsule=None - ) - ) + self.assertIsNone(self.config.lookup_resource(self.patch, self.output_container_path, exists_at_output_location=False, capsule=None)) class TestShouldPatchFunction(unittest.TestCase): @@ -144,9 +125,64 @@ def setUp(self): self.patcher.game_path.name = "swkotor" self.patcher.log = MagicMock() - def test_replace_file_exists(self): - patch = MagicMock(destination=".", replace_file=True, saveas="file2", sourcefile="file1", action="Patching") + def test_replace_file_exists_destination_dot(self): + patch = MagicMock(name="patch", destination=".", replace_file=True, saveas="file1", sourcefile="file1", action="Patch ") + result = self.patcher.should_patch(patch, exists=True) + self.patcher.log.add_note.assert_called_once_with("Patching 'file1' and replacing existing file in the 'swkotor' folder") + self.assertTrue(result) + + def test_replace_file_exists_saveas_destination_dot(self): + patch = MagicMock(name="patch", destination=".", replace_file=True, saveas="file2", sourcefile="file1", action="Patch ") result = self.patcher.should_patch(patch, exists=True) + self.patcher.log.add_note.assert_called_once_with("Patching 'file1' and replacing existing file 'file2' in the 'swkotor' folder") + self.assertTrue(result) + + def test_replace_file_exists_destination_override(self): + patch = MagicMock(name="patch", destination="Override", replace_file=True, saveas="file1", sourcefile="file1", action="Patch ") + result = self.patcher.should_patch(patch, exists=True) + self.patcher.log.add_note.assert_called_once_with("Patching 'file1' and replacing existing file in the 'Override' folder") + self.assertTrue(result) + + def test_replace_file_exists_saveas_destination_override(self): + patch = MagicMock(name="patch", destination="Override", replace_file=True, saveas="file2", sourcefile="file1", action="Compile") + result = self.patcher.should_patch(patch, exists=True) + self.patcher.log.add_note.assert_called_once_with("Compiling 'file1' and replacing existing file 'file2' in the 'Override' folder") + self.assertTrue(result) + + def test_replace_file_not_exists_saveas_destination_override(self): + patch = MagicMock(name="patch", destination="Override", replace_file=True, saveas="file2", sourcefile="file1", action="Copy ") + result = self.patcher.should_patch(patch, exists=False) + self.patcher.log.add_note.assert_called_once_with("Copying 'file1' and saving as 'file2' in the 'Override' folder") + self.assertTrue(result) + + def test_replace_file_not_exists_destination_override(self): + patch = MagicMock(name="patch", destination="Override", replace_file=True, saveas="file1", sourcefile="file1", action="Copy ") + result = self.patcher.should_patch(patch, exists=False) + self.patcher.log.add_note.assert_called_once_with("Copying 'file1' and saving to the 'Override' folder") + self.assertTrue(result) + + def test_replace_file_exists_destination_capsule(self): + patch = MagicMock(name="patch", destination="capsule.mod", replace_file=True, saveas="file1", sourcefile="file1", action="Patch ") + result = self.patcher.should_patch(patch, exists=True, capsule=True) + self.patcher.log.add_note.assert_called_once_with("Patching 'file1' and replacing existing file in the 'capsule.mod' archive") + self.assertTrue(result) + + def test_replace_file_exists_saveas_destination_capsule(self): + patch = MagicMock(name="patch", destination="capsule.mod", replace_file=True, saveas="file2", sourcefile="file1", action="Patch ") + result = self.patcher.should_patch(patch, exists=True, capsule=True) + self.patcher.log.add_note.assert_called_once_with("Patching 'file1' and replacing existing file 'file2' in the 'capsule.mod' archive") + self.assertTrue(result) + + def test_replace_file_not_exists_saveas_destination_capsule(self): + patch = MagicMock(name="patch", destination="capsule.mod", replace_file=True, saveas="file2", sourcefile="file1", action="Copy ") + result = self.patcher.should_patch(patch, exists=False, capsule=MagicMock(patch="some path")) + self.patcher.log.add_note.assert_called_once_with("Copying 'file1' and saving as 'file2' in the 'capsule.mod' archive") + self.assertTrue(result) + + def test_replace_file_not_exists_destination_capsule(self): + patch = MagicMock(name="patch", destination="capsule.mod", replace_file=True, saveas="file1", sourcefile="file1", action="Copy ") + result = self.patcher.should_patch(patch, exists=False, capsule=MagicMock(patch="some path")) + self.patcher.log.add_note.assert_called_once_with("Copying 'file1' and adding to the 'capsule.mod' archive") self.assertTrue(result) def test_not_replace_file_exists_skip_false(self):