B
    '(bD                 @   s  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m	Z	m
Z
mZmZmZmZmZ d dlmZ d dlm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mZmZm Z  d dl!m"Z" d dl#Zej$j%Z%d	Z&ee' dd
ddZ(ee' ee' e'ddddZ)e'ee' ee' ddddZ*e'ee' ee' ddddZ+eee' ddddZ,e'e'ddddZ-ee' ee' dddZ.ej/ed ddd Z0ee' ee' e1d!d"d#Z2ee' ee' e1d!d$d%Z3e'e
e1e1f d&d'd(Z4e'e'e
e1e1f d)d*d+Z5ee' ee' d,d-d.Z6dSeg ef e7dd0d1d2Z8e9e'd3d4d5Z:dTe9e9e'dd7d8d9Z;e<e'd:d;d<Z=e<e9dd=d>d?Z>e'ee1ed@dAdBZ?e@ee' dCdDdEZAe'e'ddFdGdHZBeeee f  ddIdJdKZCdUee1e'ddMdNdOZDee' e'ee' dPdQdRZEdS )V    N)	ListIterableDictTupleCallableAnyIteratorUnionPattern)defaults)TestCase)process_options)Options)DataDrivenTestCasefix_cobertura_filename
UpdateFile
DeleteFile)test_temp_dir   )argsreturnc             C   sN   d}t | ddg \}}}|dkrJtj| tj| tjddd d S )NTz--show-tracebackz--no-silence-site-packagesr   zSample check failedF)msgZpytrace)apirunsysstdoutwritestderrpytestZfail)r   __tracebackhide__ZoutvalZerrvalstatus r!   @/home/dcms/DCMS/lib/python3.7/site-packages/mypy/test/helpers.pyrun_mypy"   s    r#   )expectedactualr   r   c             C   s  d}t |}dd |D }dd | D } || kr~t| |}t| |}tjd |dkrftjd d}d	}xt|t| | D ]}|t|ks| | || kr|dk r|}tjd
| | dd n:| | }	tjd
|	d|   t|	|krtjd tjd qW |dkr*tjd tjd |dkrLtjd xt|t|| D ]}
|
t| ks| |
 ||
 krtjd
||
 dd n:||
 }tjd
