o
    tPf                  	   @   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 d dl	m
Z
 d dlmZ d dlmZ d dlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ze d W n e!e"fy   Y nw dZ#e d Z$e%ej&'dpe(e$dZ)e)dkrdZ)erdZ#de#iZ*eresdndZ)dd Z+dd  Z,G d!d" d"e-Z.G d#d$ d$e/Z0G d%d& d&e-Z1G d'd( d(e-Z2G d)d* d*e-Z3d+d, Z4d@d.d/Z5d0d1 Z6dAd3d4Z7		5dBd6d7Z8d8d9 Z9dCd;d<Z:d=d> Z;e<d?kre;  dS dS )D    N)contextmanager)	timedelta)
ThreadPool)	cpu_count)Lazy   )util)parse_resources)setup_resources)unparse_resources)RUNNING_ON_CI)PYPY)PY2)RESOLVER_ARES)RUN_LEAKCHECKS)OSX)six)travis	_testcapid   NWORKERS      
      timeout   c                 C   s*   t j| s|rt|}t j|| S | S N)ospathisfile_dir_from_package_namejoin)filenamepackagepackage_dir r&   Z/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/testing/testrunner.py_package_relative_filename>   s   r(   c                 C   s   t | }tj|j}|S r   )	importlibimport_moduler   r   dirname__file__)r$   package_modr%   r&   r&   r'   r!   E   s   
r!   c                   @   s0   e Zd Zdd Zdd Zdd Zedd Zd	S )
ResultCollectorc                 C   s.   d| _ i | _i | _d| _d| _g | _i | _d S )Nr   )totalfailedpassedtotal_casestotal_skipped_all_resultsreranselfr&   r&   r'   __init__M   s   
zResultCollector.__init__c                 C   sN   | j | |s|| j|j< nd| j|j< |  j|j7  _|  j|j7  _| S )NT)	r4   appendr0   namer1   r2   	run_countr3   skipped_countr7   resultr&   r&   r'   __iadd__W   s   zResultCollector.__iadd__c                 C   s   | j | || j|j< | S )z
        collector <<= result

        Stores the result, but does not count it towards
        the number of cases run, skipped, passed or failed.
        )r4   r9   r5   r:   r=   r&   r&   r'   __ilshift__b   s   zResultCollector.__ilshift__c                 C   s   t | jtdddS )zk
        A new list of RunResult objects, sorted from longest running
        to shortest running.
        run_durationT)keyreverse)sortedr4   operator
attrgetterr6   r&   r&   r'   longest_running_testsm   s   z%ResultCollector.longest_running_testsN)__name__
__module____qualname__r8   r?   r@   propertyrG   r&   r&   r&   r'   r.   K   s    
r.   c                   @   s   e Zd ZdS )FailFastN)rH   rI   rJ   r&   r&   r&   r'   rL   x   s    rL   c                   @   sh   e Zd ZdZdZdddddeddddZdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdddZdS )Runnerg?g?r&   Fallowed_return_codesconfigured_failing_testsfailfastquietconfigured_run_alone_testsworker_countsecond_chancec          	      C   sj   || _ || _|| _|| _|r|rJ || _|| _t | _t| j | j_	g | _
tt||p.d| _|| _dS )a  
        :keyword allowed_return_codes: Return codes other than
           0 that are counted as a success. Needed because some versions
           of Python give ``unittest`` weird return codes.
        :keyword quiet: Set to True or False to explicitly choose. Set to
            `None` to use the default, which may come from the environment variable
            ``GEVENTTEST_QUIET``.
        r   N)_tests_configured_failing_tests_quiet_configured_run_alone_tests	_failfast_second_chancer.   resultslenr/   _running_jobsmin_worker_count_allowed_return_codes)	r7   testsrO   rP   rQ   rR   rS   rT   rU   r&   r&   r'   r8      s   
zRunner.__init__c                 K   s   | j |d< | jd ur| j|d< tj|fi |}|s6| jr6|  j|K  _tjd|jdd tj|fi |}|s?| jr?t	||  j|7  _d S )NrO   rR   z> %swarningcolor)
ra   rX   r   runr[   r\   logr:   rZ   rL   )r7   cmdkwargsr>   r&   r&   r'   _run_one   s   




