a
    7Ubj                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZ d	d
lmZ d	dlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZdZG dd deZ G dd deZ!dS )z+
Module with tests for templateexporter.py
    N)default)Config)
DictLoaderTemplateNotFound)v4)patch)ProcessPoolExecutor   )ExportersTestsBase)CheesePreprocessor   TemplateExporter)RSTExporter)HTMLExporter)MarkdownExporter)tempdirzW{%- extends 'index.rst.j2' -%}
{%- block in_prompt -%}
blah
{%- endblock in_prompt -%}
c                   @   s0   e Zd ZdZeddd Zeddd ZdS )	SampleExporterz%
    Exports a Python code file.
    Zfile_extensionc                 C   s   dS )Nz.py selfr   r   Nlib/python3.9/site-packages/nbconvert/exporters/tests/test_templateexporter.py_file_extension_default&   s    z&SampleExporter._file_extension_defaulttemplate_namec                 C   s   dS )Npythonr   r   r   r   r   _template_name_default*   s    z%SampleExporter._template_name_defaultN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   "   s
   
r   c                   @   sd  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdWddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dXdUdVZ-dS )YTestExporterz'Contains test functions for exporter.pyc                 C   s
   t   dS )z8
        Can a TemplateExporter be constructed?
        Nr   r   r   r   r   test_constructor2   s    zTestExporter.test_constructorc                 C   s.   |   }||  \}}t|dks*J dS )z:
        Can a TemplateExporter export something?
        r   N)_make_exporterfrom_filename_get_notebooklenr   exporteroutput	resourcesr   r   r   test_export9   s    zTestExporter.test_exportc                 C   sd   t dddii}| j|d}||  \}}|dus:J t|d tsLJ t|d dks`J dS )zU
        If the ExtractOutputPreprocessor is enabled, are outputs extracted?
        ZExtractOutputPreprocessorenabledTconfigNZoutputsr   )r   r"   r#   r$   
isinstancedictr%   r   r-   r'   r(   r)   r   r   r   test_extract_outputsB   s    z!TestExporter.test_extract_outputsc                 C   sP   t ddtgii}| j|d}||  \}}|dus<J |d dksLJ dS )zV
        Can a preprocessor be added to the preprocessors list by class type?
        Exporterpreprocessorsr,   Ncheeserealr   r   r"   r#   r$   r0   r   r   r   test_preprocessor_classN   s
    z$TestExporter.test_preprocessor_classc                 C   sR   t ddt gii}| j|d}||  \}}|dus>J |d dksNJ dS )zT
        Can a preprocessor be added to the preprocessors list by instance?
        r2   r3   r,   Nr4   r5   r6   r0   r   r   r   test_preprocessor_instanceY   s
    z'TestExporter.test_preprocessor_instancec                 C   sP   t dddgii}| j|d}||  \}}|dus<J |d dksLJ dS )z^
        Can a preprocessor be added to the preprocessors list by dotted object name?
        r2   r3   z3nbconvert.exporters.tests.cheese.CheesePreprocessorr,   Nr4   r5   )r   r"   r#   r$   r0   r   r   r   "test_preprocessor_dottedobjectnamed   s
    z/TestExporter.test_preprocessor_dottedobjectnamec                 C   sH   |   }|jtdd ||  \}}|dus4J |d dksDJ dS )zR
        Can a preprocessor be added via the Exporter convenience method?
        T)r+   Nr4   r5   )r"   Zregister_preprocessorr   r#   r$   r&   r   r   r   test_preprocessor_via_methodo   s
    z)TestExporter.test_preprocessor_via_methodc                 C   s<   |   }t }||j|   \}}t|dks8J dS )zE
        Can exporters be pickled & called across processes?
        r   N)r"   r   Zsubmitr#   r$   resultr%   )r   r'   Zexecutorr(   r)   r   r   r   test_pickley   s    zTestExporter.test_picklec              	   C   s   t  }tj|d}d}t|d}|| W d    n1 sF0    Y  t }||j_	| j
|d}|jj|kszJ tj||jv sJ W d    n1 s0    Y  d S )Nzabstemplate.ext.j2	absolute!wr,   )r   TemporaryDirectoryospathjoinopenwriter   r   template_filer"   templatefilenamedirnametemplate_pathsr   tdrF   test_outputfr-   r'   r   r   r   test_absolute_template_file   s    
(z(TestExporter.test_absolute_template_filec              
   C   s$  t  }tdtj|d tjdd}tjtj||}ttj| d}t	|d}|
| W d    n1 s0    Y  t }||j_| j|d}tj|jj|ksJ tj|dd	 |jD v sJ W d    n1 s0    Y  W d    n1 s0    Y  d S )
N	os.getcwdZreturn_valuerelativezrelative_template.ext.j2	relative!r>   r,   c                 S   s   g | ]}t j|qS r   r@   rA   abspath.0dr   r   r   
<listcomp>       z<TestExporter.test_relative_template_file.<locals>.<listcomp>)r   TemporaryWorkingDirectoryr   r@   rA   rT   rB   mkdirrH   rC   rD   r   r   rE   r"   rF   rG   rI   r   rK   rF   template_absrL   rM   r-   r'   r   r   r   test_relative_template_file   s    (z(TestExporter.test_relative_template_filec              	   C   s   t  }tj|d}d}t|d}|| W d    n1 sF0    Y  t }||j_	t
t | j|d}W d    n1 s0    Y  |jj|ksJ tj||jv sJ W d    n1 s0    Y  d S Nzabstemplate.tplr=   r>   r,   )r   r?   r@   rA   rB   rC   rD   r   r   rE   pytestwarnsDeprecationWarningr"   rF   rG   rH   rI   rJ   r   r   r   )test_absolute_template_file_compatibility   s    
(*z6TestExporter.test_absolute_template_file_compatibilityc              
   C   sR  t  2}tdtj|d tjdd}tjtj||}ttj| d}t	|d}|
| W d    n1 s0    Y  t }||j_tt | j|d}W d    n1 s0    Y  tj|jj|ksJ tj|dd	 |jD v sJ W d    n1 s$0    Y  W d    n1 sD0    Y  d S )
NrO   rP   rQ   relative_template.tplrR   r>   r,   c                 S   s   g | ]}t j|qS r   rS   rU   r   r   r   rX      rY   zJTestExporter.test_relative_template_file_compatibility.<locals>.<listcomp>)r   rZ   r   r@   rA   rT   rB   r[   rH   rC   rD   r   r   rE   r`   ra   rb   r"   rF   rG   rI   r\   r   r   r   )test_relative_template_file_compatibility   s    (*z6TestExporter.test_relative_template_file_compatibilityc              	   C   s   t  }tj|d}d}t|d}|| W d    n1 sF0    Y  t }||j_	t
t | j|d}W d    n1 s0    Y  |jj|ksJ tj||jv sJ W d    n1 s0    Y  d S r_   )r   r?   r@   rA   rB   rC   rD   r   r   r   r`   ra   rb   r"   rF   rG   rH   rI   rJ   r   r   r   -test_absolute_template_name_tpl_compatibility   s    
(*z:TestExporter.test_absolute_template_name_tpl_compatibilityNc           	      C   s   t  }||j_tt | j|d}W d    n1 s:0    Y  tj	|j
j\}}tj	|\}}|dkstJ ||d ksJ ||jv sJ d S )Nr,   Zcompatibilityz.tpl)r   r   r   r`   ra   rb   r"   r@   rA   splitrF   rG   rI   )	r   rF   Zmimetyper-   r'   template_dirrE   _Z
compat_dirr   r   r   ,absolute_template_name_5x_compatibility_test   s    *z9TestExporter.absolute_template_name_5x_compatibility_testc                 C   s   |  dd d S )NZfull	text/htmlrj   r   r   r   r   1test_absolute_template_name_5x_compatibility_full   s    z>TestExporter.test_absolute_template_name_5x_compatibility_fullc                 C   s   |  d d S )NZdisplay_priorityrl   r   r   r   r   =test_absolute_template_name_5x_compatibility_display_priority   s    zJTestExporter.test_absolute_template_name_5x_compatibility_display_priorityc           	   
   C   sT  t  4}tdtj|d tjtj||}tj|}tj|sZt	| d}t
