B
    u9a0Z                 @   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Zd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZmZmZmZmZmZmZ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"m#Z#m$Z$m%Z% d dl&m'Z' G dd dZ(dd	d
Z)dS )    N)_parse_args)	findtestsruntestget_abs_moduleSTDTESTSNOTTESTSPASSEDFAILEDENV_CHANGEDSKIPPEDRESOURCE_DENIEDINTERRUPTEDCHILD_ERRORTEST_DID_NOT_RUNPROGRESS_MIN_TIMEformat_test_result	is_failed)setup_tests)removepycountformat_duration	printlist)supportc               @   s   e Zd ZdZdd Zdd Zd0d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d1d*d+Zd,d- Zd.d/ Zd)S )2Regrtestal  Execute a test suite.

    This also parses command-line options and modifies its behavior
    accordingly.

    tests -- a list of strings containing test names (optional)
    testdir -- the directory in which to look for tests (optional)

    Users other than the Python test suite will certainly want to
    specify testdir; if it's omitted, the directory containing the
    Python test suite is searched for.

    If the tests argument is omitted, the tests listed on the
    command-line will be used.  If that's empty, too, then all *.py
    files beginning with test_ will be used.

    The other default arguments (verbose, quiet, exclude,
    single, randomize, findleaks, use_resources, trace, coverdir,
    print_slow, and random_seed) allow programmers calling main()
    directly to set the values that would normally be set by flags
    on the command line.
    c             C   s   d | _ g | _g | _g | _g | _g | _g | _g | _g | _g | _	d | _
d| _g | _d | _t | _d| _d| _d | _d | _d | _d | _d | _d | _d S )NF    )nstestsselectedgoodbadskippedresource_deniedsenvironment_changedrun_no_testsrerunfirst_resultinterrupted
test_timestracertime	monotonic
start_time
test_counttest_count_widthnext_single_testnext_single_filenametestsuite_xmlwin_load_trackertmp_dirworker_test_name)self r6   /usr/lib/python3.7/main.py__init__/   s.    
zRegrtest.__init__c             C   s<   t | jt | jB t | jB t | jB t | jB t | jB S )N)setr   r    r!   r"   r#   r$   )r5   r6   r6   r7   get_executedY   s    zRegrtest.get_executedFc          	   C   sv  |j }|j}|ttfkr.|s.| j|j|f |tkrD| j| n|t	tfkrb|s| j
| n|tkrx| j| nj|tkr| j| nT|tkr| j| | j| n2|tkr| j| n|tkrd| _ntd| |r|t	tthkr| j
| |j}|rrdd lm  m} xN|D ]F}y| j|| W n( |jk
rj   t|tjd  Y nX q(W d S )NTzinvalid test result: %rr   )file) 	test_nameresultr   r   r(   append	test_timer   r   r	   r    r
   r#   r   r!   r   r"   r   r$   r'   
ValueErrorremovexml_dataxml.etree.ElementTreeetreeElementTreer1   Z
fromstringZ
ParseErrorprintsys
__stderr__)r5   r=   r%   r<   okrB   ETer6   r6   r7   accumulate_result^   s@    
zRegrtest.accumulate_resultc             C   s   | j jrd S || j | j }t| jt| j }|rN| j jsN| d| }d| d| }|  }|d k	rd|dd| }t	
 | j }tjt|d}| d| }t|dd	 d S )
N/[z] z
load avg: z.2f )ZsecondsT)flush)r   quietr.   r-   lenr    r#   pgo
getloadavgr*   r+   r,   datetimeZ	timedeltaintrF   )r5   
test_indextextlineZfailsZload_avgr?   r6   r6   r7   display_progress   s    zRegrtest.display_progressc             C   s   t tjdd  f|}|jr:ttds:tdtjd d |_|jrLg  t	_
| _|j}|d k	rddlm} ||j\}}||_|| _t|j |S )Nr   Zdump_traceback_laterzFWarning: The timeout option requires faulthandler.dump_traceback_later)r;   r   )parse_worker_args)r   rG   argvZtimeouthasattrfaulthandlerrF   stderrxmlpathr   Zjunit_xml_listr1   worker_argstest.libregrtest.runtest_mpr[   r4   r   args)r5   kwargsr   ra   r[   r<   r6   r6   r7   
parse_args   s    

zRegrtest.parse_argsc          	   C   s  || _ | jjrftj| jd| _y0t| jd}|	 
 }|g| _ W d Q R X W n tk
