Skip to content

Commit fd06911

Browse files
committed
Extend tests to cover delete_symlink reusing make_symlink tests.
1 parent bc032ac commit fd06911

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/test_mig_shared_fileio.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
DUMMY_FILE_GETFILESIZE = 'fileio/get_file_size'
5656
DUMMY_FILE_MAKESYMLINKSRC = 'fileio/make_symlink/link'
5757
DUMMY_FILE_MAKESYMLINKDST = 'fileio/make_symlink/target'
58+
DUMMY_FILE_DELETESYMLINKSRC = 'fileio/delete_symlink/link'
59+
DUMMY_FILE_DELETESYMLINKDST = 'fileio/delete_symlink/target'
5860
# NOTE: getsize returns 4k for directories
5961
DUMMY_DIRECTORY_SIZE = 4096
6062

@@ -540,6 +542,7 @@ def setUp(self):
540542
self.tmp_link = temppath(DUMMY_FILE_MAKESYMLINKSRC, self)
541543
self.tmp_target = temppath(DUMMY_FILE_MAKESYMLINKDST, self)
542544
# We generally need output dir to exist here
545+
os.makedirs(os.path.dirname(self.tmp_link), exist_ok=True)
543546
os.makedirs(os.path.dirname(self.tmp_target), exist_ok=True)
544547
cleanpath(os.path.dirname(self.tmp_link), self)
545548
cleanpath(os.path.dirname(self.tmp_target), self)
@@ -580,5 +583,73 @@ def test_handles_nonexistent_target(self):
580583
self.assertEqual(os.readlink(self.tmp_link), broken_target)
581584

582585

586+
class MigSharedFileio__delete_symlink(MigTestCase):
587+
"""Test the delete_symlink function from mig.shared.fileio module"""
588+
589+
def setUp(self):
590+
"""Initialize test environment for delete_symlink tests"""
591+
super(MigSharedFileio__delete_symlink, self).setUp()
592+
self.tmp_link = temppath(DUMMY_FILE_DELETESYMLINKSRC, self)
593+
self.tmp_target = temppath(DUMMY_FILE_DELETESYMLINKDST, self)
594+
# We generally need output dir to exist here
595+
os.makedirs(os.path.dirname(self.tmp_link), exist_ok=True)
596+
os.makedirs(os.path.dirname(self.tmp_target), exist_ok=True)
597+
cleanpath(os.path.dirname(self.tmp_link), self)
598+
cleanpath(os.path.dirname(self.tmp_target), self)
599+
with open(self.tmp_target, 'w') as fh:
600+
fh.write("test")
601+
602+
def create_symlink(self, target=None, link=None):
603+
"""Helper to create valid symlink before deletion"""
604+
if target is None:
605+
target = self.tmp_target
606+
if link is None:
607+
link = self.tmp_link
608+
os.symlink(target, link)
609+
610+
def test_deletes_existing_symlink(self):
611+
"""Test delete_symlink removes existing symlink"""
612+
self.create_symlink()
613+
result = fileio.delete_symlink(self.tmp_link, self.logger)
614+
self.assertTrue(result)
615+
self.assertFalse(os.path.exists(self.tmp_link))
616+
617+
def test_handles_missing_file_with_allow_missing(self):
618+
"""Test delete_symlink succeeds with allow_missing=True"""
619+
# First make sure file doesn't exist
620+
if os.path.exists(self.tmp_link):
621+
os.remove(self.tmp_link)
622+
result = fileio.delete_symlink(self.tmp_link, self.logger,
623+
allow_missing=True)
624+
self.assertTrue(result)
625+
626+
def test_handles_missing_symlink_without_allow_missing(self):
627+
"""Test delete_symlink fails with allow_missing=False"""
628+
self.logger.forgive_errors()
629+
result = fileio.delete_symlink('missing_symlink', self.logger,
630+
allow_missing=False)
631+
self.assertFalse(result)
632+
633+
@unittest.skip("TODO: implement check in tested function and enable again")
634+
def test_rejects_regular_file(self):
635+
"""Test delete_symlink returns False when path is a regular file"""
636+
self.logger.forgive_errors()
637+
with open(self.tmp_link, 'w') as fh:
638+
fh.write("dummy")
639+
640+
result = fileio.delete_symlink(self.tmp_link, self.logger)
641+
self.assertFalse(result)
642+
643+
def test_deletes_broken_symlink(self):
644+
"""Test delete_symlink removes broken symlink"""
645+
# Create broken symlink
646+
broken_target = self.tmp_target + '-nonexistent'
647+
self.create_symlink(broken_target)
648+
self.assertTrue(os.path.islink(self.tmp_link))
649+
# Now delete it
650+
result = fileio.delete_symlink(self.tmp_link, self.logger)
651+
self.assertTrue(result)
652+
653+
583654
if __name__ == '__main__':
584655
testmain()

0 commit comments

Comments
 (0)