o
    bR                     @   s   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mZm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T d dlmZ d dlZd dlmZ eeeZe jd	d
 Ze jdd ZG dd deZG dd deZdS )    N)joindirnameabspath)StringIO)Run)BaseReporter)*)JSONReporter)utilsc                 c   s>    |  t _t _zd V  W t jt _t jt _d S t jt _t jt _w N)sysstderrstdout
__stderr__
__stdout__out r   T/var/www/secure340b-portal/env/lib/python3.10/site-packages/pylint/test/test_self.py_patch_streams,   s   
r   c              	   c   sd    d}t j|}| t j|< zd V  W t j| |r"|t j|< d S d S t j| |r1|t j|< w )NLC_CTYPE)osenvirongetpop)Zlc_ctypeZlc_ctype_envZoriginal_lctyper   r   r   _configure_lc_ctype6   s   
r   c                   @   sR   e Zd Zdd Zdd Zdd Zdd Zed	d
 Zedd Z	e	j
dd Z	dS )MultiReporterc                 C   s   || _ t tj | _d S r   )
_reportersr   getcwdsepZpath_strip_prefixself	reportersr   r   r   __init__D   s   zMultiReporter.__init__c                 O   s    | j D ]
}|j|i | qd S r   )r   on_set_current_module)r!   argskwargsrepr   r   r   r$   H   s   
z#MultiReporter.on_set_current_modulec                 C   s   | j D ]}|| qd S r   )r   handle_message)r!   msgr'   r   r   r   r(   L   s   
zMultiReporter.handle_messagec                 C   s   d S r   r   )r!   Zlayoutr   r   r   display_reportsP   s   zMultiReporter.display_reportsc                 C   s   | j d jS )Nr   )r   r   r!   r   r   r   r   S   s   zMultiReporter.outc                 C   s   | j S r   )_linterr+   r   r   r   linterW   s   zMultiReporter.linterc                 C   s   || _ | jD ]}||_qd S r   )r,   r   r-   )r!   valuer'   r   r   r   r-   [   s   
N)__name__
__module____qualname__r#   r$   r(   r*   propertyr   r-   setterr   r   r   r   r   C   s    

r   c                   @   s  e Zd Zd\ddZd]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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ejjejd. d/kd0d1d2d3 Zejjejd. d/kd0d1d4d5 Zd6d7 Z d8d9 Z!d:d; Z"d<d= Z#d>d? Z$d@dA Z%dBdC Z&dDdE Z'dFdG Z(dHdI Z)dJdK Z*dLdM Z+dNdO Z,dPdQ Z-dRdS Z.dTdU Z/dVdW Z0ejjej1dXkdYd1dZd[ Z2dS )^	TestRunTCNc                 C   sz   |d u rt  }| j|||d}|r|j }nt|dr!| }nd }d||f }|d ur3d||f }||ks;J |d S )N)reporterr   getvaluez!expected output status %s, got %sz%s. Below pylint output: 
%s)r   _run_pylintr   r6   hasattr)r!   r%   r5   r   codeZpylint_codeoutputr)   r   r   r   _runtestc   s   

zTestRunTC._runtestc              
   C   s   |dg }t |@ tt'}t  td t||d W d    n1 s*w   Y  W d    n1 s9w   Y  |jj	W  d    S 1 sLw   Y  d S )Nz--persistent=noignore)r5   )
r   pytestraises
SystemExitwarningscatch_warningssimplefilterr   r.   r9   )r!   r%   r   r5   cmr   r   r   r7   r   s   



$zTestRunTC._run_pylintc                 C   s   t jdd|ddt jdS )z:Remove version-specific tox parent directories from paths.z^py.+/site-packages/ \/)flags)resubreplace	MULTILINE)r!   r:   r   r   r   _clean_paths{   s   zTestRunTC._clean_pathsc                 C   s:   t  }| j||d | | }| | v sJ d S )Nr   )r   r7   rL   r6   strip)r!   r%   expected_outputr   actual_outputr   r   r   _test_output   s   zTestRunTC._test_outputc                 C   s   | j dgtt dd dS )Make pylint check itself.zpylint.__pkginfo__r   r5   r9   Nr;   TextReporterr   r+   r   r   r   test_pkginfo   s   zTestRunTC.test_pkginfoc                 C   s<   t t tt tt g}| jttdgt|dd dS )rQ   functional/arguments.py   rR   N)rT   r   ZColorizedTextReporterr	   r;   r   HEREr   r    r   r   r   test_all   s   

