B
    u9aÅ  ã               @   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
mZ dd„ Zdd„ ZG dd„ dee	jƒƒZG d	d
„ d
ƒZG dd„ dee	jeddZG dd„ dee	jeddZedkrÌe	 ¡  dS )é    N)Úsupportc                s   t  ˆ ¡‡ fdd„ƒ}|S )z+Runs function with SOURCE_DATE_EPOCH unset.c           	      s(   t  ¡ }| d¡ ˆ | |ŽS Q R X d S )NÚSOURCE_DATE_EPOCH)r   ÚEnvironmentVarGuardZunset)ÚargsÚkwargsÚenv)Úfxn© ú%/usr/lib/python3.7/test_py_compile.pyÚwrapper   s    

z*without_source_date_epoch.<locals>.wrapper)Ú	functoolsÚwraps)r   r   r	   )r   r
   Úwithout_source_date_epoch   s    r   c                s   t  ˆ ¡‡ fdd„ƒ}|S )z)Runs function with SOURCE_DATE_EPOCH set.c           	      s&   t  ¡ }d|d< ˆ | |ŽS Q R X d S )NZ	123456789r   )r   r   )r   r   r   )r   r	   r
   r      s    
z'with_source_date_epoch.<locals>.wrapper)r   r   )r   r   r	   )r   r
   Úwith_source_date_epoch   s    r   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚSourceDateEpochTestMetac      	         s^   t ƒ  | |||¡}xFt|ƒD ]:}| d¡rt||ƒ}|rBt|ƒ}nt|ƒ}t|||ƒ qW |S )NZtest_)ÚsuperÚ__new__ÚdirÚ
startswithÚgetattrr   r   Úsetattr)	ÚmclsÚnameÚbasesZdctÚsource_date_epochÚclsÚattrZmethr   )Ú	__class__r	   r
   r   $   s    


zSourceDateEpochTestMeta.__new__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r	   r	   )r   r
   r   #   s   r   c               @   sæ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e	j
 e	j¡ pHe	j
 e	j¡d	¡d
d„ ƒZdd„ Zdd„ Zdd„ Ze ee	dƒo†e	 ¡ dkd¡e e	jdkd¡dd„ ƒƒZdd„ Zdd„ Ze ejjdkd¡dd„ ƒZd d!„ Zd"d#„ Zd$S )%ÚPyCompileTestsBasec          	   C   s”   t  ¡ | _tj | jd¡| _| jd | _tj	 
| j¡| _tj t ¡ ¡d | _tj | j¡d }|rnt |¡ t| jdƒ}| d¡ W d Q R X d S )Nz_test.pyÚcr   Úwzx = 123
)ÚtempfileZmkdtempÚ	directoryÚosÚpathÚjoinÚsource_pathÚpyc_pathÚ	importlibÚutilÚcache_from_sourceÚ
cache_pathÚ
splitdriveÚgetcwdÚ	cwd_driveÚchdirÚopenÚwrite)ÚselfZdriveÚfiler	   r	   r
   ÚsetUp5   s    

zPyCompileTestsBase.setUpc             C   s"   t  | j¡ | jrt | j¡ d S )N)ÚshutilZrmtreer&   r2   r'   r3   )r6   r	   r	   r
   ÚtearDownE   s    zPyCompileTestsBase.tearDownc             C   s<   t  | j| j¡ |  tj | j¡¡ |  tj | j	¡¡ d S )N)
Ú
py_compileÚcompiler*   r+   Ú
assertTruer'   r(   ÚexistsÚassertFalser/   )r6   r	   r	   r
   Útest_absolute_pathJ   s    z%PyCompileTestsBase.test_absolute_pathc          	   C   sx   yt  | jd | j¡ W n" ttfk
r:   |  d¡ Y n:X t j | j¡sNt‚|  	t
¡ t | j| j¡ W d Q R X d S )Nz.actualz.need to be able to create a symlink for a file)r'   Úsymlinkr+   ÚNotImplementedErrorÚOSErrorZskipTestr(   ÚislinkÚAssertionErrorÚassertRaisesÚFileExistsErrorr;   r<   r*   )r6   r	   r	   r
   Útest_do_not_overwrite_symlinksO   s    z1PyCompileTestsBase.test_do_not_overwrite_symlinksz7requires os.devnull and for it to be a non-regular filec          	   C   s*   |   t¡ t | jtj¡ W d Q R X d S )N)rF   rG   r;   r<   r*   r'   Údevnull)r6   r	   r	   r
   Ú&test_do_not_overwrite_nonregular_files[   s    z9PyCompileTestsBase.test_do_not_overwrite_nonregular_filesc             C   s$   t  | j¡ |  tj | j¡¡ d S )N)r;   r<   r*   r=   r'   r(   r>   r/   )r6   r	   r	   r
   Útest_cache_pathc   s    z"PyCompileTestsBase.test_cache_pathc          	   C   sd   t  | j¡& t tj | j¡tj | j	¡¡ W d Q R X |  
