3
,6`L                 @   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mZ d dlmZ ejZG dd de
jZdd Zedkre  dS )	    N)support)script_helperc               @   s  e Zd Zdd Zdd Zdd Zejd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ejeed d!d"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"d:d; Z#d<d= Z$d>d? Z%d@dA Z&dBdC Z'dDdE Z(dFdG Z)G dHdI dIZ*G dJdK dKZ+dLdM Z,dNdO Z-dPdQ Z.d]dSdTZ/dUdV Z0dWdX Z1dYdZ Z2d[d\ Z3dRS )^TestSupportc             C   s    t jd | jtjt jd d S )Nftplibfoo)r   import_moduleassertRaisesunittestSkipTest)self r   '/usr/lib/python3.6/test/test_support.pytest_import_module   s    
zTestSupport.test_import_modulec             C   s   t jd d S )Nr   )r   Zimport_fresh_module)r   r   r   r   test_import_fresh_module   s    z$TestSupport.test_import_fresh_modulec             C   s.   | j tj| d| j | jtjtj| d d S )Ntest_get_attributer   )assertEqualr   Zget_attributer   r   r	   r
   )r   r   r   r   r      s    zTestSupport.test_get_attributezfailing buildbotsc             C   s   | j tj tj d S )N)r   r   Zget_original_stdoutsysstdout)r   r   r   r   test_get_original_stdout    s    z$TestSupport.test_get_original_stdoutc             C   s2   dd l }| jdtj tjd | jdtj d S )Nr   sched)r   assertInr   modulesr   ZunloadassertNotIn)r   r   r   r   r   test_unload$   s    
zTestSupport.test_unloadc          
   C   s@   t td}W d Q R X tjt | jtjjt tjt d S )Nw)openTESTFNr   unlinkassertFalseospathexists)r   fr   r   r   test_unlink*   s
    

zTestSupport.test_unlinkc             C   s$  t jd }tjj|d}tj| tj| t j| | jtjj| t j	t dd t j| W d Q R X tj| tj| tj
|tjtjB  t j	t dd t j| W d Q R X | jtjj| tj| tj| tj
|d t j	t dd t j| W d Q R X | jtjj| d S )NdZsubdirverboser   )r   r   r   r    joinmkdirrmtreer   r!   	swap_attrchmodstatS_IRUSRS_IXUSR)r   dirpathZ
subdirpathr   r   r   test_rmtree1   s(    







zTestSupport.test_rmtreec             C   s   t d }t|d}td|d W d Q R X tjjdtj tj	  z2t
t }| jt tj tjt  | jt tj W d tjd= tj| tjd X d S )Nz.pyr   zfoo = 1)filer   __pycache__)r   r   printr   r    insertr   curdir	importlibinvalidate_caches
__import__r   r   r   Zforgetr   r   r(   )r   Zmod_filenamer"   modr   r   r   test_forgetI   s    

zTestSupport.test_forgetc             C   s$   t j  }|jtjdf |j  d S )Nr   )socketbindr   HOSTclose)r   sr   r   r   	test_HOSTZ   s    zTestSupport.test_HOSTc             C   s,   t j }tj }|jt j|f |j  d S )N)r   Zfind_unused_portr:   r;   r<   r=   )r   Zportr>   r   r   r   test_find_unused_port_   s    z!TestSupport.test_find_unused_portc             C   s&   t j  }tj| |j  |j  d S )N)r:   r   Z	bind_portZlistenr=   )r   r>   r   r   r   test_bind_porte   s    
zTestSupport.test_bind_portc             C   s   t j }tjj|}zjtjj|d}| jtjj| tj	|$}| j
|| | jtjj| W dQ R X | jtjj| W dtj| X dS )z8Test that temp_dir() creates and destroys its directory.ZtempN)tempfilemkdtempr   r    realpathr&   r   isdirr   temp_dirr   
assertTruer(   )r   
parent_dirr    	temp_pathr   r   r   test_temp_dirm   s    zTestSupport.test_temp_dirc          
   C   s<   t j }| jtjj| W dQ R X | jtjj| dS )zTest passing no path.N)r   rF   rG   r   r    rE   r   )r   rI   r   r   r   test_temp_dir__path_none|   s    
z$TestSupport.test_temp_dir__path_nonec             C   sd   dd }t j }tjj|}z6| jtjj| | jt|| | jtjj| W dt	j
| X dS )z-Test passing a directory that already exists.c          	   S   s"   t j| }tdW d Q R X d S )Nzshould not get here)r   rF   	Exception)r    rI   r   r   r   call_temp_dir   s    zMTestSupport.test_temp_dir__existing_dir__quiet_default.<locals>.call_temp_dirN)rB   rC   r   r    rD   rG   rE   r   FileExistsErrorshutilr(   )r   rM   r    r   r   r   *test_temp_dir__existing_dir__quiet_default   s    z6TestSupport.test_temp_dir__existing_dir__quiet_defaultc             C   s   t j }tjj|}z`tj <}tj|dd}| j|| W dQ R X dd |j	D }W dQ R X | j
tjj| W dtj| X d| g}| j|| dS )z=Test passing a directory that already exists with quiet=True.T)quietNc             S   s   g | ]}t |jqS r   )strmessage).0r   r   r   r   
<listcomp>   s    zGTestSupport.test_temp_dir__existing_dir__quiet_true.<locals>.<listcomp>z+tests may fail, unable to create temp dir: )rB   rC   r   r    rD   r   check_warningsrF   r   warningsrG   rE   rO   r(   )r   r    recorderrI   rW   expectedr   r   r   'test_temp_dir__existing_dir__quiet_true   s    

