B
    Z[T                 @   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 ddl	m
Z
 dd	lmZ dd
lmZ ddlmZ ddlZG dd deZG dd deZdS )zTest NbConvertApp    N   )	TestsBase   )PostProcessorBase)onlyif_cmds_exist)nbconvertapp)Exporter)check_help_all_output)tempdirc               @   s   e Zd Zdd ZdS )	DummyPostc             C   s   t d|  d S )NzDummy:%s)print)selffilename r   @lib/python3.7/site-packages/nbconvert/tests/test_nbconvertapp.pypostprocess   s    zDummyPost.postprocessN)__name__
__module____qualname__r   r   r   r   r   r      s   r   c               @   s  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edddd Zedddd Zdd Zeddd Zeddd Zedd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zeddd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%edddFdG Z&eddHdI Z'eddJdK Z(eddLdM Z)dNS )OTestNbConvertAppz Collection of NbConvertApp testsc          	   C   s6   |   $ | jddd\}}| d| W dQ R X dS )z-Will help show if no notebooks are specified?z--log-level 0T)ignore_return_codez
--help-allN)create_temp_cwd	nbconvertassertIn)r   outerrr   r   r   test_notebook_help!   s    
z#TestNbConvertApp.test_notebook_helpc             C   s   t d dS )z"ipython nbconvert --help-all worksr   N)r	   )r   r   r   r   test_help_output'   s    z!TestNbConvertApp.test_help_outputc          	   C   sF   |  dg0 | d tjds(ttjds8tW dQ R X dS )z=
        Do search patterns work for notebook names?
        znotebook*.ipynbz!--to python *.ipynb --log-level 0znotebook1.pyznotebook2.pyN)r   r   ospathisfileAssertionError)r   r   r   r   	test_glob+   s    
zTestNbConvertApp.test_globc          	   C   sr   |   ` | dgd | dtjdd  tjtjddsJttjtjddsdtW dQ R X dS )	zJ
        Do search patterns work for subdirectory notebook names?
        znotebook*.ipynbzsubdir/z--to python --log-level 0 subdirz*.ipynbznotebook1.pyznotebook2.pyN)r   copy_files_tor   r   r   joinr    r!   )r   r   r   r   test_glob_subdir4   s    
z!TestNbConvertApp.test_glob_subdirc          	   C   s^   |   L | dgd | dtjdd  tjds@ttjdsPtW dQ R X dS )	z'build_directory affects export locationznotebook*.ipynbzsubdir/z)--to python --log-level 0 --output-dir . r#   z*.ipynbznotebook1.pyznotebook2.pyN)r   r$   r   r   r   r%   r    r!   )r   r   r   r   test_build_dir?   s    
zTestNbConvertApp.test_build_dirc          	   C   sr   |   ` | dgd | dtjdd  tjtjddsJttjtjddsdtW dQ R X dS )z|
        Test that nbconvert can convert file using a full qualified name for a
        package, import and use it.
        znotebook*.ipynbr#   z=--to nbconvert.tests.fake_exporters.MyExporter --log-level 0 z*.ipynbznotebook1.test_extznotebook2.test_extN)r   r$   r   r   r   r%   r    r!   )r   r   r   r    test_convert_full_qualified_nameH   s    
z1TestNbConvertApp.test_convert_full_qualified_namec          	   C   sF   |  dg0 | d tjdr(ttjds8tW dQ R X dS )z2
        Do explicit notebook names work?
        znotebook*.ipynbz#--log-level 0 --to python notebook2znotebook1.pyznotebook2.pyN)r   r   r   r   r    r!   )r   r   r   r   test_explicitT   s    
zTestNbConvertApp.test_explicitc             C   s   |  dg t ~}tj|d}d}t|d}|| W dQ R X | d|  tj	dsht
td}| }W dQ R X ||kst
W dQ R X W dQ R X dS )z"--template '/path/to/template.tpl'znotebook*.ipynbzmytemplate.tplzsuccess!wNz%--log-level 0 notebook2 --template %sznotebook2.html)r   r
   TemporaryDirectoryr   r   r%   openwriter   r    r!   read)r   Ztdtemplatetest_outputftextr   r   r   test_absolute_template_file]   s    