zTestRunTC.test_allc                 C      | j ttddgdd d S )NinputZnoextr   r9   r;   r   rX   r+   r   r   r   test_no_ext_file      zTestRunTC.test_no_ext_filec                 C   rZ   )Nr[   z ignore_except_pass_by_default.pyr   r\   r]   r+   r   r   r   test_w0704_ignored   r_   zTestRunTC.test_w0704_ignoredc                 C   s   | j dttddgdd d S )Nz--exit-zeroregrtest_datasyntax_error.pyr   r\   r]   r+   r   r   r   test_exit_zero   s   
zTestRunTC.test_exit_zeroc                 C   s   | j dgdd d S )N--generate-rcfiler   r\   r;   r+   r   r   r   test_generate_config_option   s   z%TestRunTC.test_generate_config_optionc                 C   sP   t  }t  }| jdgd|d | jdgd|d | }| }||ks&J d S )Nrd   r   )r9   r   )r   r;   r6   )r!   Zout1Zout2Zoutput1Zoutput2r   r   r   !test_generate_config_option_order   s   z+TestRunTC.test_generate_config_option_orderc                 C   st   t  }| jddg|d | }td|}t || d  }t }|| t	
|dd}d|v s8J d S )Nrd   z	--rcfile=r   z\[MASTERzMESSAGES CONTROLdisablezsuppressed-message)r   r7   r6   rH   searchstartconfigparserRawConfigParser	read_filer
   Z_splitstripr   )r!   r   r:   Zmasterparsermessagesr   r   r   +test_generate_config_disable_symbolic_names   s   
z5TestRunTC.test_generate_config_disable_symbolic_namesc                 C   s.   t  }| jdg|d | }d|vsJ d S )Nrd   r   Zprofile)r   r7   r6   )r!   r   r:   r   r   r   (test_generate_rcfile_no_obsolete_methods   s   z2TestRunTC.test_generate_rcfile_no_obsolete_methodsc                 C   sV   t  }tt}| jdg|d W d    n1 sw   Y  dt|jks)J d S )Nz--rcfile=/tmp/norcfile.txtr   z0The config file /tmp/norcfile.txt doesn't exist!)r   r=   r>   IOErrorr7   strr.   )r!   r   excinfor   r   r   test_inexisting_rcfile   s
   z TestRunTC.test_inexisting_rcfilec                 C      | j ddgdd d S )N
--help-msgZW0101r   r\   re   r+   r   r   r   test_help_message_option      z"TestRunTC.test_help_message_optionc                 C   rv   )Nrw   ZWX101r   r\   re   r+   r   r   r   test_error_help_message_option   ry   z(TestRunTC.test_error_help_message_optionc                 C   s   | j g dd d S )N    r\   re   r+   r   r   r   test_error_missing_arguments   s   z&TestRunTC.test_error_missing_argumentsc                 C   sF   t jdk r
t }nt }|jdu sJ | jttddg|dd dS )z<test redirection of stdout with non ascii caracters
        )   r   Nz#regrtest_data/no_stdout_encoding.py--enable=all   )r   r9   )	r   version_infotempfileTemporaryFiler   encodingr;   r   rX   )r!   Zstrior   r   r   test_no_out_encoding   s   


zTestRunTC.test_no_out_encodingc                 C   s$   | j dttdttdgdd d S )N-j 2rV   rW   r\   r]   r+   r   r   r   test_parallel_execution   s   
z!TestRunTC.test_parallel_executionc                 C   s   | j g ddd d S )N)r   Znot_hereZnot_here_too   r\   re   r+   r   r   r   )test_parallel_execution_missing_arguments   ry   z3TestRunTC.test_parallel_execution_missing_argumentsc                 C   s"   d}| j ttdddg|d d S )Nr   
functionalunpacked_exceptions.py--py3kr\   r]   r!   Zrc_coder   r   r   test_py3k_option   s   
zTestRunTC.test_py3k_optionc                 C   s$   d}| j ttddddg|d d S )Nr   r   r   r   r   r\   r]   r   r   r   r   test_py3k_jobs_option   s
   
zTestRunTC.test_py3k_jobs_optionr   rW   zRequires the --py3k flag.)reasonc                 C   sd   t tdd}td}| j|ddg|d td}| j|dddg|d | j|dddg|d d S )	Nra   zpy3k_error_flag.pyzZ
        ************* Module py3k_error_flag
        Explicit return in __init__
        -E--msg-template='{msg}'rN   z|
        ************* Module py3k_error_flag
        Use raise ErrorClass(args) instead of raise ErrorClass, args.
        r   r   rX   textwrapdedentrP   r!   moduleexpectedr   r   r   &test_py3k_commutative_with_errors_only  s    