zRunner._run_onec                 C   sV   | j dd D ]}| sq| r| j | q|  td|f  qt| j S )zLClean up the list of running jobs, returning how many are still outstanding.Nz#Internal error in testrunner.py: %r)r^   ready
successfulremovegetsysexitr]   )r7   rr&   r&   r'   _reap   s   
zRunner._reapc                 C   sB   t jdt| jdd | jr|  sd S t | j | jsd S d S )NzReaping %d jobsdebugrd   )r   rg   r]   r^   rr   sleepTIME_WAIT_REAPr6   r&   r&   r'   	_reap_all   s   zRunner._reap_allc                 C   sD   	 |   | jk r|| j|f|pi }| j| d S t| j qr   )	rr   r`   apply_asyncrj   r^   r9   r   rt   TIME_WAIT_SPAWN)r7   poolrh   optionsjobr&   r&   r'   _spawn   s   zRunner._spawnc              	   C   s   t d| jt ddt f  | jdkp| jt _t  }z|   W n t	y;   | j
t  | dd t d     t   |   | j
t  | dd d S )	Nz1Running tests in parallel with concurrency %s %s.numberz(concurrency available: %d)r   F)rp   z(partial results)
T)r   rg   r`   	_colorizeAVAIL_NWORKERSrX   BUFFER_OUTPUTperf_counter
_run_testsKeyboardInterrupt_report	traceback	print_excrv   )r7   startr&   r&   r'   __call__   s"   
zRunner.__call__c                 C   s   g }| j }t| j}zE|D ]\}}|pi }t| j|r#|||f q| ||| q|  |  |rJt	
d |D ]\}}| j|fi | q<W dS W dS  typ   zt	
d |   W Y dS  tyo   |   w    |   )z#Runs the tests, produces no report.zRunning tests marked standalonez*Waiting for currently running to finish...N)rV   r   r`   matchesrY   r9   r|   closer"   r   rg   rj   r   rv   	terminate)r7   	run_alonerb   ry   rh   rz   r&   r&   r'   r      s8   


zRunner._run_testsc                 C   s   | j }t|||| jd d S )N)rp   tookrP   )r\   reportrW   )r7   elapsed_timerp   r\   r&   r&   r'   r     s   
zRunner._reportNF)rH   rI   rJ   ru   rx   DEFAULT_NWORKERSr8   rj   rr   rv   r|   r   r   r   r&   r&   r&   r'   rM   {   s$    "	rM   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TravisFoldingRunnerc                    s<   |_ |_ttt _|j  fdd}||_d S )Nc                      s(      z
   W   d S   w r   )_begin_fold	_end_foldr&   	run_testsr7   r&   r'   r     s   z0TravisFoldingRunner.__init__.<locals>._run_tests)_runner_travis_fold_msgstrintr   r   _travis_fold_namer   )r7   runnertravis_fold_msgr   r&   r   r'   r8     s   
zTravisFoldingRunner.__init__c                 C   s   t | j| j d S r   )r   
fold_startr   r   r6   r&   r&   r'   r   #  s   zTravisFoldingRunner._begin_foldc                 C   s   t | j d S r   )r   fold_endr   r6   r&   r&   r'   r   '  s   zTravisFoldingRunner._end_foldc                 C   s   |   S r   )r   r6   r&   r&   r'   r   *  s   zTravisFoldingRunner.__call__N)rH   rI   rJ   r8   r   r   r   r&   r&   r&   r'   r     s
    r   c                   @   sh   e Zd ZdZdZ							dddZG dd deZee	d	d
 Z
edd Zdd Zdd ZdS )	DiscoveryNr&   FTc           	      C   s   |pi | _ t|p	d| _|| _|dt | _|| _|r1|d}|D ]}| jtt	|| q#|r>| j|dt  |rJ|| _
t|| _d S d S )Nr&   TEST_FILE_OPTIONS,IGNORE_COVERAGE)configsetignorerb   rn   configured_test_optionsallow_combinesplitupdateload_list_from_filer$   r!   r%   )	r7   rb   ignore_filesignoredcoverager$   r   r   fr&   r&   r'   r8   2  s   