z,TestNbConvertApp.test_absolute_template_filec          
   C   s   |  dg td tjdd}d}t|d}|| W dQ R X | d|  tjdsht	td}|
 }W dQ R X ||kst	W dQ R X dS )	z#Test --template 'relative/path.tpl'znotebook*.ipynbZrelativezpath.tplzsuccess!r*   Nz%--log-level 0 notebook2 --template %sznotebook2.html)r   r   mkdirr   r%   r,   r-   r   r    r!   r.   )r   r/   r0   r1   r2   r   r   r   test_relative_template_filej   s    

z,TestNbConvertApp.test_relative_template_fileZpandocZxelatexc          	   C   sB   |  dg, tdd | d tjds4tW dQ R X dS )zS
        Generate PDFs with graphics if notebooks have spaces in the name?
        znotebook2.ipynbznotebook with spaces.ipynbzd--log-level 0 --to pdf "notebook with spaces" --PDFExporter.latex_count=1 --PDFExporter.verbose=Trueznotebook with spaces.pdfN)r   r   renamer   r   r    r!   )r   r   r   r   test_filename_spacesx   s    
z%TestNbConvertApp.test_filename_spacesc          	   C   s6   |  dg  | d tjds(tW dQ R X dS )zT
        Check to see if pdfs compile, even if strikethroughs are included.
        znotebook2.ipynbzY--log-level 0 --to pdf "notebook2" --PDFExporter.latex_count=1 --PDFExporter.verbose=Trueznotebook2.pdfN)r   r   r   r   r    r!   )r   r   r   r   test_pdf   s    
zTestNbConvertApp.test_pdfc          	   C   s6   |  dg  | d\}}| d| W dQ R X dS )zDo post processors work?znotebook1.ipynbzV--log-level 0 --to python notebook1 --post nbconvert.tests.test_nbconvertapp.DummyPostzDummy:notebook1.pyN)r   r   r   )r   r   r   r   r   r   test_post_processor   s    z$TestNbConvertApp.test_post_processorc          
   C   s   |  dgr | d tjds(ttd}| }W dQ R X | d tjds^ttd}| }W dQ R X W dQ R X | |	d|	d | |	d|	d dS )	zCheck for extra CR charactersznotebook2.ipynbz"--log-level 0 --to latex notebook2znotebook2.texNz!--log-level 0 --to html notebook2znotebook2.htmlz
)
r   r   r   r   r    r!   r,   r.   ZassertEqualcount)r   r1   ZtexZhtmlr   r   r   test_spurious_cr   s    



z!TestNbConvertApp.test_spurious_crc          
   C   sZ   |  dgD | d tjds(ttd}d| ksBtW dQ R X W dQ R X dS )z)Is embedded png data well formed in HTML?znotebook2.ipynbz7--log-level 0 --to HTML notebook2.ipynb --template fullznotebook2.htmlzdata:image/png;base64,b'N)r   r   r   r   r    r!   r,   r.   )r   r1   r   r   r   test_png_base64_html_ok   s
    

z(TestNbConvertApp.test_png_base64_html_okc          
   C   sZ   |  dgD | d tjds(ttd}d| ksBtW dQ R X W dQ R X dS )z+
        Do export templates work?
        znotebook2.ipynbz)--log-level 0 --to slides notebook2.ipynbznotebook2.slides.htmlz/reveal.cssN)r   r   r   r   r    r!   r,   r.   )r   r1   r   r   r   test_template   s
    

zTestNbConvertApp.test_templatec          	   C   sP   |  dg: | d tjds(t| d tjdsBtW dQ R X dS )ztest --output=outputfile[.ext]znotebook1.ipynbz8--log-level 0 --to python notebook1.ipynb --output nb.pyznb.pyz6--log-level 0 --to python notebook1.ipynb --output nb2znb2.pyN)r   r   r   r   existsr!   )r   r   r   r   test_output_ext   s
    

