Skip to content

Commit

Permalink
test: Lazy load mock packages
Browse files Browse the repository at this point in the history
This patch add the ability to lazy load the mock packages annd also a
way to copy mock packages to just modify some of the package data.

See #1104
  • Loading branch information
danigm committed Feb 1, 2024
1 parent 8bc1c69 commit c01bd1f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 75 deletions.
46 changes: 45 additions & 1 deletion test/Testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
63 changes: 63 additions & 0 deletions test/mock_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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},
Expand All @@ -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'},
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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': """
Expand All @@ -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',
Expand All @@ -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',
Expand Down
82 changes: 8 additions & 74 deletions test/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
PythonDocModulePackage,
PythonEggInfoFileackage,
PythonFlitFedoraMockPackage,
PythonFlitLeftoverRequirePackage,
PythonFlitMissingRequirePackage,
PythonFlitMockPackage,
PythonIcecreamLeftoverRequirePackage,
PythonIcecreamMissingRequirePackage,
PythonIcecreamPackage,
PythonJupyterEventsMockPackage,
PythonJupyterServerFileidMockPackage,
Expand Down Expand Up @@ -163,93 +167,23 @@ 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)
assert 'W: python-missing-require' 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': [
'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)
Expand Down

0 comments on commit c01bd1f

Please sign in to comment.