zDiscovery.__init__c                   @   s   e Zd Zdd ZeededfddZedd Zed	d
 Z	eededededfddZ
dd Zdd ZeedfddZdd Zdd Zdd Zedd Zd d! Zd*d#d$Zd%d& Zd'd( Zd)S )+zDiscovery.Discoveredc                 C   sR   t  | _|d | _|d | _|| _|| _|| _|| _g | _	g | _
g | _g | _d S )N	RUN_ALONEFAILING_TESTS)r   getcwdorig_dirconfigured_run_alonerP   r$   r   r   r   	to_importstd_monkey_patch_filesno_monkey_patch_filescommands)r7   r$   r   r   r   r   r&   r&   r'   r8   N  s   



zDiscovery.Discovered.__init__s   [^#].*patch_all\(\)s       .*patch_all\(\)c                 C   s   t || o||  S r   )boolsearch)contents_patch_present_patch_indentedr&   r&   r'   __makes_simple_monkey_patch]  s   
z0Discovery.Discovered.__makes_simple_monkey_patchc                 C      d| vS )Ns   testrunner-no-monkey-combiner&   r   r&   r&   r'   __file_allows_monkey_combinek     z1Discovery.Discovered.__file_allows_monkey_combinec                 C   r   )Ns   testrunner-no-combiner&   r   r&   r&   r'   __file_allows_combineo  r   z*Discovery.Discovered.__file_allows_combines       greentest.main\(\)s       unittest.main\(\)s    from gevent.testing import.*mains       main\(\)c                 C   s(   | | p| | p| | o| | S r   r   )r   _greentest_main_unittest_main_import_main_mainr&   r&   r'   __calls_unittest_main_toplevels  s
   
z3Discovery.Discovered.__calls_unittest_main_toplevelc                 C   s$   t p|| jv p|| jv pt| j|S r   )r   r   r   r   rP   )r7   r#   r&   r&   r'   __has_config  s   
z!Discovery.Discovered.__has_configc                 C   s:   | j o| | o| |o| |o| |o| |S r   )r   _Discovered__has_config&_Discovered__makes_simple_monkey_patch'_Discovered__file_allows_monkey_combine _Discovered__file_allows_combine)_Discovered__calls_unittest_main_toplevelr7   r#   r   r&   r&   r'   __can_monkey_combine  s   
z)Discovery.Discovered.__can_monkey_combines   [^#].*patch_\w*\(c                 C   s   | |  S r   r   )r   r   r&   r&   r'   __makes_no_monkey_patch  s   z,Discovery.Discovered.__makes_no_monkey_patchc                 C   s0   | j o| | o| |o| |o| |S r   )r   r   "_Discovered__makes_no_monkey_patchr   r   r   r&   r&   r'   __can_nonmonkey_combine  s   
z,Discovery.Discovered.__can_nonmonkey_combinec                 C   s   t jdg}|S )N-u)ro   
executable)r7   rh   r&   r&   r'   __begin_command  s   
z$Discovery.Discovered.__begin_commandc                 C   s   d|v r| j | d S | ||r"| j| jr| d S | d S | ||r8| j| jr3| d S | d S |  }| jrG|d|  n|| t	 }|
| j|i  | j||f d S )Ns
   TESTRUNNER-m)r   r9   _Discovered__can_monkey_combiner   r$   "_Discovered__can_nonmonkey_combiner   _Discovered__begin_commandDEFAULT_RUN_OPTIONScopyr   r   rn   r   )r7   qualified_namer#   r   rh   rz   r&   r&   r'   
