B
    '(bZi                 @   s`  U 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m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mZ ddlmZ ddlmZmZmZ ejeZddgZee d	< G d
d deZ!G dd deZ"ee!e"f Z#dddddZ$G dd de
j%Z&e'e'dddZ(G dd dZ)e'e'ee) dddZ*ee' ee' dddZ+ee' ee' dddZ,e'e'd d!d"Z-ee' ee' e'dd#d$d%Z.e'e'd&d'd(Z/e'e'd&d)d*Z0edd+d,d-Z1ee'e2d.d/d0d1Z3d2d3e'ed d4d5d6Z4G d7d8 d8e
j5Z6G d9d2 d2e
j7Z8e'e'e'd:d;d<Z9e&e:d=d>d?Z;e&e:d=d@dAZ<G dBd3 d3Z=dS )CzGUtilities for processing .test files containing test case descriptions.    N)abstractmethod)
ListTupleSetOptionalIteratorAnyDict
NamedTupleUnionPattern)Final)test_data_prefixtest_temp_dirPREFIXZgdbZlldbSUPPORTED_DEBUGGERSc               @   s&   e Zd ZU eed< eed< eed< dS )
UpdateFilemodulecontenttarget_pathN)__name__
__module____qualname__str__annotations__ r   r   =/home/dcms/DCMS/lib/python3.7/site-packages/mypy/test/data.pyr      s   
r   c               @   s   e Zd ZU eed< eed< dS )
DeleteFiler   pathN)r   r   r   r   r   r   r   r   r   r   "   s   
r   DataDrivenTestCase)casereturnc       &      C   s  t | j| j}| jj}| jjr(tjj}nt	j}| jj
}d}g }g }g }i }	i }
i }i }g }i }|d  }}x~|dd D ]l}|jdkr|jdk	sttd|j}|||j|f}|jdkr|| n<|jdkr||d t|d  tjf n
|| qz|jd	kr|jdk	s*t|tj| j|j}|jd
krPdnd}t|dd}||||| f W dQ R X qz|jdkr|jdk	st|tj| j|j}t|dd}|||d| f W dQ R X qztd|jr`|jdkr
dnt|jtdd }|dks.t|jdkr@t ndd |jdD }|||< qztd|jr|jdkrdnt|jtdd }|dkst|jdkrt ndd |jdD }|||< qztd|jrJ|jdkrdnt|jtdd }|dkst|jdkr*g ndd |jdD }|||< qz|jdkr|jdk	sfttd|j}|std|j t|d }|d kstd!| |||d}|
|t | qztd"|jr|jdkrg }n|jd}d}x|D ]
}|d#kr$d$}| d%r|d&d' } | d(krZt!d)"| j|j#|d'd }!yt$d*d+ |!d,D }"W n, t!k
r   t!d-"| j|j#|!Y nX | d.krt%j&|"k}nT| d/krdt|"  k rd0k sn t!d1"| j|j#|!t%j&dt|" |"k}qW |rd2d |jD }#tjj'd3krX|rXd4d |#D }#|jd5ksp|jd6krv|#}n,t|jtd5d }|dkst|#|	|< d$}qz|jd7kr|jdkr|j}qzt!d8|j d9| j d:|j# qzW |r
t!| j d;|j# d<xf|( D ]Z}||kr.|| ||< ||kr||kr|| )|| st!d="|| j|j#qW |j}$t*|$|d> x$|D ]\}%}t*|d||% qW |$| _+|| _,|	| _-| j#|j# t|j d  | _.|| _/|| _0|| _1|| _2|
| _3|pg | _4|| _5|| _6dS )?zParse and prepare a single case from suite with test case descriptions.

    This method is part of the setup phase, just before the test case is run.
    Tr      N>   outfile
outfile-refile
r%   z
outfile-re)builtinsZbuiltins_py2r'   zbuiltins.pyiz__builtin__.pyiutf8)encodingtypingz
typing.pyizstale[0-9]*$stalec             S   s   h | ]}|  qS r   )strip).0tr   r   r   	<setcomp>b   s    z"parse_test_case.<locals>.<setcomp>,zrechecked[0-9]*$Z	recheckedc             S   s   h | ]}|  qS r   )r,   )r-   r.   r   r   r   r/   g   s    ztargets[0-9]*$targetsc             S   s   g | ]}|  qS r   )r,   )r-   r.   r   r   r   
<listcomp>l   s    z#parse_test_case.<locals>.<listcomp>deletez(.*)\.([0-9]+)$zInvalid delete section:    zCan't delete during step z
out[0-9]*$zskip-path-normalizationFversion   	   >   ==>=zB{}, line {}: Only >= and == version checks are currently supportedc             s   s   | ]}t |V  qd S )N)int)r-   xr   r   r   	<genexpr>   s    z"parse_test_case.<locals>.<genexpr>.z/{}, line {}: "{}" is not a valid python versionz>=z==   zW{}, line {}: Only minor or patch version checks are currently supported with "==": "{}"c             S   s   g | ]}t |qS r   )expand_variables)r-   liner   r   r   r2      s    \c             S   s   g | ]}t |qS r   )fix_win_path)r-   r@   r   r   r   r2      s    outZout1	triggeredzInvalid section header z in z	 at line z, line z#: Required output section not foundzIStale modules after pass {} must be a subset of rechecked modules ({}:{})main)7parse_test_datadatanamesuite	base_path
native_seposr   join	posixpathrequired_out_sectionidargAssertionErrorr?   appendrecompilerstripSdirnamer%   openreadmatchr:   lensetsplitgroup
setdefaultadd
startswith
ValueErrorformatr@   tuplesysversion_infosepkeysissubsetexpand_errorsinputoutputoutput2	last_linefilesoutput_filesexpected_stale_modulesexpected_rechecked_modulesdeleted_pathsrD   normalize_outputexpected_fine_grained_targets)&r    Z
test_itemsrJ   rM   Zout_section_missingru   rp   rq   rm   rn   rt   Zstale_modulesZrechecked_modulesrD   r1   itemZ
first_itemcontentsZ
file_entryZmpathfnamfZsrc_pathZpassnummodulesZreprocessedmnumfullargsZversion_checkrQ   Z
compare_opversion_strr5   Z
tmp_outputrl   	file_pathr   r   r   parse_test_case*   s   

&$$&(
&(
&&




"


r   c                   s  e Zd ZU dZded< ee ed< ee ed< eeee f ed< dZ	dZ
eeeef  ed	< eeee f ed
< eeee f ed< eeee f ed< dZeed< eeeeeee f f  ed< eeee f ed< ee ed< ddeeeeee eeeedd fddZddddZddddZddddZeeeef dddZd%eee ed d!d"Zeee  dd#d$Z  ZS )&r   zNHolds parsed data-driven test cases, and handles directory setup and teardown.DataSuiteCollectorparentrl   rm   rn    r   rp   rr   rs   rv   Tro   rq   rt   rD   	DataSuiteN)r   rI   r%   rH   writescache	only_whenplatformskipxfailrG   r@   r!   c                sv   t  || || _|| _|| _|| _|dkr8tjdksJ|dkrNtjdkrNd}|| _|	| _	|
| _
|| _d | _d | _d S )NZwindowswin32posixT)super__init__rI   r%   r   r   rf   r   r   r   rG   r@   old_cwdtmpdir)selfr   rI   r%   rH   r   r   r   r   r   rG   r@   )	__class__r   r   r      s    zDataDrivenTestCase.__init__)r!   c          	   C   s   | j rt   n| jr$| tjj | t}|d k	s>td| }|	  y|
|  W n tk
r   | jdd }|r| jd k	sttj|tj| jj}td| j d|  tj|s| jsttj| j|}t| jj|  Y nX d S )NzShould not happenz--save-failures-tozCopying data from test z to )r   pytestr   Z
add_markermarkZ	getparentr   rR   objsetuprun_case	ExceptionconfigZ	getoptionr   rL   r   rM   basenamerH   printisabsr   shutilcopytree)r   r   rI   save_dir
target_dirr   r   r   runtest	  s*    


zDataDrivenTestCase.runtestc          
      sz  t | d t | _tjdd| _t| jj t	t
 i  x| jD ]\}}td|}|rt|d}|dksxttdd|}t|}t|||} |g | qFtj|}tj|d	d
 t|ddd}	|	| W d Q R X qFW xT| j D ]F\}}
|dkstx.|
D ]&}t|} |g t|| qW qW  rTt nd} fddtd|d D | _ d S )N)r    z
mypy-test-)prefixz.*\.([0-9]+)$r"   r4   z	\.[0-9]+$r   T)exist_okwr(   )r)   c                s   g | ]}  |g qS r   )get)r-   r}   )stepsr   r   r2   D  s    z,DataDrivenTestCase.setup.<locals>.<listcomp>)!r   rL   getcwdr   tempfileTemporaryDirectoryr   chdirrH   mkdirr   rp   rT   r[   r:   r_   rR   submodule_from_pathr   r`   rS   r   rX   makedirsrY   writert   itemsr   maxranger   )r   r   r   r|   r}   r   r   Z	operationdirrz   pathsZmax_stepr   )r   r   r   "  s4    



