From 0bb8d6dd81735e77164edc30c622844542c52ac5 Mon Sep 17 00:00:00 2001 From: Peter Story Date: Tue, 23 Jul 2024 10:27:20 -0400 Subject: [PATCH] Delete decrypted files For #21 --- src/pydiode/decrypt.py | 6 +++++- tests/msg.gpg | Bin 0 -> 616 bytes tests/test_decrypt.py | 31 +++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 tests/msg.gpg diff --git a/src/pydiode/decrypt.py b/src/pydiode/decrypt.py index ca8bd49..4a0c8ab 100644 --- a/src/pydiode/decrypt.py +++ b/src/pydiode/decrypt.py @@ -20,8 +20,9 @@ def main(): for line in sys.stdin: path = Path(line.strip()) + # Decrypt files ending in .gpg if path.exists() and path.suffix == ".gpg": - subprocess.run( + gpg = subprocess.run( [ "gpg", "--batch", @@ -33,6 +34,9 @@ def main(): path, ] ) + # If decryption succeeded, delete the encrypted file + if not gpg.returncode: + path.unlink() if __name__ == "__main__": diff --git a/tests/msg.gpg b/tests/msg.gpg new file mode 100644 index 0000000000000000000000000000000000000000..1ab70adb9cf0610bb38ca7c3bff270fde7a28f52 GIT binary patch literal 616 zcmV-u0+;>4!At|g=fuFGAR8(H5CDu{^u~Q>nKFm$cj8+C5NQ3*YCQ2>_l7%FiYu{o zRJteW>Cu@Wo(Zs@LITX7TAqsu!KcY+d9ay#0h66uk6C2q^b}Lg<%F1mHc#-Ah}gIc zt`*P0=y!`wly9$w4;Pa__(ewZgGH>H=leiR+E{t*ZXVokl7y)$+umS26OZkI9n_}E zuU(nf146m!^&#w1ME&~2lM4vOc5^Z;3dl!f5bF@W!!HXjBwGxE!3w(sgALXTddmj{ z`*NYXNP&e_eiD{E;sd}2wU?iooj#07B^nnK`WXELWnxexyS!BRs?XHlMHN$;u&|)> zirbo4^@i%y^{2i)t+>(*dyO^j_xVBtNk5`Hm4#=QN%J$NdpZ5a_g#q4;P~_)pu#{~ z)A^W@JHd?=$XbQId_I!=GXkZS$2*Hg{gH2;4 zVwWVI#A<=R&8h)I^%ZFdL?TX*R`Ij> literal 0 HcmV?d00001 diff --git a/tests/test_decrypt.py b/tests/test_decrypt.py index 363ec7c..ca59f53 100644 --- a/tests/test_decrypt.py +++ b/tests/test_decrypt.py @@ -17,25 +17,40 @@ def setUp(self): ) def test_decrypt(self): - files = { - Path("1.txt.gpg"): "ABC", - Path("2.txt.gpg"): "DEF", - Path("3.txt.gpg"): "GHI", + # We have a secret key for all but the last file + encrypted_files = [ + Path("1.txt.gpg"), + Path("2.txt.gpg"), + Path("3.txt.gpg"), + Path("msg.gpg"), + ] + decrypted_files = { + Path("1.txt"): "ABC", + Path("2.txt"): "DEF", + Path("3.txt"): "GHI", } with tempfile.TemporaryDirectory() as tmpdir: # Copy the encrypted files - for file in files: + for file in encrypted_files: shutil.copy(PARENT_DIR / file, tmpdir) # Decrypt the files decrypt = subprocess.run( [sys.executable, "-m", "pydiode.decrypt"], - input="\n".join([str(tmpdir / file) for file in files.keys()]), + input="\n".join([str(tmpdir / f) for f in encrypted_files]), + capture_output=True, + check=True, text=True, ) # Check the files were decrypted - for file, data in files.items(): - with open(tmpdir / file.with_suffix(""), "r") as f: + for file, data in decrypted_files.items(): + with open(tmpdir / file, "r") as f: self.assertEqual(data, f.read().strip()) + + # The properly decrypted files should be deleted + for file in encrypted_files[:-1]: + self.assertFalse((tmpdir / file).exists()) + # The file that wan't decrypted should exist + self.assertTrue((tmpdir / encrypted_files[-1]).exists())