__add_test  s     	
zDiscovery.Discovered.__add_testc                 C   s   dd | D S )Nc                 S   s   g | ]}|r| d s|qS )-)
startswith).0xr&   r&   r'   
<listcomp>  s    z9Discovery.Discovered.__remove_options.<locals>.<listcomp>r&   )lstr&   r&   r'   __remove_options  s   z%Discovery.Discovered.__remove_optionsc                 C   s^   | j D ]#}t|}| D ]\}}| |d | jv rq| j||f qq| j d d = d S )N)r   r)   r*   
TESTRUNNER_Discovered__remove_optionsr   r   r9   )r7   r   modulerh   rz   r&   r&   r'   __expand_imports  s   

z%Discovery.Discovered.__expand_imports   c           	         s   |sd S ddl m} ddg  fdd}|||D ]'\}}|  }|d |d |D ]}|| q.| jd|t f q|d d = d S )Nr   )groupbyc                    s<    d krd d<  d  d7  <  d  d7  <  d S )Nr   r   r&   )_cnt
group_sizer&   r'   
make_group  s
   z;Discovery.Discovered.__combine_commands.<locals>.make_groupr   unittest)	itertoolsr   r   r9   r   insertr   r   )	r7   filesr   r   r   r   grouprh   r:   r&   r   r'   __combine_commands  s   

z'Discovery.Discovered.__combine_commandsc                 C   s   | dr| }}|tdd d  }|dtjd }ntj|d }| jr/| jd | n|}tj|}tj	|sN|
dsNtj	|d rN|d7 }t|d}| }W d    n1 sbw   Y  | ||| d S )Ngevent.testsr   ..pyr   rb)r   r]   replacer   sepr   splitextr$   abspathexistsendswithopenread_Discovered__add_test)r7   r#   r   module_nameabs_filenamer   r   r&   r&   r'   
visit_file  s$   



zDiscovery.Discovered.visit_filec                 C   sd   |D ]}|  | qt| j |   W d    n1 sw   Y  | | j | | j d S r   )r  r   _in_dirr   _Discovered__expand_imports_Discovered__combine_commandsr   r   )r7   	filenamesr#   r&   r&   r'   visit_files   s   
z Discovery.Discovered.visit_filesNr   )rH   rI   rJ   r8   staticmethodrecompiler   r   r   r   r   r   r   r   r   r  r   r
  r  r  r  r&   r&   r&   r'   
DiscoveredM  s:    


	

	r  c              	   c   s<    t  }| rt |  zd V  W t | d S t | w r   )r   r   chdir)r%   olddirr&   r&   r'   r	  (  s   
zDiscovery._in_dirc                 C   s   | j }| | j| j| j| j| j}| | j- |s't	t

dt	dg }nt	|}| jr3|| j8 }t|}|| W d    |S 1 sGw   Y  |S )Nz	test_*.pyztest_support.py)rb   r  r$   r   r   r   r   r	  r%   r   globrD   r  )r7   rb   
discoveredr&   r&   r'   r  3  s    

zDiscovery.discoveredc                 C      t | jjS r   )iterr  r   r6   r&   r&   r'   __iter__M     zDiscovery.__iter__c                 C   r  r   )r]   r  r   r6   r&   r&   r'   __len__P  r  zDiscovery.__len__)NNr&   FNNT)rH   rI   rJ   r%   r$   r8   objectr  r  r   r	  r   r  r  r  r&   r&   r&   r'   r   .  s(    
 \	
r   c                 C   sh   g }| r2t t| |}|D ]}|ddd  }|r!|| qW d    |S 1 s-w   Y  |S )N#r   r   )r  r(   r   stripr9   )r#   r$   r>   r   r   r&   r&   r'   r   S  s   

