B
    -
x\R                 @   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 d t jt _t jt _X d S )N)sysstderrstdout
__stderr__
__stdout__)out r   4lib/python3.7/site-packages/pylint/test/test_self.py_patch_streams,   s
    
r   c          	   c   sF   d}t j|}| t j|< z
d V  W d t j| |r@|t j|< X d S )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 )N)
_reportersr   getcwdsepZpath_strip_prefix)self	reportersr   r   r   __init__D   s    zMultiReporter.__init__c             O   s    x| j D ]}|j|| qW d S )N)r   on_set_current_module)r   argskwargsrepr   r   r   r!   H   s    z#MultiReporter.on_set_current_modulec             C   s   x| j D ]}|| qW d S )N)r   handle_message)r   msgr$   r   r   r   r%   L   s    zMultiReporter.handle_messagec             C   s   d S )Nr   )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 )N)_linter)r   r   r   r   linterW   s    zMultiReporter.linterc             C   s    || _ x| jD ]
}||_qW d S )N)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 krt  }| j|||d}|r.|j }nt|drB| }nd }d||f }|d k	rfd||f }||ksvt|d S )N)reporterr   getvaluez!expected output status %s, got %sz%s. Below pylint output: 
%s)r   _run_pylintr   r2   hasattrAssertionError)r   r"   r1   r   codeZpylint_codeoutputr&   r   r   r   _runtestc   s    

zTestRunTC._runtestc             C   sf   |dg }t |J tt0}t  td t||d W d Q R X W d Q R X |jj	S Q R X d S )Nz--persistent=noignore)r1   )
r   pytestraises
SystemExitwarningscatch_warningssimplefilterr   r*   r6   )r   r"   r   r1   cmr   r   r   r3   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   r7   r   r   r   _clean_paths{   s    zTestRunTC._clean_pathsc             C   s:   t  }| j||d | | }| | ks6td S )N)r   )r   r3   rI   r2   stripr5   )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 )zMake pylint check itself.zpylint.__pkginfo__r   )r1   r6   N)r8   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 )zMake pylint check itself.zfunctional/arguments.py   )r1   r6   N)rN   r   ZColorizedTextReporterr	   r8   r   HEREr   )r   r   r   r   r   test_all   s    
zTestRunTC.test_allc             C   s   | j ttddgdd d S )NinputZnoextr   )r6   )r8   r   rQ   )r   r   r   r   test_no_ext_file   s    zTestRunTC.test_no_ext_filec             C   s   | j ttddgdd d S )NrS   z ignore_except_pass_by_default.pyr   )r6   )r8   r   rQ   )r   r   r   r   test_w0704_ignored   s    zTestRunTC.test_w0704_ignoredc             C   s   | j dttddgdd d S )Nz--exit-zeroregrtest_datazsyntax_error.pyr   )r6   )r8   r   rQ   )r   r   r   r   test_exit_zero   s    zTestRunTC.test_exit_zeroc             C   s   | j dgdd d S )Nz--generate-rcfiler   )r6   )r8   )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sLtd S )Nz--generate-rcfiler   )r6   r   )r   r8   r2   r5   )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|ksptd S )Nz--generate-rcfilez	--rcfile=)r   z\[MASTERzMESSAGES CONTROLdisablezsuppressed-message)r   r3   r2   rE   searchstartconfigparserZRawConfigParserZ	read_filer
   Z_splitstripr   r5   )r   r   r7   ZmasterparserZ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|ks*td S )Nz--generate-rcfile)r   Zprofile)r   r3   r2   r5   )r   r   r7   r   r   r   (test_generate_rcfile_no_obsolete_methods   s    z2TestRunTC.test_generate_rcfile_no_obsolete_methodsc          	   C   sB   t  }tt}| jdg|d W d Q R X dt|jks>td S )Nz--rcfile=/tmp/norcfile.txt)r   z0The config file /tmp/norcfile.txt doesn't exist!)r   r:   r;   IOErrorr3   strr*   r5   )r   r   excinfor   r   r   test_inexisting_rcfile   s    z TestRunTC.test_inexisting_rcfilec             C   s   | j ddgdd d S )Nz
