2019-03-25 13:44:57 -07:00
|
|
|
import packaging_scripts.pkgfiles as pkgfiles
|
|
|
|
import re
|
|
|
|
import unittest
|
|
|
|
|
2019-03-27 21:46:33 -07:00
|
|
|
from hypothesis import given
|
|
|
|
from hypothesis.strategies import iterables, text
|
2019-03-25 13:44:57 -07:00
|
|
|
from pathlib import Path
|
|
|
|
from types import GeneratorType
|
2019-03-25 14:20:36 -07:00
|
|
|
from unittest.mock import MagicMock, patch
|
2019-03-25 13:44:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Constants ==========
|
2024-03-02 17:42:26 -08:00
|
|
|
TESTING_MODULE = "packaging_scripts.pkgfiles"
|
2019-03-25 13:44:57 -07:00
|
|
|
# Match any pkgfile of any name with the .pkg.tar.* extension
|
2019-03-25 16:59:15 -07:00
|
|
|
PKGREGEX = r"^[\w.+/-]+\.pkg\.tar(\.\w+)?$"
|
2019-03-25 13:44:57 -07:00
|
|
|
# Match any sigfile of any name with the .pkg.tar.*.sig extension
|
2019-03-25 16:59:15 -07:00
|
|
|
SIGREGEX = r"^[\w.+/-]+\.pkg\.tar(\.\w+)?\.sig$"
|
2019-03-25 13:44:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Unit Tests ==========
|
2019-03-27 21:46:33 -07:00
|
|
|
class TestFilterByRegex(unittest.TestCase):
|
|
|
|
@given(iterables(text()))
|
|
|
|
def test_pkgregex(self, i):
|
|
|
|
result = pkgfiles._filter_by_regex(pkgfiles.PKGREGEX, i)
|
|
|
|
expected = [Path(p) for p in i if re.match(PKGREGEX, str(p))]
|
2019-03-27 17:22:11 -07:00
|
|
|
|
|
|
|
self.assertListEqual(sorted(result), expected)
|
2019-03-25 21:53:27 -07:00
|
|
|
self.assertIsInstance(result, GeneratorType)
|
|
|
|
|
2019-03-27 21:46:33 -07:00
|
|
|
@given(iterables(text()))
|
|
|
|
def test_sigregex(self, i):
|
|
|
|
result = pkgfiles._filter_by_regex(pkgfiles.SIGREGEX, i)
|
|
|
|
expected = [Path(p) for p in i if re.match(SIGREGEX, str(p))]
|
2019-03-25 21:53:27 -07:00
|
|
|
|
|
|
|
self.assertListEqual(list(result), expected)
|
|
|
|
self.assertIsInstance(result, GeneratorType)
|
|
|
|
|
|
|
|
|
2019-03-25 14:20:36 -07:00
|
|
|
class TestAddPkgfile(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.patched_shutil = patch(f"{TESTING_MODULE}.shutil")
|
|
|
|
self.mocked_shutil = self.patched_shutil.start()
|
|
|
|
|
2019-03-27 21:46:33 -07:00
|
|
|
@given(text())
|
|
|
|
def test_pkgfile_path(self, p):
|
|
|
|
pkgfile = Path(p)
|
|
|
|
cachedir = Path("/var/cache/repo")
|
2019-03-25 14:20:36 -07:00
|
|
|
|
2019-03-27 21:46:33 -07:00
|
|
|
pkgfiles.add(pkgfile, cachedir)
|
2019-03-25 14:20:36 -07:00
|
|
|
|
|
|
|
self.mocked_shutil.move.assert_called_with(
|
2019-03-27 21:46:33 -07:00
|
|
|
pkgfile, Path(f"/var/cache/repo/{pkgfile.name}")
|
2019-03-25 14:20:36 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.patched_shutil.stop()
|
|
|
|
|
|
|
|
|
2019-03-25 21:53:27 -07:00
|
|
|
class TestDeletePkgfile(unittest.TestCase):
|
2019-03-25 14:20:36 -07:00
|
|
|
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()
|