First tests for main program arguments validation

This commit is contained in:
2024-06-14 00:54:31 -04:00
parent c0a706104f
commit 5d4d459f07
5 changed files with 106 additions and 21 deletions

0
__init__.py Normal file
View File

View File

@@ -1,7 +1,39 @@
import argparse
import os
from pathlib import Path
import sys
def link_fixer(ln_path, tgt_dir_path):
ln_is_dir = False
link = Path(ln_path)
tgt_dir = Path(tgt_dir_path)
if link.is_dir():
ln_is_dir = True
elif not link.is_symlink():
if not link.exists():
sys.exit("Link argument matches no file or directory")
else:
sys.exit("Link argument is not a symbolic link")
elif link.exists():
sys.exit("Link is not broken!")
if not tgt_dir.exists():
sys.exit("Target directory not found.")
if not tgt_dir.is_dir():
sys.exit("Pointed target is not a directory")
print("Starting link fixer")
if ln_is_dir:
print("Links dir: \t", link)
else:
print("Link: \t\t", link)
print("Targets dir: \t", tgt_dir)
if ln_is_dir:
sys.exit("But ln dir version not yet implemented. Sorry!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
@@ -18,24 +50,4 @@ if __name__ == "__main__":
link = Path(args.link)
tgt_dir = Path(args.tgt_path)
ln_is_dir = False
if not link.is_symlink():
sys.exit("Link not found or not a symbolic link")
if link.is_dir():
ln_is_dir = True
if not tgt_dir.exists():
sys.exit("Target directory not found.")
if not tgt_dir.is_dir():
sys.exit("Pointed target is not a directory")
print("Starting link fixer")
if ln_is_dir:
print("Links dir: \t", link)
else:
print("Link: \t\t", link)
print("Targets dir: \t", tgt_dir)
if ln_is_dir:
sys.exit("But ln dir version not yet implemented. Sorry!")
link_fixer(link, tgt_dir)

0
search_file.py Normal file
View File

0
test/__init__.py Normal file
View File

73
test/test_link_fixer.py Normal file
View File

@@ -0,0 +1,73 @@
from link_fixer import link_fixer
import os
from pathlib import Path
import unittest
class TestFixLinker(unittest.TestCase):
tgt_dir = Path("test/data/tgt_dir")
lnk_dir = Path("test/data/lnk_dir")
tgt_file = Path("test/data/tgt_dir/tgt_file.txt")
ln_non_exists = Path("test/data/link.txt")
ln_valid = Path("test/data/lnk_dir/ln_valid")
ln_valid_path = "../tgt_dir/tgt_file.txt"
ln_broken = Path("test/data/lnk_dir/tgt_file.txt")
ln_broken_path = "../tgt/tgt_.txt"
inexistent = Path("test/data/tgt_dir/inexistent.txt")
ln_ne_error = "Link argument matches no file or directory"
ln_gd_error = "Link is not broken!"
ln_ns_error = "Link argument is not a symbolic link"
def reset_dir(self, dir_path):
if not dir_path.is_dir():
if dir_path.exists():
dir_path.unlink()
os.mkdir(dir_path)
return
def reset_file(self, file_path):
if not file_path.is_file():
if file_path.exists():
filepath.unlink()
open(file_path, 'a').close()
def setup_test(self):
self.reset_dir(self.tgt_dir)
self.reset_dir(self.lnk_dir)
self.reset_file(self.tgt_file)
self.reset_file(self.inexistent)
if self.ln_valid.exists() or self.ln_valid.is_symlink():
self.ln_valid.unlink()
self.ln_valid.symlink_to(self.ln_valid_path)
if self.ln_broken.exists() or self.ln_broken.is_symlink():
self.ln_broken.unlink()
self.ln_broken.symlink_to(self.ln_broken_path)
self.inexistent.unlink()
def test_link_fixer(self):
self.setup_test()
print("Test for inexistent link")
with self.assertRaises(SystemExit) as err:
link_fixer(self.ln_non_exists, self.tgt_dir)
self.assertEqual(err.exception.code, self.ln_ne_error)
print("Test for non-broken link")
with self.assertRaises(SystemExit) as err:
link_fixer(self.ln_valid, self.tgt_dir)
self.assertEqual(err.exception.code, self.ln_gd_error)
print("Test for non-link link")
with self.assertRaises(SystemExit) as err:
link_fixer(self.tgt_file, self.tgt_dir)
self.assertEqual(err.exception.code, self.ln_ns_error)
if __name__ == "__main__":
unittest.main()