B
    [x                 @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZ ddlmZmZ ddlmZ ddlmZmZ G dd dejZdd
dZdS )zBase test class for nbconvert    N)PopenPIPE)v4write)TemporaryWorkingDirectory)string_typesbytes_to_strc               @   sP   e Zd ZdZdddZdd Zdd	d
Zdd ZdddZdd Z	dddZ
dS )	TestsBasezPBase tests class.  Contains useful fuzzy comparison and nbconvert
    functions.TFc
       
      C   s   |	s|  }|  }|r0|dd}|dd}|rL|dd}|dd}|rh|dd}|dd}|r|dd}|dd}|r| |dd}| |dd}|s| }| }| || dS )a  
        Performs a fuzzy comparison of two strings.  A fuzzy comparison is a
        comparison that ignores insignificant differences in the two comparands.
        The significance of certain differences can be specified via the keyword
        parameters of this method.
        
  	z  N)stripreplacerecursive_replacelowerZassertEqual)
selfabZnewlines_are_spacesZtabs_are_spacesZfuzzy_spacingZignore_spacesZignore_newlinesZcase_sensitiveZleave_padding r   3lib/python3.7/site-packages/nbconvert/tests/base.pyfuzzy_compare   s,    
zTestsBase.fuzzy_comparec             C   s   x||kr| ||}qW |S )a  
        Performs a recursive replacement operation.  Replaces all instances
        of a search string in a text string with a replacement string until
        the search string no longer exists.  Recursion is needed because the
        replacement string may generate additional search strings.

        For example:
           Replace "ii" with "i" in the string "Hiiii" yields "Hii"
           Another replacement cds "Hi" (the desired output)

        Parameters
        ----------
        text : string
            Text to replace in.
        search : string
            String to search for within "text"
        replacement : string
            String to replace "search" with
        )r   )r   textsearchZreplacementr   r   r   r   C   s    
zTestsBase.recursive_replaceNc             C   s"   t  }|d k	r| j||jd |S )N)dest)r   copy_files_toname)r   copy_filenamesZtemp_dirr   r   r   create_temp_cwd[   s    zTestsBase.create_temp_cwdc          	   C   s4   t  }tj|ddd}t||d W d Q R X d S )Nwzutf-8)encoding   )r   Znew_notebookioopenr   )r   pathZnbfr   r   r   create_empty_notebooke   s    zTestsBase.create_empty_notebook.c             C   sx   t j|st | |  }xT|D ]L}tt j||}|sDtx*|D ]"}t	|t j|t j
| qJW q$W dS )z.Copy test files into the destination directoryN)osr$   isdirmakedirs_get_files_pathglobjoinAssertionErrorshutilZcopyfilebasename)r   r   r   Z
files_pathpatternfilesmatchr   r   r   r   j   s    


zTestsBase.copy_files_toc             C   s>   | j ddd }|d tjtj}tjj|f| S )Nr'      r2   )	
__module__splitappendr(   r$   dirname	nbconvert__file__r-   )r   namesr$   r   r   r   r+   u   s    
zTestsBase._get_files_pathc             C   sv   t |trt|}tjddg| }t|tttd}|j|d\}}|j	dks^|s^t
t||dd|ddfS )a  
        Run nbconvert as a shell command, listening for both Errors and
        non-zero return codes. Returns the tuple (stdout, stderr) of
        output produced during the nbconvert run.

        Parameters
        ----------
        parameters : str, list(str)
            List of parameters to pass to IPython.
        ignore_return_code : optional bool (default False)
            Throw an OSError if the return code
        z-mr:   )stdoutstderrstdin)inputr   utf8r   )
isinstancer   shlexr7   sys
executabler   r   Zcommunicate
returncodeOSErrorr   decode)r   Z
parametersZignore_return_coder?   cmdpr=   r>   r   r   r   r:      s    

zTestsBase.nbconvert)TTTFFFF)N)r'   )FN)__name__r6   __qualname____doc__r   r   r   r&   r   r+   r:   r   r   r   r   r	      s     
&


r	   P   c             C   s   xRt dt| |D ]>}| |||  }||||  }||kstd|||f qW t| t|krtdt| t|| t|d f n6t| t|k rtdt| t|| t|d f dS )zassert that large strings are equal

    Zooms in on first chunk that differs,
    to give better info than vanilla assertEqual for large text blobs.
    r   z[offset: %i]
%r != 
%rz.Length doesn't match (%i > %i). Extra text:
%rNz.Length doesn't match (%i < %i). Extra text:
%r)rangelenr.   )r   r   Z
chunk_sizeiZchunk_aZchunk_br   r   r   assert_big_text_equal   s    $rR   )rN   )rM   r"   r(   r,   rC   r/   rD   Zunittestr:   
subprocessr   r   Znbformatr   r   Ztestpath.tempdirr   Zipython_genutils.py3compatr   r   ZTestCaser	   rR   r   r   r   r   <module>   s    