]> git.ipfire.org Git - pbs.git/commitdiff
tests: Test the unlink function
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Oct 2022 13:39:44 +0000 (13:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Oct 2022 13:39:44 +0000 (13:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/setup.py

index 015e1c4dad6e9083d134c3e0837138af73bdc163..89ce9c5365a3495c026de4890b01a8a4480ed9df 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/python3
 
+import os
 import unittest
 
 import test
@@ -12,6 +13,45 @@ class SetupTestCase(test.TestCase):
                with self.backend.pakfire() as p:
                        pass
 
+       async def test_unlink(self):
+               """
+                       Tests whether the unlink() function works correctly
+               """
+               path = self.backend.path("tmp/file")
+
+               # Fetch the parent directory
+               parent = os.path.dirname(path)
+
+               # Create the parent directory
+               os.mkdir(parent)
+
+               # Create an empty file
+               with open(path, "w") as f:
+                       pass
+
+               # Check if the file exists
+               self.assertTrue(os.path.exists(path))
+
+               # Try to unlink the file
+               await self.backend.unlink(path)
+
+               # Check that the file is gone
+               self.assertFalse(os.path.exists(path))
+
+               # Check that the parent directory is gone, too
+               self.assertFalse(os.path.exists(parent))
+
+               # Check that the base path is still there
+               self.assertTrue(os.path.exists(self.backend.basepath))
+
+               # Try to delete some file outside the base path
+               with self.assertRaises(OSError):
+                       await self.backend.unlink("/tmp/file-that-should-not-exist")
+
+               # Try to leave the base path through a relative thing
+               with self.assertRaises(OSError):
+                       await self.backend.unlink("%s/../file-that-should-not-exist" % self.backend.basepath)
+
 
 if __name__ == "__main__":
        unittest.main()