|d}|| W d    n1 s0    Y  t }||j_tt | j|d}W d    n1 s0    Y  tj|jj|ksJ tj|dd |jD v sJ W d    n1 s&0    Y  W d    n1 sF0    Y  d S )NrO   rP   rR   r>   r,   c                 S   s   g | ]}t j|qS r   rS   rU   r   r   r   rX      rY   z7TestExporter.relative_template_test.<locals>.<listcomp>)r   rZ   r   r@   rA   rT   rB   rH   existsr[   rC   rD   r   r   r   r`   ra   rb   r"   rF   rG   rI   )	r   rF   rK   r]   rH   rL   rM   r-   r'   r   r   r   relative_template_test   s    
(*z#TestExporter.relative_template_testc                 C   s   |  d d S )Nrd   )rp   r   r   r   r   3test_relative_template_name_tpl_compatibility_local   s    z@TestExporter.test_relative_template_name_tpl_compatibility_localc                 C   s   |  tjdd d S )NrQ   rd   rp   r@   rA   rB   r   r   r   r   4test_relative_template_name_tpl_compatibility_nested   s    zATestExporter.test_relative_template_name_tpl_compatibility_nestedc                 C   s   |  tjdd d S )N.rd   rr   r   r   r   r   1test_relative_template_name_tpl_compatibility_dot   s    z>TestExporter.test_relative_template_name_tpl_compatibility_dotc                 C   s   |  tjddd d S )Nrt   rQ   rd   rr   r   r   r   r   8test_relative_template_name_tpl_compatibility_dot_nested   s    zETestExporter.test_relative_template_name_tpl_compatibility_dot_nestedc           	   	   C   s   t  }d}tj||d}tj|}t| d}t|d}|| W d    n1 sb0    Y  t	 }||j
_|g|j
_| j|d}|jj|ksJ |j|ksJ tj|||jv sJ W d    n1 s0    Y  d S )N
mytemplateindex.py.j2r=   r>   r,   )r   r?   r@   rA   rB   rH   r[   rC   rD   r   r   r   Zextra_template_basedirsr"   rF   rG   rI   )	r   rK   rF   rE   rh   rL   rM   r-   r'   r   r   r   test_absolute_template_dir   s    

(
z'TestExporter.test_absolute_template_dirc           
      C   s  t  v}tdtj|d> d}tj|d}tjtj||}tjtj||d}ttj| d}t	|d}|
| W d    n1 s0    Y  t	|d,}|
ttddd	d
 W d    n1 s0    Y  t }||j_| j|d}	tj|	jj|ks(J |	j|ks8J tj|||	jv sRJ W d    n1 sh0    Y  W d    n1 s0    Y  d S )NrO   rP   rw   rx   z	conf.jsonzlocal!r>   T)ztext/x-pythonrk   )Z	mimetypesr,   )r   rZ   r   r@   rA   rT   rB   r[   rH   rC   rD   jsondumpsr   r   r   r"   rF   rG   rI   )
r   rK   rF   rE   r]   Ztemplate_confrL   rM   r-   r'   r   r   r   test_local_template_dir  s*    (
,z$TestExporter.test_local_template_dirc                 C   sV   t j|  d}t|dd}t }|jt	d |
|\}}d|v sRJ d S )Nzlablike.html.j2Zlab)rE   r   	some_textZUNIQUE)r@   rA   rB   Z_get_files_pathr   r   new_notebookcellsappendnew_code_cellfrom_notebook_node)r   rE   r'   nbr(   r)   r   r   r   &test_local_template_file_extending_lab$  s    z3TestExporter.test_local_template_file_extending_labc                 C   sR   t  }|jt d G dd dt}|dd}||\}}d|v sNJ dS )z
        Verify that you can assign a in memory template string by overwriting
        `raw_template` as simple(non-traitlet) attribute
        r}   c                   @   s   e Zd ZeZdS )z9TestExporter.test_raw_template_attr.<locals>.AttrExporterN)r   r   r   raw_templater   r   r   r   AttrExporter5  s   r   rstr   blahN)r   r~   r   r   r   r   r   )r   r   r   Zexporter_attrZoutput_attrri   r   r   r   test_raw_template_attr-  s    
z#TestExporter.test_raw_template_attrc                 C   s|   t  }|jt d G dd dt}| }||\}}d|v sJJ d|_|jdks^J ||\}}d|vsxJ dS )4  
        Test that template_file and raw_template traitlets play nicely together.
        - source assigns template_file default first, then raw_template
        - checks that the raw_template overrules template_file if set
        - checks that once raw_template is set to '', template_file returns
        r}   c                       s   e Zd Z fddZ  ZS )z9TestExporter.test_raw_template_init.<locals>.AttrExporterc                    s   t  j|i | t| _d S N)super__init__r   )r   argskwargs	__class__r   r   r   H  s    zBTestExporter.test_raw_template_init.<locals>.AttrExporter.__init__)r   r   r   r   __classcell__r   r   r   r   r   F  s   r   r    index.rst.j2N)	r   r~   r   r   r   r   r   r   rE   )r   r   r   Zexporter_initZoutput_initri   r   r   r   test_raw_template_init<  s    z#TestExporter.test_raw_template_initc                 C   s   t  }|jt d G dd dt}|dd}||\}}d|v sNJ d|_|jdksbJ ||\}}d|vs|J d	S )
r   r}   c                   @   s,   e Zd Zeddd Zeddd ZdS )zHTestExporter.test_raw_template_dynamic_attr.<locals>.AttrDynamicExporterdefault_template_filec                 S   s   dS Nr   r   r   r   r   r   _template_file_default_  s    z_TestExporter.test_raw_template_dynamic_attr.<locals>.AttrDynamicExporter._template_file_defaultr   c                 S   s   t S r   r   r   r   r   r   _raw_template_defaultc  s    z^TestExporter.test_raw_template_dynamic_attr.<locals>.AttrDynamicExporter._raw_template_defaultN)r   r   r   r   r   r   r   r   r   r   AttrDynamicExporter^  s   
r   r   r   r   r   r   N	r   r~   r   r   r   r   r   r   rE   r   r   r   Zexporter_attr_dynamicZoutput_attr_dynamicri   r   r   r   test_raw_template_dynamic_attrT  s    	
z+TestExporter.test_raw_template_dynamic_attrc                 C   s   t  }|jt d G dd dt}|dd}||\}}d|v sNJ d|_|jdksbJ ||\}}d|vs|J d	S )
a4  
        Test that template_file and raw_template traitlets play nicely together.
        - source assigns raw_template default first, then template_file
        - checks that the raw_template overrules template_file if set
        - checks that once raw_template is set to '', template_file returns
        r}   c                   @   s,   e Zd Zeddd Zeddd ZdS )zQTestExporter.test_raw_template_dynamic_attr_reversed.<locals>.AttrDynamicExporterr   c                 S   s   t S r   r   r   r   r   r   r   z  s    zgTestExporter.test_raw_template_dynamic_attr_reversed.<locals>.AttrDynamicExporter._raw_template_defaultr   c                 S   s   dS r   r   r   r   r   r   r   ~  s    zhTestExporter.test_raw_template_dynamic_attr_reversed.<locals>.AttrDynamicExporter._template_file_defaultN)r   r   r   r   r   r   r   r   r   r   r   y  s   
r   r   r   r   r   r   Nr   r   r   r   r   'test_raw_template_dynamic_attr_reversedo  s    	
z4TestExporter.test_raw_template_dynamic_attr_reversedc                 C   s@   t  }|jt d tdtd|\}}d|v s<J dS )zX
        Test `raw_template` as a keyword argument in the exporter constructor.
        r}   r   )r   r   r   Nr   r~   r   r   r   r   r   r   )r   r   Zoutput_constructorri   r   r   r   test_raw_template_constructor  s    z*TestExporter.test_raw_template_constructorc                 C   sH   t  }|jt d tdd}t|_||\}}d|v sDJ dS )zU
        Test `raw_template` assigned after the fact on non-custom Exporter.
        r}   r   r   r   Nr   )r   r   Zexporter_assignZoutput_assignri   r   r   r   test_raw_template_assignment  s    
z)TestExporter.test_raw_template_assignmentc                 C   sp   t  }|jt d tdd}t|_||\}}d|v sDJ tdd|_||\}}d|v slJ dS )zW
        Test `raw_template` reassigned after the fact on non-custom Exporter.
        r}   r   r   r   bazN)	r   r~   r   r   r   r   r   r   replace)r   r   Zexporter_reassignZoutput_reassignri   r   r   r   test_raw_template_reassignment  s    
z+TestExporter.test_raw_template_reassignmentc                 C   sr   t  }|jt d t }t|_||\}}d|v s@J d|_|jdksTJ ||\}}d|vsnJ dS )
        Test `raw_template` does not overwrite template_file if deassigned after
        being assigned to a non-custom Exporter.
        r}   r   r   r   N)	r   r~   r   r   r   r   r   r   rE   )r   r   Zexporter_deassignZoutput_deassignri   r   r   r   test_raw_template_deassignment  s    z+TestExporter.test_raw_template_deassignmentc                 C   s   t  }|jt d t }t|_||\}}d|v s@J tdd|_||\}}d|v shJ d|_|j	dks|J ||\}}d|vsJ dS )r   r}   r   r   r   r   N)
r   r~   r   r   r   r   r   r   r   rE   )r   r   Zexporter_dereassignZoutput_dereassignri   r   r   r    test_raw_template_dereassignment  s    z-TestExporter.test_raw_template_dereassignmentc                 C   sj   d}t |d}||jjdgdvs&J t }tt ||\}}W d    n1 s\0    Y  d S )Nzdoes_not_exist.tpl)rE   Ztpl)
extensions)	r   ZenvironmentZlist_templatesr   r~   r`   Zraisesr   r   )r   rF   r'   r   outr)   r   r   r   test_fail_to_find_template_file  s    
z,TestExporter.test_fail_to_find_template_filec              	   C   s   dddddddddi}t |}t|dd}d|_||  \}}|d d	 rTJ |d d
 rdJ dddddddddi}t |}t|dd}d|_||  \}	}