rd   Y nX | jjrg | _ td}ttjtj| jjN}xF|D ]>}|ddd }|
 }||}|d k	r| j |  qW W d Q R X t| j  td d  }t }| jjrLx0| jjD ]$}	|	|kr4||	 ||	 qW g | j_| jjrlt| jjt t  }
nt| jj||}
| jjs| j p| jjp|
| _!n| j | _!| jjr| j!d d | _!y"|
"| j!d }|
|d  | _#W n t$k
r   Y nX | jj%rPy| j!d | j!"| jj%= W n, t&k
rN   t'd| jj% t(j)d Y nX | jj*r| jj+d krvt,-d	| j_+t,.| jj+ t,/| j! d S )
NZ
pynexttestrz\btest_[a-zA-Z0-9_]+\b#r   r   z1Couldn't find starting test (%s), using all tests)r;   i )0r   r   singleospathjoinr3   r0   openreadstripOSErrorZfromfilerecompiler   SAVEDCWDsplitsearchr>   groupr   r   r   copyZexcluderc   rA   addZtestdirr   listr9   r   indexr/   
IndexErrorstartr@   rF   rG   r_   	randomizerandom_seedrandomZ	randrangeZseedZshuffle)r5   r   fpZ	next_testZregexrY   matchZstdtestsZnottestsargZalltestsposr6   r6   r7   
find_tests   sh    











zRegrtest.find_testsc             C   s   x| j D ]}t| qW d S )N)r   rF   )r5   namer6   r6   r7   
list_tests   s    zRegrtest.list_testsc             C   s\   xV|D ]N}t |tjjrqt |tjr2| | qt |tjrt|rt	|
  qW d S )N)
isinstanceunittestloaderZ_FailedTestZ	TestSuite_list_casesZTestCaser   Z
match_testrF   id)r5   suitetestr6   r6   r7   r      s    

zRegrtest._list_casesc          	   C   s   dt _t | jj xX| jD ]N}t| j|}ytj	|}| 
| W q tjk
rh   | j| Y qX qW | jrttjd ttt| jddtjd t| jtjd d S )NF)r;   r   zskipped:)r   verboseZset_match_testsr   Zmatch_testsr   r   r   ZdefaultTestLoaderZloadTestsFromNamer   ZSkipTestr!   r>   rF   rG   r_   r   rR   r   )r5   r<   Zabstestr   r6   r6   r7   
list_cases  s    zRegrtest.list_casesc             C   s   d| j _d| j _d| j _|  | _t  td | jd d  | _xP| jD ]F}td| ddd d| j _t	| j |}| j
|dd |jtkrHP qHW | jrttt| jdd	 t| j |   d S )
NTFz'Re-running failed tests in verbose modezRe-running z in verbose mode)rP   )r%   r   zfailed again:)r   r   failfastZverbose3get_tests_resultr&   rF   r    r%   r   rL   r=   r   r   rR   r   display_result)r5   r<   r=   r6   r6   r7   rerun_failed_tests  s$    


