B
    0zfV;                 @   s   d dl Z d dlmZ d dlZG dd de jZG dd de jZe Zdd Z	G d	d
 d
e
ZG dd de
ZG dd de
ZG dd de
ZG dd de
ZG dd de
ZG dd de
ZG dd de
ZdS )    N)DebuggerCheckerc                   s,   e Zd ZdZ fddZ fddZ  ZS )CaptureReportz"Collect the results of the checks.c                s   g | _ tt| | d S )N)_resultssuperr   __init__)selfoptions)	__class__ N/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/test_linter.pyr      s    zCaptureReport.__init__c                sF   t t| ||||}|rB||d||dd d}| j| |S )zStore each error.z{0} {1}   N)linecolmessage)r   r   errorformatr   append)r   line_numberoffsettextcheckcoderecord)r	   r
   r   r      s
    zCaptureReport.error)__name__
__module____qualname____doc__r   r   __classcell__r
   r
   )r	   r   r      s   r   c               @   sF   e Zd Zg Zg ZdedddgfgZdZdZdZ	dZ
ddddZd	ZdS )
DebuggerTestStyleGuideZdebugger_usagetreefilenamelinesNFr   )fileszphysical lineszlogical lines   )r   r   r   Zlogical_checksZphysical_checksr   Z
ast_checksmax_line_lengthZmax_doc_lengthZhang_closingverboseZbenchmark_keysindent_sizer
   r
   r
   r   r      s   r   c             C   sl   ddl m} |dd}||   |  ttd}dd | dD }tj	|j
|t|d	}|  |jS )
z=Process code using pycodestyle Checker and return all errors.r   )NamedTemporaryFileF)delete)r   c             S   s   g | ]}|d  qS )
r
   ).0r   r
   r
   r   
<listcomp>0   s    z6check_code_for_debugger_statements.<locals>.<listcomp>r)   )r    r!   r   report)tempfiler'   writeencodeflushr   _debugger_test_stylesplitpycodestyleZCheckernameZ	check_allr   )r   r'   Z	test_filer,   r!   checkerr
   r
   r   "check_code_for_debugger_statements(   s    

r6   c               @   s<   e Zd Zdd Zdd Zdd Zejjddd	d
d Z	dS )TestQAc             C   s0   t d}ddddddddg}||ks,td S )Nz#from ipdb import set_trace as r
r()   z%T100 trace found: set_trace used as rr   )r   r   r      z$T100 import for set_trace found as r)r6   AssertionError)r   resultexpected_resultr
   r
   r   test_catches_simple_debugger8   s    
z#TestQA.test_catches_simple_debuggerc             C   s0   t d}ddddddddg}||ks,td S )Nzimport ipdb
ipdb.set_trace()r8   z%T100 trace found: ipdb.set_trace usedr   )r   r   r   r9   zT100 import for ipdb found)r6   r:   )r   r;   r<   r
   r
   r   0test_catches_simple_debugger_when_called_off_libB   s    
z7TestQA.test_catches_simple_debugger_when_called_off_libc             C   s&   t d}ddddg}||ks"td S )Nz__import__('ipdb').set_trace()r9   z T100 trace found: set_trace usedr   )r   r   r   )r6   r:   )r   r;   r<   r
   r
   r   3test_catches_simple_debugger_when_called_off_globalL   s    z:TestQA.test_catches_simple_debugger_when_called_off_globalTzNot supported just yet)reasonc             C   s0   t d}ddddddddg}||ks,td S )Nz(import ipdb
test = ipdb.set_trace
test()r9   zT100 import for ipdb foundr   )r   r   r      z%T100 trace found: ipdb.set_trace used)r6   r:   )r   r;   r<   r
   r
   r   0test_catches_simple_debugger_when_called_off_varS   s    
z7TestQA.test_catches_simple_debugger_when_called_off_varN)
r   r   r   r=   r>   r?   pytestmarkZskipifrB   r
   r
   r
   r   r7   7   s   

