
    e/2                    R   d dl m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m	Z	 d dl
mZ d dlmZ d dl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 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( eee)e*f      Z+ ee,      jZ                  dz  Z. G d d      Z/y)    )annotationsN)Counter)StringIO)Path)TextIOTuple)Config)checkers)_config_initialization)IS_PYPY)PyLinter)Message)_EXPECTED_RE
_OPERATORSUPDATE_OPTION)FunctionalTestFileNoFileErrorparse_python_version)
OutputLine)FunctionalTestReportertesting_pylintrcc                      e Zd ZdZ	 d	 	 	 	 	 ddZddZddZddZddZe	dd       Z
e		 	 	 	 	 	 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y)LintModuleTestNc                   t               }t               | _        d| j                  j                  _        t        j                  | j                         t        }	 |j                  }| j                  j                  d       | j                  j                  d       | j                  j                  d       || _        	 |j                  g}|r|j                  d      r| j                         5 }| j                  |      D ch c]  }|d   	 }}|j!                  d       |j!                  d	       |j!                  d
       d d d        |j#                  dddj%                         g       | j                  j&                  j)                  dt*        d       | j                  j&                  j)                  dt*        d       | j                  j&                  j)                  dt*        d       | j                  j&                  j)                  dd g        | j                  j&                  j)                  dd g        | j                  j&                  j)                  dd g        | j                  j&                  j)                  dd       t-        | j                  |||       t.        j0                  | j                  j                  j2                  k\  | _        | j4                  rt6        rt.        j0                  dk\  | _        || _        y # t        $ r Y mw xY w# t        $ r dg}Y kw xY wc c}w # 1 sw Y   xY w) Nr   zsuppressed-messagezlocally-disabledzuseless-suppression minimal_messages_config   zastroid-errorfatalzsyntax-errorz--disable=allz	--enable=,z--min_pyver)      )typedefaultz--max_pyver)   r   z--min_pyver_end_position)      z
--requiresc                f    | j                  d      D cg c]  }|j                          c}S c c}w Nr   splitstripsis     C/usr/lib/python3/dist-packages/pylint/testutils/lint_module_test.py<lambda>z)LintModuleTest.__init__.<locals>.<lambda>X   s"    QWWS\)J!''))J )J   .z--except_implementationsc                f    | j                  d      D cg c]  }|j                          c}S c c}w r(   r)   r,   s     r/   r0   z)LintModuleTest.__init__.<locals>.<lambda>\   "    qwws|<!AGGI< <r1   z--exclude_platformsc                f    | j                  d      D cg c]  }|j                          c}S c c}w r(   r)   r,   s     r/   r0   z)LintModuleTest.__init__.<locals>.<lambda>a   r3   r1   z&--exclude_from_minimal_messages_configF)r#   )	args_listconfig_filereporter)r%   	   )r   r   _linterconfig
persistentr
   
initializePYLINTRCoption_filedisabler   
_test_filesource	getoption_open_source_fileget_expected_messagesaddextendjoin_arg_parseradd_argumentr   r   sysversion_infomin_pyver_end_position_check_end_positionr   _config)	self	test_filer:   _test_reporterrc_fileargsfmsgmessages_to_enables	            r/   __init__zLintModuleTest.__init__,   s    01z)*&DLL) '	++GLL  !56LL  !34LL  !67 $	$$%D f&&'@A'') 7Q8<8R8RST8U%Vc!f%V"%V"&&7"&&w/"&&~67 KKIchh?Q6R5S*TUV 	  -- 4f 	. 	
 	  -- 4f 	. 	
 	  --&-A6 	. 	
 	  --JTV 	. 	
 	  --&< 	. 	

 	  --!< 	. 	

 	  --4e 	. 	
 	LLDg	

  3 3 J JJ 	  ##'*'7'76'AD$q  		  	4D	
 &W7 7sC   AL  L) 0ML<5M	L&%L&)L98L9<MMc                   | j                         r;t        j                  dt        j                  j                  dd      d    d       g }| j                  j                  j                  D ]  }	 t        |        |r(t        j                  ddj                  |       d	       | j                  j                  j                  }|rBt        j                         |v r,d
}t        j                  |t        j                         z         | j                  j                  j                   }|rFt        j                  j#                         |v r&t        j                  dt        j                         | j$                  rS| j$                  j'                  d      r7| j                  j                  j(                  rt        j                  d       y y y y # t        $ r |j                  |       Y w xY w)NzTest cannot run with Python  r   )maxsplitr   .z	Requires r   z to be present.z-Test cannot run with Python implementation %rzTest cannot run on platform r   z,Test excluded from --minimal-messages-config)!_should_be_skipped_due_to_versionpytestskiprJ   versionr*   r9   r:   requires
__import__ImportErrorappendrG   except_implementationsplatformpython_implementationexclude_platformslowerrN   rB   $exclude_from_minimal_messages_config)rO   missingrequirementrd   rU   excluded_platformss         r/   setUpzLintModuleTest.setUpu   s   113KK.s{{/@/@q/@/QRS/T.UUVW <<..77 	,K,;'	,
 KK)CHHW$5#6oFG!%!4!4!K!K!--/3IIEC("@"@"BBC!\\00BB||!!#'99:3<<:JKLLL&&'@A##HHKKFG I B   ,{+,s   2GG32G3c                $    | j                          y N)_runTestrO   s    r/   runTestzLintModuleTest.runTest   s        c                    t         j                  | j                  j                  j                  k  xs1 t         j                  | j                  j                  j
                  kD  S ro   )rJ   rK   r9   r:   	min_pyver	max_pyverrq   s    r/   r\   z0LintModuleTest._should_be_skipped_due_to_version   sI    t||22<<< @$,,"5"5"?"??	
