3
ÌœŸW   ã               @   s¦   d Z ddlmZ ddlZddlmZmZmZmZ ddl	m	Z	 ddl
Z
ddlZddlZddlZdadd„ Zd	d
„ Zdd„ Zddd„Zddd„Zdd„ Zedkr¢eƒ  dS )zÙScript to run all the tests each in its own subprocess to ensure
maximal correctness and minimal pain.  This does make running the tests
quite slow but the tests will be run correctly -- which is the important
thing.
é    )Úprint_functionN)ÚdirnameÚjoinÚisfileÚisdir)ÚglobFc              C   s@   t jg} tr<yddl}W n tk
r4   t jg} Y nX dg} | S )z9Get a test runner for the tests.  Uses nose if available.r   NÚ	nosetests)ÚsysÚ
executableÚUSE_NOSEÚnoseÚImportError)Úresultr   © r   úX/home/yuhw/wc/wire-cell-python/venv/lib/python3.6/site-packages/mayavi/tests/runtests.pyÚget_test_runner   s    r   c             C   sˆ   g }|j tt| dƒƒƒ |j tt| dƒƒƒ |j tt| ddƒƒƒ |j tt| ddƒƒƒ |j tt| ddƒƒƒ |j tt| ddƒƒƒ |S )z'Get all tests in given directory `pth`.ztest*.pyz*_test_case.pyÚtestÚtests)Úextendr   r   )ÚpthÚfilesr   r   r   Úget_tests_in_dir&   s    r   c       
      C   sø   g }xî| D ]æ}t |ƒr"|j|ƒ q
t|ƒr:|jt|ƒƒ q
y�|jdƒ}t|ƒdkr–dj|dd… ƒ}|d }t|t	ƒ t
ƒ |gƒ}t||ƒ}t|jƒ}n$|d }t|t	ƒ t
ƒ g ƒ}t|jƒ}|jt|ƒƒ W q
 tk
rî   d| }	t|	ƒ Y q
X q
W |S )zWFind test files given list of arguments which may be files,
    directories or modules.Ú.é   Nr   z<Warning: %s is neither a file/directory or module. Ignoring.éÿÿÿÿr   )r   Úappendr   r   r   ÚsplitÚlenr   Ú
__import__ÚglobalsÚlocalsÚgetattrr   Ú__file__r   Úprint)
r   r   r   Ú
componentsÚmodnameÚsymbolÚmodÚsÚdÚmsgr   r   r   Ú
find_tests1   s.    



r+   r   c             C   sD  t j}g }d}d}d}d}tƒ }d|k}	�xŒ| D �]‚}
|dkrP|	rP|d|
g }n
||
g }|dkrrtddj|ƒƒ tjƒ }t j|||dd	�}|jƒ }tjƒ }|jƒ \}}t	j
d
|ƒ}d}|rÊt|jdƒƒ}||7 }t	j
d|ƒ}d}|rôt|jdƒƒ}d}t	j
d|ƒ}|�rt|jdƒƒ}||7 }||7 }|dk�rb|| | }d| d|  d|  }tjj|ƒ |dk�r„|j|
||||| gƒ |dk�ržt|ƒ t|ƒ tjjƒ  ||| 7 }q0W tddd  ƒ td||f ƒ d}t|ƒdk�r8td||f ƒ x6|D ].}|\}
}}}}td|
ƒ t|ƒ t|ƒ �q W d}ntdƒ |S )aÚ  Run the given tests.  Each test file is run as a unittest in a
    subprocess.

    **Parameters**

     :tests: List of test file paths to run as tests.  These tests are
             best written as unittests.

     :verbose: An integer in (0, 1, 2).  0 specifies no output.  1
               specifies moderate output and 2 very verbose output.

    **Returns**

    The error code is returned.  If there are errors this is 1 if
    everything ran fine it returns 0.

    r   g        r   r   z-vzRunning:ú T)ÚstdoutÚstderrÚ	close_fdszRan\s*(\d*)\s*testzfailures=(\d*)zerrors=(\d*)r   ÚFÚEÚ
ú-éF   zRan %d tests in %.4g seconds
z,FAILED: there were %d failures and %d errorszFile:ÚOK)Ú
subprocessÚPIPEr   r#   r   ÚtimeÚPopenÚwaitÚcommunicateÚreÚsearchÚintÚgroupr	   r-   Úwriter   Úflushr   )r   Úverboser7   ÚerrorsÚtotalZtot_errZtot_failZ
total_timeZcmd_baseZhas_noser   ÚcmdÚt1ÚpipeÚstÚt2ÚoutÚerrÚresÚntZnfailZnerrZnsuccessÚ	errorcodeÚtr   r   r   ÚrunQ   sv    






rP   c             C   sh   | dkrt dd ƒ t dƒ tdgƒ}t|| ƒ}| dkrLt dd ƒ t dƒ tdgƒ}|t|| ƒ7 }|S )z'Run all the TVTK and mayavi tests.
    r   r3   r4   zRunning TVTK tests.ÚtvtkzRunning Mayavi tests.Úmayavi)r#   r+   rP   )rB   r   rK   r   r   r   Úm2_tests¬   s    


rS   c              C   sÆ   d} t j| ƒ}|jddddddd� |jd	d
ddddd� |jddddddd� |jƒ \}}d}|jrjda|jrtd}|jr~d}d}t|ƒdkr˜t	|ƒ}n t
|ƒ}t|ƒdkr¸t||d�}tj|ƒ d S )Na  %prog [options] directories/files/modules

    This program runs any tests in the given files and directories and
    also scans a module's directory for directories called 'tests' or
    'test' which contain 'test_*.py' files and runs them each in a
    separate subprocess.

    z-vz	--verboseÚ
store_trueFrB   zrun tests in verbose mode)ÚactionÚdefaultÚdestÚhelpz-qz--quietÚquietzrun tests in quiet modez-nz--noser   z'run tests using nose if it is availabler   Té   r   )rB   )ÚoptparseÚOptionParserÚ
add_optionÚ
parse_argsr   r   rB   rY   r   rS   r+   rP   r	   Úexit)ÚusageÚparserÚoptionsÚargsrB   Ústatusr   r   r   r   Úmain»   s@    

re   Ú__main__)r   )r   )Ú__doc__Ú
__future__r   r	   Úos.pathr   r   r   r   r   r6   r8   r<   r[   r   r   r   r+   rP   rS   re   Ú__name__r   r   r   r   Ú<module>   s"    
[
-