U
    d»{j[  ã                   @   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
t
jdt
jdœ–d�}| ¡ \}}t|ˆ ˆƒ ‡fdd„‰‡ fdd„‰‡fdd„tddƒD ƒ\}}}t |ˆdƒ¡sæt‚t |ˆdƒ¡sút‚t |ˆdƒ¡�st‚t |ˆdƒ¡�s&t‚t |ˆdƒ¡�s<t‚t |ˆdƒ¡�sRt‚ˆd  ¡ }	d|	k�slt‚t |ˆd ¡�s‚t‚t |ˆd ¡�s˜t‚d |	k�s¦t‚d!|	k�s´t‚d"|	k�sÂt‚d#|	k�sÐt‚|	 d$¡d%k�sät‚t |ˆd&ƒ¡�sút‚|d'  ¡ }
|
 d(d)¡}
|d'  |
¡ ‡fd*d„d+D ƒ}t|ˆ ˆƒ t |ˆd&ƒ¡�sTt‚ˆdƒ ¡ j|d, k�spt‚ˆd%ƒ ¡ j|d k�sŒt‚ˆd-ƒ ¡ j|d% k�s¨t‚t |ˆdƒ¡�s¾t‚t |ˆd%ƒ¡�sÔt‚t |ˆd-ƒ¡�sêt‚ˆdƒ ¡ j|d- k�st‚t |ˆdƒ¡�st‚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   ú^/var/www/html/bwave/python/venv/lib/python3.8/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>,   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.rsts   plt.plot(range(6))s   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ÚAssertionErrorÚ
read_bytesÚcountÚreplaceÚwrite_bytesr-   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   sl      þ   þ
ÿrI   c                 C   sÌ   t | ƒd }| ¡  t tƒj}t |d |d ¡ t |d |d ¡ |d }|d  d¡ |d	 d
 }t|||ƒ ddd„ | 	¡ D ƒksŽt
‚|d	 d }t|||ddgd� ddd„ | 	¡ D ƒksÈt
‚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   s     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   rJ   rL   r   r   r   r$   z   s     )r   Úmkdirr1   r   r/   Úcopyfiler0   Ú
write_textr9   Úiterdirr=   )rB   rC   r   r   Z	html_dir1Z	html_dir2r   r   r   Útest_plot_html_show_source_linke   s    
ÿrS   c              
   C   s¤   |d krg n|}t jdddddt|ƒt| ƒt|ƒf	|•}t|ttdtjddi–d	�}| ¡ \}}|jd
ks€t	d|› d|› d�ƒ‚|r”t
 d|› �¡ | ¡ s t	‚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:
)r2   r3   r4   r   r   r5   r6   r8   Ú
returncoder=   ÚpytestZfailÚis_dir)rC   r   r   rN   rD   rE   rF   rG   r   r   r   r9   }   s&       ÿÿ
ÿÿr9   )N)Ú__doc__r;   r5   Úpathlibr   r/   Ú
subprocessr   r   r2   rV   ZimportorskipÚversion_inforI   rS   r9   r   r   r   r   Ú<module>   s   ÿT