z0TestRunTC.test_py3k_commutative_with_errors_onlyc                 C   s   t tdd}t tdd}|ddg}td}| j|dg |d td	}| j|dd
|g |d td}| j|ddd
|g |d | j|ddd
|g |d d S )Nra   zpy3k_errors_and_warnings.pyzpy3k-disabled.rcr   z--reports=na  
        ************* Module py3k_errors_and_warnings
        import missing `from __future__ import absolute_import`
        Use raise ErrorClass(args) instead of raise ErrorClass, args.
        Calling a dict.iter*() method
        print statement used
        r   r   z
        ************* Module py3k_errors_and_warnings
        Use raise ErrorClass(args) instead of raise ErrorClass, args.
        Calling a dict.iter*() method
        print statement used
        z--rcfilez
        ************* Module py3k_errors_and_warnings
        Use raise ErrorClass(args) instead of raise ErrorClass, args.
        print statement used
        r   r   )r!   r   Zrcfilecmdr   r   r   r   )test_py3k_commutative_with_config_disable   s,   
	
z3TestRunTC.test_py3k_commutative_with_config_disablec                 C   s   d}| j ddg|d d S )Nzno such option: --load-plugin.z--load-pluginr   )rP   )r!   r   r   r   r   $test_abbreviations_are_not_supportedL  s   z.TestRunTC.test_abbreviations_are_not_supportedc                 C   s0   t tdd}td}| j|dddg|d d S )Ndataclientmodule_test.pyaz  
        ************* Module data.clientmodule_test
        pylint/test/data/clientmodule_test.py:10:8: W0612: Unused variable 'local_variable' (unused-variable)
        pylint/test/data/clientmodule_test.py:18:4: C0111: Missing method docstring (missing-docstring)
        pylint/test/data/clientmodule_test.py:22:0: C0111: Missing class docstring (missing-docstring)
        --disable=allr~   -rnr   r   r   r   r   r   test_enable_all_worksP  s   
zTestRunTC.test_enable_all_worksc                 C   s   t d}ttdd}ttdd}||ddddg}t }| j||d	 | |  }d
}||v r:|t	|d  }|
drH||dd  }| | ksRJ d S )Nz
        ************* Module wrong_import_position
        pylint/test/regrtest_data/wrong_import_position.py:11:0: C0413: Import "import os" should be placed at the top of the module (wrong-import-position)
        ra   zimport_something.pyzwrong_import_position.pyr   z--enable=wrong-import-positionr   z-snr   z1No config file found, using default configurationzUsing config file 
)r   r   r   rX   r   r7   rL   r6   rM   len
startswithfind)r!   rN   Zmodule1Zmodule2r%   r   rO   	to_remover   r   r   /test_wrong_import_position_when_others_disabled^  s*   
z9TestRunTC.test_wrong_import_position_when_others_disabledc                 C   s(   d}t tdd}| j|ddg|d d S )N$Your code has been rated at 10.00/10ra   dummyz--disable=locally-disabledr   r   r   rX   rP   )r!   r   packager   r   r   5test_import_itself_not_accounted_for_relative_imports|  s
   