z3TestSupport.test_temp_dir__existing_dir__quiet_trueforkztest requires os.forkc             C   s   t jdtjd dS )z?Test that a forked child process does not remove the directory.z-ca  
            import os
            from test import support
            with support.temp_cwd() as temp_path:
                pid = os.fork()
                if pid != 0:
                    # parent process (child has pid == 0)

                    # wait for the child to terminate
                    (pid, status) = os.waitpid(pid, 0)
                    if status != 0:
                        raise AssertionError(f"Child process failed with exit "
                                             f"status indication 0x{status:x}.")

                    # Make sure that temp_path is still present. When the child
                    # process leaves the 'temp_cwd'-context, the __exit__()-
                    # method of the context must not remove the temporary
                    # directory.
                    if not os.path.isdir(temp_path):
                        raise AssertionError("Child removed temp_path.")
        N)r   Zassert_python_oktextwrapZdedent)r   r   r   r   test_temp_dir__forked_child   s    
z'TestSupport.test_temp_dir__forked_childc             C   sb   t j }tj 8}tj|"}| j|| | jt j | W d Q R X W d Q R X | jt j | d S )N)r   getcwdr   rF   
change_cwdr   )r   original_cwdrI   new_cwdr   r   r   test_change_cwd   s    
$zTestSupport.test_change_cwdc             C   sT   t j }dd }tj "}t jj|d}| jt|| W dQ R X | jt j | dS )z&Test passing a non-existent directory.c          	   S   s"   t j| }tdW d Q R X d S )Nzshould not get here)r   r_   rL   )r    ra   r   r   r   call_change_cwd   s    zFTestSupport.test_change_cwd__non_existent_dir.<locals>.call_change_cwddoes_not_existN)	r   r^   r   rF   r    r&   r   FileNotFoundErrorr   )r   r`   rc   rH   Znon_existent_dirr   r   r   !test_change_cwd__non_existent_dir   s    
z-TestSupport.test_change_cwd__non_existent_dirc             C   s   t j }tj n}t jj|d}tj L}tj|dd"}| j|| | jt j | W dQ R X dd |j	D }W dQ R X W dQ R X d| g}| j|| dS )z6Test passing a non-existent directory with quiet=True.rd   T)rQ   Nc             S   s   g | ]}t |jqS r   )rR   rS   )rT   r   r   r   r   rU      s    zMTestSupport.test_change_cwd__non_existent_dir__quiet_true.<locals>.<listcomp>z)tests may fail, unable to change CWD to: )
r   r^   r   rF   r    r&   rV   r_   r   rW   )r   r`   rH   Zbad_dirrX   ra   rW   rY   r   r   r   -test_change_cwd__non_existent_dir__quiet_true   s    

$
z9TestSupport.test_change_cwd__non_existent_dir__quiet_truec             C   s\   t d }tj 0}tj|dd W dQ R X dd |jD }W dQ R X | j|d| g dS )z0Check the warning message when os.chdir() fails.Z_does_not_existT)r    rQ   Nc             S   s   g | ]}t |jqS r   )rR   rS   )rT   r   r   r   r   rU      s    z>TestSupport.test_change_cwd__chdir_warning.<locals>.<listcomp>z)tests may fail, unable to change CWD to: )r   r   rV   r_   rW   r   )r   r    rX   Zmessagesr   r   r   test_change_cwd__chdir_warning   s    

z*TestSupport.test_change_cwd__chdir_warningc          
   C   s^   t j }tjtd | jt jjt j t W d Q R X | jt jj	t | jt j | d S )N)name)
