a
    ضaW                     @   s   d Z ddlZddlZddlmZ ddlZddlmZmZ ddl	Z	ddl
Z
e
jde	jdk r\dndd d	d
 Zdd ZdddZdS )z2Tests for tinypages build using sphinx extensions.    N)Path)PopenPIPEZsphinx)   
   z4.1.3)Z
minversionc              
      s$  t | d }tt tjd | |d d |d  tjddddd	t tt tjd tg	}t|t	t	d
i t
jdt
jdd}| \}}t|  fdd fddfddtddD \}}}t|dsJ t|dsJ t|dsJ t|ds*J t|ds@J t|dsVJ d  }	d|	v spJ t|d sJ t|d sJ d |	v sJ d!|	v sJ d"|	v sJ d#|	v sJ |	d$d%ksJ t|d&sJ |d'  }
|
d(d)}
|d' |
 fd*dd+D }t|  t|d&sXJ d j|d, kstJ d% j|d ksJ d- j|d% ksJ t|dsJ t|d%sJ t|d-sJ d j|d- ks
J t|ds J d S ).NsrcZ	tinypages_buildhtmldoctrees-msphinx-W-b-dT )
MPLBACKENDZGCOV_ERROR_FILEstdoutstderruniversal_newlinesenvc                    s    d|  d S )Nsome_plots-.png num)html_dirr   o/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/matplotlib/tests/test_sphinxext.py	plot_file%   s    z!test_tinypages.<locals>.plot_filec                    s    j d d|  d S )NZplot_directiver   r   )parentr   )doctree_dirr   r   plot_directive_file(   s    z+test_tinypages.<locals>.plot_directive_filec                    s   g | ]} |qS r   r   .0i)r   r   r   
<listcomp>,       z"test_tinypages.<locals>.<listcomp>               Z12_00Z12_01   zsome_plots.htmls   # Only a commentz
range4.pngz
range6.pngs    This is the caption for plot 15.s    Plot 17 uses the caption option.s    This is the caption for plot 18.s&   plot-directive my-class my-other-classs#   This caption applies to both plots.      zincluded_plot_21.rstzplt.plot(range(6))zplt.plot(range(4))c                    s   g | ]} |  jqS r   )statst_mtimer!   )r    r   r   r$   R   s   )r&   r,   r   r(   r   r   )r   shutilcopytree__file__r   sys
executablestrr   r   osenvirondevnullcommunicatebuild_sphinx_htmlrangefilecmpcmp
read_bytescount	read_textreplace
write_textr.   r/   )tmpdir
source_dircmdprocouterrZrange_10Zrange_6Zrange_4Zhtml_contentscontentsZmodification_timesr   )r   r   r    r   r   test_tinypages   sb    
rJ   c                 C   s   t | d }|  t tj}t|d |d  t|d |d  |d }|d d |d	 d
 }t||| ddd |	 D v sJ |d	 d }t|||ddgd ddd |	 D vsJ d S )Nr   ztinypages/conf.pyzconf.pyztinypages/_staticZ_staticr
   z	index.rstz#
.. plot::

    plt.plot(range(2))
r   Zhtml1z
index-1.pyc                 S   s   g | ]
}|j qS r   namer"   pr   r   r   r$   t   r%   z3test_plot_html_show_source_link.<locals>.<listcomp>Zhtml2z-Dzplot_html_show_source_link=0)
extra_argsc                 S   s   g | ]
}|j qS r   rK   rM   r   r   r   r$   z   r%   )
r   mkdirr2   r   r0   copyfiler1   rB   r:   iterdir)rC   rD   r   r   Z	html_dir1Z	html_dir2r   r   r   test_plot_html_show_source_linke   s    
rS   c              
   C   s   |d u rg n|}t jdddddt|t| t|g	|}t|ttdi tjddid	}| \}}|jd
ksJ d| d| d|rt	
d|  | sJ d S )Nr   r   r   r	   r   Tr   r   r   r   z!sphinx build failed with stdout:
z	
stderr:

z-sphinx build emitted the following warnings:
)r3   r4   r5   r   r   r6   r7   r9   
returncodepytestZfailis_dir)rD   r   r   rO   rE   rF   rG   rH   r   r   r   r:   }   s     
r:   )N)__doc__r<   r6   pathlibr   r0   
subprocessr   r   r3   rV   Zimportorskipversion_inforJ   rS   r:   r   r   r   r   <module>   s   T