Skip to content

Commit

Permalink
Make it possible to handle all *.rpmnew files in a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Sandakov committed Mar 1, 2024
1 parent 21e06c7 commit 11911a0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def handle_rpmnew(original_path: str) -> bool:
return True


def handle_all_rpmnew_files(directory: str) -> None:
fixed_list = []
for file in files.find_files_case_insensitive(directory, ["*.rpmnew"]):
original_file = file[:-len(".rpmnew")]
if handle_rpmnew(original_file):
fixed_list.append(original_file)

return fixed_list


def find_related_repofiles(repository_file: str) -> typing.List[str]:
return files.find_files_case_insensitive("/etc/yum.repos.d", repository_file)

Expand Down
43 changes: 43 additions & 0 deletions tests/rpmtests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 1999-2023. Plesk International GmbH. All rights reserved.
import unittest
import os
import shutil

import src.rpm as rpm

Expand Down Expand Up @@ -232,12 +233,19 @@ def test_write_exsisted_repodata(self):


class HandleRpmnewFilesTests(unittest.TestCase):
def __init__(self, methodName: str = "runTest") -> None:
self.test_dir = "rpm_test_dir"
super().__init__(methodName)

def tearDown(self):
tests_related_files = ["test.txt", "test.txt.rpmnew", "test.txt.rpmsave"]
for file in tests_related_files:
if os.path.exists(file):
os.remove(file)

if os.path.exists(self.test_dir):
shutil.rmtree(self.test_dir)

def test_no_rpmnew(self):
with open("test.txt", "w") as f:
f.write("test")
Expand Down Expand Up @@ -267,3 +275,38 @@ def test_missing_original(self):
self.assertEqual(open("test.txt").read(), "2")

self.assertFalse(os.path.exists("test.txt.rpmsave"))

def test_handle_whole_directory(self):
os.mkdir(self.test_dir)

original_files = {
"test1.txt": "1",
"test1.txt.rpmnew": "2",
"test2.txt": "3",
"test2.txt.rpmnew": "4",
"test3.txt": "5",
"test4.txt.rpmnew": "6"
}

expected_files = {
"test1.txt": "2",
"test2.txt": "4",
"test3.txt": "5",
"test4.txt": "6"
}

for file, content in original_files.items():
with open(f"{self.test_dir}/{file}", "w") as f:
f.write(content)

result = rpm.handle_all_rpmnew_files(self.test_dir)

for file, content in expected_files.items():
# since test3.txt was not substituted, it should not be in the result
if file != "test3.txt":
self.assertTrue(f"{self.test_dir}/{file}" in result)

self.assertTrue(os.path.exists(f"{self.test_dir}/{file}"))
self.assertEqual(open(f"{self.test_dir}/{file}").read(), content)

shutil.rmtree(self.test_dir)

0 comments on commit 11911a0

Please sign in to comment.