"zDataDrivenTestCase.setupc             C   s\   | j d k	r| jd k	stdt| j  y| j  W n tk
rJ   Y nX d | _ d | _d S )Nztest was not properly set up)r   r   rR   rL   r   cleanupOSError)r   r   r   r   teardownF  s    zDataDrivenTestCase.teardownc             C   s   | j | j| jfS )N)r%   r@   rH   )r   r   r   r   
reportinfoQ  s    zDataDrivenTestCase.reportinfo)excinfostyler!   c             C   sF   | tr| }n| j| |jdd}d| j d| j d| S )Nshort)r   zdata: :z:
)Zerrisinstance
SystemExitZexconlyr   Z_prunetracebackZgetreprr%   r@   )r   r   r   Zexcreprr   r   r   repr_failureT  s
    

zDataDrivenTestCase.repr_failurec             C   s   | j S )ax  Return a list of descriptions of file operations for each incremental step.

        The first list item corresponds to the first incremental step, the second for the
        second step, etc. Each operation can either be a file modification/creation (UpdateFile)
        or deletion (DeleteFile).

        Defaults to having two steps if there aern't any operations.
        )r   )r   r   r   r   
find_stepsa  s    	zDataDrivenTestCase.find_steps)N)r   r   r   __doc__r   r   r   r	   r:   r%   r@   r   r   ru   r   r   boolr   r   r   r   r   r   r   r   FileOperationr   __classcell__r   r   )r   r   r      sB   
 $)r   r!   c             C   s<   t dd| } t d| }|d= d|}t dd|}|S )Nz\.pyi?$r   z[/\\]r   r=   z\.__init__$)rT   r   r^   rM   )r   partsr   r   r   r   r   m  s    
