66from pathlib import Path
77import shutil
88import string
9+ import subprocess
910import sys
1011import unittest
1112import 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,55 @@ 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+ # Check that git is installed.
511+ subprocess .run (shutil .which ('git' ),
512+ stderr = subprocess .STDOUT ,
513+ stdout = open (os .devnull , 'w' ),
514+ universal_newlines = True ) == None
515+ # Check if current repository is a git repository
516+ subprocess .run (['git' , 'status' ],
517+ cwd = Path (mpl .__file__ ).parent ,
518+ stderr = subprocess .STDOUT ,
519+ stdout = open (os .devnull , 'w' ),
520+ universal_newlines = True ) == None
521+ # Create virtual env in tmpdir.
522+ subprocess .run (["python" , "-mvenv" , "tmp/testenv" ],
523+ cwd = "Path(mpl.__file__).parent" ,
524+ stderr = subprocess .STDOUT ,
525+ stdout = subprocess .DEVNULL ),
526+ universal_newlines = True )
527+ # Clone mpl repo to tmpvenv and run pytests from new mpl repo created
528+ subprocess .run (["git" , "clone" , cwd , "tmp/testenv" ],
529+ cwd = Path (mpl .__file__ ).parent ,
530+ stderr = subprocess .STDOUT ,
531+ stdout = subprocess .DEVNULL ,
532+ universal_newlines = True )
533+ subprocess .run (["tmp/testenv/bin/python" ],
534+ cwd = Path (mpl .__file__ ).parent ,
535+ stderr = subprocess .STDOUT ,
536+ stdout = subprocess .DEVNULL ,
537+ universal_newlines = True )
538+ subprocess .run (["git" , "checkout" , "HEAD~" ],
539+ cwd = Path (mpl .__file__ ).parent ,
540+ stderr = subprocess .STDOUT ,
541+ stdout = subprocess .DEVNULL ,
542+ universal_newlines = True )
543+ subprocess .run (["python" , "-mpip" , "install" , "-e" , cwd ],
544+ cwd = Path (mpl .__file__ ).parent ,
545+ stderr = subprocess .STDOUT ,
546+ stdout = subprocess .DEVNULL ,
547+ universal_newlines = True )
548+ subprocess .run (["python" , "-mpytest" ],
549+ cwd = Path (mpl .__file__ ).parent ,
550+ stderr = subprocess .STDOUT ,
551+ stdout = subprocess .DEVNULL ,
552+ universal_newlines = True )
553+ # Copy result_images in the temporary directory and cache it
554+ subprocess .run (shutil .copytree ("result_images" , "tempenv/.pytest_cache" ),
555+ cwd = Path (mpl .__file__ ).parent ,
556+ stderr = subprocess .STDOUT ,
557+ stdout = subprocess .DEVNUL ,
558+ universal_newlines = True )
0 commit comments