B
    A!p\z&                 @   s  d Z ddlm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mZm	Z	m
Z
 ddlmZmZmZmZm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mZ dd	lmZ dd
lmZ ddlmZ ddlm Z  dej!dd  Z"ddddgZ#eee$Z%ej!d dkZ&dd Z'G dd deZ(G dd deZ)G dd de*ddddddgZ+G d d! d!Z,d"d# Z-G d$d% d%Z.e( Z/e  Z0e01e/ de0j2_3e4e0 d&d' Z5ej6d,d(d)Z7ej6d*d+ Z8dS )-z*functional/non regression tests for pylint    )print_functionN)glob)linesepgetcwdsep)abspathbasenamedirnamejoinsplitext)StringIO)checkers)PyLintASTWalker)BaseReporter)	IReporter)PyLinterz%d%d%d    =-.c             C   s   g }xt t| |d | D ]}t|}t|d }|ddd }| rVt|k rVq|dr|dd  rt|dd krqt t||d }	|	rxNt|	d	d
D ].}
|
ddd dd }| rt|krP qW nt||d }
|	||
f qW |S )a  get python input examples and output messages

    We use following conventions for input files and messages:
    for different inputs:
        test for python  >= x.y    ->  input   =  <name>_pyxy.py
        test for python  <  x.y    ->  input   =  <name>_py_xy.py
    for one input and different messages:
        message for python >=  x.y ->  message =  <name>_pyxy.txt
        lower versions             ->  message with highest num
    *r   Z_py   _Nz*.txtT)reversez.txt)
r   r
   r   r   rsplitisdigitSYS_VERS_STR
