66from pathlib import Path
77import shutil
88import string
9+ import subprocess
910import sys
1011import unittest
1112import warnings
@@ -486,7 +487,7 @@ 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." ):
@@ -504,3 +505,23 @@ def _image_directories(func):
504505 result_dir = Path ().resolve () / "result_images" / module_path .stem
505506 result_dir .mkdir (parents = True , exist_ok = True )
506507 return baseline_dir , result_dir
508+
509+
510+ def generate_matplotlib_baseline_images_from_previous_commit ():
511+ # Check if current repository is a git repository
512+ subprocess .call (['git' , '-C' , '.' , 'status' ],
513+ stderr = subprocess .STDOUT ,
514+ stdout = open (os .devnull ,
515+ 'w' )) == 0
516+ # Check that git is installed.
517+ subprocess .call (shutil .which ('git' ),
518+ stderr = subprocess .STDOUT ,
519+ stdout = open (os .devnull ,
520+ 'w' )) == 0
521+ # Create virtual env in tmpdir.
522+ subprocess .run ([sys .executable , "-mvenv" , tmpenv ])
523+ subprocess .run (["git" , "clone" , "<current repo>" , "<tmpenv/...>" ])
524+ ... git checkout HEAD ~
525+ ... python - mpytest ...
526+ # -> this will generate result_images in that temporary directory
527+ # -> copy it and cache it.
0 commit comments