o
    b0                     @   s  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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 G dd	 d	ejZed
e G dd deZdZG dd dedg dZddiZede Zdd ZG dd dejZG dd de Z!ej"ej#ej$ej%dZ&dd Z'dd Z(dd  Z)G d!d" d"e Z*G d#d$ d$e*Z+d%d& Z,e, Z-d'd( e-D Z.ej/j0d)e-e.d*d+d, Z1e2d-krd.ej3v rd/Zej34d. e5ej3 dS dS )0z(Functional full-module tests for PyLint.    N)configparser)checkers)
interfaces)lint)	reportersc                   @   s.   e Zd Zejd dk rdZdZdS dZdZdS )test_dialectr         :   
:
N)__name__
__module____qualname__sysversion_info	delimiterlineterminator r   r   Z/var/www/secure340b-portal/env/lib/python3.10/site-packages/pylint/test/test_functional.pyr   "   s    r   testc                   @   s   e Zd ZdS )NoFileErrorN)r   r   r   r   r   r   r   r   .   s    r   Fc                   @   s,   e Zd Zedd Zedd Zdd ZdS )
OutputLinec                 C   s>   | |j |j|jp	d|jdd|jtjkr|jjS tj	jS )N z
r   )
symbollineobjmsgreplace
confidencer   	UNDEFINEDnameHIGH)clsr   r   r   r   from_msg=   s   
zOutputLine.from_msgc                 C   s@   t |dkr
|d ntjj}| |d t|d |d |d |S )N      r         r   )lenr   r"   r!   int)r#   rowr   r   r   r   from_csvD   s   $zOutputLine.from_csvc                 C   s   | j tjjkr| d d S | S )N)r   r   r"   r!   selfr   r   r   to_csvI   s   zOutputLine.to_csvN)r   r   r   classmethodr$   r,   r0   r   r   r   r   r   ;   s    

r   )r   linenoobjectr   r   r   z[a-z][a-z\-]+zt\s*#\s*(?:(?P<line>[+-]?[0-9]+):)?(?:(?P<op>[><=]+) *(?P<version>[0-9.]+):)?\s*\[(?P<msgs>%(msg)s(?:,\s*%(msg)s)*)\]c                 C   s   t dd | dD S )Nc                 s   s    | ]}t |V  qd S N)r*   ).0digitr   r   r   	<genexpr>^       z'parse_python_version.<locals>.<genexpr>.)tuplesplit)strr   r   r   parse_python_version]   s   r=   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )FunctionalTestReporterc                 C   s   | j | d S r4   )messagesappend)r/   r   r   r   r   handle_messageb   s   z%FunctionalTestReporter.handle_messagec                 C   s
   g | _ d S r4   )r?   )r/   modulefilepathr   r   r   on_set_current_modulee   s   
z,FunctionalTestReporter.on_set_current_modulec                 C   s   dS )zIgnore layouts.Nr   )r/   Zlayoutr   r   r   display_reportsh       z&FunctionalTestReporter.display_reportsN)r   r   r   rA   rD   rE   r   r   r   r   r>   a   s    r>   c                   @   sj   e Zd ZdZeedd dZdd Zdd Zed	d
 Z	edd Z
edd Zedd ZdddZdS )FunctionalTestFilez0A single functional test case file with options.c                 C   
   |  dS )N,)r;   )sr   r   r   <lambda>r   s   
 zFunctionalTestFile.<lambda>)	min_pyver	max_pyverrequiresc                 C   s0   || _ |dd| _ddg g d| _|   d S )N.pyr   )r(   r%   )r&   r   )rL   rM   rN   except_implementations)
