a
    xd3                     @   s~   d dl m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 ejdv ZG dd deZdS )    )absolute_importN   )
TestFormat)win32cygwinc                   @   sD   e Zd Zg fddZdd Zdd Zdd Zd	d
 Zedd Z	dS )
GoogleTestc                 C   sD   t  | _t|d| _t|}tr,|d7 }||d h| _|| _d S )N;z.exe.py)setseen_executablesstrsplittest_sub_dirs
kIsWindowstest_suffixes	run_under)selfr   Ztest_suffixr   Z
exe_suffix r   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lit/formats/googletest.py__init__   s    zGoogleTest.__init__c              
   C   s   |  |ddg}ztj||jd}W nF tjyj } z,|d|t d |jf  W Y d }~d S d }~0 0 t	t
dd |dS )	Nz--gtest_list_testsz--gtest_filter=-*DISABLED_*)envz=unable to discover google-tests in %r: %s. Process output: %sr   c                 S   s   t j| dS )Nz  )litutilZ	to_string
startswith)liner   r   r   <lambda>*       z*GoogleTest.get_num_tests.<locals>.<lambda>F)
prepareCmd
subprocesscheck_outputenvironmentCalledProcessErrorwarningsysexc_infooutputsummap
splitlines)r   path	litConfiglocalConfigZlist_test_cmdoutexcr   r   r   get_num_tests   s&    
zGoogleTest.get_num_testsc                 c   s  d}t j }||}| jD ]\}tj||}	tj|	s@qt jj	|	| j
dD ]&}
tj|||
}|| jv r|d|  qRn| j| | |||}|d urV|}tt|| }||k r|dkr|d }tt|| }qt|D ]`}|||
t|t|f }d||jjtt t|t|gd }t jj|||||dV  qqR|||
d	f }t jj||||d
V  qRqd S )Ni   )suffixesz7Skip adding %r since it has been added to the test poolr      -z.json)	file_pathgtest_json_fileZ#failed_to_discover_tests_from_gtest)r2   )r   r   Zusable_core_countgetSourcePathr   osr)   joinisdirZlistdir_filesr   r   r"   addr.   intmathceilranger   confignamegetpidTest)r   Z	testSuitepath_in_suiter*   r+   Zinit_shard_sizeZ
core_countsource_pathsubdirZdir_pathfnZexecpathZ	num_testsZ
shard_sizeZnshardidxtestPathZ	json_filer   r   r   getTestsInDirectory-   sh    




zGoogleTest.getTestsInDirectoryc              
      s`  |j d u rtjjdfS tj| \}}tj|sTtj|\}}|d | }q*tj|\}}tj|\}}ddl	m
} ||j|jk}	d|j  |	rdndtjd|tjd	|d
}
|jj|
 |g |   |jr|j   |jrtjjdfS  fdd}||
}z$tjj |jj|jdd\}}W n^ tjjy } z@d|j d|j d}tjj| | d|j dfW  Y d }~S d }~0 0 tj|j sd|j  }d d| d}tjj|| | fS |dkrtjjdfS fdd}d}t|j dd}t |}|	r2t!|d |
d< ||
d }|d D ]}|d D ]}|d }|dksR|d krvqR|d! d" |d!  }d#d$" |f }d%|v rd}||7 }||}|r||d& 7 }|d% D ]}||d' d 7 }q|d7 }n|d(krR||7 }|d)7 }qRqFW d    n1 s.0    Y  |sT|d d| d7 }tjj|fS )*N /r   )	TestOrderzjson:10GTEST_TOTAL_SHARDSGTEST_SHARD_INDEX)ZGTEST_OUTPUTZGTEST_SHUFFLErM   rN   c                    s*   d dd |  D }d|d  f S )N c                 S   s   g | ]\}}|d  | qS )=r   ).0kvr   r   r   
<listcomp>   r   z@GoogleTest.execute.<locals>.get_shard_header.<locals>.<listcomp>zScript(shard):
--
%s %s
--
)r6   items)	shard_envZ
shard_envs)cmdr   r   get_shard_header   s    z,GoogleTest.execute.<locals>.get_shard_headerT)r   timeoutredirect_stderr
z

