73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
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() |