r   Tc                 C   sz   t |tr
d|}| D ].}|s|drq|dd}|d| s,||ddr/ dS d|vr:||kr: dS qdS )N zFLAKY  r   TF)
isinstancelistr"   r   r   r  )possibilitiescommandinclude_flakyliner&   r&   r'   r   _  s   

 r   c                 C   s<   | dk rd|  S t tt| d} | dr| dd  } | S )N   z%.1fssecondsz0:r   )r   r   roundr   r(  r&   r&   r'   format_secondsq  s   
r+  r   c                 C   s~   | j }|sd S td td|d j }dt| d }t }|D ]}t||j|j ||j t||kr< d S q"d S )Nz
Longest-running tests:z%.1fr   %z.1f seconds: %s)	rG   r   rg   r]   rA   r   r   r:   add)result_collectorhow_manyrG   #length_of_longest_formatted_decimalfrmt
seen_namesr>   r&   r&   r'   _show_longest_runningz  s   
r3  r&   c              	   C   s  | j }| j}| j}| j}| j}t|  |rdt| }nd}g }	g }
g }|D ]}t||ddr4|| q&|rFt	j
dt||dd t| | jrZt	j
dt| j|d	d t| j |rt	j
d
t|||d	d |D ]}t||ddry|	| qj|
| qj|	rt	j
dt|	|d	d t|	 |
rt	j
dt|
|dd t|
 t	
d||rt	dd| nd|| |r|
rttdt|
 |rtd |dkrtd d S d S d S )Nz in %sr   F)r%  z
%s/%s unexpected passeserrorrd   z
%s/%s tests rerunrc   z
%s/%s tests failed%sTz
%s/%s expected failuresz
%s/%s unexpected failuresz
Ran %s tests%s in %s files%sskippedz (skipped=%d)r   e   r   zNo tests found.)r/   r0   r1   r2   r3   r3  r+  r   r9   r   rg   r]   
print_listr5   r~   ro   rp   r_   )r.  rp   r   rP   r/   r0   r1   r2   r3   failed_expectedfailed_unexpectedpassed_unexpectedr:   r&   r&   r'   r     sd   


r   c                 C   s   | D ]}t d| qd S )Nz - %s)r   rg   )r   r:   r&   r&   r'   r7    s   r7  Fc                 C   s8  dd }|dr(t jrt jdgkr(ddg}ts|d d|g d tjd< |d	r1d
tjd	< |dr<| r<dtjd< |drG| rGdtjd< |drPdtjd< |dr[| r[dtjd< t jjdkrm| smdtjd< dtjd< tj	dd  dd tjD }t
dd |D }t| D ]\}}tjd|||dd qd S )Nc                 S   s   t tj|  S r   )r   r   environrn   )rB   r&   r&   r'   not_set  s   z_setup_environ.<locals>.not_setPYTHONWARNINGSdefaultzdefault::DeprecationWarningzdefault::ResourceWarningr   )zignore:::site:zignore:::pkgutil:zignore:::importlib._bootstrap:z'ignore:::importlib._bootstrap_external:z)ignore:::pkg_resources._vendor.pyparsing:zignore:::dns.namedict:zignore:::dns.hash:zignore:::dns.zone:PYTHONFAULTHANDLERtrueGEVENT_DEBUGrs   PYTHONTRACEMALLOC10PYTHONDEVMODE1PYTHONMALLOCfinalr   PYTHONSAFEPATHc                 S   s"   i | ]}| d r|tj| qS ))PYTHONGEVENT)r   r   r;  r   kr&   r&   r'   
<dictcomp>%  s    
z"_setup_environ.<locals>.<dictcomp>c                 s   s    | ]}t |V  qd S r   )r]   rK  r&   r&   r'   	<genexpr>*  s    z!_setup_environ.<locals>.<genexpr>z%*s	=	%srd   )ro   warnoptionsr   r9   r"   r   r;  version_inforeleaselevelpopmaxrD   itemsr   rg   )rs   r<  defaultsinteresting_envswidest_krL  vr&   r&   r'   _setup_environ  s@   







rY  c               
   C   s  dd l } |  }|d |jdddd |jddd	d
 |jdddd |jddddd |jdddd |jddddd |jdddd
 |jddttdd |jdddd d! |jd"d#d$d%td&d' |jd(d)d*d+ | }|jd,ddd-d |jd.d/ddd0d |jd1d2d3 | }tt	|j
d u rt n|j
|_
t|j
tjd4< t|j
 |jt_d5tjvrt|jtjd5< g }g }g }d}|jstjd6rEtrtrtd7 nmd}tjd8 }tjd9< trtjd: }tjd9< tjt}	tj|	d;}
