Fix conftest for metadata redesign and test version

This is a first attempt on fixing the tests for the redesign of the
metadata handling:

- The metadata_markers (old exif_markers) have been adapted. However, I
  do not think that they make sense. For `metadata` and `nometadata` the
  condition only indicates whether the current instance of vimiv has
  metadata support (or not). For `piexif` and `pyexiv2`they indicate if
  the backend package is installed.

- In addition, having condition `metadata_pyexiv2.pyexiv2 is not None`
  fails for mark `pyexiv2`. I am not sure why that is the case.

- Fixtures `metadata_support`, `no_metadata_support`, `piexif` and
  `pyexiv2` have been added and implemented. They bring
  `metadata._registry` into the respective state, and make use of
  `reset_metadata_registration`, to undo the changes after each tests.

- Updated `test_version`. Am I required to set for each testcase the
  mark, as well as appropriated fixture (i.e. `@mark.piexif` and specify
  `test_XXX(piexif)`. This seems a bit tedious.
This commit is contained in:
Jean-Claude 2023-01-10 18:07:40 +01:00
parent e2e9c4a050
commit 446f4bd588
Signed by: jeanclaude
GPG Key ID: 8A300F57CBB9F63E
3 changed files with 65 additions and 30 deletions

View File

@ -5,9 +5,9 @@ faulthandler_timeout = 30
markers =
current: Mark tests during development
imageformats: Require retrieving images from the web to test additional formats
exif: Require exif support
metadata: Require metadata support
piexif: Require piexif
pyexiv2: Require pyexiv2
noexif: Requires exif support NOT to be available
nometadata: Requires metadata support NOT to be available
ci: Run test only on ci
ci_skip: Skip test on ci

View File

@ -12,7 +12,9 @@ import urllib.request
import pytest
from vimiv.imutils import exif
from vimiv.imutils import metadata
# from vimiv.plugins import metadata_pyexiv2, metadata_piexif
from vimiv.plugins import metadata_piexif
CI = "CI" in os.environ
@ -23,11 +25,12 @@ PLATFORM_MARKERS = (
("ci_skip", not CI, "Skipped on ci"),
)
EXIF_MARKERS = (
("exif", exif.has_exif_support, "Only run with exif support"),
("noexif", not exif.has_exif_support, "Only run without exif support"),
("pyexiv2", exif.pyexiv2 is not None, "Only run with pyexiv2"),
("piexif", exif.piexif is not None, "Only run with piexif"),
METADATA_MARKERS = (
("metadata", metadata.has_metadata_support(), "Only run with metadata support"),
("nometadata", not metadata.has_metadata_support(), "Only run without metadata support"),
("piexif", metadata_piexif.piexif is not None, "Only run with piexif"),
# ("pyexiv2", metadata_pyexiv2.pyexiv2 is not None, "Only run with pyexiv2"),
("pyexiv2", False, "Only run with pyexiv2"),
)
# fmt: on
@ -37,13 +40,13 @@ def apply_platform_markers(item):
apply_markers_helper(item, PLATFORM_MARKERS)
def apply_exif_markers(item):
"""Apply markers that skip tests depending on specific exif support."""
def apply_metadata_markers(item):
"""Apply markers that skip tests depending on specific metadata support."""
if os.path.basename(item.fspath) in ("test_exif.py",):
for marker_name in "exif", "pyexiv2", "piexif":
for marker_name in "metadata", "pyexiv2", "piexif":
marker = getattr(pytest.mark, marker_name)
item.add_marker(marker)
apply_markers_helper(item, EXIF_MARKERS)
apply_markers_helper(item, METADATA_MARKERS)
def apply_markers_helper(item, markers):
@ -61,7 +64,7 @@ def pytest_collection_modifyitems(items):
"""Handle custom markers via pytest hook."""
for item in items:
apply_platform_markers(item)
apply_exif_markers(item)
apply_metadata_markers(item)
@pytest.fixture
@ -166,15 +169,46 @@ def tmpdir():
@pytest.fixture()
def piexif(monkeypatch):
"""Pytest fixture to ensure only piexif is available."""
monkeypatch.setattr(exif, "pyexiv2", None)
def reset_metadata_registration():
"""Fixture to ensure everything is reset to default after testing."""
initial_content = metadata._registry.copy()
yield
metadata._registry = initial_content
@pytest.fixture()
def noexif(monkeypatch, piexif):
"""Pytest fixture to ensure no exif library is available."""
monkeypatch.setattr(exif, "piexif", None)
def metadata_support(reset_metadata_registration):
"""Pytest fixture to ensure piexif and pyexiv2 based metadata plugins are registered."""
from vimiv.plugins.metadata_piexif import MetadataPiexif
from vimiv.plugins.metadata_pyexiv2 import MetadataPyexiv2
metadata._registry = []
metadata.register(MetadataPiexif)
metadata.register(MetadataPyexiv2)
@pytest.fixture()
def piexif(reset_metadata_registration):
"""Pytest fixture to ensure only piexif based metadata plugin is registered."""
from vimiv.plugins.metadata_piexif import MetadataPiexif
metadata._registry = []
metadata.register(MetadataPiexif)
@pytest.fixture()
def pyexiv2(reset_metadata_registration):
"""Pytest fixture to ensure only pyexiv2 based metadata plugin is registered."""
from vimiv.plugins.metadata_pyexiv2 import MetadataPyexiv2
metadata._registry = []
metadata.register(MetadataPyexiv2)
@pytest.fixture()
def no_metadata_support(reset_metadata_registration):
"""Pytest fixture to ensure no metadata plugin is registered."""
metadata._registry = {}
@pytest.fixture()
@ -182,11 +216,12 @@ def add_exif_information():
"""Fixture to retrieve a helper function that adds exif content to an image."""
def add_exif_information_impl(path: str, content):
assert exif.piexif is not None, "piexif required to add exif information"
exif_dict = exif.piexif.load(path)
import piexif
exif_dict = piexif.load(path)
for ifd, ifd_dict in content.items():
for key, value in ifd_dict.items():
exif_dict[ifd][key] = value
exif.piexif.insert(exif.piexif.dump(exif_dict), path)
piexif.insert(piexif.dump(exif_dict), path)
return add_exif_information_impl

View File

@ -9,7 +9,7 @@
import pytest
from vimiv import version
from vimiv.imutils import exif
from vimiv.imutils import metadata
@pytest.fixture
@ -26,15 +26,15 @@ def test_no_svg_support_info(no_svg_support):
@pytest.mark.pyexiv2
def test_pyexiv2_info():
assert exif.pyexiv2.__version__ in version.info()
def test_pyexiv2_info(pyexiv2):
assert f"pyexiv2: {metadata._registry[0]('').version}" in version.info().lower()
@pytest.mark.piexif
def test_piexif_info():
assert exif.piexif.VERSION in version.info()
def test_piexif_info(piexif):
assert f"piexif: {metadata._registry[0]('').version}" in version.info().lower()
def test_no_exif_support_info(noexif):
assert "piexif: none" in version.info().lower()
assert "pyexiv2: none" in version.info().lower()
@pytest.mark.nometadata
def test_no_metadata_support_info(no_metadata_support):
assert "metadata support: false" in version.info().lower()