startswithsortedappend)Z	input_dirZmsg_dirprefixsuffixresultfnameZinfileZfbaseZpyrestrmessagesZoutfileZpy_rest r(   /lib/python3.7/site-packages/pylint/testutils.py_get_tests_info5   s(    r*   c               @   sH   e Zd ZdZeZdd Zdd Zdd Zdd	 Z	d
d Z
dd ZdZdS )TestReporterz$reporter storing plain text messagesc             C   s   i | _ |   t t | _d S )N)message_idsresetr   r   Zpath_strip_prefix)selfr(   r(   r)   __init__`   s    zTestReporter.__init__c             C   s   t  | _g | _d S )N)r   outr'   )r.   r(   r(   r)   r-   f   s    zTestReporter.resetc             C   sj   |j }|j}|j}|j}d| j|< |r.d| }|d }trNtdkrN|dd}| j	d||||f  dS )z<manage message of different type and in the context of path r   z:%sr   
z
z%s:%3s%s: %sN)
objlinemsg_idmsgr,   PY3Kr   replacer'   r"   )r.   r5   r2   r3   r4   Zsigler(   r(   r)   handle_messagej   s    
zTestReporter.handle_messagec             C   s>   | j   x| j D ]}t|| jd qW | j }|   |S )N)file)r'   sortprintr0   getvaluer-   )r.   r5   r%   r(   r(   r)   finalizez   s    

zTestReporter.finalizec             C   s   d S )Nr(   )r.   modulefilepathr(   r(   r)   on_set_current_module   s    z"TestReporter.on_set_current_modulec             C   s   dS )zignore layoutsNr(   )r.   Zlayoutr(   r(   r)   display_reports   s    zTestReporter.display_reportsN)__name__
__module____qualname____doc__r   Z__implements__r/   r-   r8   r=   r@   rA   _displayr(   r(   r(   r)   r+   [   s   	r+   c               @   s    e Zd Zdd Zdd ZdZdS )MinimalTestReporterc             C   s   | j | d S )N)r'   r"   )r.   r5   r(   r(   r)   r8      s    z"MinimalTestReporter.handle_messagec             C   s
   g | _ d S )N)r'   )r.   r>   r?   r(   r(   r)   r@      s    z)MinimalTestReporter.on_set_current_moduleN)rB   rC   rD   r8   r@   rF   r(   r(   r(   r)   rG      s   rG   c                   s*   e Zd ZdddZ fddZdZ  ZS )MessageNc             C   s   t | |||||fS )N)tuple__new__)clsr4   r3   nodeargs
confidencer(   r(   r)   rJ      s    zMessage.__new__c                sB   t |tr>| jr&|jr&tt| |S | d d |d d kS tS )Nr   )
isinstancerH   rN   super__eq__NotImplemented)r.   other)	__class__r(   r)   rQ      s
    
zMessage.__eq__)NNNN)rB   rC   rD   rJ   rQ   __hash____classcell__r(   r(   )rT   r)   rH      s   
rH   r4   r3   rL   rM   rN   c               @   sF   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Ze	dd Z
dS )UnittestLinterz0A fake linter class to capture checker messages.c             C   s   g | _ i | _d S )N)	_messagesstats)r.   r(   r(   r)   r/      s    zUnittestLinter.__init__c             C   s   z| j S g | _ X d S )N)rX   )r.   r(   r(   r)   release_messages   s    zUnittestLinter.release_messagesNc             C   s   | j t||||| d S )N)rX   r"   rH   )r.   r4   r3   rL   rM   rN   
col_offsetr(   r(   r)   add_message   s    zUnittestLinter.add_messagec             O   s   dS )NTr(   )r.   Zunused_argsZunused_kwargsr(   r(   r)   is_message_enabled   s    z!UnittestLinter.is_message_enabledc             K   s&   x|  D ]\}}|| j|< q
W | jS )N)itemsrY   )r.   kwargsnamevaluer(   r(   r)   	add_stats   s    zUnittestLinter.add_statsc             C   s   t jS )N)linteroptions_providers)r.   r(   r(   r)   rd      s    z UnittestLinter.options_providers)NNNNN)rB   rC   rD   rE   r/   rZ   r\   r]   rb   propertyrd   r(   r(   r(   r)   rW      s   
rW   c                 s    fdd}|S )z1Decorator for setting config values on a checker.c                s   t   fdd}|S )Nc                sF   x$  D ]\}}t| jj|| q
W t| tr:| j   |  d S )N)r^   setattrcheckerconfigrO   CheckerTestCaseopen)r.   keyra   )funr_   r(   r)   _forward   s
    

z.set_config.<locals>._wrapper.<locals>._forward)	functoolswraps)rl   rm   )r_   )rl   r)   _wrapper   s    	zset_config.<locals>._wrapperr(   )r_   rp   r(   )r_   r)   
set_config   s    rq   c               @   sD   e Zd ZdZdZi Zdd Zejdd Z	ejdd Z
d	d
 ZdS )ri   zBA base testcase class for unit testing individual checker classes.Nc             C   sL   t  | _| | j| _x&| j D ]\}}t| jj|| q"W | j  d S )N)	rW   rc   CHECKER_CLASSrg   CONFIGr^   rf   rh   rj   )r.   rk   ra   r(   r(   r)   setup_method   s
    zCheckerTestCase.setup_methodc          	   c   s   |    dV  W dQ R X dS )z6Assert that no messages are added by the given method.N)assertAddsMessages)r.   r(   r(   r)   assertNoMessages   s    
z CheckerTestCase.assertNoMessagesc             g   sT   dV  | j  }dddd |D ddd |D f }t||ksPt|dS )a#  Assert that exactly the given method adds the given messages.

        The list of messages must exactly match *all* the messages added by the
        method. Additionally, we check to see whether the args in each message can
        actually be substituted into the message string.
        Nz<Expected messages did not match actual.
Expected:
%s
Got:
%sr1   c             s   s   | ]}t |V  qd S )N)repr).0mr(   r(   r)   	<genexpr>   s    z5CheckerTestCase.assertAddsMessages.<locals>.<genexpr>c             s   s   | ]}t |V  qd S )N)rw   )rx   ry   r(   r(   r)   rz      s    )rc   rZ   r
   listAssertionError)r.   r'   Zgotr5   r(   r(   r)   ru      s    
z"CheckerTestCase.assertAddsMessagesc             C   s"   t t}|| j || dS )z recursive walk on the given nodeN)r   rc   Zadd_checkerrg   walk)r.   rL   Zwalkerr(   r(   r)   r}      s    zCheckerTestCase.walk)rB   rC   rD   rE   rr   rs   rt   
contextlibcontextmanagerrv   ru   r}   r(   r(   r(   r)   ri      s   ri   c             C   s   t tt| jS )N)r{   tokenizegenerate_tokensr   readline)coder(   r(   r)   _tokenize_str  s    r   c          	   c   s`   t  \}}| r:tjdkr.t|t| d nt||  z
|V  W dt| t| X dS )zCreate a new temporary file.

    If *content* parameter is given, then it will be written
    in the temporary file, before passing it back.
    This is a context manager and should be used with a *with* statement.
    )r   r   asciiN)	tempfileZmkstempsysversion_infooswritebytescloseremove)ZcontentZfile_handleZtmpr(   r(   r)   _create_tempfile  s    


r   c          	   c   s,   t  }t| }||_|V  W dQ R X dS )zCCreate an astroid module for the given code, backed by a real file.N)r   astroidparser9   )r   Ztempr>   r(   r(   r)   _create_file_backed_module+  s    
r   )N)9rE   Z
__future__r   collectionsr~   rn   r   r   r   r   r   os.pathr   r   r	   r
   r   r   r   r   ior   r   Zpylintr   Zpylint.utilsr   Zpylint.reportersr   Zpylint.interfacesr   Zpylint.lintr   r   r   ZTITLE_UNDERLINES__file__ZPREFIXr6   r*   r+   rG   
namedtuplerH   rW   rq   ri   Ztest_reporterrc   Zset_reporterrh   Z
persistentZ
initializer   r   r   r   r(   r(   r(   r)   <module>   sN   &3

"+