|
d d rJ ||	ksJ d S )Nr   TFZexclude_outputZexclude_inputZexclude_input_promptZexclude_output_promptZexclude_markdownZexclude_code_cellZexclude_output_stdinmarkdownr-   r   zindex.md.j2global_content_filterZinclude_inputZinclude_outputZinclude_coder   r   rE   r#   r$   )r   Zno_ioZc_no_ioZexporter_no_ioZnb_no_ioZresources_no_ioZno_codeZ	c_no_codeZexporter_no_codeZ
nb_no_codeZresources_no_coder   r   r   test_exclude_code_cell  s@    z#TestExporter.test_exclude_code_cellc              	   C   s\   dddddddddi}t |}t|d}||  \}}|d d rLJ d|vsXJ d S )	Nr   FTr   r,   r   Zinclude_input_promptz# In[r   r   r#   r$   )r   Zno_input_promptZc_no_input_promptZexporter_no_input_promptZnb_no_input_promptZresources_no_input_promptr   r   r   test_exclude_input_prompt  s    
z&TestExporter.test_exclude_input_promptc              	   C   sd   dddddddddi}t |}t|dd}d|_||  \}}|d d	 rTJ d
|vs`J d S )Nr   FTr   r   r   rx   r   Zinclude_markdownz!First import NumPy and Matplotlibr   )r   Zno_mdZc_no_mdZexporter_no_mdZnb_no_mdZresources_no_mdr   r   r   test_exclude_markdown  s     z"TestExporter.test_exclude_markdownc              	   C   s\   dddddddddi}t |}t|d}||  \}}|d d rLJ d|vsXJ d S )	Nr   FTr   r,   r   Zinclude_output_promptzOut[1]r   r   r#   r$   )r   Zno_output_promptZc_no_output_promptexporter_no_output_promptZnb_no_output_promptZresources_no_output_promptr   r   r   test_exclude_output_prompt/  s    
z'TestExporter.test_exclude_output_promptc              	   C   s^   dddddddddi}t |}t|d}|| d\}}|d d rNJ d	|vsZJ d S 
Nr   FTr   r,   znotebook3.ipynbr   Zinclude_output_stdinztest input: input valuer   )r   Zno_output_stdinZc_no_output_stdinr   Znb_no_output_stdinZresources_no_output_stdinr   r   r   test_exclude_output_stdinB  s"    
z&TestExporter.test_exclude_output_stdinc              	   C   s^   dddddddddi}t |}t|d}|| d\}}|d d sNJ d	|v sZJ d S r   r   )r   Zoutput_stdinZc_output_stdinZexporter_output_stdinZnb_output_stdinZresources_output_stdinr   r   r   test_include_output_stdinW  s"    
z&TestExporter.test_include_output_stdinc                 C   s^   t ddgdgdgdi}t|d}||  \}}d|vsBJ d|vsNJ d	|vsZJ d S )
NZTagRemovePreprocessorZremove_cellZremove_outputZremove_input)Zremove_cell_tagsZremove_all_outputs_tagsZremove_input_tagsr,   zhist(evs.real)z%cell is just markdown testing whetherz(100,)r   )r   Zconfr'   r   r)   r   r   r   test_remove_elements_with_tagsl  s    
z+TestExporter.test_remove_elements_with_tagsc                 C   s   t |d}|S )Nr,   )r   )r   r-   r'   r   r   r   r"   }  s    
zTestExporter._make_exporter)N)N).r   r   r   r   r!   r*   r1   r7   r8   r9   r:   r<   rN   r^   rc   re   rf   rj   rm   rn   rp   rq   rs   ru   rv   ry   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r   r   r   r   r    .   sV   	
	
	(r    )"r   r@   rz   Z	traitletsr   Ztraitlets.configr   Zjinja2r   r   Znbformatr   Zunittest.mockr   Zconcurrent.futuresr   baser
   r4   r   Ztemplateexporterr   r   r   Zhtmlr   r   r   Ztestpathr   r`   r   r   r    r   r   r   r   <module>   s&   