tj|
}
|
tj tjd<d= tjd<< tjd>tj d? tjd@< tdAtjd@ dB|
dC| tj|s7J tjtj|
dDsEJ t |j!dE |j"ri }t#|j"|j$|_"t%|j"}|& }W d    n	1 snw   Y  t'(|| |dF }|dG }|dH }t)|j*|j+|||j$||j,dI}|j-r|D ]\}}ttj.||dJ|dKdL qtdMt/|  d S trt0rtdN d S |j$rt1|j$}tjd<d=tj | tjd<< dO}t2j3d dP dQkr|dR7 }t4||||j5|j||j6|j7dS}|j8rt9||j8}|  d S )TNr   z--ignorez
--discover
store_truezOnly print the tests found.)actionhelpz--configzknown_failures.pyzkThe path to the config file containing FAILING_TESTS, IGNORED_TESTS and RUN_ALONE. Defaults to %(default)s.)r>  r\  z
--coveragez+Enable coverage recording with coverage.py.z--quietTzRBe quiet. Defaults to %(default)s. Also the GEVENTTEST_QUIET environment variable.)r[  r>  r\  z	--verbosestore_falserR   )r[  destz--debugFzEnable debug settings. If the GEVENT_DEBUG environment variable is not set, this sets it to 'debug'. This can also enable PYTHONTRACEMALLOC and the debug PYTHONMALLOC allocators, if not already set. Defaults to %(default)s.z	--packager   z;Load tests from the given package. Defaults to %(default)s.z--processesz-jz[Use up to the given number of parallel processes to execute tests. Defaults to %(default)s.)r>  typer\  z--no-combinez)Do not combine tests into process groups.)r>  r[  r\  r   z--usezRES1,RES2,...storea  specify which special resource intensive tests to run. "all" is the default; "none" may also be used. Disable individual resources with a leading -.For example, "-u-network". GEVENTTEST_USE_RESOURCES is used if no argument is given. To only use one resources, specify "-unone,resource".)metavarr[  r_  r\  z--travis-foldMSGz2Emit Travis CI log fold markers around the output.)ra  r\  z--second-chancez"Give failed tests a second chance.z
--failfastz-xz%Stop running after the first failure.rb   *)nargsGEVENTTEST_USE_RESOURCESGEVENTTEST_QUIETGEVENTTEST_COVERAGEz,Ignoring coverage option on PyPy on CI; slowz.coveragercCOVERAGE_PROCESS_STARTz.coveragerc-pypycoveragesite
PYTHONPATHr   r   z	.coverageCOVERAGE_FILEzEnabling coverage toz	with sitezand configuration filezsitecustomize.py)rs   r   IGNORED_TESTSr   )r   r   r   r$   r   r   envsetenv)rm  rn  z%s tests found.zDNot running tests on pypy with c-ares; not a supported configurationr&      )ro     r   r  rN   ):argparseArgumentParseradd_argumentr   r   r	   add_mutually_exclusive_group
parse_argsr"  r   user   r   r;  r
   rR   r   QUIETr   r   rn   r   r   printr   r   r+   r,   r"   pathsepr   r  rY  rs   r   r(   r$   r  r  r   exec_r   rb   r   
no_combinediscovergetnamer]   r   r!   ro   rP  rM   rQ   	processesrU   travis_foldr   )rq  parserfail_parserrz   r   rl  r   r   
cov_configthis_dirsite_dirr   r   config_datarb   rh   r%   rO   r   r&   r&   r'   main/  s   

 



	$

r  __main__)Tr  )TNr&   r   )=r  ro   r   r  rE   r   r)   
contextlibr   datetimer   multiprocessing.poolr   multiprocessingr   gevent._utilr   r   r   	resourcesr	   r
   r   sysinfor   r   r   r   r   r   r   r   
__import__ImportErrorOSErrorTIMEOUTr   r   r;  rn   rS  r   r   r(   r!   r  r.   	ExceptionrL   rM   r   r   r   r   r+  r3  r   r7  rY  r  rH   r&   r&   r&   r'   <module>   s   
-   '

	
C
Z 
0