r   c               @   sX   e Zd ZU dZdZdZee ed< e	e ed< dZ
dZeee e	e edddd	ZdS )
TestItemzVParsed test caseitem.

    An item is of the form
      [id arg]
      .. data ..
    r   rQ   rG   r   N)rP   rQ   rG   r@   r!   c             C   s   || _ || _|| _|| _d S )N)rP   rQ   rG   r@   )r   rP   rQ   rG   r@   r   r   r   r     s    zTestItem.__init__)r   r   r   r   rP   rQ   r   r   r   r   r%   r@   r:   r   r   r   r   r   r   w  s   
r   )raw_datarH   r!   c       
      C   s  dd| d g|  d }g }g }d}d}d}d}x&|t|k rZ||  }	|| dr|	dr|rt|}t|}|t||t||d  |}|	dd	 }d}d
|kr||	d
d d }|d|	d
 }g }nn|| dr
||| dd  nF|| ds*|||  n&|| drP||| dd  |d7 }q6W |rt|}t|}|t||||d  |S )z>Parse a list of lines that represent a sequence of test items.r   z[case ]r&   Nr   [r"    z\[z--z----r4   )
r^   r\   r,   rb   endswithcollapse_line_continuation
strip_listrS   r   index)
r   rH   linesretrG   rP   rQ   iZi0sr   r   r   rF     sB    rF   )lr!   c             C   sP   g }x | D ]}| tdd| q
W x$t|dkrJ|d dkrJ|  q(W |S )zReturn a stripped copy of l.

    Strip whitespace at the end of all lines, and strip all empty
    lines from the end of the array.
    z\s+$r   r   r   )rS   rT   r   r\   pop)r   rr   r   r   r   r     s    
