o
    g3&                     @   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mZ d dl	m
Z
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# e
rpd dl	mZ$ G dd dZ%dS )    N)Counter)StringIOTextIOWrapper)TYPE_CHECKING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)dedee fddZdd Zdd	 Z	d
d Z
dd ZededdfddZedddddedeeef f fddZdd Zdd Zdd Zdd Zd d! Zd"ee fd#d$Zdef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 z'| j	|j
 | jjddrKt| jjdd}| j| | j  W n	 tyZ   Y nw || _|| _d S )Nr   zsuppressed-messagezlocally-disabledzuseless-suppressionMASTERzload-plugins)r   r   _linterset_reporterr   
persistentr   
initializedisableread_config_fileoption_filecfgfile_parser
has_optionr   _splitstripgetload_plugin_modulesload_config_filer   
_test_file_config)selfr   r   _test_reporterplugins r,   _/home/ubuntu/cloudmapper/venv/lib/python3.10/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  d g }| jjd D ]}zt| W q t	y3   |
| Y qw |rBtdd	| d
 | jjd }|redd |d	D }t |v red}t|t   | jjd }|rdd |d	D }tj |v rtdtj d S d S d S )NzTest cannot run with Python     )maxsplitr   .requiresz	Requires ,z to be present.except_implementationsc                 S      g | ]}|  qS r,   strip).0ir,   r,   r-   
<listcomp>G       z(LintModuleTest.setUp.<locals>.<listcomp>z-Test cannot run with Python implementation %rexclude_platformsc                 S   r6   r,   r7   )r9   pr,   r,   r-   r;   M   r<   zTest cannot run on platform )!_should_be_skipped_due_to_versionpytestskipsysversionsplitr'   options
__import__ImportErrorappendjoinplatformpython_implementationlower)r)   missingrequirementr5   implementationsmsgexcluded_platforms	platformsr,   r,   r-   setUp8   s4   zLintModuleTest.setUpc                 C   s   |    d S N)_runTestr)   r,   r,   r-   runTestQ   s   zLintModuleTest.runTestc                 C   s$   t j| jjd k pt j| jjd kS )N	min_pyver	max_pyver)rB   version_infor'   rE   rV   r,   r,   r-   r?   T   s   z0LintModuleTest._should_be_skipped_due_to_versionc                 C   s"   | j j d| jj d| jj dS )Nz (r2   ))r'   base	__class__
__module____name__rV   r,   r,   r-   __str__Z   s   "zLintModuleTest.__str__streamreturnzCounterType[Tuple[int, str]]c           
      C   s   t  }t| D ]c\}}t|}|du rq|d}|du r#|d }n|ds-|dr6|d t| }nt|}|d}|d}|rSt|}t| t	j
|sSq|dd	D ]}	|||	 f  d7  < q[q|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liner0   +-rC   opmsgsr4   )r   	enumerater   searchgroup
startswithintr   r   rB   rZ   rD   r8   )
ra   messagesr:   rc   matchlinenorC   rf   requiredmsg_idr,   r,   r-   get_expected_messages]   s*   	




z$LintModuleTest.get_expected_messagesexpected_entriesCounterTypeactual_entriesc                 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)rs   ru   rM   
unexpectedkeyvaluer,   r,   r-   multiset_difference~   s   


z"LintModuleTest.multiset_differencec                 C   s.   z	t | jjddW S  ty   td Y S w )Nutf-8encoding )openr'   expected_outputFileNotFoundErrorr   rV   r,   r,   r-   _open_expected_file   s
   z"LintModuleTest._open_expected_filec                 C   sH   | j jdkrt| j jddS d| j jv rt| j jddS t| j jddS )Ninvalid_encoded_datar   r   latin1utf8)r'   r\   r   sourcerV   r,   r,   r-   _open_source_file   s
   z LintModuleTest._open_source_filec                 C   s   |   }| |}W d    n1 sw   Y  |st }|  }dd t|dD }W d    ||fS 1 s;w   Y  ||fS )Nc                 S   s   g | ]}t |qS r,   )r   from_csv)r9   rowr,   r,   r-   r;      s    
z0LintModuleTest._get_expected.<locals>.<listcomp>test)r   rr   r   r   csvreader)r)   fexpected_msgsexpected_output_linesr,   r,   r-   _get_expected   s   



zLintModuleTest._get_expectedc                 C   sz   | j jj}|jdd d t }g }|D ]$}|jdks$J d|j d||j|jf  d7  < |t	
| q||fS )Nc                 S   s   | j | j| jfS rT   )rc   symbolrP   )mr,   r,   r-   <lambda>   s    z,LintModuleTest._get_actual.<locals>.<lambda>r|   fatalz#Pylint analysis failed because of ''r0   )r   reporterrm   sortr   r   rP   rc   rH   r   from_msg)r)   rm   received_msgsreceived_output_linesrP   r,   r,   r-   _get_actual   s   
zLintModuleTest._get_actualc                 C   s^   d}| j jg}| j| |  \}}|  \}}||ks&J | |||| ||| d S )NT)r'   r   r   checkr   r   error_msg_for_unequal_messages_check_output_text)r)   __tracebackhide__modules_to_checkexpected_messagesr   actual_messagesactual_outputr,   r,   r-   rU      s   

zLintModuleTest._runTestr   c                 C   s   d| j j dg}| ||\}}|r$|d |dd t|D  |r7|d |dd t|D  d|}| jrW| jd	d
krW|d7 }|ddd |D 7 }|S )NzWrong results for file "z":z
Expected in testdata:c                 s       | ]}d | V  qdS z %3d: %sNr,   r9   rP   r,   r,   r-   	<genexpr>   s
    
z@LintModuleTest.error_msg_for_unequal_messages.<locals>.<genexpr>z
Unexpected in testdata:c                 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 rT   )str)r9   or,   r,   r-   r      r   )	r'   r\   r~   rH   extendsortedrI   r(   	getoption)r)   r   r   r   rP   rM   r{   	error_msgr,   r,   r-   r      s"   



z-LintModuleTest.error_msg_for_unequal_messagesc                 C   s   t |t | }t |t | }d| jj dt d| jj d}td}|r;|d7 }t||dD ]	}|| d7 }q1|rQ|d	7 }t||dD ]	}|| d7 }qG|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]"'

ro   z
- Missing lines:
r   r   z
- Unexpected lines:
)setr'   r\   r   operator
attrgetterr   )r)   expected_linesreceived_linesrM   r{   r   sort_by_line_numberrc   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)   _r   r   r,   r,   r-   r      s   
z!LintModuleTest._check_output_textrT   )r_   r^   __qualname__maxDiffr   r   r
   r.   rS   rW   r?   r`   staticmethodr   rr   r	   r   r   rl   r~   r   r   r   r   rU   r   r   r   r   r   r,   r,   r,   r-   r      s6     
r   )&r   r   rJ   rB   collectionsr   ior   r   typingr   r   r   r   r	   r@   _pytest.configr
   pylintr   pylint.lintr   pylint.testutils.constantsr   r   r   %pylint.testutils.functional_test_filer   r   r   pylint.testutils.output_liner   #pylint.testutils.reporter_for_testsr   pylint.utilsr   rt   r   r,   r,   r,   r-   <module>   s&   