r7   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestBreakpointc             C   s&   t d}ddddg}||ks"td S )Nzbreakpoint()r9   z!T100 trace found: breakpoint usedr   )r   r   r   )r6   r:   )r   r;   r<   r
   r
   r   5test_catches_breakpoint_call_for_python_3_7_and_above_   s    zDTestBreakpoint.test_catches_breakpoint_call_for_python_3_7_and_abovec             C   s&   t d}ddddg}||ks"td S )Nzfrom builtins import breakpointr9   z T100 import for breakpoint foundr   )r   r   r   )r6   r:   )r   r;   r<   r
   r
   r   test_catches_breakpoint_importf   s    z-TestBreakpoint.test_catches_breakpoint_importc             C   s   t d}g }||kstd S )Nzimport builtins)r6   r:   )r   r;   r<   r
   r
   r   test_allows_builtins_importm   s    z*TestBreakpoint.test_allows_builtins_importc             C   s&   t d}ddddg}||ks"td S )Nz%import builtins
builtins.breakpoint()r   r8   z!T100 trace found: breakpoint used)r   r   r   )r6   r:   )r   r;   r<   r
   r
   r   +test_catches_breakpoint_usage_from_builtinst   s    z:TestBreakpoint.test_catches_breakpoint_usage_from_builtinsc             C   s0   t d}ddddddddg}||ks,td S )Nz(from builtins import breakpoint as b
b()r8   z&T100 trace found: breakpoint used as br   )r   r   r   r9   z%T100 import for breakpoint found as b)r6   r:   )r   r;   r<   r
   r
   r   .test_catches_breakpoint_imported_as_other_name{   s    
z=TestBreakpoint.test_catches_breakpoint_imported_as_other_nameN)r   r   r   rF   rG   rH   rI   rJ   r
   r
   r
   r   rE   ^   s
   rE   c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestNoQAc             C   s&   t d}ddddg}||ks"td S )Nz*from ipdb import set_trace as r # noqa
r()r8   z%T100 trace found: set_trace used as rr   )r   r   r   )r6   r:   )r   r;   r<   r
   r
   r   test_skip_import   s    zTestNoQA.test_skip_importc             C   s&   t d}ddddg}||ks"td S )Nz*from ipdb import set_trace as r
r() # noqar9   z$T100 import for set_trace found as rr   )r   r   r   )r6   r:   )r   r;   r<   r
   r
   r   test_skip_usage   s    zTestNoQA.test_skip_usagec             C   s   t d}g }||kstd S )Nz1from ipdb import set_trace as r # noqa
r() # noqa)r6   r:   )r   r;   r<   r
   r
   r   test_skip_import_and_usage   s    z#TestNoQA.test_skip_import_and_usageN)r   r   r   rL   rM   rN   r
   r
   r
   r   rK      s   rK   c               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestImportCasesc             C   s"   t d}|ddddgkstd S )Nzimport math, ipdb, collectionsr   r9   zT100 import for ipdb found)r   r   r   )r6   r:   )r   r;   r
   r
   r   test_import_multiple   s    z$TestImportCases.test_import_multiplec             C   s"   t d}|ddddgkstd S )Nz
import pdbr   r9   zT100 import for pdb found)r   r   r   )r6   r:   )r   r;   r
   r
   r   test_import   s    zTestImportCases.test_importc             C   s"   t d}|ddddgkstd S )Nz8from IPython.terminal.embed import InteractiveShellEmbedr   r9   z+T100 import for InteractiveShellEmbed found)r   r   r   )r6   r:   )r   r;   r
   r
   r   #test_import_interactive_shell_embed   s    z3TestImportCases.test_import_interactive_shell_embedc             C   s@   t d}t|dd d}ddddddd	dg}||ks<td S )
Nzimport pdb, ipdbc             S   s   | d S )Nr   r
   )Zdebuggerr
   r
   r   <lambda>       z<TestImportCases.test_import_both_same_line.<locals>.<lambda>)keyr   r9   zT100 import for ipdb found)r   r   r   zT100 import for pdb found)r6   sortedr:   )r   r;   r<   r
   r
   r   test_import_both_same_line   s
    
