From c8b4fd9ffba2b345ab93f6982baaa0ba00349eb1 Mon Sep 17 00:00:00 2001 From: Umit Kablan Date: Thu, 26 Sep 2024 10:41:17 +0300 Subject: [PATCH] fix tests and common default extension definition --- pleskdistup/actions/emails.py | 2 +- pleskdistup/common/src/files.py | 9 +++++---- pleskdistup/common/src/motd.py | 8 ++++---- pleskdistup/common/tests/motdtests.py | 9 +++++---- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pleskdistup/actions/emails.py b/pleskdistup/actions/emails.py index 83bcab4..a37f8d3 100644 --- a/pleskdistup/actions/emails.py +++ b/pleskdistup/actions/emails.py @@ -80,7 +80,7 @@ def _prepare_action(self) -> action.ActionResult: return action.ActionResult() def _post_action(self) -> action.ActionResult: - path_to_backup = os.path.join(self.temp_directory, "dovecot.conf.bak") + path_to_backup = os.path.join(self.temp_directory, "dovecot.conf" + files.DEFAULT_BACKUP_EXTENSION) if os.path.exists(self.dovecot_config_path): shutil.copy(self.dovecot_config_path, path_to_backup) motd.add_finish_ssh_login_message(f"The dovecot configuration '{self.dovecot_config_path}' has been restored from original distro. Modern configuration was placed in '{path_to_backup}'.\n") diff --git a/pleskdistup/common/src/files.py b/pleskdistup/common/src/files.py index 1e6d185..61a2b1d 100644 --- a/pleskdistup/common/src/files.py +++ b/pleskdistup/common/src/files.py @@ -11,6 +11,7 @@ PathType = typing.Union[os.PathLike, str] +DEFAULT_BACKUP_EXTENSION = ".conversion.bak" def replace_string(filename: str, original_substring: str, new_substring: str) -> None: with open(filename, "r") as original, open(filename + ".next", "w") as dst: @@ -59,24 +60,24 @@ def get_last_lines(filename: PathType, n: int) -> typing.List[str]: return f.readlines()[-n:] -def backup_file(filename: str, ext: str = ".conversion.bak") -> None: +def backup_file(filename: str, ext: str = DEFAULT_BACKUP_EXTENSION) -> None: if os.path.exists(filename): shutil.copy(filename, filename + ext) -def backup_exists(filename: str, ext: str = ".conversion.bak") -> bool: +def backup_exists(filename: str, ext: str = DEFAULT_BACKUP_EXTENSION) -> bool: return os.path.exists(filename + ext) def restore_file_from_backup(filename: str, remove_if_no_backup: bool = False, - ext: str = ".conversion.bak") -> None: + ext: str = DEFAULT_BACKUP_EXTENSION) -> None: if os.path.exists(filename + ext): shutil.move(filename + ext, filename) elif remove_if_no_backup and os.path.exists(filename): os.remove(filename) -def remove_backup(filename: str, logf = None, ext: str = ".conversion.bak") -> None: +def remove_backup(filename: str, logf = None, ext: str = DEFAULT_BACKUP_EXTENSION) -> None: try: if os.path.exists(filename + ext): os.remove(filename + ext) diff --git a/pleskdistup/common/src/motd.py b/pleskdistup/common/src/motd.py index af2b15c..dc9f666 100644 --- a/pleskdistup/common/src/motd.py +++ b/pleskdistup/common/src/motd.py @@ -13,11 +13,11 @@ def restore_ssh_login_message(motd_path: str = MOTD_PATH) -> None: def add_inprogress_ssh_login_message(message: str, motd_path: str = MOTD_PATH) -> None: try: - if not os.path.exists(motd_path + ".bak"): + if not files.backup_exists(motd_path): if os.path.exists(motd_path): files.backup_file(motd_path) else: - with open(motd_path + ".bak", "a") as motd: + with open(motd_path + files.DEFAULT_BACKUP_EXTENSION, "a") as motd: pass with open(motd_path, "a") as motd: @@ -39,8 +39,8 @@ def add_inprogress_ssh_login_message(message: str, motd_path: str = MOTD_PATH) - def add_finish_ssh_login_message(message: str, motd_path: str = MOTD_PATH) -> None: try: if not os.path.exists(motd_path + ".next"): - if os.path.exists(motd_path + ".bak"): - shutil.copy(motd_path + ".bak", motd_path + ".next") + if os.path.exists(motd_path + files.DEFAULT_BACKUP_EXTENSION): + shutil.copy(motd_path + files.DEFAULT_BACKUP_EXTENSION, motd_path + ".next") with open(motd_path + ".next", "a") as motd: motd.write(FINISH_INTRODUCE_MESSAGE) diff --git a/pleskdistup/common/tests/motdtests.py b/pleskdistup/common/tests/motdtests.py index 5ec4125..0af6820 100644 --- a/pleskdistup/common/tests/motdtests.py +++ b/pleskdistup/common/tests/motdtests.py @@ -4,6 +4,7 @@ import tempfile import src.motd as motd +import src.files class InprogressSshLoginMessageTests(unittest.TestCase): @@ -11,7 +12,7 @@ def setUp(self): self.motd_path = tempfile.mktemp() def tearDown(self): - for path in [self.motd_path, self.motd_path + ".bak"]: + for path in [self.motd_path, self.motd_path + files.DEFAULT_BACKUP_EXTENSION]: if os.path.exists(path): os.remove(path) @@ -37,7 +38,7 @@ def test_old_backed_up(self): with open(self.motd_path) as motd_file: self.assertEqual(motd_file.read(), "old\nnew\n") - with open(self.motd_path + ".bak") as motd_file: + with open(self.motd_path + files.DEFAULT_BACKUP_EXTENSION) as motd_file: self.assertEqual(motd_file.read(), "old\n") def test_restore(self): @@ -57,7 +58,7 @@ def setUp(self): self.motd_path = tempfile.mktemp() def tearDown(self): - for path in [self.motd_path, self.motd_path + ".bak", self.motd_path + ".next"]: + for path in [self.motd_path, self.motd_path + files.DEFAULT_BACKUP_EXTENSION, self.motd_path + ".next"]: if os.path.exists(path): os.remove(path) @@ -106,7 +107,7 @@ def test_backed_up_message_saved(self): =============================================================================== """.format(motd.MOTD_PATH) - with open(self.motd_path + ".bak", "w") as motd_file: + with open(self.motd_path + files.DEFAULT_BACKUP_EXTENSION, "w") as motd_file: motd_file.write("old\n") motd.add_inprogress_ssh_login_message("new\n", self.motd_path)