z TestNbConvertApp.test_output_extc          	   C   sF   |  dg0 | d tjds(ttjds8tW dQ R X dS )z[
        Can a search pattern be used along with matching explicit notebook names?
        znotebook*.ipynbzA--log-level 0 --to python *.ipynb notebook1.ipynb notebook2.ipynbznotebook1.pyznotebook2.pyN)r   r   r   r   r    r!   )r   r   r   r   test_glob_explicit   s    
z#TestNbConvertApp.test_glob_explicitc          	   C   sF   |  dg0 | d tjds(ttjds8tW dQ R X dS )zY
        Can explicit notebook names be used and then a matching search pattern?
        znotebook*.ipynbzA--log-level 0 --to=python notebook1.ipynb notebook2.ipynb *.ipynbznotebook1.pyznotebook2.pyN)r   r   r   r   r    r!   )r   r   r   r   test_explicit_glob   s    
z#TestNbConvertApp.test_explicit_globc          	   C   sH   |  ddg0 | d tjds*ttjdr:tW dQ R X dS )z/
        Does the default config work?
        znotebook*.ipynbzjupyter_nbconvert_config.pyz--log-level 0znotebook1.pyznotebook2.pyN)r   r   r   r   r    r!   )r   r   r   r   test_default_config   s    
z$TestNbConvertApp.test_default_configc          	   C   sJ   |  dddg0 | d tjdr,ttjds<tW dQ R X dS )z7
        Can the default config be overridden?
        znotebook*.ipynbzjupyter_nbconvert_config.pyzoverride.pyz$--log-level 0 --config="override.py"znotebook1.pyznotebook2.pyN)r   r   r   r   r    r!   )r   r   r   r   test_override_config   s    

z%TestNbConvertApp.test_override_configc          	   C   s<   |   * | d | d tjds.tW dQ R X dS )z5
        Can notebook names include accents?
        u   nb1_análisis.ipynbz--log-level 0 --to Python nb1_*u   nb1_análisis.pyN)r   create_empty_notebookr   r   r   r    r!   )r   r   r   r   test_accents_in_filename   s    


z)TestNbConvertApp.test_accents_in_filenamec          	   C   s<   |   * | d | d tjds.tW dQ R X dS )zJ
        Generate PDFs if notebooks have an accent in their name?
        u   nb1_análisis.ipynbzU--log-level 0 --to pdf "nb1_*" --PDFExporter.latex_count=1 --PDFExporter.verbose=Trueu   nb1_análisis.pdfN)r   rE   r   r   r   r    r!   )r   r   r   r   test_filename_accent_pdf   s    


z)TestNbConvertApp.test_filename_accent_pdfc          	   C   s@   |  dg* | d | d tjds2tW dQ R X dS )zF
        Verify that an extension in the cwd can be imported.
        zhello.pyzempty.ipynbz?empty --to html --NbConvertApp.writer_class='hello.HelloWriter'z	hello.txtN)r   rE   r   r   r   r    r!   )r   r   r   r   test_cwd_plugin  s    

z TestNbConvertApp.test_cwd_pluginc          	   C   s<   |   * | d | d tjds.tW dQ R X dS )z:
        Verify that the output suffix is applied
        zempty.ipynbzempty.ipynb --to notebookzempty.nbconvert.ipynbN)r   rE   r   r   r   r    r!   )r   r   r   r   test_output_suffix  s    


z#TestNbConvertApp.test_output_suffixc          	   C   sF   |   4 | d td | d tjds8tW dQ R X dS )z>
        Verify that the output suffix is not applied
        zempty.ipynboutputz>empty.ipynb --to notebook --FilesWriter.build_directory=outputzoutput/empty.ipynbN)r   rE   r   r4   r   r   r    r!   )r   r   r   r   test_different_build_dir  s    


z)TestNbConvertApp.test_different_build_dirc          	   C   s\   |   J | d | d tjds.ttjdr>ttjdrNtW dQ R X dS )z@
        Verify that the notebook is converted in place
        zempty.ipynbzempty.ipynb --inplacezempty.nbconvert.ipynbz
