Skip to content

Commit

Permalink
add more patch log tests for should_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
th3w1zard1 committed Nov 7, 2023
1 parent 7a72473 commit 535365f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
8 changes: 4 additions & 4 deletions pykotor/tslpatcher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
84 changes: 60 additions & 24 deletions tests/tslpatcher/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 535365f

Please sign in to comment.