a
    zeX                     @   s  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mZ ddl	m
Z
mZmZ dZedejZG dd de
jZd	d
 Zdd Zdd Zdd Zdd ZG dd deZe ZdddZejdfddZdejejfddZdejejfddZ dS )    N)fnmatchfnmatchcase   )casesuiteutilTz[_a-z]\w*\.py$c                       s,   e Zd ZdZ fddZ fddZ  ZS )_FailedTestNc                    s   || _ tt| | d S N)
_exceptionsuperr   __init__)selfZmethod_name	exception	__class__ %/usr/lib/python3.9/unittest/loader.pyr      s    z_FailedTest.__init__c                    s*   | j krtt |S  fdd}|S )Nc                      s
    j d S r	   )r
   r   r   r   r   testFailure!   s    z,_FailedTest.__getattr__.<locals>.testFailure)_testMethodNamer   r   __getattr__)r   namer   r   r   r   r      s    
z_FailedTest.__getattr__)__name__
__module____qualname__r   r   r   __classcell__r   r   r   r   r      s   r   c                 C   s"   d| t  f }t| t|||S )Nz#Failed to import test module: %s
%s)	traceback
format_exc_make_failed_testImportError)r   
suiteClassmessager   r   r   _make_failed_import_test&   s    r"   c                 C   s   dt  f }t| |||S )NzFailed to call load_tests:
%s)r   r   r   )r   r   r    r!   r   r   r   _make_failed_load_tests+   s    r#   c                 C   s   t | |}||f|fS r	   )r   )
methodnamer   r    r!   testr   r   r   r   0   s    
r   c                 C   s<   t t|dd }| |i}tdt jf|}||| fS )Nc                 S   s   d S r	   r   r   r   r   r   testSkipped5   s    z'_make_skipped_test.<locals>.testSkippedZModuleSkipped)r   skipstrtypeTestCase)r$   r   r    r&   attrsZ	TestClassr   r   r   _make_skipped_test4   s
    
r,   c                 C   s*   |   dr| d d S tj| d S )Nz	$py.classir   )lowerendswithospathsplitext)r0   r   r   r   _jython_aware_splitext<   s    r2   c                       s   e Zd ZdZeejZdZe	j
ZdZ fddZdd Zdddd	Zd d
dZd!ddZdd Zd"ddZdd Zdd Zdd Zdd Zd#ddZd$ddZ  ZS )%
TestLoaderr%   Nc                    s    t t|   g | _t | _d S r	   )r   r3   r   errorsset_loading_packagesr   r   r   r   r   M   s    zTestLoader.__init__c                 C   sF   t |tjrtd| |}|s2t|dr2dg}| t||}|S )NzYTest cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?ZrunTest)
issubclassr   	TestSuite	TypeErrorgetTestCaseNameshasattrr    map)r   testCaseClassZtestCaseNamesZloaded_suiter   r   r   loadTestsFromTestCaseT   s    
z TestLoader.loadTestsFromTestCasepatternc             
   O   s8  t |dksd|v r,tdt |dd  t |dkrRt |d }td|t |dkrxt|d }td|g }t|D ]4}t	||}t
|trt|tjr|| | qt	|dd }	| |}|	d ur4z|	| ||W S  ty2 }
 z0t|j|
| j\}}| j| |W  Y d }
~
S d }
~
0 0 |S )Nr   Zuse_load_testsz(use_load_tests is deprecated and ignoredr   zCloadTestsFromModule() takes 1 positional argument but {} were givenz=loadTestsFromModule() got an unexpected keyword argument '{}'
load_tests)lenwarningswarnDeprecationWarningpopr9   formatsorteddirgetattr
isinstancer)   r7   r   r*   appendr>   r    	Exceptionr#   r   r4   )r   moduler@   argskwsZ	complainttestsr   objrA   e
error_caseerror_messager   r   r   loadTestsFromModuleb   s8    