z?TestRunTC.test_import_itself_not_accounted_for_relative_importsc                 C   &   d}t tdd}| j|dg|d d S )Nzindent string can't be emptyr   r   z--indent-string=r   r   )r!   r   r   r   r   r    test_reject_empty_indent_strings  s   z*TestRunTC.test_reject_empty_indent_stringsc                 C   s   t  }ttdd}| j|gdt|d t| }t|t	s"J t
|dks*J t|d ts3J ddddd	d
d}|d }| D ]\}}||v sNJ || |ksVJ qDd|d  v saJ d S )Nra   rb   rW   r9   r5   r   r   rD   errorzsyntax-errorsyntax_errorobjcolumnlinetypesymbolr   zinvalid syntaxmessage)r   r   rX   r;   r	   jsonloadsr6   
isinstancelistr   dictitemslowerr!   r   r   r:   r   r   keyr.   r   r   r   +test_json_report_when_file_has_syntax_error  s&   z5TestRunTC.test_json_report_when_file_has_syntax_errorc                 C   s   t  }ttdd}| j|gdt|d t| }t|t	s"J t
|dks*J t|d ts3J ddddd|d}|d }| D ]\}}||v sNJ || |ksVJ qD|d	 d
s`J d S )Nra   ztotally_missing.pyr   r   r   rD   fatalr   r   zNo module named)r   r   rX   r;   r	   r   r   r6   r   r   r   r   r   r   r   r   r   r   %test_json_report_when_file_is_missing  s&   z/TestRunTC.test_json_report_when_file_is_missingc                 C   s   t  }ttdd}| j|gdt|d t| }t|t	s"J t
|dks*J t|d ts3J dddd	d
ddd}|d }| D ]\}}||v sOJ || |ksWJ qEd S )Nra   zunused_variable.py   r   r   r   zunused-variableZunused_variablezUnused variable 'variable'ZW0612warning)r   r   r   r   z
message-idr   r   )r   r   rX   r;   r	   r   r   r6   r   r   r   r   r   r   r   r   r   'test_json_report_does_not_escape_quotes  s(   	z1TestRunTC.test_json_report_does_not_escape_quotesc                 C   $   d}t tdd}| j|g|d d S )Nr   ra   meta.pyr   r   r!   r   pathr   r   r   -test_information_category_disabled_by_default     z7TestRunTC.test_information_category_disabled_by_defaultc                 C   s,   t d}ttdd}| j|dg|d d S )N
        ************* Module application_crash
        pylint/test/regrtest_data/application_crash.py:1:6: E0602: Undefined variable 'something_undefined' (undefined-variable)
        ra   application_crash.pyr   r   )r   r   r   rX   rP   r!   rN   r   r   r   r   test_error_mode_shows_no_score  s
   z(TestRunTC.test_error_mode_shows_no_scorec                 C   r   )NzYour code has been rated at ra   r   r   r   r   r   r   r   &test_evaluation_score_shown_by_default  r   z0TestRunTC.test_evaluation_score_shown_by_defaultc                 C   r   )NzYour code has been rated atra   r   z--confidence=HIGH,INFERENCEr   r   r   r   r   r   test_confidence_levels  s
   
z TestRunTC.test_confidence_levelsc                 C   s8   t tdd}t tdd}d}| j|d| dg|d d S )Nra   r   z	.pylintrcr   --rcfile=%sr   r   r   r!   r   config_pathr   r   r   r   test_bom_marker  s   
zTestRunTC.test_bom_markerc                 C   sh   t tdd}tj| t tdd}d}| jd| dg|d d}| jd| d	g|d tj| d S )
Nra   Zdummy_pluginzdummy_plugin.rcz:dummy-message-01 (I9061): *Dummy short desc 01*
  Dummy long desc This message belongs to the dummy_plugin checker.

:dummy-message-02 (I9060): *Dummy short desc 02*
  Dummy long desc This message belongs to the dummy_plugin checker.r   z,--help-msg=dummy-message-01,dummy-message-02r   zl[DUMMY_PLUGIN]

# Dummy option 1
dummy_option_1=dummy value 1

# Dummy option 2
dummy_option_2=dummy value 2rd   )r   rX   r   r   appendrP   remove)r!   Zdummy_plugin_pathr   r   r   r   r   &test_pylintrc_plugin_duplicate_options  s"   z0TestRunTC.test_pylintrc_plugin_duplicate_optionsc                 C   s>   t tdd}t tdd}td}| j|d| dg|d d S )Nra   ztest_pylintrc_comments.pyZcomments_pylintrca  
        ************* Module test_pylintrc_comments
        pylint/test/regrtest_data/test_pylintrc_comments.py:2:0: W0311: Bad indentation. Found 1 spaces, expected 4 (bad-indentation)
        pylint/test/regrtest_data/test_pylintrc_comments.py:1:0: C0111: Missing module docstring (missing-docstring)
        pylint/test/regrtest_data/test_pylintrc_comments.py:1:0: C0111: Missing function docstring (missing-docstring)
        r   r   r   r   r   r   r   r    test_pylintrc_comments_in_values  s   
z*TestRunTC.test_pylintrc_comments_in_valuesc                 C   s   | j dgtt dd d S )Nz--ignore-patterns=ar{   rR   rS   r+   r   r   r   ,test_no_crash_with_formatting_regex_defaults  s   
z6TestRunTC.test_no_crash_with_formatting_regex_defaultsc                 C   sV   t d}ttdd}td | j|dg|d W d    d S 1 s$w   Y  d S )Nr   ra   r   zUTF-8r   r   )r   r   r   rX   r   rP   r   r   r   r   2test_getdefaultencoding_crashes_with_lc_ctype_utf8  s   
"z<TestRunTC.test_getdefaultencoding_crashes_with_lc_ctype_utf8win32zonly occurs on *nixc              	   C   s   d}t t  }t||}z:t| t|d}|d W d    n1 s(w   Y  | j|dgtt |d W t| t	| d S t| t	| w )Nztest_target.pywza,b = object()z--output-format=parseabler   )
rX   r   r   r   makedirsopenwriterP   r   
removedirs)r!   	file_nameZ	fake_pathr   Ztest_targetr   r   r   test_parseable_file_path'  s    



z"TestRunTC.test_parseable_file_path)NNNr   )3r/   r0   r1   r;   r7   rL   rP   rU   rY   r^   r`   rc   rf   rg   rp   rq   ru   rx   rz   r|   r   r   r   r   r   r=   markZskipifr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   platformr   r   r   r   r   r4   b   s\    

		


+
r4   ) 
contextlibr   rH   r   r   os.pathr   r   r   r   r   rk   ior   Zpylint.lintr   Zpylint.reportersr   Zpylint.reporters.textZpylint.reporters.jsonr	   r=   Zpylintr
   __file__rX   contextmanagerr   r   r   objectr4   r   r   r   r   <module>   s.   
	