--help-msgZW0101r   )r6   )r8   )r   r   r   r   test_help_message_option   s    z"TestRunTC.test_help_message_optionc             C   s   | j ddgdd d S )Nz
--help-msgZWX101r   )r6   )r8   )r   r   r   r   test_error_help_message_option   s    z(TestRunTC.test_error_help_message_optionc             C   s   | j g dd d S )N    )r6   )r8   )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ks(t| 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.pyz--enable=all   )r   r6   )
r   version_infotempfileZTemporaryFiler   encodingr5   r8   r   rQ   )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 )Nz-j 2zfunctional/arguments.pyrP   )r6   )r8   r   rQ   )r   r   r   r   test_parallel_execution   s
    
z!TestRunTC.test_parallel_executionc             C   s   | j dddgdd d S )Nz-j 2Znot_hereZnot_here_too   )r6   )r8   )r   r   r   r   )test_parallel_execution_missing_arguments   s    z3TestRunTC.test_parallel_execution_missing_argumentsc             C   s"   d}| j ttdddg|d d S )Nr   
functionalzunpacked_exceptions.pyz--py3k)r6   )r8   r   rQ   )r   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   rr   zunpacked_exceptions.pyz--py3kz-j 2)r6   )r8   r   rQ   )r   rs   r   r   r   test_py3k_jobs_option   s    zTestRunTC.test_py3k_jobs_optionr   rP   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 )	NrV   zpy3k_error_flag.pyzZ
        ************* Module py3k_error_flag
        Explicit return in __init__
        z-Ez--msg-template='{msg}')rK   z|
        ************* Module py3k_error_flag
        Use raise ErrorClass(args) instead of raise ErrorClass, args.
        z--py3k)r   rQ   textwrapdedentrM   )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 )NrV   zpy3k_errors_and_warnings.pyzpy3k-disabled.rcz--msg-template='{msg}'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
        z--py3k)rK   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
        z-E)r   rQ   rw   rx   rM   )r   ry   Zrcfilecmdrz   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-plugin)rK   )rM   )r   rz   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dataz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)
        z--disable=allz--enable=allz-rn)rK   )r   rQ   rw   rx   rM   )r   ry   rz   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
}||krt|t	|d  }|
dr||dd  }| | kst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)
        rV   zimport_something.pyzwrong_import_position.pyz--disable=allz--enable=wrong-import-positionz-rnz-sn)r   z1No config file found, using default configurationzUsing config file 
)rw   rx   r   rQ   r   r3   rI   r2   rJ   len
startswithfindr5   )r   rK   Zmodule1Zmodule2r"   r   rL   Z	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 )Nz$Your code has been rated at 10.00/10rV   Zdummyz--disable=locally-disabledz-rn)rK   )r   rQ   rM   )r   rz   packager   r   r   5test_import_itself_not_accounted_for_relative_imports|  s    z?TestRunTC.test_import_itself_not_accounted_for_relative_importsc             C   s&   d}t tdd}| j|dg|d d S )Nzindent string can't be emptyr   zclientmodule_test.pyz--indent-string=)rK   )r   rQ   rM   )r   rz   ry   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	sDt
t|dksTt
t|d tsft
ddddd	d
d}|d }x0| D ]$\}}||kst
|| |kst
qW d|d  kst
d S )NrV   zsyntax_error.pyrP   )r6   r1   rp   r   rA   errorzsyntax-errorZsyntax_error)objcolumnlinetypesymbolry   zinvalid syntaxmessage)r   r   rQ   r8   r	   jsonloadsr2   
isinstancelistr5   r   dictitemslower)r   r   ry   r7   rz   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	sDt
t|dksTt
t|d tsft
ddddd|d}|d }x0| D ]$\}}||kst
|| |kst
qW |d	 d
st
d S )NrV   ztotally_missing.pyrp   )r6   r1   r   rA   Zfatal)r   r   r   r   r   ry   r   zNo module named)r   r   rQ   r8   r	   r   r   r2   r   r   r5   r   r   r   r   )r   r   ry   r7   rz   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	sDt
t|dksTt
t|d tsft
dddd	d
ddd}|d }x0| D ]$\}}||kst
|| |kst
qW d S )NrV   zunused_variable.py   )r6   r1   rp   r   zunused-variableZunused_variablezUnused variable 'variable'ZW0612Zwarning)r   ry   r   r   z
message-idr   r   )r   r   rQ   r8   r	   r   r   r2   r   r   r5   r   r   r   )r   r   ry   r7   rz   r   r   r*   r   r   r   'test_json_report_does_not_escape_quotes  s$    z1TestRunTC.test_json_report_does_not_escape_quotesc             C   s$   d}t tdd}| j|g|d d S )Nz$Your code has been rated at 10.00/10rV   zmeta.py)rK   )r   rQ   rM   )r   rz   pathr   r   r   -test_information_category_disabled_by_default  s    z7TestRunTC.test_information_category_disabled_by_defaultc             C   s,   t d}ttdd}| j|dg|d d S )Nz
        ************* Module application_crash
        pylint/test/regrtest_data/application_crash.py:1:6: E0602: Undefined variable 'something_undefined' (undefined-variable)
        rV   zapplication_crash.pyz-E)rK   )rw   rx   r   rQ   rM   )r   rK   ry   r   r   r   test_error_mode_shows_no_score  s    z(TestRunTC.test_error_mode_shows_no_scorec             C   s$   d}t tdd}| j|g|d d S )NzYour code has been rated at rV   zapplication_crash.py)rK   )r   rQ   rM   )r   rK   ry   r   r   r   &test_evaluation_score_shown_by_default  s    z0TestRunTC.test_evaluation_score_shown_by_defaultc             C   s&   d}t tdd}| j|dg|d d S )NzYour code has been rated atrV   zmeta.pyz--confidence=HIGH,INFERENCE)rK   )r   rQ   rM   )r   rz   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 )NrV   zmeta.pyz	.pylintrcz$Your code has been rated at 10.00/10z--rcfile=%sz-rn)rK   )r   rQ   rM   )r   r   config_pathrz   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 )
