a
    =a$                     @   s   d dl Z d dlZd dlZd dl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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 d d	lmZmZmZ d d
lmZ d dlmZ d dl m!Z! G dd dZ"dS )    N)Counter)StringIO)DictListOptionalTuple)Config)checkers)PyLinter)_EXPECTED_RE
_OPERATORSUPDATE_OPTION)FunctionalTestFileNoFileErrorparse_python_version)
OutputLine)FunctionalTestReporter)utilsc                   @   s   e Zd ZdZd$eee dddZdd Zdd Z	d	d
 Z
dd Zedd Zeeeeeeeef f dddZdd Zdd Zdd Zdd Zdd Zee dddZedd d!Zd"d# ZdS )%LintModuleTestN)	test_fileconfigc                 C   s   t  }t | _| j| d| jj_t| j | jd | jd | jd zN| j	|j
 | jjddrt| jjdd}| j| | j  W n ty   Y n0 || _|| _d S )Nr   zsuppressed-messagezlocally-disabledzuseless-suppressionZMASTERzload-plugins)r   r
   _linterZset_reporterr   Z
persistentr	   Z
initializedisableZread_config_fileZoption_fileZcfgfile_parserZ
has_optionr   Z_splitstripgetZload_plugin_modulesZload_config_filer   
_test_file_config)selfr   r   Z_test_reporterZplugins r   @lib/python3.9/site-packages/pylint/testutils/lint_module_test.py__init__   s(    
zLintModuleTest.__init__c              	   C   s  |   r&tdtjjdddd   g }| jjd D ].}zt| W q6 t	yb   |
| Y q60 q6|r~tdd|  | jjd	 }|rd
d |dD }t |v rd}t|t   | jjd }|r
dd |dD }tj |v r
tdtj  d S )NzTest cannot run with Python %s.    )maxsplitr   ZrequireszRequires %s to be present.,except_implementationsc                 S   s   g | ]}|  qS r   strip).0ir   r   r   
<listcomp>E       z(LintModuleTest.setUp.<locals>.<listcomp>z-Test cannot run with Python implementation %rZexclude_platformsc                 S   s   g | ]}|  qS r   r%   )r'   pr   r   r   r)   K   r*   zTest cannot run on platform %r)!_should_be_skipped_due_to_versionpytestskipsysversionsplitr   options
__import__ImportErrorappendjoinplatformZpython_implementationlower)r   missingZrequirementr$   ZimplementationsmsgZexcluded_platformsZ	platformsr   r   r   setUp5   s2    zLintModuleTest.setUpc                 C   s   |    d S N)_runTestr   r   r   r   runTestO   s    zLintModuleTest.runTestc                 C   s$   t j| jjd k p"t j| jjd kS )NZ	min_pyverZ	max_pyver)r/   version_infor   r2   r>   r   r   r   r,   R   s    z0LintModuleTest._should_be_skipped_due_to_versionc                 C   s"   | j j d| jj d| jj dS )Nz (.))r   base	__class__
__module____name__r>   r   r   r   __str__X   s    zLintModuleTest.__str__c           	      C   s   t  }t| D ]\}}t|}|du r*q|d}|du rF|d }n.|dsZ|drl|d t| }nt|}|d}|d}|rt|}t| t	j
|sq|dd	D ]}||| f  d7  < qq|S )
zParses a file and get expected messages.

        :param stream: File-like input stream.
        :type stream: enumerable
        :returns: A dict mapping line,msg-symbol tuples to the count on this line.
        :rtype: dict
        Nliner!   +-r0   opZmsgsr#   )r   	enumerater   searchgroup
startswithintr   r   r/   r@   r1   r&   )	streammessagesr(   rH   matchr0   rK   ZrequiredZmsg_idr   r   r   get_expected_messages[   s(    	




z$LintModuleTest.get_expected_messages)expected_entriesactual_entriesreturnc                 C   sX   |   }|| i }t| D ],\}}|dkr"|| |dk r"| ||< q"||fS )zrTakes two multisets and compares them.

        A multiset is a dict with the cardinality of the key as the value.r   )copysubtractlistitemspop)rU   rV   r9   
unexpectedkeyvaluer   r   r   multiset_difference|   s    