|d|   t||krtjd tjd q`W |stjd |dkrtjd tjd d|  kr<t|k rvn n6t| | tksdt|| tkrvt	| | ||  t
|dS )zAssert that two string arrays are equal.

    We consider "can't" and "cannot" equivalent, by replacing the
    former with the latter before comparing.

    Display any differences in a human-readable form.
    Tc             S   s   g | ]}| d dqS )zcan'tcannot)replace).0liner!   r!   r"   
<listcomp>:   s    z.assert_string_arrays_equal.<locals>.<listcomp>c             S   s   g | ]}| d dqS )zcan'tr&   )r'   )r(   r)   r!   r!   r"   r*   ;   s    z
Expected:
r   z  ...
K   z  z<45z (diff)Nz...
zActual:
z
  (empty)
)clean_upnum_skipped_prefix_linesnum_skipped_suffix_linesr   r   r   rangelenMIN_LINE_LENGTH_FOR_ALIGNMENTshow_align_messageAssertionError)r$   r%   r   r   Znum_skip_startZnum_skip_endZ
first_diffwidthiejar!   r!   r"   assert_string_arrays_equal.   sZ    	




 
 r;   )namer$   r%   r   c          	   C   sB   t |}t t|dh}t||dd|d||  d S )N__main__zEActual modules ({}) do not match expected modules ({}) for "[{} ...]"z, )sortedset
differencer;   formatjoin)r<   r$   r%   Zexpected_normalizedZactual_normalizedr!   r!   r"   assert_module_equivalence{   s    rC   c          	   C   s&   t ||dd|d||  dS )z6Compare actual and expected targets (order sensitive).zEActual targets ({}) do not match expected targets ({}) for "[{} ...]"z, N)r;   rA   rB   )r<   r$   r%   r!   r!   r"   assert_target_equivalence   s    rD   )testcaseoutputr   c          
   C   s  | j d k	stdtj| j | j}t|dd}|  }W d Q R X d|| j	| j
 }i }xt| j|D ]r\}}d}	||	}
|
dkr|d |
 |d |
 kr||
t|	 d  ||
t|	 d   }}||g | qpW xl|D ]d}||t|| kr||}ddlm} |d gt|t|| |d	d   }d
|}qW |g|| j	| j
< d|}t|ddd}t||d W d Q R X d S )Nztest was not properly set uputf8)encodingr-   zerror:r+   r   )chain    w)file)Zold_cwdr5   ospathrB   rM   openread
splitlinesr)   	last_lineziprF   findr2   
setdefaultappendcountsplit	itertoolsrI   listfrom_iterableprint)rE   rF   Ztestcase_pathfZ
data_linestestmappingoldnewPREFIXindZbetweensrI   Zinterleaveddatar!   r!   r"   update_testcase_output   s.    
 *

"
rf   )s1s2r   c             C   sT  t | dk rdS d}tjd d}x8| dd |dd kr\| dd } |dd }d}q&W |rrd	|  } d	| }tt | t |}d
}||krd	}tjd| d|  | d tjd|d|  | d tjd xbtt|tt | t |D ]B}| ||d  |||d  kr4tjd P qtjd qW tjd dS )aQ  Align s1 and s2 so that the their first difference is highlighted.

    For example, if s1 is 'foobar' and s2 is 'fobar', display the
    following lines:

      E: foobar
      A: fobar
           ^

    If s1 and s2 are long, only display a fragment of the strings around the
    first difference. If s1 is very short, do nothing.
       NH   z$Alignment of first line difference:
F   
   Tz...rK   z  E: r-   z  A: z     rJ   ^ )r2   r   r   r   maxr1   min)rg   rh   Zmaxwtruncmax_lenextrar9   r!   r!   r"   r4      s2      ""r4   )r:   r   c             C   s   g }t  }|d }x| D ]}t j}|}xF||t jdfD ]0}|dkr<|dkr<|dkr<|dkr<||d}q<W tdd|}||d}|td	d| qW |S )
zRemove common directory prefix from all strings in a.

    This uses a naive string replace; it seems to work well enough. Also
    remove trailing carriage returns.
    z
/driver.py/z//\z\\rK   z +$z	driver.pyz\r$)rN   getcwdsepr'   resubrW   )r:   respwdZdriversprefixsspr!   r!   r"   r.      s    
 r.   )r   c              c   sH   t jdd } dt jks0dt jks0t jdd z
dV  W d| t _X dS )zTemporary insert current directory into sys.path.

    This can be used by test cases that do runtime imports, for example
    by the stubgen tests.
    NrK   .r   )r   rO   insert)Zold_sys_pathr!   r!   r"   local_sys_path_set   s    
r   )a1a2r   c             C   sF   d}x2|t t| t|k r6| | || kr6|d7 }qW td|d S )Nr   rJ   ri   )rp   r2   ro   )r   r   num_eqr!   r!   r"   r/     s    (r/   c             C   sR   d}x>|t t| t|k rB| | d  || d  krB|d7 }qW td|d S )Nr   rJ   ri   )rp   r2   ro   )r   r   r   r!   r!   r"   r0     s
    r0   )rO   r   c             C   s(   |  drtjS |  drdS tjS d S )Nzpython2.testzpython310.test)   rl   )endswithr   PYTHON2_VERSIONZPYTHON3_VERSION)rO   r!   r!   r"   testfile_pyversion  s
    

r   )rO   testcase_namer   c             C   s   | drtjS t| S d S )NZpython2)r   r   r   r   )rO   r   r!   r!   r"   testcase_pyversion(  s    
r   )messagesr   c             C   s*   g }x | D ]}| |tjd q
W |S )z@Translate an array of error messages to use / as path separator.rt   )rW   r'   rN   rw   )r   r:   mr!   r!   r"   normalize_error_messages/  s    
r         ?)funcmax_waitr   c             C   sd   t   }d}xRy
|   dS  tk
rZ   t|d || t    }|dkrL t | Y qX qW dS )a   Retry callback with exponential backoff when it raises OSError.

    If the function still generates an error after max_wait seconds, propagate
    the exception.

    This can be effective against random file system operation failures on
    Windows.
    g{Gz?N   )timeOSErrorrp   sleep)r   r   t0Z	wait_timer!   r!   r"   retry_on_error8  s    	r   )objr   c             C   sn   t | trf| ddkrfdg}x,| dD ]}|td| dd  q*W |d  d7  < d|S t| S )Nr-   rJ   z'''\"r   r+   z''')
isinstancestrrX   rY   rW   reprrB   )r   bitsr)   r!   r!   r"   	good_reprO  s    

r   {} != {})r:   bfmtr   c             C   s(   d}| |kr$t |t| t|d S )NT)r5   rA   r   )r:   r   r   r   r!   r!   r"   assert_equal[  s    r   )tr   c             C   s8   dt | kr$t | dd dS t | dd S d S )Nr   r+   z'>   )r   rY   rstrip)r   r!   r!   r"   typenamea  s    r   )typvaluer   c             C   s0   d}t || kr,tdtt |t| d S )NTzInvalid type {}, expected {})typer5   rA   r   )r   r   r   r!   r!   r"   assert_typeh  s    r   )program_textrE   incremental_stepr   c             C   s   t  }tjd| tjd}|dkrBtjd| d| tjd}|rB|}|r||d }|d t|dd\}}|rtd	ng }t  }d|_	d|_
td
d |D rt|j|j|_|jdr|jd|_|S )z4Parse comments like '# flags: --foo' in a test case.z# flags: (.*)$)flagsrJ   z# flagsz: (.*)$z--no-site-packagesF)Zrequire_targetsz9Specifying targets via the flags pragma is not supported.c             s   s    | ]}| d d dkV  qdS )=r   )z--python-versionz-2z--py2N)rY   )r(   flagr!   r!   r"   	<genexpr>  s    z parse_options.<locals>.<genexpr>z--mypy-verbose)r   rx   search	MULTILINEgrouprY   rW   r   RuntimeErrorZstrict_optionalZerror_summaryallr   rM   r<   python_versionconfigZ	getoption	verbosity)r   rE   r   optionsr   flags2Z	flag_listtargetsr!   r!   r"   parse_optionso  s,    


r   )streamsr   c              G   s   dd | D S )zDReturns a single list of string lines from the byte streams in args.c             S   s$   g | ]}| d  D ]}|qqS )rG   )decoderR   )r(   streamr|   r!   r!   r"   r*     s   zsplit_lines.<locals>.<listcomp>r!   )r   r!   r!   r"   split_lines  s    r   )contenttarget_pathr   c          	   C   sx   d }t j|r t |jd }t j|}t j|dd t|ddd}||  W d Q R X |rtt j	|||fd d S )NrJ   T)exist_okrL   zutf-8)rH   )times)
rN   rO   isfilestatst_mtimedirnamemakedirsrP   r   utime)r   r   Znew_timedirtargetr!   r!   r"   write_and_fudge_mtime  s    r   )
operationsr   c                sn   xh| D ]`}t |tr$t|j|j qtj|jrP|jdsBt	t
|j q|j t fdd qW d S )Ntmpc                  s
   t  S )N)rN   remover!   )rO   r!   r"   <lambda>      z)perform_file_operations.<locals>.<lambda>)r   r   r   r   r   rN   rO   isdir