--
exit: z
--
zReached timeout of z secondsz$shard JSON output does not exist: %sc                    s   g }d|  }d|  }d}   D ]J}||r4d}q ||r\t|dkrN nd|  S |r || q ds|J d|  d S )Nz[ RUN      ] z[  FAILED  ] FTr   r[   z$gtest did not report the result for )r(   r   lenr6   append)Z	test_nameresheaderfooterZin_rangel)r,   r   r   get_test_stdout   s    

z+GoogleTest.execute.<locals>.get_test_stdoutFutf-8encodingZrandom_seedZGTEST_RANDOM_SEED
testsuites	testsuiteresult
SUPPRESSEDSKIPPEDr>   .#Script:
--
%s --gtest_filter=%s
--
rO   failuresz

failure	COMPLETEDunresolved test result
)#r3   r   r@   FAILr5   r)   r   r4   existsZlit.cl_argumentsrJ   orderZRANDOMenvirongetr=   r    updater   ZuseValgrindZvalgrindArgsZ	noExecutePASSr   ZexecuteCommandZmaxIndividualTestTimeZExecuteCommandTimeoutExceptionr,   exitCodeTIMEOUTopenjsonloadr   r6   )r   testr*   rF   ZtestNameZ
namePrefixZtotal_shardsZ	shard_idxrJ   Zuse_shufflerV   rX   Zshard_header_rx   eZ
stream_msgerrmsgrb   Zfound_failed_testfZjfr%   testcasetestinforh   testnamer_   Ztest_outfailr   )rW   r,   r   executeh   s    



 





0zGoogleTest.executec                 C   sL   |d  drtjg| }| jrHt| jtr8| j| }nt| j| }|S )a?  Insert interpreter if needed.

        It inserts the python exe into the command if cmd[0] ends in .py or caller
        specified run_under.
        We cannot rely on the system to interpret shebang lines for us on
        Windows, so add the python executable to the command if this is a .py
        script.
        r   r	   )endswithr#   
executabler   
isinstancelistshlexr   )r   rW   r   r   r   r      s    	zGoogleTest.prepareCmdc                 C   s  dd }||}dd | D }|| } |D ]J}t j|jsT| | || q*|jjp^d}d}t|jdd}zt	|d	 }W n> tj
y }	 z$td
|j d |	j W Y d }	~	n
d }	~	0 0 |D ]Z}
|
d D ]J}|d dkrq|jd d |
d |d f }tj|j||j|j}|
d d |d  }d|j|f }d}|d dkrbtjj}ndd|v rd}tjj}|}|d D ]}||d d 7 }qn(|d dkrtjj}ntjj}|d }t|d d d }tj|||}|jjpd|_||_|| }|| | | || qqW d    n1 s@0    Y  t |j |s*| r*| | || q*| |fS )Nc                 S   s   dd | D S )Nc                 S   s   g | ]}|j d u r|qS )Nr3   rQ   tr   r   r   rT      r   zOGoogleTest.post_process_shard_results.<locals>.remove_gtest.<locals>.<listcomp>r   )testsr   r   r   remove_gtest   s    z;GoogleTest.post_process_shard_results.<locals>.remove_gtestc                 S   s   g | ]}|j r|qS r   r   r   r   r   r   rT      r   z9GoogleTest.post_process_shard_results.<locals>.<listcomp>g        Frc   rd   rf   zFailed to parse json file: r[   rg   rh   ri   r>   rk   rl   rH   rj   rm   Trn   ro   rp   timer   )r5   r)   rr   r3   r]   rh   startrz   r{   r|   JSONDecodeErrorRuntimeErrordocrA   r   r@   Zsuiter=   r2   rj   rq   rw   Z
UNRESOLVEDfloatResultpidZ	setResultremoveZ	isFailure)Zselected_testsZdiscovered_testsr   Zgtestsr}   
start_timeZhas_failure_in_shardr   rf   r   r   r   rF   Zsubtestr   r_   r%   Z
returnCoder   Zelapsed_timer^   r   r   r   post_process_shard_results   s|    








.
z%GoogleTest.post_process_shard_resultsN)
__name__
__module____qualname__r   r.   rG   r   r   staticmethodr   r   r   r   r   r      s   ;jr   )
__future__r   r{   r:   r5   r   r   r#   Zlit.Testr   Zlit.TestRunnerZlit.utilbaser   platformr   r   r   r   r   r   <module>   s   