r   c          	   C   s\   g }d}xN| D ]F}t dd|}|r@|d  t dd|7  < n
|| |d}qW |S )NFz\\$r   r   z^ +rA   )rT   r   rS   r   )r   r   Zcontr   ssr   r   r   r     s    

r   )r   r!   c             C   s   |  dtS )Nz<ROOT>)replaceroot_dir)r   r   r   r   r?     s    r?   )rl   rm   ry   r!   c       	      C   s   xt t| D ]}x| | ddd D ]}td| }|r*|ddkrVd}n&|ddkrjd}n|dd	kr|d
}|d}|d}|dd}|dkr|| d|d  d| d|  q*|| d|d  d| d| d| 	 q*W qW dS )zTransform comments such as '# E: message' or
    '# E:3: message' in input.

    The result is lines like 'fnam:line: error: message'.
    z # r"   Nz*^([ENW]):((?P<col>\d+):)? (?P<message>.*)$EerrorNZnoteWwarningcolmessagez\##r   z: )	r   r\   r^   rT   searchr,   r_   r   rS   )	rl   rm   ry   r   Zpossible_err_commentr|   Zseverityr   r   r   r   r   rk     s&    


"rk   )r@   r!   c             C   sT   |  tt dd} td| }|s(| S | \}}}d| dd|pJd|S dS )z`Changes Windows paths to Linux paths in error messages.

    E.g. foo\bar.py -> foo/bar.py.
    rA   /z^([\S/]+):(\d+:)?(\s+.*)z{}:{}{}r   N)r   r   rT   r[   groupsrd   )r@   r|   filenamelinenor   r   r   r   rB     s    rB   c             C   sL   t d| }|s| S d| d|d |ddd| |dd S )zChanges filename paths to Linux paths in Cobertura output files.

    E.g. filename="pkg\subpkg\a.py" -> filename="pkg/subpkg/a.py".
    z&<class .* filename="(?P<filename>.*?)"z{}{}{}Nr"   r   rA   r   )rT   r   rd   startr_   r   end)r@   r|   r   r   r   fix_cobertura_filename  s    r   )parserr!   c             C   sf   |  d}|jddddd |jdd dd	 |jd
ddd |jddddd |jdd dtdd d S )NZmypyz--update-data
store_trueFzLUpdate test data to reflect actual output (supported only for certain tests))actiondefaulthelpz--save-failures-tozBCopy the temp directories from failing tests to a target directory)r   r   z--mypy-verbosecountz/Set the verbose flag when creating mypy Options)r   r   z--mypyc-showcz%Display C code on mypyc test failuresz--mypyc-debugZdebuggerz8Run the first mypyc run test with the specified debugger)r   destchoicesr   )ZgetgroupZ	addoptionr   )r   r_   r   r   r   pytest_addoption#  s    


r   zOptional[Any])	collectorrH   r   r!   c             C   s.   t |tr*t|tr*|tk	r*tj| |dS dS )zCalled by pytest on each object in modules configured in conftest.py files.

    collector is pytest.Collector, returns Optional[pytest.Class]
    )r   rH   N)
isinstancetype
issubclassr   r   from_parent)r   rH   r   r   r   r   pytest_pycollect_makeitem9  s
    