startswithr5   shutilrmtreer   )r   opr!   )rO   r"   perform_file_operations  s    

r   rK   )rE   stepstrip_prefixr   c          
   C   s  x| j D ]\}}||r.|t|d  }tj|sXtd|| jrPd| ndt	|dd}|
 }W d Q R X t|tr||d k	rq
td||t| tjt}| jr| jjrtjjdkrdd	 |D }t|}t| |d
|| jrd| nd q
W d S )Nz0Expected file {} was not produced by test case{}z on step %drK   rG   )rH   zCOutput file {} did not match its expected output pattern
---
{}
---ru   c             S   s   g | ]}t |qS r!   )r   )r(   r)   r!   r!   r"   r*     s   z+check_test_output_files.<locals>.<listcomp>z2Output file {} did not match its expected output{})Zoutput_filesr   r2   rN   rO   existsr5   rA   Zoutput2rP   rQ   r   r
   	fullmatchnormalize_file_outputrR   abspathr   Znormalize_outputZsuiteZ
native_seprw   r   r;   )rE   r   r   rO   Zexpected_contentZoutput_fileZactual_output_contentZnormalized_outputr!   r!   r"   check_test_output_files  s2    



r   )r   current_abs_pathr   c                sl   t dfdd| D }tjjfdd|D }ttjd  fdd|D }fdd|D }|S )z%Normalize file output for comparison.z\d{10}c                s   g | ]}|  d qS )z$PWD)r'   )r(   x)r   r!   r"   r*     s    z)normalize_file_output.<locals>.<listcomp>c                s(   g | ] }t d t   d  d|qS )z\bz$VERSION)rx   ry   escape)r(   r   )versionr!   r"   r*     s    base_versionc                s(   g | ] }t d t   d  d|qS )z\bz$VERSION)rx   ry   r   )r(   r   )r   r!   r"   r*     s    c                s   g | ]}  d |qS )z
$TIMESTAMP)ry   )r(   r   )timestamp_regexr!   r"   r*     s    )rx   compilemypyr   __version__getattr)r   r   resultr!   )r   r   r   r   r"   r     s    
r   )r   )r   )rK   )FrN   rx   r   r   r   
contextlibtypingr   r   r   r   r   r   r   r	   r
   r   r   Zmypy.apir   r   Zunittestr   ZSuiteZ	mypy.mainr   Zmypy.optionsr   Zmypy.test.datar   r   r   r   Zmypy.test.configr   Zmypy.versionmarkskipr3   r   r#   r;   rC   rD   rf   r4   r.   contextmanagerr   intr/   r0   r   r   r   floatr   objectr   r   r   r   r   r   bytesr   r   r   r   r   r!   r!   r!   r"   <module>   s\   ,L4		"	!