z*TestImportCases.test_import_both_same_linec             C   s   t d}|g kstd S )Nzimport math)r6   r:   )r   r;   r
   r
   r   test_import_math   s    z TestImportCases.test_import_mathc             C   s   t d}|g kstd S )Nzimport ipdb # noqa)r6   r:   )r   r;   r
   r
   r   test_import_noqa   s    z TestImportCases.test_import_noqaN)	r   r   r   rP   rQ   rR   rW   rX   rY   r
   r
   r
   r   rO      s   	rO   c               @   sD   e 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S )TestModuleSetTraceCasesc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	NzSfrom IPython.terminal.embed import InteractiveShellEmbed; InteractiveShellEmbed()():   r9   z,T100 trace found: InteractiveShellEmbed used)r   r   r   r   z+T100 import for InteractiveShellEmbed foundr   )r6   r:   )r   r;   r<   itemr
   r
   r   <test_import_ipython_terminal_embed_use_InteractiveShellEmbed   s    

zTTestModuleSetTraceCases.test_import_ipython_terminal_embed_use_InteractiveShellEmbedc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nzimport ipdb;ipdb.set_trace();   r9   z%T100 trace found: ipdb.set_trace used)r   r   r   r   zT100 import for ipdb foundr   )r6   r:   )r   r;   r<   r\   r
   r
   r   test_import_ipdb_use_set_trace   s    

z6TestModuleSetTraceCases.test_import_ipdb_use_set_tracec             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nzimport pdb;pdb.set_trace();   r9   z$T100 trace found: pdb.set_trace used)r   r   r   r   zT100 import for pdb foundr   )r6   r:   )r   r;   r<   r\   r
   r
   r   test_import_pdb_use_set_trace   s    

