Move add and delete functionality to pkgfiles module, add tests

This commit is contained in:
Eric Torres
2019-03-25 14:20:36 -07:00
parent 9048a55c0f
commit fdc5510c46
4 changed files with 82 additions and 49 deletions

View File

@ -4,23 +4,23 @@ import unittest
from pathlib import Path
from types import GeneratorType
from unittest.mock import patch
from unittest.mock import MagicMock, patch
# ========== Constants ==========
TESTING_MODULE = f"packaging_scripts.pkgfiles"
# Match any pkgfile of any name with the .pkg.tar.* extension
PKGREGEX = "^[\w]+[.]pkg[.]tar([.][\w]+)?$"
PKGREGEX = r"^\w+[.]pkg[.]tar([.]\w+)?$"
# Match any sigfile of any name with the .pkg.tar.*.sig extension
SIGREGEX = '^.+[.]pkg[.]tar[.].+[.]sig$'
SIGREGEX = r"^\w+[.]pkg[.]tar([.]\w+)?[.]sig$"
ALL_PKGFILES = [
Path("pkg1.pkg.tar.xz"),
Path("pkg1.pkg.tar.xz.sig"),
Path("pkg2.pkg.tar.xz"),
Path("pkg3.pkg.tar.xz"),
Path("pkg3.pkg.tar.xz.sig"),
Path('notapkg.sig'),
Path("pkg1.pkg.tar.xz"),
Path("pkg1.pkg.tar.xz.sig"),
Path("pkg2.pkg.tar.xz"),
Path("pkg3.pkg.tar.xz"),
Path("pkg3.pkg.tar.xz.sig"),
Path("notapkg.sig"),
]
@ -31,10 +31,7 @@ def get_all_files(*args):
def filter_files(query):
for f in [
Path("pkg3.pkg.tar.xz"),
Path("pkg3.pkg.tar.xz.sig"),
]:
for f in [Path("pkg3.pkg.tar.xz"), Path("pkg3.pkg.tar.xz.sig")]:
yield f
@ -47,7 +44,7 @@ class TestFilterPkgfiles(unittest.TestCase):
self.mocked_glob.side_effect = get_all_files
def test_yield_no_query(self):
result = pkgfiles.get_pkgfiles()
result = pkgfiles.get()
expected = [s for s in get_all_files() if re.match(PKGREGEX, str(s))]
self.assertListEqual(list(result), expected)
@ -56,7 +53,7 @@ class TestFilterPkgfiles(unittest.TestCase):
def test_yield_with_query(self):
self.mocked_glob.side_effect = filter_files
result = pkgfiles.get_pkgfiles('pkg3')
result = pkgfiles.get("pkg3")
expected = [Path("pkg3.pkg.tar.xz")]
self.assertListEqual(list(result), expected)
@ -74,7 +71,7 @@ class TestFilterSigfiles(unittest.TestCase):
self.mocked_glob.side_effect = get_all_files
def test_yield_no_query(self):
result = pkgfiles.get_pkgfiles(signatures_only=True)
result = pkgfiles.get(signatures_only=True)
expected = [s for s in get_all_files() if re.match(SIGREGEX, str(s))]
self.assertListEqual(list(result), expected)
@ -83,7 +80,7 @@ class TestFilterSigfiles(unittest.TestCase):
def test_yield_with_query(self):
self.mocked_glob.side_effect = filter_files
result = pkgfiles.get_pkgfiles('pkg3', signatures_only=True)
result = pkgfiles.get("pkg3", signatures_only=True)
expected = [Path("pkg3.pkg.tar.xz.sig")]
self.assertListEqual(list(result), expected)
@ -91,3 +88,32 @@ class TestFilterSigfiles(unittest.TestCase):
def tearDown(self):
self.patched_path.stop()
class TestAddPkgfile(unittest.TestCase):
def setUp(self):
self.patched_shutil = patch(f"{TESTING_MODULE}.shutil")
self.mocked_shutil = self.patched_shutil.start()
self.pkgfile = Path("/home/user/pkgfile")
self.cachedir = Path("/var/cache/repo")
def test_pkgfile_path(self):
pkgfiles.add(self.pkgfile, self.cachedir)
self.mocked_shutil.move.assert_called_with(
self.pkgfile, Path("/var/cache/repo/pkgfile")
)
def tearDown(self):
self.patched_shutil.stop()
class TestDeletePkgfiles(unittest.TestCase):
def setUp(self):
self.pkgfile = MagicMock("/var/cache/repo/pkgfile", spec_set=Path)
def test_delete(self):
pkgfiles.delete(self.pkgfile)
self.pkgfile.unlink.assert_called()