tj | j	¡¡ |  tj | j¡¡ d S )N)r   Z
change_cwdr&   r;   r<   r'   r(   Úbasenamer*   r+   r=   r>   r?   r/   )r6   r	   r	   r
   Útest_cwdg   s
    zPyCompileTestsBase.test_cwdc             C   sL   t  tj | j¡tj | j¡¡ |  tj | j¡¡ |  	tj | j
¡¡ d S )N)r;   r<   r'   r(   Úrelpathr*   r+   r=   r>   r?   r/   )r6   r	   r	   r
   Útest_relative_pathn   s    z%PyCompileTestsBase.test_relative_pathÚgeteuidr   znon-root user requiredÚntz/cannot control directory permissions on Windowsc          
   C   s^   t  | j¡}t  | jtj¡ z*|  t¡ t | j	| j
¡ W d Q R X W d t  | j|j¡ X d S )N)r'   Ústatr&   ÚchmodÚS_IREADrF   ÚIOErrorr;   r<   r*   r+   Úst_mode)r6   Úmoder	   r	   r
   Útest_exceptions_propagatet   s    z,PyCompileTestsBase.test_exceptions_propagatec          	   C   s\   t j t j t¡d¡}t ¡  |  tj	|dd¡ W d Q R X |  
t j tj |¡¡¡ d S )Nzbad_coding2.pyF)Údoraise)r'   r(   r)   ÚdirnameÚ__file__r   Zcaptured_stderrZassertIsNoner;   r<   r?   r>   r,   r-   r.   )r6   Z
bad_codingr	   r	   r
   Útest_bad_coding„   s
    

z"PyCompileTestsBase.test_bad_codingc          	   C   sŠ   t  | j| j¡ |  tj | j¡¡ |  tj | j	¡¡ t
| jdƒ}tj | ¡ di ¡}W d Q R X tj d¡rvd}nd}|  ||¡ d S )NÚrbÚtestr   é   r   )r;   r<   r*   r+   r=   r'   r(   r>   r?   r/   r4   r,   Ú_bootstrap_externalÚ_classify_pycÚreadÚenvironÚgetÚassertEqual)r6   ÚfpÚflagsZexpected_flagsr	   r	   r
   Útest_source_date_epoch‹   s    z)PyCompileTestsBase.test_source_date_epochztest does not work with -Oc          	   C   s¼   t j | jd¡}tj |¡}|d }t j |¡\}}t j |¡}|  	t j ||¡t j dd 
tjj¡¡¡ t|dƒ}| d¡ W d Q R X t |¡ |  t j |¡¡ |  t j |¡¡ d S )Nz
foo.bar.pyr#   Ú__pycache__zfoo.bar.{}.pycr$   zx = 123
)r'   r(   r)   r&   r,   r-   r.   ÚsplitrL   re   ÚformatÚsysÚimplementationÚ	cache_tagr4   r5   r;   r<   r=   r>   r?   )r6   Z
weird_pathr/   r+   ÚheadÚtailZpenultimate_tailr7   r	   r	   r
   Útest_double_dot_no_clobber™   s    
z-PyCompileTestsBase.test_double_dot_no_clobberc             C   s   |   dtj| jdd¡ d S )Nzopt-2é   )Úoptimize)ZassertInr;   r<   r*   )r6   r	   r	   r
   Útest_optimization_path­   s    z)PyCompileTestsBase.test_optimization_pathc          	   C   sœ   t j| jt jjd t| jdƒ}tj 	| 
¡ di ¡}W d Q R X |  |d¡ t j| jt jjd t| jdƒ}tj 	| 
¡ di ¡}W d Q R X |  |d¡ d S )N)Úinvalidation_moder]   r^   r_   é   )r;   r<   r*   ÚPycInvalidationModeÚCHECKED_HASHr4   r/   r,   r`   ra   rb   re   ÚUNCHECKED_HASH)r6   rf   rg   r	   r	   r
   Útest_invalidation_mode±   s    z)PyCompileTestsBase.test_invalidation_modeN)r   r   r    r8   r:   r@   rH   ÚunittestZskipIfr'   r(   r>   rI   ÚisfilerJ   rK   rM   rO   ÚhasattrrP   r   rX   r\   rh   rl   rg   rs   rq   rt   rz   r	   r	   r	   r
   r"   3   s$    r"   c               @   s   e Zd ZdS )ÚPyCompileTestsWithSourceEpochN)r   r   r    r	   r	   r	   r
   r~   Ä   s   r~   T)Ú	metaclassr   c               @   s   e Zd ZdS )Ú PyCompileTestsWithoutSourceEpochN)r   r   r    r	   r	   r	   r
   r€   Ë   s   r€   FÚ__main__)r   Úimportlib.utilr,   r'   r;   r9   rR   rl   r%   r{   r^   r   r   r   ÚtypeZTestCaser   r"   r~   r€   r   Úmainr	   r	   r	   r
   Ú<module>   s0   
 