r   r^   r   temp_cwdr   r   r    basenamer   r!   )r   herer   r   r   test_temp_cwd   s
    "zTestSupport.test_temp_cwdc          
   C   sb   t j }tjdd4}| j|| | jt jj| | jt j | W dQ R X | jt j | dS )z Test passing None to temp_cwd().N)ri   )	r   r^   r   rj   ZassertNotEqualrG   r    rE   r   )r   r`   ra   r   r   r   test_temp_cwd__name_none   s    z$TestSupport.test_temp_cwd__name_nonec             C   s   | j tjddddd d S )N         )ro   rp   rq   z{1: 1, 2: 2, 3: 3})r   r   Zsortdict)r   r   r   r   test_sortdict  s    zTestSupport.test_sortdictc          
   C   s@   t j }| jt}tj|d W d Q R X | j|jjtj	 d S )Ns   foo)
r   Zmake_bad_fdr   OSErrorr   writer   Z	exceptionerrnoZEBADF)r   fdcmr   r   r   test_make_bad_fd
  s    zTestSupport.test_make_bad_fdc          
   C   s8   t j| dddd | jt t j| d W d Q R X d S )Nz	def classrq   	   )linenooffsetzx=1)r   Zcheck_syntax_errorr   AssertionError)r   r   r   r   test_check_syntax_error  s    z#TestSupport.test_check_syntax_errorc          	   C   s,   dd l }tjd |jd W d Q R X d S )Nr   Zasyncore)r5   r   ZCleanImportr   )r   r5   r   r   r   test_CleanImport  s    zTestSupport.test_CleanImportc          
   C   sT   t jdd" | jdtj | jdtj W d Q R X | jdtj | jdtj d S )Nr   Zbar)r   ZDirsOnSysPathr   r   r    r   )r   r   r   r   test_DirsOnSysPath  s
    zTestSupport.test_DirsOnSysPathc          
   C   s0   t j }td W d Q R X | j|j d d S )Nhellozhello
)r   Zcaptured_stdoutr2   r   getvalue)r   r   r   r   r   test_captured_stdout!  s    
z TestSupport.test_captured_stdoutc             C   s6   t j }tdtjd W d Q R X | j|j d d S )Nr   )r0   zhello
)r   Zcaptured_stderrr2   r   stderrr   r   )r   r   r   r   r   test_captured_stderr&  s    
z TestSupport.test_captured_stderrc          
   C   s>   t j  }|jd |jd t }W d Q R X | j|d d S )Nzhello
r   r   )r   Zcaptured_stdinrt   seekinputr   )r   stdinZcapturedr   r   r   test_captured_stdin+  s
    


zTestSupport.test_captured_stdinc             C   s   t j  d S )N)r   Z
gc_collect)r   r   r   r   test_gc_collect3  s    zTestSupport.test_gc_collectc             C   s   | j tj t d S )N)ZassertIsInstancer   Zpython_is_optimizedbool)r   r   r   r   test_python_is_optimized6  s    z$TestSupport.test_python_is_optimizedc             C   s   G dd d}| }d|_ tj|dd }| j|j d | j|d W d Q R X | j|j d tj|dd}| j|jd | j| W d Q R X | jt|d tj|dd
 |`W d Q R X | jt|d d S )Nc               @   s   e Zd ZdS )z'TestSupport.test_swap_attr.<locals>.ObjN)__name__
__module____qualname__r   r   r   r   Obj:  s   r   rq   x   y)r   r   r)   r   r   assertIsNoner   hasattr)r   r   objr   r   r   r   r   test_swap_attr9  s    zTestSupport.test_swap_attrc             C   s   ddi}t j|dd"}| j|d d | j|d W d Q R X | j|d d t j|dd }| j|d d | j| W d Q R X | jd| t j|dd |d= W d Q R X | jd| d S )Nr   rq   r   r   )r   Z	swap_itemr   r   r   )r   Dr   r   r   r   r   test_swap_itemJ  s    zTestSupport.test_swap_itemc               @   s   e Zd ZdZdZdZdZdS )zTestSupport.RefClassN)r   r   r   
attribute1
attribute2Z_hidden_attribute1__magic_1__r   r   r   r   RefClassX  s   r   c               @   s   e Zd ZdZdZdZdZdS )zTestSupport.OtherClassN)r   r   r   r   
attribute3r   __magic_2__r   r   r   r   
OtherClass^  s   r   c             C   sB   t j| j| j}| jdh| t j| j| j}| jddh| d S )Nr   r   r   )r   detect_api_mismatchr   r   r   )r   missing_itemsr   r   r   test_detect_api_mismatchd  s    z$TestSupport.test_detect_api_mismatchc             C   sT   ddddg}t j| j| j|d}| jt | t j| j| j|d}| jt | d S )Nr   r   r   Znot_in_either)ignore)r   r   r   r   r   set)r   r   r   r   r   r    test_detect_api_mismatch__ignorem  s    z,TestSupport.test_detect_api_mismatch__ignorec          
   C   sZ   dh}dh}t j| t||d ddh}dddh}t j| td||d | jtt j| t d S )NZtempdirtemplate)extra	blacklistZTextTestResultZinstallHandlerZ
