diff --git a/test/Testing.py b/test/Testing.py index 18cc0c93a..e386f181d 100644 --- a/test/Testing.py +++ b/test/Testing.py @@ -62,7 +62,51 @@ def get_tested_spec_package(name): return FakePkg(candidates[0]) -def get_tested_mock_package(files=None, header=None, name='mockPkg'): +class LazyMock: + """ + Class to store mock package definition and create the actual mock package + when needed, when some internal attribute or method is requested. + """ + + def __init__(self, files, header, name): + self._lazy_pkg = None + self._lazy_files = files + self._lazy_header = header + self._lazy_name = name + + @property + def _fake_pkg(self): + if not self._lazy_pkg: + self._lazy_pkg = get_tested_mock_package(self._lazy_files, + self._lazy_header, + self._lazy_name) + return self._lazy_pkg + + def clone(self, files=None, header=None, name=None): + """ + Copies this LazyMock modifying some properties + """ + + if files is None: + files = self._lazy_files + if header is None: + header = self._lazy_header + if name is None: + name = self._lazy_name + + return LazyMock(files, header, name) + + def __getitem__(self, key): + return self._fake_pkg.__getitem__(key) + + def __getattr__(self, name): + return getattr(self._fake_pkg, name) + + +def get_tested_mock_package(files=None, header=None, name='mockPkg', lazyload=False): + if lazyload: + return LazyMock(files, header, name) + mockPkg = FakePkg(name) if files is not None: if isinstance(files, dict): diff --git a/test/mock_packages.py b/test/mock_packages.py index 367388f63..928a09333 100644 --- a/test/mock_packages.py +++ b/test/mock_packages.py @@ -7,6 +7,7 @@ # Not valid doc folder in python packages PythonDocFolderPackage = get_tested_mock_package( + lazyload=True, files=[ '/usr/lib/python2.7/site-packages/python-mypackage/doc', '/usr/lib/python2.7/site-packages/python-mypackage/docs', @@ -22,6 +23,7 @@ # Valid doc folder in python package if it's a python module PythonDocModulePackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python2.7/site-packages/python-mypackage/doc/__init__.py': {'create_dirs': True, 'include_dirs': 2}, '/usr/lib/python2.7/site-packages/python-mypackage/docs/__init__.py': {'create_dirs': True, 'include_dirs': 1}, @@ -37,6 +39,7 @@ # Python package with old egginfo file, not folder PythonEggInfoFileackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python2.7/site-packages/mydistutilspackage.egg-info': {'content': 'Metadata-Version: 2.1\nName: pythoncheck'}, '/usr/lib/python3.10/site-packages/mydistutilspackage.egg-info': {'content': 'Metadata-Version: 2.1\nName: pythoncheck'}, @@ -47,6 +50,7 @@ PythonFlitMockPackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python3.10/site-packages/flit-3.8.0.dist-info/METADATA': { 'content-path': 'files/python-flit-metadata.txt', @@ -64,7 +68,34 @@ ) +# Python package with missing require +PythonFlitMissingRequirePackage = PythonFlitMockPackage.clone( + header={ + 'requires': [ + 'python3-flit-core', + 'python3-requests', + 'python3-tomli-w', + ], + }, +) + + +# Python package with missing require +PythonFlitLeftoverRequirePackage = PythonFlitMockPackage.clone( + header={ + 'requires': [ + 'python3-docutils', + 'python3-flit-core', + 'python3-poetry', + 'python3-requests', + 'python3-tomli-w', + ], + }, +) + + PythonJupyterServerFileidMockPackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python3.10/site-packages/jupyter_server_fileid-0.9.0.dist-info/METADATA': { 'content-path': 'files/python-jupyter_server_fileid-metadata.txt', @@ -82,6 +113,7 @@ PythonJupyterEventsMockPackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python3.10/site-packages/jupyter_events-0.6.3.dist-info/METADATA': { 'content-path': 'files/python-jupyter-events-metadata.txt', @@ -116,6 +148,7 @@ PythonScikitBuildMockPackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python3.10/site-packages/scikit_build-0.17.2.dist-info/METADATA': { 'content-path': 'files/python-scikit_build-metadata.txt', @@ -136,6 +169,7 @@ # Python flit package with python3.12dist(foo) requirements (used in Fedora see #1171) PythonFlitFedoraMockPackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python3.10/site-packages/flit-3.8.0.dist-info/METADATA': { 'content-path': 'files/python-flit-metadata.txt', @@ -155,6 +189,7 @@ # Python package with old setuptools metadata format, egg-info folder PythonIcecreamPackage = get_tested_mock_package( + lazyload=True, files={ '/usr/lib/python3.10/site-packages/icecream-2.1.3-py3.10.egg-info/requires.txt': { 'content': """ @@ -177,9 +212,36 @@ ) +# Python package with missing require +PythonIcecreamMissingRequirePackage = PythonIcecreamPackage.clone( + header={ + 'requires': [ + 'asttokens>=2.0.1', + 'executing>=0.3.1', + 'pygments>=2.2.0', + ], + }, +) + + +# Python package with leftover require +PythonIcecreamLeftoverRequirePackage = PythonIcecreamPackage.clone( + header={ + 'requires': [ + 'python3-asttokens >= 2.0.1', + 'python3-colorama >= 0.3.9', + 'python3-executing >= 0.3.1', + 'python3-poetry', + 'python3-pygments >= 2.2.0', + ], + }, +) + + # Python package with multiple pyc for different python versions in the same # sitelib PythonMultiplePYCMockPackage = get_tested_mock_package( + lazyload=True, files=[ '/usr/lib/python3.9/site-packages/blinker/__pycache__/base.cpython-310.pyc', '/usr/lib/python3.9/site-packages/blinker/__pycache__/base.cpython-39.opt-1.pyc', @@ -199,6 +261,7 @@ # Python package with pyc files but just the correct version PythonSinglePYCMockPackage = get_tested_mock_package( + lazyload=True, files=[ '/usr/lib/python3.9/site-packages/blinker/__pycache__/base.cpython-39.opt-1.pyc', '/usr/lib/python3.9/site-packages/blinker/__pycache__/base.cpython-39.pyc', diff --git a/test/test_python.py b/test/test_python.py index c60640047..fa9e6cf44 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -3,7 +3,11 @@ PythonDocModulePackage, PythonEggInfoFileackage, PythonFlitFedoraMockPackage, + PythonFlitLeftoverRequirePackage, + PythonFlitMissingRequirePackage, PythonFlitMockPackage, + PythonIcecreamLeftoverRequirePackage, + PythonIcecreamMissingRequirePackage, PythonIcecreamPackage, PythonJupyterEventsMockPackage, PythonJupyterServerFileidMockPackage, @@ -163,47 +167,14 @@ def test_python_dependencies_requires(package, test, output): assert 'W: python-leftover-require' not in out -@pytest.mark.parametrize('package', [get_tested_mock_package( - files={ - '/usr/lib/python3.10/site-packages/icecream-2.1.3-py3.10.egg-info/requires.txt': { - 'content': """ -asttokens>=2.0.1 -colorama>=0.3.9 -executing>=0.3.1 -pygments>=2.2.0 -""", - 'create_dirs': True - }, - }, - header={ - 'requires': [ - 'asttokens>=2.0.1', - 'executing>=0.3.1', - 'pygments>=2.2.0', - ], - }, -)]) +@pytest.mark.parametrize('package', [PythonIcecreamMissingRequirePackage]) def test_python_dependencies_missing_requires(package, test, output): test.check(package) out = output.print_results(output.results) assert 'W: python-missing-require' in out -@pytest.mark.parametrize('package', [get_tested_mock_package( - files={ - '/usr/lib/python3.10/site-packages/flit-3.8.0.dist-info/METADATA': { - 'content-path': 'files/python-flit-metadata.txt', - 'create_dirs': True - }, - }, - header={ - 'requires': [ - 'python3-flit-core', - 'python3-requests', - 'python3-tomli-w', - ], - }, -)]) +@pytest.mark.parametrize('package', [PythonFlitMissingRequirePackage]) def test_python_dependencies_missing_metadata(package, test, output): test.check(package) out = output.print_results(output.results) @@ -211,45 +182,8 @@ def test_python_dependencies_missing_metadata(package, test, output): @pytest.mark.parametrize('package', [ - get_tested_mock_package( - files={ - '/usr/lib/python3.10/site-packages/icecream-2.1.3-py3.10.egg-info/requires.txt': { - 'content': """ -asttokens>=2.0.1 -colorama>=0.3.9 -executing>=0.3.1 -pygments>=2.2.0 -""", - 'create_dirs': True - }, - }, - header={ - 'requires': [ - 'python3-asttokens >= 2.0.1', - 'python3-colorama >= 0.3.9', - 'python3-executing >= 0.3.1', - 'python3-poetry', - 'python3-pygments >= 2.2.0', - ], - }, - ), - get_tested_mock_package( - files={ - '/usr/lib/python3.10/site-packages/flit-3.8.0.dist-info/METADATA': { - 'content-path': 'files/python-flit-metadata.txt', - 'create_dirs': True - }, - }, - header={ - 'requires': [ - 'python3-docutils', - 'python3-flit-core', - 'python3-poetry', - 'python3-requests', - 'python3-tomli-w', - ], - }, - ), + PythonIcecreamLeftoverRequirePackage, + PythonFlitLeftoverRequirePackage, ]) def test_python_dependencies_leftover(package, test, output): test.check(package)