empty.htmlN)r   rE   r   r   r   r    r!   )r   r   r   r   test_inplace%  s    


zTestNbConvertApp.test_inplacec          
   C   s   |  dg | d tjds(ttdd&}| }d|ksHtd|ksTtW dQ R X | d tjdsxttdd&}| }d|kstd|kstW dQ R X W dQ R X dS )	zM
        Verify that the html has no prompts when given --no-prompt.
        znotebook1.ipynbz3notebook1.ipynb --log-level 0 --no-prompt --to htmlznotebook1.htmlrz	In&nbsp;[zOut[Nz'notebook1.ipynb --log-level 0 --to html)r   r   r   r   r    r!   r,   r.   )r   r1   r2   text2r   r   r   test_no_prompt0  s    

zTestNbConvertApp.test_no_promptc          
   C   s   |  dg | d tjds(ttdd2}| }d|ksHtd|ksTtd|ks`tW dQ R X | d	 tjdsttdd2}| }d|kstd|kstd|kstW dQ R X W dQ R X dS )
zJ
        Verify that the html has no input when given --no-input.
        znotebook1.ipynbz2notebook1.ipynb --log-level 0 --no-input --to htmlznotebook1.htmlrM   z	In&nbsp;[zOut[a  <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>Nz'notebook1.ipynb --log-level 0 --to html)r   r   r   r   r    r!   r,   r.   )r   r1   r2   rN   r   r   r   test_no_inputB  s"    
	
	zTestNbConvertApp.test_no_inputc          
   C   s   |  dg | d\}}| d\}}| d\}}d|ksDtd|ksPtd|ks\td|kshtd|ksttd|ksttt | d W dQ R X W dQ R X dS )	z
        Verify that conversion is aborted with '--execute' if an error is
        encountered, but that conversion continues if '--allow-errors' is
        used in addition.
        znotebook3*.ipynbz'--to markdown --stdout notebook3*.ipynbz6--to markdown --allow-errors --stdout notebook3*.ipynbz@--execute --allow-errors --to markdown --stdout notebook3*.ipynbZ23Z42z1--execute --to markdown --stdout notebook3*.ipynbN)r   r   r!   pytestZraisesOSError)r   output1_output2Zoutput3r   r   r   test_allow_errorsh  s    z"TestNbConvertApp.test_allow_errorsc          	   C   sR   |  dg< | jddd\}}d|ks,td|ks8td|ksDtW dQ R X dS )	zg
        Verify that the stderr output contains the traceback of the cell execution exception.
        znotebook3_with_errors.ipynbz<--execute --to markdown --stdout notebook3_with_errors.ipynbT)r   z#print("Some text before the error")z4raise RuntimeError("This is a deliberate exception")z,RuntimeError: This is a deliberate exceptionN)r   r   r!   )r   rT   Zerror_outputr   r   r   test_errors_print_traceback  s    z,TestNbConvertApp.test_errors_print_tracebackc          	   C   s   |  dg, | d\}}d|ks(td|ks4tW dQ R X |  dg, | d\}}d|ksftd|ksrtW dQ R X dS )	z
        Verify that input cells use fenced code blocks with the language
        name in nb.metadata.kernelspec.language, if that exists
        znotebook1*.ipynbz&--to markdown --stdout notebook1.ipynbz	```pythonz```Nznotebook_jl*.ipynbz(--to markdown --stdout notebook_jl.ipynbz```julia)r   r   r!   )r   rS   rT   rU   r   r   r    test_fenced_code_blocks_markdown  s    z1TestNbConvertApp.test_fenced_code_blocks_markdownc          
   C   sh   |  dgR td$}|  }| jd|d\}}W dQ R X d|ksNtd|ksZtW dQ R X dS )zH
        Verify that conversion can be done via stdin to stdout
        znotebook1.ipynbz--to markdown --stdin --stdout)stdinNz	```pythonz```)r   ior,   r.   encoder   r!   )r   r1   notebookrS   rT   r   r   r   !test_convert_from_stdin_to_stdout  s    z2TestNbConvertApp.test_convert_from_stdin_to_stdoutc          
   C   s   |  dg| td }|  }| jd|d W dQ R X tjdsNt	td&}| }d|ksnt	d|kszt	W dQ R X W dQ R X dS )z?
        Verify that conversion can be done via stdin.
        znotebook1.ipynbz--to markdown --stdin)rY   Nznotebook.mdz	```pythonz```)