load_testsZTestProgramZBaseTestSuiteunittest.resultunittest.caseunittest.suiteunittest.loaderunittest.mainunittest.runnerunittest.signals)r   r   r   r   r   r   r   )r   Zcheck__all__rB   r	   r   r|   )r   r   r   r   r   r   test_check__all__x  s"    
   zTestSupport.test_check__all__Nc             C   s   d| d| d}t jf|d|f}dd tjj D }tj|tjtjd|d}|d kr\|}| j	|j
j t| | j	|jd	 d S )
Nzfrom test.support import z; print(repr(z()))z-cc             S   s    i | ]\}}|j d s||qS )ZPYTHON)
startswith)rT   keyvaluer   r   r   
<dictcomp>  s    z-TestSupport.check_options.<locals>.<dictcomp>T)r   r   Zuniversal_newlinesenvr   )r   
executabler   environitems
subprocessZrunPIPEZDEVNULLr   r   rstriprepr
returncode)r   argsfuncrY   codecmdr   procr   r   r   check_options  s    zTestSupport.check_optionsc             C   s   x|g dgdgdgdgdgdgdgdgd	gd
gdgddgddgddgddgddgfD ](}| j |d | j|d W d Q R X qRW | jdddgddg d S )Nz-Bz-sz-Sz-Ez-vz-bz-qz-Iz-bbz-vvvz-Wignorez-XZfaulthandlerZshowalloccountZshowrefcounttracemallocztracemalloc=3)optsZargs_from_interpreter_flags)subTestr   )r   r   r   r   r    test_args_from_interpreter_flags  s,    z,TestSupport.test_args_from_interpreter_flagsc             C   sD   x>g dgdgdgfD ](}| j |d | j|d W d Q R X qW d S )Nz-Oz-OOz-OOOO)r   Z!optim_args_from_interpreter_flags)r   r   )r   r   r   r   r   &test_optim_args_from_interpreter_flags  s    z2TestSupport.test_optim_args_from_interpreter_flagsc          
   C   s  G dd d}|d}|d}t jt dd  t jg  | jt j| | jt j| t jd  | jt j| | jt j| t j|j g | jt j| | jt j| t jdg | jt j| | jt j| t jdg | jt j| | jt j| t jdg | jt j| t jd	g | jt j| t jd
g | jt j| | jt j| t j|j |j g | jt j| | jt j| t jddg | jt j| | jt j| W d Q R X d S )Nc               @   s   e Zd Zdd Zdd ZdS )z)TestSupport.test_match_test.<locals>.Testc             S   s
   || _ d S )N)test_id)r   r   r   r   r   __init__  s    z2TestSupport.test_match_test.<locals>.Test.__init__c             S   s   | j S )N)r   )r   r   r   r   id  s    z,TestSupport.test_match_test.<locals>.Test.idN)r   r   r   r   r   r   r   r   r   Test  s   r   z"test.test_os.FileTests.test_accessz'test.test_os.Win32ErrorTests.test_chdirZ_match_test_funcZtest_osztest_*Z	filetestsZ	FileTestsz*test_os.*.test_*test_accessZ	DONTMATCH)r   r)   Zset_match_testsrG   Z
match_testr   r   )r   r   r   Z
test_chdirr   r   r   test_match_test  s@    

zTestSupport.test_match_testc             C   sD   t j }tjttj}zt j }W d tj| X | j|| d d S )Nrq   )r   Zfd_countr   r   __file__O_RDONLYr=   r   )r   startrv   Zmorer   r   r   test_fd_count  s    zTestSupport.test_fd_count)N)4r   r   r   r   r   r   r	   skipr   r   r#   r/   r9   r?   r@   rA   rJ   rK   rP   rZ   Z
skipUnlessr   r   r]   rb   rf   rg   rh   rm   rn   rr   rx   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sX   
		
 9r   c              C   s   t g} tj|   d S )N)r   r   Zrun_unittest)Ztestsr   r   r   	test_main)  s    r   __main__)ru   r5   r   rO   r:   r+   r   r   rB   r\   r	   Ztestr   Ztest.supportr   r   ZTestCaser   r   r   r   r   r   r   <module>   s*       