Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mock packages #1183

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
98 changes: 91 additions & 7 deletions test/mock_packages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# mock_packages.py
from Testing import get_tested_mock_package

PythonSitePackage = get_tested_mock_package(

########################
# Python mock packages #
########################

# 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 @@ -14,7 +20,10 @@
]
)

PythonModuleMockPackage = get_tested_mock_package(

# 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 @@ -27,7 +36,10 @@
}
)

PythonDistutilsMockPackage = get_tested_mock_package(

# 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 @@ -36,7 +48,9 @@
}
)


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 @@ -53,7 +67,35 @@
},
)


# 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 @@ -69,7 +111,9 @@
},
)


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 @@ -102,7 +146,9 @@
},
)


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 @@ -120,7 +166,10 @@
},
)

PythonFlitUpdatedMockPackage = get_tested_mock_package(

# 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 @@ -137,7 +186,10 @@
},
)


# 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 @@ -160,7 +212,36 @@
)


PythonBlinkerMockPackage = get_tested_mock_package(
# 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 @@ -177,7 +258,10 @@
]
)

PythonSiteBlinkerMockPackage = get_tested_mock_package(

# 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
Loading
Loading