rs   c                    | j                   j                   d| j                  j                   d| j                  j                   dS )Nz (r[   ))r@   base	__class__
__module____name__rq   s    r/   __str__zLintModuleTest.__str__   s=    //&&'r$..*C*C)DAdnnF]F]E^^_``rs   c                B   t               }t        |       D ]  \  }}t        j                  |      }||j	                  d      }||dz   }n?|j                  d      s|j                  d      r|dz   t        |      z   }nt        |      }|j	                  d      }|j	                  d      }|r)t        |      }t        |   t        j                  |      s|j	                  d      j                  d      D ]  }	|||	j                         fxx   dz  cc<   !  |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
        liner   +-r_   opmsgsr   )r   	enumerater   searchgroup
startswithintr   r   rJ   rK   r*   r+   )
streammessagesr.   r   matchlinenor_   r   requiredmsg_ids
             r/   rD   z$LintModuleTest.get_expected_messages   s    $+9 ( 	6GAt ''-E};;v&D|Q%)=QT*Tkk),GT"B/8!"~c&6&6A++f-33C8 6/0A506'	6* rs   c                    | j                         }|j                  |       i }t        |j                               D ](  \  }}|dk  s|j	                  |       |dk  s#| ||<   * ||fS )z{Takes 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)expected_entriesactual_entriesrj   
unexpectedkeyvalues         r/   multiset_differencez"LintModuleTest.multiset_difference   su     #'')(
w}}/ 	-JCzC 19',fJsO		-
 
""rs   c                z    	 t        | j                  j                  d      S # t        $ r t	        d      cY S w xY w)Nutf-8encodingr   )openr@   expected_outputFileNotFoundErrorr   rq   s    r/   _open_expected_filez"LintModuleTest._open_expected_file   s7    	 77'JJ  	 B<	 s    # ::c                *   | j                   j                  dk(  r!t        | j                   j                  d      S d| j                   j                  v r!t        | j                   j                  d      S t        | j                   j                  d      S )Ninvalid_encoded_datar   r   latin1utf8)r@   ry   r   rA   rq   s    r/   rC   z LintModuleTest._open_source_file   sl    ??#99..AAt+++..BBDOO**V<<rs   c                j   | j                         5 }| j                  |      }d d d        s
t               }| j                         5 }t	        j
                  |d      D cg c]"  }t        j                  || j                        $ }}d d d        ||fS # 1 sw Y   sxY wc c}w # 1 sw Y   |fS xY w)Ntest)	rC   rD   r   r   csvreaderr   from_csvrM   )rO   rT   expected_msgsrowexpected_output_liness        r/   _get_expectedzLintModuleTest._get_expected   s    ##% 	: 66q9M	:#IM%%' 	1 ::a0% ##C)A)AB%! %	
 333	: 	:
%	
 333s)   BB& 'B!B&B!B&&B2c                   | j                   j                  j                  }|j                  d        t	               }g }|D ]x  }|j
                  dk7  sJ d|j                   d       ||j                  |j
                  fxx   dz  cc<   |j                  t        j                  || j                               z ||fS )Nc                H    | j                   | j                  | j                  fS ro   )r   symbolrU   )ms    r/   r0   z,LintModuleTest._get_actual.<locals>.<lambda>   s    QVVQXXquu$= rs   r   r   z#Pylint analysis failed because of ''r   )r9   r7   r   sortr   r   rU   r   rc   r   from_msgrM   )rO   r   received_msgsreceived_output_linesrU   s        r/   _get_actualzLintModuleTest._get_actual   s    "&,,"7"7"@"@=>(/	 " 	C