zRegrtest.rerun_failed_testsc             C   s  | j jrd S t  td|    | jr0td t| j|   }|rht  ttt	|dd t
| | jr| j jst  | js| js| jst	| jdkrtddd ttt	| jdd	 | j jr| jjd
d t  td x.| jd d D ]\}}td|t|f  qW | jrBt  ttt	| jdd t
| j | jrtt  tdtt	| jd t
| j | jr| j jst  ttt	| jdd t
| j | jrt  tdtt	| jd  t
| j | jr
t  ttt	| jdd t
| j d S )Nz== Tests result: %s ==z(Test suite interrupted by signal SIGINT.r   zomitted:r   ZAllrO   )endzOK.T)reversez10 slowest tests:
   z- %s: %szfailed:z%{} altered the execution environment:zskipped:z%s:zre-run testzrun no tests:)r   rS   rF   r   r'   r9   r   r:   r   rR   r   r   rQ   r    r!   Z
print_slowr(   sortr   r#   formatr%   r$   )r5   Zomittedr?   r   r6   r6   r7   r   0  s\    




zRegrtest.display_resultc             C   sl  | j jr dd l}|jddd| _tj }td d }x t| j	dD ]\}}t
 }|}|rld||f }| || | jrd}tt }	| jj|t |	d	 |	d
 }
nt| j |}
| |
 |
jtkrP t|
}t
 | }|tkrd|t|f }n|
jtkr
d }x4tj D ]&}||kr|drt| qW | j jrFt|
| j rFP qFW |rht| d S )Nr   FT)tracer   zRun tests sequentiallyr   z%s -- %szDresult = runtest(self.ns, test_name); self.accumulate_result(result))globalslocalsr=   z%s in %sztest.)r   r   ZTracer)   rG   moduleskeysrF   	enumerater   r*   r+   rZ   dictr   Zrunctxr   r   rL   r=   r   r   r   r   r   
startswithr   Zunloadr   r   )r5   r   Zsave_modulesZprevious_testrW   r<   r,   rX   cmdr   r=   r?   moduler6   r6   r7   run_tests_sequentialk  sD    




zRegrtest.run_tests_sequentialc             c   s:   x4x.|D ]&}|V  | j rd S | jjr| jrd S qW qW d S )N)r    r   fail_env_changedr#   )r5   r   r<   r6   r6   r7   _test_forever  s    
zRegrtest._test_foreverc             C   sv   t dt ftj   t dtjdddtj  t dt  t	 }|rXt d| t dt
dt f  d S )	Nz==T)Zaliasedz	%s-endianz== cwd:z== CPU count:z== encodings: locale=%s, FS=%sF)rF   platformZpython_implementationrG   versionrs   	byteorderri   getcwd	cpu_countlocalegetpreferredencodinggetfilesystemencoding)r5   r   r6   r6   r7   display_header  s    
zRegrtest.display_headerc             C   s   g }| j r|d n@| jjr0| jr0|d n&t| j| j | j| j| jfsV|d | jrf|d |st|d d	|}| j
rd| j
|f }|S )NFAILUREzENV CHANGEDzNO TEST RUNr   SUCCESSz, z
%s then %s)r    r>   r   r   r#   anyr   r!   r'   rk   r&   )r5   r=   r6   r6   r7   r     s     




zRegrtest.get_tests_resultc             C   s   | j js.| j js6| j js6| j js6| js6| j js6|   | j jrh| j j\}}}|dk rhd}t	|t
jdd | j jr~t	d| j j | j jr| t| j| _d| _d| _n.t| j| _dt| j| _t| jd | _| j jrd	d
lm} ||  n|   d S )N   zhWARNING: Running tests with --huntrleaks/-R and less than 3 warmup repetitions can give false positives!T)r;   rP   zUsing random seedr   z/{}r   r   )run_tests_multiprocess)r   headerrS   rQ   rh   r   rc   r   
huntrleaksrF   rG   stdoutr|   r}   Zforeverr   rx   r   r-   r.   iterr   rR   Zuse_mprb   r   r   )r5   warmuprepetitions_msgr   r6   r6   r7   	run_tests  s,    
zRegrtest.run_testsc          	   C   s   | j d k	r| j   d | _ | jr\| jrPt| jd}|| jd  W d Q R X nt| j | jr| j	 }|j
dd| jjd t  t | j }tdt|  td|    | jjrtdt   d S )Nw
T)Zshow_missingZsummarycoverdirzTotal duration: %szTests result: %szleaks %d)r2   closer0   r/   rl   writeri   unlinkr)   resultsZwrite_resultsr   r   rF   r*   r+   r,   r   r   Zrunleakssystemgetpid)r5   r   rf   Zdurationr6   r6   r7   finalize  s$    