NrV   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.z--rcfile=%sz,--help-msg=dummy-message-01,dummy-message-02)rK   zl[DUMMY_PLUGIN]

# Dummy option 1
dummy_option_1=dummy value 1

# Dummy option 2
dummy_option_2=dummy value 2z--generate-rcfile)r   rQ   r   r   appendrM   remove)r   Zdummy_plugin_pathr   rz   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 )NrV   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)
        z--rcfile=%sz-rn)rK   )r   rQ   rw   rx   rM   )r   r   r   rz   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=arg   )r1   r6   )r8   rN   r   )r   r   r   r   ,test_no_crash_with_formatting_regex_defaults  s    z6TestRunTC.test_no_crash_with_formatting_regex_defaultsc          	   C   s@   t d}ttdd}td | j|dg|d W d Q R X d S )Nz
        ************* Module application_crash
        pylint/test/regrtest_data/application_crash.py:1:6: E0602: Undefined variable 'something_undefined' (undefined-variable)
        rV   zapplication_crash.pyzUTF-8z-E)rK   )rw   rx   r   rQ   r   rM   )r   rK   ry   r   r   r   2test_getdefaultencoding_crashes_with_lc_ctype_utf8  s
    
z<TestRunTC.test_getdefaultencoding_crashes_with_lc_ctype_utf8Zwin32zonly occurs on *nixc          	   C   s   d}t t  }t||}zJt| t|d}|d W d Q R X | j|dgtt |d W d t| t	| X d S )Nztest_target.pywza,b = object()z--output-format=parseable)rK   )
rQ   r   r   r   makedirsopenwriterM   r   
removedirs)r   	file_nameZ	fake_pathry   Ztest_targetr   r   r   test_parseable_file_path'  s    


z"TestRunTC.test_parseable_file_path)NNN)N)3r+   r,   r-   r8   r3   rI   rM   rO   rR   rT   rU   rW   rX   rY   r_   r`   rd   re   rf   rh   rn   ro   rq   rt   ru   r:   ZmarkZskipifr   rk   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   platformr   r   r   r   r   r0   b   sT   

		
"",
r0   ) 
contextlibr   rE   r   r   os.pathr   r   r   rl   rw   r]   ior   Zpylint.lintr   Zpylint.reportersr   Zpylint.reporters.textZpylint.reporters.jsonr	   r:   Zpylintr
   __file__rQ   contextmanagerr   r   r   objectr0   r   r   r   r   <module>   s(   
