Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c885729

Browse files
Added logic for creation of baseline images
1 parent 917aae5 commit c885729

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,7 @@ lib/z.lib
104104
#########################
105105
lib/matplotlib/backends/web_backend/node_modules/
106106
lib/matplotlib/backends/web_backend/package-lock.json
107+
108+
# Matplotlib tempenv #
109+
######################
110+
temp/testenv

lib/matplotlib/testing/decorators.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
import shutil
88
import string
9+
import subprocess
910
import sys
1011
import unittest
1112
import warnings
@@ -486,15 +487,14 @@ def _image_directories(func):
486487
try:
487488
import matplotlib_baseline_images
488489
except:
489-
raise ImportError("Not able to import matplotlib_baseline_images")
490+
generate_matplotlib_baseline_images_from_previous_commit()
490491
baseline_dir = (Path(matplotlib_baseline_images.__file__).parent /
491492
module_path.stem)
492493
elif func.__module__.startswith("mpl_toolkits."):
493494
try:
494495
import mpl_toolkits_baseline_images
495496
except:
496-
raise ImportError("Not able to import "
497-
"mpl_toolkits_baseline_images")
497+
generate_matplotlib_baseline_images_from_previous_commit()
498498
baseline_dir = (Path(mpl_toolkits_baseline_images.__file__).parent /
499499
module_path.stem)
500500
else:
@@ -504,3 +504,44 @@ def _image_directories(func):
504504
result_dir = Path().resolve() / "result_images" / module_path.stem
505505
result_dir.mkdir(parents=True, exist_ok=True)
506506
return baseline_dir, result_dir
507+
508+
509+
def generate_matplotlib_baseline_images_from_previous_commit():
510+
mplRepoPath = Path(mpl.__file__).parent
511+
# Check that git is installed.
512+
run_text_mode(shutil.which('git')) == None
513+
# Check if current repository is a git repository
514+
run_text_mode(['git', 'status'],
515+
cwd = mplRepoPath) == None
516+
# Create virtual env in tmpdir.
517+
if not (mplRepoPath / 'testenv').exists():
518+
modify_existing_baseline_images()
519+
else
520+
# Clone mpl repo to tmpvenv and run pytests from new mpl repo created
521+
run_text_mode(["git", "worktree", "add", "testenv"],
522+
cwd = mplRepoPath)
523+
testenvPath = mplRepoPath / testenv
524+
run_text_mode([sys.executable, "-mvenv", "venv"],
525+
cwd = testenvPath)
526+
run_text_mode(["source", "venv/bin/activate"],
527+
cwd = testenvPath)
528+
run_text_mode(["git", "checkout", "HEAD~"],
529+
cwd = testenvPath)
530+
run_text_mode(["python", "-mpip", "install", "-e", mplRepoPath],
531+
cwd = testenvPath)
532+
run_text_mode(["python", "-mpytest"],
533+
cwd = testenvPath)
534+
# Copy result_images in the temporary directory and cache it
535+
run_text_mode(shutil.copytree(testenvPath / "result_images",
536+
mplRepoPath / ".pytest_cache"),
537+
cwd = mplRepoPath)
538+
539+
def modify_existing_baseline_images():
540+
# TODO
541+
return
542+
543+
def run_text_mode(*args, **kwargs):
544+
return subprocess.run(*args, **kwargs,
545+
stderr=subprocess.STDOUT,
546+
stdout = open(os.devnull, 'w'),
547+
universal_newlines=True)

0 commit comments

Comments
 (0)