zRegrtest.finalizec       
   
   C   s
  | j js| jsd S dd lm  m} |d}dddd}x\| jD ]R}|| xB|D ]:}y ||  t|	|d7  < W qV t
k
r   Y qVX qVW qBW x$| D ]\}}||t| qW tjtj| j j}t|d$}x||D ]}	||	 qW W d Q R X d S )Nr   Z
testsuites)r   errorsZfailureswb)r   r`   r1   rC   rD   rE   ZElementr>   rV   getr@   itemsr9   strri   rj   rk   r   rr   rl   Ztostringlistr   )
r5   rJ   rootZtotalsr   kvr`   fsr6   r6   r7   save_xml_result  s$    


 zRegrtest.save_xml_resultc             C   s   | j jr| j j| _| js`t rVtd| _| jd krBtd| _tj| jd| _n
t	
 | _tj| j| _tj| jdd t }| jd k	rd|}n
d|}tj| j|}|S )NZabs_builddirZsrcdirZbuildT)exist_okz	worker_{}ztest_python_{})r   tempdirr3   	sysconfigZis_python_buildZget_config_varri   rj   rk   tempfileZ
gettempdirabspathmakedirsr   r4   r   )r5   pidtest_cwdr6   r6   r7   create_temp_dir  s"    




zRegrtest.create_temp_dirNc          	   K   sF   |  || _|  }tj|dd || j_| || W d Q R X d S )NT)rQ   )re   r   r   r   Ztemp_cwdr   _main)r5   r   rd   r   r6   r6   r7   main>  s
    zRegrtest.mainc             C   s.   | j d k	r| j  S ttdr*t d S d S )NrT   r   )r2   rT   r]   ri   )r5   r6   r6   r7   rT   N  s
    


zRegrtest.getloadavgc       
   
   C   s  | j jrD| j j\}}}|dk s&|dk rDd}t|tjdd td | jd k	rhddlm} || j | j | j j	rxt
d | j jt_t| j  | | | j jr|   td | j jr|   td tjd	kr.| jd kr.dd
lm} y| | _W n2 tk
r, }	 ztd|	  W d d }	~	X Y nX |   |   | j jrX| jrX|   |   |   | jrztd | jrtd | j jr| j rtd td d S )Nr   zwInvalid values for the --huntrleaks/-R parameters. The number of warmups and repetitions must be at least 1 each (1:1).T)r;   rP      r   )run_tests_workerzPress any key to continue...win32)WindowsLoadTrackerz%Failed to create WindowsLoadTracker:    r   )!r   r   rF   rG   r_   exitr4   rb   r   waitinputrS   r   ZPGOr   r   r   r   r   Ztest.libregrtest.win_utilsr   r2   FileNotFoundErrorr   r   Zverbose2r    r   r   r   r'   r   r#   )
r5   r   rd   r   r   r   r   r   r   errorr6   r6   r7   r   W  sN    






 


zRegrtest._main)F)N)__name__
__module____qualname____doc__r8   r:   rL   rZ   re   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rT   r   r6   r6   r6   r7   r      s.   *
'E
;4	 "
	r   c             K   s   t  jf d| i| dS )zRun the Python suite.r   N)r   r   )r   rd   r6   r6   r7   r     s    r   )N)*rU   r^   r   ri   r   r~   rp   rG   r   r   r*   r   Ztest.libregrtest.cmdliner   Ztest.libregrtest.runtestr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Ztest.libregrtest.setupr   Ztest.libregrtest.utilsr   r   r   r   r   r   r   r   r6   r6   r6   r7   <module>   s.   H     