U
    ªÃT_‚%  ã                   @   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mZ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mZmZmZmZmZmZmZmZmZ d dlmZ d	d
lmZ eZ G dd„ deƒZ!G dd„ dƒZ"G dd„ deƒZ#dS )é    N)ÚDistutilsErrorÚDistutilsOptionError)Úlog)Ú
TestLoader)Úsix)ÚmapÚfilter)	Úresource_listdirÚresource_existsÚnormalize_pathÚworking_setÚ_namespace_packagesÚevaluate_markerÚadd_activation_listenerÚrequireÚ
EntryPoint)ÚCommandé   )Ú_unique_everseenc                   @   s   e Zd Zdd„ Zddd„ZdS )ÚScanningLoaderc                 C   s   t  | ¡ tƒ | _d S ©N)r   Ú__init__ÚsetÚ_visited©Úself© r   ú;/tmp/pip-unpacked-wheel-sr6cxcdi/setuptools/command/test.pyr      s    
zScanningLoader.__init__Nc                 C   sâ   || j krdS | j  |¡ g }| t | |¡¡ t|dƒrH| | ¡ ¡ t|dƒrÀt|jdƒD ]`}| 	d¡rŒ|dkrŒ|jd |dd…  }n"t
|j|d	 ƒr^|jd | }nq^| |  |¡¡ q^t|ƒd
krÖ|  |¡S |d S dS )a  Return a suite of all tests cases contained in the given module

        If the module is a package, load tests from all the modules in it.
        If the module has an ``additional_tests`` function, call it and add
        the return value to the tests.
        NÚadditional_testsÚ__path__Ú z.pyz__init__.pyÚ.éýÿÿÿz/__init__.pyr   r   )r   ÚaddÚappendr   ÚloadTestsFromModuleÚhasattrr   r	   Ú__name__Úendswithr
   ZloadTestsFromNameÚlenZ
suiteClass)r   ÚmoduleÚpatternÚtestsÚfileÚ	submoduler   r   r   r%      s$    



z"ScanningLoader.loadTestsFromModule)N)r'   Ú
__module__Ú__qualname__r   r%   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ Zddd„ZdS )ÚNonDataPropertyc                 C   s
   || _ d S r   ©Úfget)r   r3   r   r   r   r   A   s    zNonDataProperty.__init__Nc                 C   s   |d kr| S |   |¡S r   r2   )r   ÚobjZobjtyper   r   r   Ú__get__D   s    zNonDataProperty.__get__)N)r'   r/   r0   r   r5   r   r   r   r   r1   @   s   r1   c                   @   s¢   e Zd ZdZdZdddgZdd„ Zdd	„ Zed
d„ ƒZ	dd„ Z
dd„ Zejg fdd„ƒZeejdd„ ƒƒZedd„ ƒZdd„ Zdd„ Zedd„ ƒZedd„ ƒZdS )Útestz.Command to run unit tests after in-place buildz0run unit tests after in-place build (deprecated))ztest-module=Úmz$Run 'test_suite' in specified module)ztest-suite=Úsz9Run single test, case or suite (e.g. 'module.test_suite'))ztest-runner=ÚrzTest runner to usec                 C   s   d | _ d | _d | _d | _d S r   )Ú
test_suiteÚtest_moduleÚtest_loaderÚtest_runnerr   r   r   r   Úinitialize_optionsV   s    ztest.initialize_optionsc                 C   sŒ   | j r| jrd}t|ƒ‚| j d krD| jd kr8| jj | _ n| jd | _ | jd kr^t| jdd ƒ| _| jd krnd| _| jd krˆt| jdd ƒ| _d S )Nz1You may specify a module or a suite, but not bothz.test_suiter<   z&setuptools.command.test:ScanningLoaderr=   )r:   r;   r   Údistributionr<   Úgetattrr=   )r   Úmsgr   r   r   Úfinalize_options\   s    




ztest.finalize_optionsc                 C   s   t |  ¡ ƒS r   )ÚlistÚ
_test_argsr   r   r   r   Ú	test_argso   s    ztest.test_argsc                 c   s4   | j stjdkrdV  | jr"dV  | j r0| j V  d S )N)é   é   Zdiscoverz	--verbose)r:   ÚsysÚversion_infoÚverboser   r   r   r   rD   s   s    ztest._test_argsc              	   C   s   |   ¡ � |ƒ  W 5 Q R X dS )zI
        Backward compatibility for project_on_sys_path context.
        N)Úproject_on_sys_path)r   Úfuncr   r   r   Úwith_project_on_sys_path{   s    
ztest.with_project_on_sys_pathc           	   
   c   sP  t jot| jddƒ}|rv| jddd� |  d¡ |  d¡}t|jƒ}| jd|d� |  d¡ | jddd� |  d¡ n"|  d¡ | jdd	d� |  d¡ |  d¡}t	j
d d … }t	j ¡ }zbt|jƒ}t	j
 d|¡ t ¡  td
d„ ƒ td|j|jf ƒ |  |g¡� d V  W 5 Q R X W 5 |t	j
d d …< t	j ¡  t	j |¡ t ¡  X d S )NÚuse_2to3FÚbuild_pyr   )ZinplaceÚegg_info)Úegg_baseÚ	build_extr   c                 S   s   |   ¡ S r   )Úactivate)Údistr   r   r   Ú<lambda>¦   ó    z*test.project_on_sys_path.<locals>.<lambda>z%s==%s)r   ÚPY3r@   r?   Úreinitialize_commandÚrun_commandÚget_finalized_commandr   Ú	build_librH   ÚpathÚmodulesÚcopyÚclearÚupdater   r   rQ   Úinsertr   r   Úegg_nameZegg_versionÚpaths_on_pythonpath)	r   Zinclude_distsZ	with_2to3Zbpy_cmdZ
build_pathZei_cmdÚold_pathZold_modulesZproject_pathr   r   r   rK   ‚   s8    









ztest.project_on_sys_pathc              
   c   sŽ   t ƒ }tj d|¡}tj dd¡}zBtj t| ƒ¡}td||gƒ}tj |¡}|r\|tjd< dV  W 5 ||kr~tj dd¡ n
|tjd< X dS )zë
        Add the indicated paths to the head of the PYTHONPATH environment
        variable so that subprocesses will also see the packages at
        these paths.

        Do this in a context that restores the value on exit.
        Ú
PYTHONPATHr    N)	ÚobjectÚosÚenvironÚgetÚpopÚpathsepÚjoinr   r   )ÚpathsZnothingZorig_pythonpathZcurrent_pythonpathÚprefixZto_joinÚnew_pathr   r   r   rc   °   s    


ztest.paths_on_pythonpathc                 C   sD   |   | j¡}|   | jpg ¡}|   dd„ | j ¡ D ƒ¡}t |||¡S )z†
        Install the requirements indicated by self.distribution and
        return an iterable of the dists that were built.
        c                 s   s0   | ](\}}|  d ¡rt|dd… ƒr|V  qdS )ú:r   N)Ú
startswithr   )Ú.0ÚkÚvr   r   r   Ú	<genexpr>Ò   s   
 ÿz%test.install_dists.<locals>.<genexpr>)Zfetch_build_eggsZinstall_requiresZtests_requireZextras_requireÚitemsÚ	itertoolsÚchain)rT   Zir_dZtr_dZer_dr   r   r   Úinstall_distsÊ   s    
ÿztest.install_distsc              
   C   s’   |   dtj¡ |  | j¡}d | j¡}| jr>|   d| ¡ d S |   d| ¡ tt	 
d¡|ƒ}|  |¡�" |  ¡ � |  ¡  W 5 Q R X W 5 Q R X d S )Nz»WARNING: Testing via this command is deprecated and will be removed in a future version. Users looking for a generic test entry point independent of test runner are encouraged to use tox.ú zskipping "%s" (dry run)zrunning "%s"Úlocation)Úannouncer   ÚWARNry   r?   rl   Ú_argvÚdry_runr   ÚoperatorÚ
attrgetterrc   rK   Ú	run_tests)r   Zinstalled_distsÚcmdrm   r   r   r   ÚrunØ   s    û
ztest.runc                 C   sÔ   t jr~t| jddƒr~| j d¡d }|tkr~g }|tjkrD| 	|¡ |d7 }tjD ]}| 
|¡rR| 	|¡ qRtttjj|ƒƒ tjd d | j|  | j¡|  | j¡dd�}|j ¡ sÐd|j }|  |tj¡ t|ƒ‚d S )NrN   Fr!   r   )Z
testLoaderZ
testRunnerÚexitzTest failed: %s)r   rW   r@   r?   r:   Úsplitr   rH   r]   r$   rq   rC   r   Ú__delitem__ÚunittestÚmainr~   Ú_resolve_as_epr<   r=   ÚresultZwasSuccessfulr|   r   ÚERRORr   )r   r*   Zdel_modulesÚnamer6   rA   r   r   r   r‚   ï   s.    



  

ü

ztest.run_testsc                 C   s   dg| j  S )Nrˆ   )rE   r   r   r   r   r~   
  s    z
test._argvc                 C   s$   | dkrdS t  d|  ¡}| ¡ ƒ S )zu
        Load the indicated attribute value, called, as a as if it were
        specified as an entry point.
        Nzx=)r   ÚparseÚresolve)ÚvalÚparsedr   r   r   rŠ     s    ztest._resolve_as_epN)r'   r/   r0   Ú__doc__ÚdescriptionÚuser_optionsr>   rB   r1   rE   rD   rM   Ú
contextlibÚcontextmanagerrK   Ústaticmethodrc   ry   r„   r‚   Úpropertyr~   rŠ   r   r   r   r   r6   J   s2   ü
-

r6   )$rg   r€   rH   r•   rw   rˆ   Údistutils.errorsr   r   Ú	distutilsr   r   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   Úpkg_resourcesr	   r
   r   r   r   r   r   r   r   Ú
setuptoolsr   rO   r   ÚtypeÚ__metaclass__r   r1   r6   r   r   r   r   Ú<module>   s"   ,)