zTestLoader.loadTestsFromModulec                 C   s`  | d}d\}}|d u r|d d  }|rzd|}t|}W qW q& ty   | }t|| j\}}|s| j| | Y S Y q&0 q&|dd  }|}	|D ]}
z|	t	|	|
 }}	W q t
yV } zt	|	dd d ur|d ur| j| |W  Y d }~  S t|
|| jdt f \}}| j| |W  Y d }~  S W Y d }~qd }~0 0 qt|	tjrr| |	S t|	trt|	tjr| |	S t|	tjrt|trt|tjr|d }||}tt	||tjs| |gS nt|	tjr|	S t|	rP|	 }t|tjr$|S t|tjr>| |gS td|	|f ntd|	 d S )	N.)NNr   __path__zFailed to access attribute:
%sz"calling %s returned %s, not a testz$don't know how to make test from: %s)splitjoin
__import__r   rF   r"   r    r4   rL   rJ   AttributeErrorr   r   r   rK   types
ModuleTyperV   r)   r7   r   r*   r>   FunctionTyper   r8   callabler9   )r   r   rN   partsrT   rU   Z
parts_copymodule_nameZnext_attributerR   partparentrS   instr%   r   r   r   loadTestsFromName   sz    	

,



zTestLoader.loadTestsFromNamec                    s    fdd|D } |S )Nc                    s   g | ]} | qS r   )rg   ).0r   rN   r   r   r   
<listcomp>       z1TestLoader.loadTestsFromNames.<locals>.<listcomp>)r    )r   namesrN   Zsuitesr   ri   r   loadTestsFromNames   s    zTestLoader.loadTestsFromNamesc                    s>    fdd}t t|t} jr:|jt jd |S )Nc                    sZ   |  jsdS t| }t|s&dS djj| f  jd u pXt fddjD S )NFz%s.%s.%sc                 3   s   | ]}t  |V  qd S r	   )r   )rh   r@   ZfullNamer   r   	<genexpr>   rk   zKTestLoader.getTestCaseNames.<locals>.shouldIncludeMethod.<locals>.<genexpr>)
startswithtestMethodPrefixrJ   ra   r   r   testNamePatternsany)attrnameZtestFuncr   r=   rn   r   shouldIncludeMethod   s    


z8TestLoader.getTestCaseNames.<locals>.shouldIncludeMethod)key)listfilterrI   sortTestMethodsUsingsort	functools
cmp_to_key)r   r=   rv   ZtestFnNamesr   ru   r   r:      s
    zTestLoader.getTestCaseNamestest*.pyc                 C   sD  d}|d u r| j d ur| j }n|d u r.d}|}tj|}|tjvrRtjd| || _ d}d}g }tjtj|rtj|}||krtjtj|d }njzt	| W n t
y   d}Y nF0 tj| }|dd }	ztjtj|j}W n ty   z
|j}
W n ty2   d }
Y n0 |
r|
jd u r|
jd urd}|jD ]P}|sv||svq\||jdtjjd | _ || j||dd q\n*|jtjv rtdd ntd|d Y n0 |r|s| |	| _ tj| ntj| |r$t
d	| |s:t| ||}|  |S )
NFTr   __init__.pyrW   )	namespacez2Can not use builtin modules as dotted module namesz$don't know how to discover from {!r}z%Start directory is not importable: %r)!_top_level_dirr/   r0   abspathsysinsertisdirisfiler[   r\   r   modulesrZ   dirname__file__r]   __spec__loadersubmodule_search_locationsrX   rp   r   replacesepextend_find_testsbuiltin_module_namesr9   rG    _get_directory_containing_moduleremoverx   r    )r   	start_dirr@   Ztop_level_dirZset_implicit_topZis_not_importableis_namespacerQ   Z
the_moduleZtop_partspecr0   r   r   r   discover   s    






zTestLoader.discoverc                 C   sR   t j| }tj|j}tj| drBtj	tj	|S tj	|S d S )Nr   )
r   r   r/   r0   r   r   basenamer-   rp   r   )r   rc   rN   	full_pathr   r   r   r   `  s
    
z+TestLoader._get_directory_containing_modulec                 C   sB   || j krdS ttj|}tj|| j }|tjjd}|S NrW   )r   r2   r/   r0   normpathrelpathr   r   )r   r0   Z_relpathr   r   r   r   _get_name_from_pathl  s    
zTestLoader._get_name_from_pathc                 C   s   t | tj| S r	   )r\   r   r   )r   r   r   r   r   _get_module_from_namex  s    z TestLoader._get_module_from_namec                 C   s
   t ||S r	   )r   )r   r0   r   r@   r   r   r   _match_path|  s    zTestLoader._match_pathFc           
   
   c   s   |  |}|dkrD|| jvrD| |||\}}|d ur<|V  |sDd S tt|}|D ]}tj||}	| |	||\}}|d ur|V  |rV|  |	}| j| z$| 	|	||E d H  W | j
| qV| j
| 0 qVd S r   )r   r6   _find_test_pathrH   r/   listdirr0   r[   addr   discard)
r   r   r@   r   r   rQ   Zshould_recursepathsr0   r   r   r   r   r     s.    

zTestLoader._find_testsc              
   C   s  t j|}t j|rTt|s(dS | |||s:dS | |}z| |}W nf t	j
y } zt||| jdfW  Y d }~S d }~0    t|| j\}}	| j|	 |df Y S 0 t jt|d|}
tt j|
}tt j|}| | kr>t j|}tt j|}t j|}d}t||||f | j||ddfS n,t j|r||st jt j|dsdS d }d }| |}z| |}W nh t	j
y } zt||| jdfW  Y d }~S d }~0    t|| j\}}	| j|	 |df Y S 0 t|dd }| j| zD| j||d}|d urV|dfW | j| S |dfW | j| S | j| 0 ndS d S )	N)NFFr   zW%r module incorrectly imported from %r. Expected %r. Is this module globally installed?r?   r   rA   T)r/   r0   r   r   VALID_MODULE_NAMEmatchr   r   r   r   ZSkipTestr,   r    r"   r4   rL   r   rJ   r2   realpathr-   r   r   rV   r   r[   r6   r   r   )r   r   r@   r   r   r   rN   rS   rT   rU   Zmod_filer   Zfullpath_noextZ
module_dirZmod_nameZexpected_dirmsgrA   rQ   packager   r   r   r     s    

&





&

zTestLoader._find_test_path)N)N)r~   N)F)F)r   r   r   rq   staticmethodr   three_way_cmprz   rr   r   r8   r    r   r   r>   rV   rg   rm   r:   r   r   r   r   r   r   r   r   r   r   r   r   r3   B   s$   
(
N

n
"r3   c                 C   s&   t  }||_| |_||_|r"||_|S r	   )r3   rz   rq   rr   r    )prefix	sortUsingr    rr   r   r   r   r   _makeLoader  s    r   c                 C   s   t |||d| S )N)rr   )r   r:   )r=   r   r   rr   r   r   r   r:     s    r:   r%   c                 C   s   t |||| S r	   )r   r>   )r=   r   r   r    r   r   r   	makeSuite  s    r   c                 C   s   t |||| S r	   )r   rV   )rN   r   r   r    r   r   r   findTestCases  s    r   )NN)!r/   rer   r   r^   r|   rC   r   r    r   r   r   Z
__unittestcompile
IGNORECASEr   r*   r   r"   r#   r   r,   r2   objectr3   ZdefaultTestLoaderr   r   r:   r8   r   r   r   r   r   r   <module>   s:      /
	