r   DataFileCollectorr   )r   rI   r%   r!   c             c   s  t |dd}| }W dQ R X d}tjd|tjtjB d}|d dd	 }t }xtd	t	||D ]}	||	|	|  \}
}}}}}}|
|krt
d
|
| j||r|d	d nd}tj| ||t|
|jt|||t|t|||dV  ||dd	 7 }||
h qfW dS )zIterate over raw test cases in file, at collection time, ignoring sub items.

    The collection phase is slow, so any heavy processing should be deferred to after
    uninteresting tests are filtered (when using -k PATTERN switch).
    zutf-8)r)   Nr6   zz^\[case ([a-zA-Z_0-9]+)(-writescache)?(-only_when_cache|-only_when_nocache)?(-posix|-windows)?(-skip)?(-xfail)?\][ \t]*$\n)flagsr   r&   r"   z1Found a duplicate test name "{}" in {} on line {})r   rI   r%   rH   r   r   r   r   r   rG   r@   )rY   rZ   rT   r^   DOTALL	MULTILINEr   r]   r   r\   RuntimeErrorrd   rH   r   r   add_test_name_suffixtest_name_suffixr   update)r   rI   r%   rz   rG   Z
NUM_GROUPSZcasesline_noZ
test_namesr   rH   r   r   Zplatform_flagr   r   r   r   r   r   split_test_casesK  s8    

r  c               @   s   e Zd Zed dddZdS )r   r   )r!   c             c   sJ   | j }tj|js&td|j dx|jD ]}tj| |dV  q.W dS )zNCalled by pytest on each of the object returned from pytest_pycollect_makeitemzTest data prefix (z) not set correctly)r   rH   N)	r   rL   r   isdirdata_prefixrR   rp   r   r   )r   rI   Z	data_filer   r   r   collect|  s
    zDataSuiteCollector.collectN)r   r   r   r   r  r   r   r   r   r   {  s   r   c                   sJ   e Zd ZU dZeed< eeed d fddZe	d ddd	Z
  ZS )
r   zvRepresents a single `.test` data driven test file.

    More context: https://github.com/python/mypy/issues/11662
    r   )r   rH   r!   c               s   t  j||dS )N)rH   )r   r   )clsr   rH   )r   r   r   r     s    zDataFileCollector.from_parentr   )r!   c             c   s.   t | | jjtj| jjj| jdE d H  d S )N)r   rI   r%   )r  r   r   rL   r   rM   r  rH   )r   r   r   r   r    s    zDataFileCollector.collect)r   r   r   r   r   r   classmethodr   r   r   r  r   r   r   )r   r   r     s   
)rH   suffixr!   c             C   s@   t d| }|r4|d}| d t|  | | S | | S d S )Nz-[-A-Za-z0-9]+$r   )rT   r   r_   r\   )rH   r  r|   Zmagic_suffixr   r   r   r     s
    
r   )testcaser!   c             C   s   d| j  kpd| jkS )Nincremental)rH   lowerr%   )r	  r   r   r   is_incremental  s    r  c             C   sF   t dd | jD rdS x(| jD ]\}}tj|dr dS q W dS )Nc             s   s   | ]}t d |V  qdS )z# flags[2-9]:N)rT   r[   )r-   r@   r   r   r   r<     s    z#has_stable_flags.<locals>.<genexpr>Fz	mypy.ini.T)anyrl   rp   rL   r   r   rb   )r	  r   rx   r   r   r   has_stable_flags  s    r  c               @   sP   e Zd ZU ee ed< eZeZ	dZ
dZdZddddZeeddd	d
ZdS )r   rp   Fr   N)r!   c             C   s   dS )zSetup fixtures (ad-hoc)Nr   )r   r   r   r   r     s    zDataSuite.setup)r	  r!   c             C   s   t d S )N)NotImplementedError)r   r	  r   r   r   r     s    zDataSuite.run_case)r   r   r   r   r   r   r   rJ   r   r  rO   rK   r   r   r   r   r   r   r   r   r   r     s   
)>r   os.pathrL   r   rN   rT   r   abcr   rf   r   r*   r   r   r   r   r   r   r	   r
   r   r   Ztyping_extensionsr   Zmypy.test.configr   r   r   r   normpathr   r   r   r   r   r   r   ZItemr   r   r   r   rF   r   r   r?   rk   rB   r   r   objectr   r  ZClassr   Z	Collectorr   r   r   r  r  r   r   r   r   r   <module>   sR   0 ' 
-/	