z"LintModuleTest.multiset_differencec                 C   s.   zt | jjW S  ty(   td Y S 0 d S )N )openr   expected_outputFileNotFoundErrorr   r>   r   r   r   _open_expected_file   s    z"LintModuleTest._open_expected_filec                 C   sD   | j jdkrt| j jS d| j jv r4t| j jddS t| j jddS )NZinvalid_encoded_datalatin1)encodingutf8)r   rC   rb   sourcer>   r   r   r   _open_source_file   s
    z LintModuleTest._open_source_filec                 C   s   |   }| |}W d    n1 s(0    Y  |s<t }|  &}dd t|dD }W d    n1 sp0    Y  ||fS )Nc                 S   s   g | ]}t |qS r   )r   Zfrom_csv)r'   rowr   r   r   r)      s   z0LintModuleTest._get_expected.<locals>.<listcomp>test)rj   rT   r   re   csvreader)r   fZexpected_msgsZexpected_output_linesr   r   r   _get_expected   s    
(

$zLintModuleTest._get_expectedc                 C   sz   | j jj}|jdd d t }g }|D ]H}|jdksHJ d|j d||j|jf  d7  < |t	
| q(||fS )Nc                 S   s   | j | j| jfS r<   )rH   symbolr:   )mr   r   r   <lambda>   r*   z,LintModuleTest._get_actual.<locals>.<lambda>r^   Zfatalz#Pylint analysis failed because of ''r!   )r   ZreporterrR   sortr   rq   r:   rH   r5   r   Zfrom_msg)r   rR   Zreceived_msgsZreceived_output_linesr:   r   r   r   _get_actual   s    
zLintModuleTest._get_actualc                 C   s^   d}| j jg}| j| |  \}}|  \}}||ksLJ | |||| ||| d S )NT)r   ri   r   Zcheckrp   rw   error_msg_for_unequal_messages_check_output_text)r   Z__tracebackhide__Zmodules_to_checkexpected_messagesrc   actual_messagesactual_outputr   r   r   r=      s    
zLintModuleTest._runTest)r|   c                 C   s   d| j j g}| ||\}}|rD|d |dd t|D  |rj|d |dd t|D  d|}| jr| jdd	kr|d
7 }|ddd |D 7 }|S )NzWrong results for file "%s":z
Expected in testdata:c                 s   s   | ]}d | V  qdS z %3d: %sNr   r'   r:   r   r   r   	<genexpr>   r*   z@LintModuleTest.error_msg_for_unequal_messages.<locals>.<genexpr>z
Unexpected in testdata:c                 s   s   | ]}d | V  qdS r}   r   r~   r   r   r   r      r*   
verboser   z&

Actual pylint output for this file:
c                 s   s   | ]}t |V  qd S r<   )str)r'   or   r   r   r      r*   )	r   rC   r`   r5   extendsortedr6   r   Z	getoption)r   r{   rz   r|   r:   r9   r]   	error_msgr   r   r   rx      s    


z-LintModuleTest.error_msg_for_unequal_messages)rW   c                 C   s   t |t | }t |t | }d| jj dt d| jj d}td}|rv|d7 }t||dD ]}|| d7 }qb|r|d	7 }t||dD ]}|| d7 }q|S )
NzWrong output for 'z_.txt':
You can update the expected output automatically with: 'python tests/test_functional.py z -k "test_functional[z]"'

linenoz
- Missing lines:
rt   r   z
- Unexpected lines:
)setr   rC   r   operator
attrgetterr   )r   Zexpected_linesZreceived_linesr9   r]   r   Zsort_by_line_numberrH   r   r   r   error_msg_for_unequal_output   s$    
z+LintModuleTest.error_msg_for_unequal_outputc                 C   s   ||ksJ |  ||dS )zZThis is a function because we want to be able to update the text in LintModuleOutputUpdateN)r   )r   _rc   r|   r   r   r   ry      s    z!LintModuleTest._check_output_text)N)rF   rE   __qualname__ZmaxDiffr   r   r   r   r;   r?   r,   rG   staticmethodrT   r   r   r   r   rP   r`   re   rj   rp   rw   r=   r   r   rx   r   ry   r   r   r   r   r      s*   
 r   )#rm   r   r7   r/   collectionsr   ior   typingr   r   r   r   r-   Z_pytest.configr   Zpylintr	   Zpylint.lintr
   Zpylint.testutils.constantsr   r   r   Z%pylint.testutils.functional_test_filer   r   r   Zpylint.testutils.output_liner   Z#pylint.testutils.reporter_for_testsr   Zpylint.utilsr   r   r   r   r   r   <module>   s    