_directoryr   baseoptions_parse_options)r/   	directoryfilenamer   r   r   __init__v   s   zFunctionalTestFile.__init__c                 C   sn   t  }|d z|| j W n	 ty   Y nw |dD ]\}}| j|dd }||| j	|< q d S )NZtestoptionsc                 S   s   | S r4   r   )vr   r   r   rK      rF   z3FunctionalTestFile._parse_options.<locals>.<lambda>)
r   ConfigParseradd_sectionreadoption_filer   items_CONVERTERSgetrS   )r/   cpr!   valueconvr   r   r   rT      s   
z!FunctionalTestFile._parse_optionsc                 C   rH   )Nz.rc
_file_typer.   r   r   r   r\         
zFunctionalTestFile.option_filec                 C   s   t j| j}d|| jgS )Nr9   )ospathbasenamerQ   joinrR   )r/   packager   r   r   rB      s   zFunctionalTestFile.modulec                 C   s   | j dddS )Nz.txtF)check_existsrc   r.   r   r   r   expected_output   s   z"FunctionalTestFile.expected_outputc                 C   rH   )NrO   rc   r.   r   r   r   source   re   zFunctionalTestFile.sourceTc                 C   s.   t j| j| j| }|rt j|r|S tr4   )rf   rg   ri   rQ   rR   existsr   )r/   extrk   r!   r   r   r   rd      s   zFunctionalTestFile._file_typeN)T)r   r   r   __doc__r=   r^   rW   rT   propertyr\   rB   rl   rm   rd   r   r   r   r   rG   l   s"    



rG   )><z>=z<=c                 C   s   dd t | dD S )Nc                 S   s   g | ]}t |qS r   )r   r,   )r5   r+   r   r   r   
<listcomp>       z)parse_expected_output.<locals>.<listcomp>r   )csvreader)streamr   r   r   parse_expected_output   s   ry   c           	      C   s   t  }t| D ]c\}}t|}|du rq|d}|du r$|d }n|ds.|dr7|d t| }nt|}|d}|d}|rTt|}t	| t
j|sTq|dd	D ]}||| f  d7  < q\q|S )
zParses a file and get expected messages.

    :param stream: File-like input stream.
    :returns: A dict mapping line,msg-symbol tuples to the count on this line.
    Nr   r'   +-versionopZmsgsrI   )collectionsCounter	enumerate_EXPECTED_REsearchgroup
startswithr*   r=   
_OPERATORSr   r   r;   strip)	rx   r?   ir   matchr|   r}   requiredZmsg_idr   r   r   get_expected_messages   s*   




r   c                 C   sZ   |   }|| i }tt|D ]\}}|dkr(|| |dk r(| ||< q||fS )a  Takes two multisets and compares them.

    A multiset is a dict with the cardinality of the key as the value.

    :param left_op: The expected entries.
    :param right_op: Actual entries.

    :returns: The two multisets of missing and unexpected messages.
    r   )copysubtractlistsix	iteritemspop)Zleft_opZright_opmissing
unexpectedkeyra   r   r   r   multiset_difference   s   



r   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )LintModuleTestNc                 C   sx   t  }t | _| j| d| jj_t| j | j	d z| j
|j | j  W n	 ty6   Y nw || _d S )Nr   I)r>   r   ZPyLinter_linterZset_reporterconfig
persistentr   Z
initializedisableZread_config_filer\   Zload_config_filer   
_test_file)r/   	test_fileZtest_reporterr   r   r   rW      s   


zLintModuleTest.__init__c              	   C   s   |   rtdtjdd f  g }| jjd D ]}zt| W q t	y0   |
| Y qw |r>tdd|f  | jjd rddd	 | jjd dD }t }||v rftd
|f  d S d S d S )NzTest cannot run with Python %s. r   rN   zRequires %s to be present.rI   rP   c                 S   s   g | ]}|  qS r   )r   )r5   itemr   r   r   rt     s    z(LintModuleTest.setUp.<locals>.<listcomp>z-Test cannot run with Python implementation %r)!_should_be_skipped_due_to_versionpytestskipr   r|   r;   r   rS   
__import__ImportErrorr@   ri   platformpython_implementation)r/   r   reqZimplementationsimplementationr   r   r   setUp   s0   zLintModuleTest.setUpc                 C   s$   t j| jjd k pt j| jjd kS )NrL   rM   )r   r   r   rS   r.   r   r   r   r     s   z0LintModuleTest._should_be_skipped_due_to_versionc                 C   s   d| j j| jj| jjf S )Nz
%s (%s.%s))r   rR   	__class__r   r   r.   r   r   r   __str__  s   zLintModuleTest.__str__c                 C   s   t | jjS r4   )openr   rl   r.   r   r   r   _open_expected_file  s   z"LintModuleTest._open_expected_filec                 C   sH   | j jdkrt| j jS d| j jv rtj| j jddS tj| j jddS )NZinvalid_encoded_datalatin1)encodingutf8)r   rR   r   rm   ior.   r   r   r   _open_source_file  s
   z LintModuleTest._open_source_filec                 C   s~   |   }t|}W d    n1 sw   Y  |r9|  }t|}W d    ||fS 1 s0w   Y  ||fS g }||fS r4   )r   r   r   ry   )r/   fobjZexpected_msgsZexpected_output_linesr   r   r   _get_expected  s   




