o
    :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suJ t|dsJ t|dsJ t|dsJ t|dsJ t|dsJ d  }	d|	v sJ 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&sJ d j|d, ks-J d% j|d ks;J d- j|d% ksIJ t|dsTJ t|d%s_J t|d-sjJ d j|d- ksxJ t|dsJ d S ).NsrcZ	tinypages_buildhtmldoctrees-msphinx-W-b-dT )
MPLBACKENDZGCOV_ERROR_FILEstdoutstderrZuniversal_newlinesenvc                    s    d|  d S )Nsome_plots-.png Znum)html_dirr   A/usr/lib/python3/dist-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>,   s    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filecmpZcmpZ
read_bytescountZ	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   
rD   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 sGJ |d	 d }t|||ddgd ddd |	 D vsdJ 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      g | ]}|j qS r   namer    pr   r   r   r"   t       z3test_plot_html_show_source_link.<locals>.<listcomp>Zhtml2z-Dzplot_html_show_source_link=0)
extra_argsc                 S   rE   r   rF   rH   r   r   r   r"   z   rJ   )
r   mkdirr/   r   r-   copyfiler.   r<   r7   Ziterdir)r=   r>   r   r   Z	html_dir1Z	html_dir2r   r   r   test_plot_html_show_source_linke   s   
rN   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sBJ d| d| d|rLt	
d|  | sRJ 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:
)r0   r1   r2   r   r   r3   r4   r6   
returncodepytestZfailis_dir)r>   r   r   rK   r?   r@   rA   rB   r   r   r   r7   }   s    
r7   )N)__doc__r9   r3   Zpathlibr   r-   
subprocessr   r   r0   rQ   Zimportorskipversion_inforD   rN   r7   r   r   r   r   <module>   s    T