r   rZ   r,   r.   r[   r   r   r   r    r!   )r   r1   r\   rS   r   r   r   test_convert_from_stdin  s    z(TestNbConvertApp.test_convert_from_stdinc          	   C   s8   |  ddg  | d tjds*tW dQ R X dS )zG
        Generate PDFs with an image linked in a markdown cell
        zlatex-linked-image.ipynbztestimage.pngz!--to pdf latex-linked-image.ipynbzlatex-linked-image.pdfN)r   r   r   r   r    r!   )r   r   r   r   test_linked_images  s    
z#TestNbConvertApp.test_linked_imagesc          	   C   s8   |  ddg  | d tjds*tW dQ R X dS )zf
        Verify that latex conversion succeeds
        with a notebook with an embedded .jpeg
        znotebook4_jpeg.ipynbzcontainerized_deployments.jpegz--to latex notebook4_jpeg.ipynbznotebook4_jpeg.texN)r   r   r   r   r    r!   )r   r   r   r   test_embedded_jpeg  s    

z#TestNbConvertApp.test_embedded_jpegc          
   C   s`   |  dgJ | d tjds(ttd}| }d|ksHtW dQ R X W dQ R X dS )zs
        Check to see if markdown conversion embeds PNGs,
        even if an (unsupported) PDF is present.
        zmarkdown_display_priority.ipynbz=--log-level 0 --to markdown "markdown_display_priority.ipynb"zmarkdown_display_priority.mdzAmarkdown_display_priority_files/markdown_display_priority_0_1.pngN)	r   r   r   r   r    r!   rZ   r,   r.   )r   r1   Zmarkdown_outputr   r   r   test_markdown_display_priority  s    
z/TestNbConvertApp.test_markdown_display_priorityc          	   C   s   dd }|  ddgB t }tj|jd}| d| ||sLt	|
  W dQ R X |  ddg | d |d	st	W dQ R X |  ddg | d
 |dst	W dQ R X dS )zF
        Check if figure files are copied to configured path.
        c             S   s   t t| dkS )Nr   )lenr   listdir)r   r   r   r   
fig_exists  s    zFTestNbConvertApp.test_write_figures_to_custom_path.<locals>.fig_existsznotebook4_jpeg.ipynbzcontainerized_deployments.jpegfileszN--log-level 0 notebook4_jpeg.ipynb --to rst --NbConvertApp.output_files_dir={}NzR--log-level 0 notebook4_jpeg.ipynb --to rst --NbConvertApp.output_files_dir=outputrJ   z+--log-level 0 notebook4_jpeg.ipynb --to rstZnotebook4_jpeg_files)r   r
   r+   r   r   r%   namer   formatr!   Zcleanup)r   rd   Z
output_dirr   r   r   r   !test_write_figures_to_custom_path  s(    


z2TestNbConvertApp.test_write_figures_to_custom_pathN)*r   r   r   __doc__r   r   r"   r&   r'   r(   r)   r3   r5   r   r7   r8   r9   r<   r=   r>   r@   rA   rB   rC   rD   rF   rG   rH   rI   rK   rL   rO   rP   rV   rW   rX   r]   r^   r_   r`   ra   rh   r   r   r   r   r      sL   			


				&	r   )ri   r   rZ   baser   Zpostprocessorsr   Ztests.utilsr   r   r   Znbconvert.exportersr   Ztraitlets.tests.utilsr	   Ztestpathr
   rQ   r   r   r   r   r   r   <module>   s   