g%@4SWWIQ?@%#((CJJ./14/!((##C)A)AB	 333rs   c                   d}| j                   j                  g}| j                  j                  |       | j	                         \  }}| j                         \  }}||k(  sJ | j                  |||             | j                  |||       y )NT)r@   rA   r9   checkr   r   error_msg_for_unequal_messages_check_output_text)rO   __tracebackhide__modules_to_checkexpected_messagesr   actual_messagesactual_outputs          r/   rp   zLintModuleTest._runTest   s      OO223+,-1-?-?-A*?)-)9)9);&0	
...
	
0 	 1?MRrs   c                   dt        | j                  j                        j                   dg}| j	                  ||      \  }}|r2|j                  d       |j                  d t        |      D               |r2|j                  d       |j                  d t        |      D               dj                  |      }| j                  r>| j                  j                  d      d	kD  r |d
z  }|dj                  d |D              z  }|S )NzWrong message(s) raised for "":z
Expected in testdata:c              3  :   K   | ]  }d |d   dd|d      ywrY   r   3z: r   N .0rU   s     r/   	<genexpr>z@LintModuleTest.error_msg_for_unequal_messages.<locals>.<genexpr>  s'     KC3q6!*Bs1vh/K   z
Unexpected in testdata:c              3  :   K   | ]  }d |d   dd|d      ywr   r   r   s     r/   r   z@LintModuleTest.error_msg_for_unequal_messages.<locals>.<genexpr>  s'     NC3q6!*Bs1vh/Nr   
verboser   z&

Actual pylint output for this file:
c              3  2   K   | ]  }t        |        y wro   )str)r   os     r/   r   z@LintModuleTest.error_msg_for_unequal_messages.<locals>.<genexpr>  s     "Aa3q6"As   )r   r@   rA   namer   rc   rF   sortedrG   rN   rB   )rO   r   r   r   rU   rj   r   	error_msgs           r/   r   z-LintModuleTest.error_msg_for_unequal_messages  s     /tDOO4J4J/K/P/P.QQSTU"66
 JJ01JJK6'?KKJJ23JJN6*;MNNIIcN	<<DLL229=ADDI"A="AAAIrs   c                |   t        |      t        |      z
  }t        |      t        |      z
  }dt        | j                  j                        j                   d}t        j                  d      }|r|dz  }t        ||      D ]
  }|| dz  } |r|dz  }t        ||      D ]
  }|| dz  } |dt         d	| j                  j                   d
z  }t               }t        j                  |d      }	t        ||      D ]!  }|	j                  |j                                # ||j                         z  }|S )NzWrong output for "r   r   z
- Missing lines:
r   r   z
- Unexpected lines:
zY
You can update the expected output automatically with:
'python tests/test_functional.py z -k "test_functional[z/]"'

Here's the update text in case you can't:
r   )dialect)setr   r@   r   r   operator
attrgetterr   r   ry   r   r   writerwriterowto_csvgetvalue)
rO   expected_linesreceived_linesrj   r   r   sort_by_line_numberr   expected_csvr   s
             r/   error_msg_for_unequal_outputz+LintModuleTest.error_msg_for_unequal_output  s[   
 n%N(;;(3~+>>
(doo.M.M)N)S)S(TTVW	&11(;//Iw,?@ )vR[(	)22Iz/BC )vR[(	)33@/ B$$(OO$8$8#9 :>>I $:LZZf=F~3FG /./..00Irs   c                <    ||k(  sJ | j                  ||             y)zlThis is a function because we want to be able to update the text in
        LintModuleOutputUpdate.
        N)r   )rO   _r   r   s       r/   r   z!LintModuleTest._check_output_text;  s+     -/ 	
1R1R]2
 	
/rs   ro   )rP   r   r:   zConfig | NonereturnNone)r   r   )r   bool)r   r   )r   r   r   MessageCounter)r   r   r   r   r   z1tuple[MessageCounter, dict[tuple[int, str], int]])r   r   )r   z'tuple[MessageCounter, list[OutputLine]])r   r   r   r   r   list[OutputLine]r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r|   r{   __qualname__maxDiffrW   rm   rr   r\   r}   staticmethodrD   r   r   rC   r   r   rp   r   r   r   r   rs   r/   r   r   )   s   G FJG+G5BG	GRH:
a  @ #(#&# 
;# #$ =
44S' * (	
 
,( ) 
	<

 *
 (	

 

rs   r   )0
__future__r   r   r   re   rJ   collectionsr   ior   pathlibr   typingCounterTyper   r   r]   _pytest.configr	   pylintr
   #pylint.config.config_initializationr   pylint.constantsr   pylint.lintr   pylint.message.messager   pylint.testutils.constantsr   r   r   %pylint.testutils.functional.test_filer   r   r   pylint.testutils.output_liner   #pylint.testutils.reporter_for_testsr   r   r   r   __file__parentr=   r   r   rs   r/   <module>r     s   
 # 
   
    )    !  F $   * N N 
 4 FU38_->  #55]
 ]
rs   