zLintModuleTest._get_expectedc                 C   s`   | j jj}|jdd d t }g }|D ]}||j|jf  d7  < |t	
| q||fS )Nc                 S   s   | j | j| jfS r4   )r   r   r   mr   r   r   rK   +  s    z.LintModuleTest._get_received.<locals>.<lambda>r   r'   )r   reporterr?   sortr~   r   r   r   r@   r   r$   )r/   r?   Zreceived_msgsZreceived_output_linesr   r   r   r   _get_received)  s   
zLintModuleTest._get_receivedc                 C   s   | j | jjg |  \}}|  \}}||krUd| jj g}t||\}}|r:|d |	dd t
|D  |rM|d |	dd t
|D  td| | ||| d S )NzWrong results for file "%s":z
Expected in testdata:c                 s       | ]}d | V  qdS z %3d: %sNr   r5   r   r   r   r   r7   ?  r8   z*LintModuleTest._runTest.<locals>.<genexpr>z
Unexpected in testdata:c                 s   r   r   r   r   r   r   r   r7   B  r8   r   )r   checkr   rB   r   r   rR   r   r@   extendsortedr   Zfailri   _check_output_text)r/   expected_messagesZexpected_textZreceived_messagesZreceived_textr   r   r   r   r   r   _runTest3  s    

zLintModuleTest._runTestc                 C   sF   g g }}|D ]}|d |d f|v r| | q| | q||fS )Nr'   r   )r@   )r/   r   linesemittedZomittedr   r   r   r   _split_linesF  s   
zLintModuleTest._split_linesc                 C   s$   |  ||d |ksJ | jjd S )Nr   )r   r   rR   )r/   r   expected_linesreceived_linesr   r   r   r   O  s
   z!LintModuleTest._check_output_text)r   r   r   ZmaxDiffrW   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    
	r   c                       s$   e Zd Z fddZdd Z  ZS )LintModuleOutputUpdatec                    s,   zt t|  W S  ty   t  Y S w r4   )superr   r   IOErrorr   StringIOr.   r   r   r   r   V  s
   z*LintModuleOutputUpdate._open_expected_filec           	      C   s   |sd S |  ||\}}||krJ|| |jdd d t| jjd}tj|dd}|D ]	}||	  q.W d    d S 1 sCw   Y  d S d S )Nc                 S   s   | d | d | d fS )Nr'   r   r   r   r   r   r   r   rK   c  ru   z;LintModuleOutputUpdate._check_output_text.<locals>.<lambda>r   wr   )dialect)
r   r   r   r   r   rl   rv   writerwriterowr0   )	r/   r   r   r   r   	remainingr   r   r   r   r   r   r   \  s   
"z)LintModuleOutputUpdate._check_output_text)r   r   r   r   r   __classcell__r   r   r   r   r   U  s    r   c                  C   sX   t jt jt jtd} g }t | D ]}|dkr)|dr)|t	| | q|S )NZ
functionalz__init__.pyrO   )
rf   rg   ri   dirnameabspath__file__listdirendswithr@   rG   )Z	input_dirZsuitefnamer   r   r   	get_testsi  s   r   c                 C   s   g | ]}|j qS r   )rR   )r5   tr   r   r   rt   t  s    rt   r   )idsc                 C   s(   t rt| nt| }|  |  d S r4   )UPDATEr   r   r   r   )r   ZLintTestr   r   r   test_functionalw  s   r   __main__z-uT)6rp   rv   r~   r   operatorrf   rer   r   r   Z	six.movesr   r   Zpylintr   r   r   r   excelr   register_dialect	Exceptionr   r   
namedtupler   Z_MESSAGEcompiler   r=   BaseReporterr>   r3   rG   gtltgeler   ry   r   r   r   r   r   ZTESTSZTESTS_NAMESmarkZparametrizer   r   argvremovemainr   r   r   r   <module>   sj   	;p


