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

Fix reset_image_cache #39

Merged
merged 3 commits into from
Jan 29, 2023
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
12 changes: 10 additions & 2 deletions pytest_pyvista/pytest_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,18 @@ def __call__(self, plotter):
# cached image name. We remove the first 5 characters of the function name
# "test_" to get the name for the image.
image_filename = os.path.join(self.cache_dir, test_name[5:] + ".png")
if not os.path.isfile(image_filename) and self.fail_extra_image_cache:
if (
not os.path.isfile(image_filename)
and self.fail_extra_image_cache
and not self.reset_image_cache
):
raise RuntimeError(f"{image_filename} does not exist in image cache")

if self.add_missing_images and not os.path.isfile(image_filename):
if (
self.add_missing_images
and not os.path.isfile(image_filename)
or self.reset_image_cache
):
plotter.screenshot(image_filename)

if self.generated_image_dir is not None:
Expand Down
30 changes: 29 additions & 1 deletion tests/test_pyvista.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import filecmp
import os

import pytest
Expand Down Expand Up @@ -36,7 +37,9 @@ def make_cached_images(test_path, path="image_cache_dir", name="imcache.png"):
sphere = pv.Sphere()
plotter = pv.Plotter()
plotter.add_mesh(sphere, color="red")
plotter.screenshot(os.path.join(d, name))
filename = os.path.join(d, name)
plotter.screenshot(filename)
return filename


@skip_vtk8
Expand Down Expand Up @@ -260,3 +263,28 @@ def test_imcache(verify_image_cache):

result = testdir.runpytest("--fail_extra_image_cache", "--skip_image_cache_vtk8")
result.stdout.fnmatch_lines("*[Pp]assed*")


@skip_vtk8
def test_reset_image_cache(testdir):
"""Test skip vtk8 via CLI option."""
filename = make_cached_images(testdir.tmpdir)
filename_original = make_cached_images(testdir.tmpdir, name="original.png")
assert filecmp.cmp(filename, filename_original, shallow=False)

testdir.makepyfile(
"""
import pyvista as pv
pv.OFF_SCREEN = True
def test_imcache(verify_image_cache):
sphere = pv.Sphere()
plotter = pv.Plotter()
plotter.add_mesh(sphere, color="blue")
plotter.show()
"""
)
result = testdir.runpytest("--fail_extra_image_cache", "--reset_image_cache")
# file was overwritten
assert not filecmp.cmp(filename, filename_original, shallow=False)
# should pass even if image doesn't match
result.stdout.fnmatch_lines("*[Pp]assed*")