z5TestModuleSetTraceCases.test_import_pdb_use_set_tracec             C   sv   t d}ddddddddddddg}y||ks8tW n6 tk
rp   x|D ]}d|d	< qPW ||ksltY nX d S )
Nz/import pdb;pdb.set_trace() and pdb.set_trace();r`   r9   z$T100 trace found: pdb.set_trace used)r   r   r      r   zT100 import for pdb foundr   )r6   r:   )r   r;   r<   r\   r
   r
   r   #test_import_pdb_use_set_trace_twice   s    


z;TestModuleSetTraceCases.test_import_pdb_use_set_trace_twicec             C   s   t d}|g kstd S )Nz-from math import Max as set_trace
set_trace())r6   r:   )r   r;   r
   r
   r   0test_import_other_module_as_set_trace_and_use_it   s    zHTestModuleSetTraceCases.test_import_other_module_as_set_trace_and_use_itc             C   sb   t d}ddddg}y||ks$tW n6 tk
r\   x|D ]}d|d< q<W ||ksXtY nX d S )Nz/from celery.contrib import rdb;rdb.set_trace();rb   r9   z T100 trace found: set_trace used)r   r   r   r   r   )r6   r:   )r   r;   r<   r\   r
   r
   r   test_import_rdb_use_set_trace  s    
z5TestModuleSetTraceCases.test_import_rdb_use_set_tracec             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz9import celery.contrib.rdb;celery.contrib.rdb.set_trace();   r9   z T100 trace found: set_trace used)r   r   r   r   z(T100 import for celery.contrib.rdb foundr   )r6   r:   )r   r;   r<   r\   r
   r
   r   )test_from_celery_import_rdb_use_set_trace  s    

zATestModuleSetTraceCases.test_from_celery_import_rdb_use_set_traceN)
r   r   r   r]   r_   ra   rc   rd   re   rg   r
   r
   r
   r   rZ      s   rZ   c               @   s   e Zd Zdd ZdS )TestImportAsCasesc             C   s"   t d}|ddddgkstd S )Nz%import math, ipdb as sif, collectionsr   r9   z!T100 import for ipdb found as sif)r   r   r   )r6   r:   )r   r;   r
   r
   r   test_import_ipdb_as%  s    z%TestImportAsCases.test_import_ipdb_asN)r   r   r   ri   r
   r
   r
   r   rh   $  s   rh   c               @   s   e Zd Zdd ZdS )TestModuleASSetTraceCasesc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz#import ipdb as sif;sif.set_trace();   r9   z$T100 trace found: sif.set_trace used)r   r   r   r   z!T100 import for ipdb found as sifr   )r6   r:   )r   r;   r<   r\   r
   r
   r   !test_import_ipdb_as_use_set_trace+  s    

z;TestModuleASSetTraceCases.test_import_ipdb_as_use_set_traceN)r   r   r   rl   r
   r
   r
   r   rj   *  s   rj   c               @   sD   e 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S )TestImportSetTraceCasesc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz,from ipdb import run, set_trace;set_trace();    r9   z T100 trace found: set_trace used)r   r   r   r   zT100 import for set_trace foundr   )r6   r:   )r   r;   r<   r\   r
   r
   r   test_import_set_trace_ipdb=  s    

z2TestImportSetTraceCases.test_import_set_trace_ipdbc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz'from pdb import set_trace; set_trace();   r9   z T100 trace found: set_trace used)r   r   r   r   zT100 import for set_trace foundr   )r6   r:   )r   r;   r<   r\   r
   r
   r   test_import_set_trace_pdbM  s    

z1TestImportSetTraceCases.test_import_set_trace_pdbc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz.from ipdb import run, set_trace as sif; sif();(   r9   z'T100 trace found: set_trace used as sif)r   r   r   r   z&T100 import for set_trace found as sifr   )r6   r:   )r   r;   r<   r\   r
   r
   r   %test_import_set_trace_ipdb_as_and_use]  s    

z=TestImportSetTraceCases.test_import_set_trace_ipdb_as_and_usec             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz7from ipdb import run, set_trace as sif; True and sif();1   r9   z'T100 trace found: set_trace used as sif)r   r   r   r   z&T100 import for set_trace found as sifr   )r6   r:   )r   r;   r<   r\   r
   r
   r   :test_import_set_trace_ipdb_as_and_use_with_conjunction_andm  s    

zRTestImportSetTraceCases.test_import_set_trace_ipdb_as_and_use_with_conjunction_andc             C   sl   t d}ddddddddg}y||ks.tW n6 tk
rf   x|D ]}d|d< qFW ||ksbtY nX d S )	Nz6from ipdb import run, set_trace as sif; True or sif();0   r9   z'T100 trace found: set_trace used as sif)r   r   r   r   z&T100 import for set_trace found as sifr   )r6   r:   )r   r;   r<   r\   r
   r
   r   9test_import_set_trace_ipdb_as_and_use_with_conjunction_or}  s    

zQTestImportSetTraceCases.test_import_set_trace_ipdb_as_and_use_with_conjunction_orc             C   s2   t d}y|g kstW n tk
r,   Y nX d S )Nz=from ipdb import run, set_trace as sif; True or sif(); # noqa)r6   r:   )r   r;   r
   r
   r   >test_import_set_trace_ipdb_as_and_use_with_conjunction_or_noqa  s
    zVTestImportSetTraceCases.test_import_set_trace_ipdb_as_and_use_with_conjunction_or_noqac             C   sb   t d}ddddg}y||ks$tW n6 tk
r\   x|D ]}d|d< q<W ||ksXtY nX d S )Nz;from ipdb import run, set_trace as sif # noqa
True or sif()   r8   z'T100 trace found: set_trace used as sif)r   r   r   r   r   )r6   r:   )r   r;   r<   r\   r
   r
   r   Jtest_import_set_trace_ipdb_as_and_use_with_conjunction_or_noqa_import_only  s    
zbTestImportSetTraceCases.test_import_set_trace_ipdb_as_and_use_with_conjunction_or_noqa_import_onlyN)
r   r   r   ro   rq   rs   ru   rw   rx   rz   r
   r
   r
   r   rm   <  s   rm   )r3   Zflake8_debuggerr   rC   Z
BaseReportr   Z
StyleGuider   r1   r6   objectr7   rE   rK   rO   rZ   rh   rj   rm   r
   r
   r
   r   <module>   s   '(h