B
    u9a®?  ć               @   s   d 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 G dd dejZG dd dejZed	kre ”  dS )
z#Test harness for the zipapp module.é    N)Śrequires_zlib)Śpatchc               @   s  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
e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d0d1 Ze ej d2kd3”d4d5 Z!e ej d2kd3”d6d7 Z"d8S )9Ś
ZipAppTestz!Test zipapp module functionality.c             C   s&   t  ” }|  |j” t |j”| _d S )N)ŚtempfileŚTemporaryDirectoryŚ
addCleanupŚcleanupŚpathlibŚPathŚnameŚtmpdir)Śselfr   © r   ś!/usr/lib/python3.7/test_zipapp.pyŚsetUp   s    zZipAppTest.setUpc             C   sN   | j d }| ”  |d  ”  | j d }t t|t|” |  | ” ” d S )NŚsourcez__main__.pyz
source.pyz)r   ŚmkdirŚtouchŚzipappŚcreate_archiveŚstrŚ
assertTrueŚis_file)r   r   Śtargetr   r   r   Śtest_create_archive   s    

zZipAppTest.test_create_archivec             C   sF   | j d }| ”  |d  ”  | j d }t ||” |  | ” ” d S )Nr   z__main__.pyz
source.pyz)r   r   r   r   r   r   r   )r   r   r   r   r   r   Ś test_create_archive_with_pathlib!   s    

z+ZipAppTest.test_create_archive_with_pathlibc          	   C   s¤   | j d }| ”  |d  ”  |d  ”  |d  ”  |d d  ”  t ” }t t||” | d” t	 
|d”&}|  d| ” ” |  d	| ” ” W d Q R X d S )
Nr   z__main__.pyŚfooZbarz__init__.pyr   Śrzfoo/zbar/)r   r   r   ŚioŚBytesIOr   r   r   ŚseekŚzipfileŚZipFileŚassertInŚnamelist)r   r   r   Śzr   r   r   Ś test_create_archive_with_subdirs*   s    

z+ZipAppTest.test_create_archive_with_subdirsc          	   C   s¤   dd }| j d }| ”  |d  ”  |d  ”  |d  ”  | j d }tj|||d t |d	”6}|  d| ” ” |  d| ” ” |  	d| ” ” W d Q R X d S )
Nc             S   s
   | j dkS )Nz.pyc)Śsuffix)Śpathr   r   r   Śskip_pyc_files<   s    zBZipAppTest.test_create_archive_with_filter.<locals>.skip_pyc_filesr   z__main__.pyztest.pyztest.pycz
source.pyz)Śfilterr   )
r   r   r   r   r   r!   r"   r#   r$   ZassertNotIn)r   r)   r   r   r%   r   r   r   Śtest_create_archive_with_filter9   s    

z*ZipAppTest.test_create_archive_with_filterc          	   C   sø   dd }| j d }| ”  |d  ”  |d  ”  |d  ”  |d d  ”  | j d }tj|||d	 t |d
”:}|  t| 	” d” |  
d| 	” ” |  
d| 	” ” W d Q R X d S )Nc             S   s   | j d dkS )Nr   Śdummy)Śparts)r(   r   r   r   Śskip_dummy_dirO   s    zIZipAppTest.test_create_archive_filter_exclude_dir.<locals>.skip_dummy_dirr   z__main__.pyztest.pyr,   ztest2.pyz
source.pyz)r*   r   é   )r   r   r   r   r   r!   r"   ŚassertEqualŚlenr$   r#   )r   r.   r   r   r%   r   r   r   Ś&test_create_archive_filter_exclude_dirK   s    

z1ZipAppTest.test_create_archive_filter_exclude_dirc             C   sH   | j d }| ”  |d  ”  t t|” | j d }|  | ” ” d S )Nr   z__main__.pyz
source.pyz)r   r   r   r   r   r   r   r   )r   r   Zexpected_targetr   r   r   Ś"test_create_archive_default_target_   s    

z-ZipAppTest.test_create_archive_default_targetc          	   C   s   | j d }| ”  |d  ”  |d  ”  | j d }tj||dd t |d”*}x"dD ]}|  | |”j	tj
” qXW W d Q R X d S )	Nr   z__main__.pyztest.pyz
source.pyzT)Z
compressedr   )z__main__.pyztest.py)r   r   r   r   r   r!   r"   r0   ZgetinfoZcompress_typeZZIP_DEFLATED)r   r   r   r%   r   r   r   r   Ś$test_create_archive_with_compressionh   s    


z/ZipAppTest.test_create_archive_with_compressionc          	   C   sX   | j d }| ”  |d  ”  | j d }|  tj” t t|t|” W d Q R X d S )Nr   zfoo.pyz
source.pyz)r   r   r   ŚassertRaisesr   ŚZipAppErrorr   r   )r   r   r   r   r   r   Śtest_no_mainw   s    

zZipAppTest.test_no_mainc          	   C   s\   | j d }| ”  |d  ”  | j d }|  tj” tjt|t|dd W d Q R X d S )Nr   z__main__.pyz
source.pyzz
pkg.mod:fn)Śmain)r   r   r   r5   r   r6   r   r   )r   r   r   r   r   r   Śtest_main_and_main_py   s    

z ZipAppTest.test_main_and_main_pyc          	   C   s   | j d }| ”  |d  ”  | j d }tjt|t|dd t t|d”(}|  d| 	” ” |  d| 
d”” W d Q R X d S )	Nr   zfoo.pyz
source.pyzz
pkg.mod:fn)r8   r   z__main__.pys   pkg.mod.fn())r   r   r   r   r   r   r!   r"   r#   r$   Śread)r   r   r   r%   r   r   r   Śtest_main_written   s    

zZipAppTest.test_main_writtenc          	   C   s   | j d }| ”  |d  ”  |d  ”  | j d }tjt|t|dd t t|d”}|  d| 	”  
d	”” W d Q R X d S )
Nr   zfoo.pyzbar.pyz
source.pyzz
pkg.mod:fn)r8   r   é   z__main__.py)r   r   r   r   r   r   r!   r"   r0   r$   Ścount)r   r   r   r%   r   r   r   Śtest_main_only_written_once   s    

z&ZipAppTest.test_main_only_written_oncec             C   s   | j d }| ”  | j d }ddddddd	d
ddg
}xT|D ]L}| j|d6 |  tj” tjt|t||d W d Q R X W d Q R X q:W d S )Nr   z
source.pyzŚ r   zfoo:z:barz12:barza.b.c.:dz.a:bza:b.za:.bza:silly name)r8   )r   r   ZsubTestr5   r   r6   r   r   )r   r   r   Zproblemsr8   r   r   r   Śtest_main_validation„   s    


zZipAppTest.test_main_validationc          	   C   sh   | j d }| ”  |d  ”  | j d }t t|t|” | d”}|  | d”d” W d Q R X d S )Nr   z__main__.pyz
source.pyzŚrbr/   s   #!)	r   r   r   r   r   r   ŚopenZassertNotEqualr:   )r   r   r   Śfr   r   r   Śtest_default_no_shebang³   s    

z"ZipAppTest.test_default_no_shebangc          	   C   s|   | j d }| ”  |d  ”  | j d }tjt|t|dd | d”(}|  | d”d” |  d	| 	” ” W d Q R X d S )
Nr   z__main__.pyz
source.pyzŚpython)ŚinterpreterrA   r/   s   #!s   python
)
r   r   r   r   r   r   rB   r0   r:   Śreadline)r   r   r   rC   r   r   r   Śtest_custom_interpreter½   s    

z"ZipAppTest.test_custom_interpreterc             C   sR   | j d }| ”  |d  ”  t ” }tjt||dd |  | 	”  
d”” d S )Nr   z__main__.pyrE   )rF   s	   #!python
)r   r   r   r   r   r   r   r   r   ŚgetvalueŚ
startswith)r   r   r   r   r   r   Śtest_pack_to_fileobjÉ   s    
zZipAppTest.test_pack_to_fileobjc             C   sZ   | j d }| ”  |d  ”  | j d }tjt|t|dd |  t t|”d” d S )Nr   z__main__.pyz
source.pyzrE   )rF   )r   r   r   r   r   r   r0   Śget_interpreter)r   r   r   r   r   r   Śtest_read_shebangŅ   s    

zZipAppTest.test_read_shebangc             C   sV   | j d }| ”  |d  ”  | j d }t t|t|” |  t t|”d ” d S )Nr   z__main__.pyz
source.pyz)r   r   r   r   r   r   r0   rL   )r   r   r   r   r   r   Śtest_read_missing_shebangŪ   s    

z$ZipAppTest.test_read_missing_shebangc             C   s|   | j d }| ”  |d  ”  | j d }tjt|t|dd | j d }tjt|t|dd |  t t|”d” d S )Nr   z__main__.pyz
source.pyzrE   )rF   zchanged.pyzz	python2.7)r   r   r   r   r   r   r0   rL   )r   r   r   Ś
new_targetr   r   r   Śtest_modify_shebangä   s    


zZipAppTest.test_modify_shebangc             C   st   | j d }| ”  |d  ”  | j d }tjt|t|dd t ” }tjt||dd |  | 	”  
d”” d S )Nr   z__main__.pyz
source.pyzrE   )rF   z	python2.7s   #!python2.7
)r   r   r   r   r   r   r   r   r   rI   rJ   )r   r   r   rO   r   r   r   Śtest_write_shebang_to_fileobjļ   s    

z(ZipAppTest.test_write_shebang_to_fileobjc             C   sh   | j d }| ”  |d  ”  | j d }| j d }tj||dd tj||dd |  t |”d” d S )Nr   z__main__.pyztarget1.pyzztarget2.pyzrE   )rF   z	python2.7)r   r   r   r   r   r0   rL   )r   r   Ztarget1Ztarget2r   r   r   Śtest_read_from_pathobjū   s    


z!ZipAppTest.test_read_from_pathobjc             C   s~   | j d }| ”  |d  ”  | j d }t ” }tjt||dd t ” }| d” tj||dd |  	| 
”  d”” d S )	Nr   z__main__.pyz
source.pyzrE   )rF   r   z	python2.7s   #!python2.7
)r   r   r   r   r   r   r   r   r    r   rI   rJ   )r   r   r   Ztemp_archiverO   r   r   r   Śtest_read_from_fileobj  s    


z!ZipAppTest.test_read_from_fileobjc             C   s|   | j d }| ”  |d  ”  | j d }tjt|t|dd | j d }tjt|t|d d |  t t|”d ” d S )Nr   z__main__.pyz
source.pyzrE   )rF   zchanged.pyz)r   r   r   r   r   r   r0   rL   )r   r   r   rO   r   r   r   Śtest_remove_shebang  s    


zZipAppTest.test_remove_shebangc          	   C   s   | j d }| ”  |d  ”  t ” }tjt||dd t ” }| d” tj||d d | d” t	 
|d”}|  t| ” dh” W d Q R X d S )Nr   z__main__.pyrE   )rF   r   r   )r   r   r   r   r   r   r   r   r    r!   r"   r0   Śsetr$   )r   r   r   rO   r%   r   r   r   Śtest_content_of_copied_archive  s    


z)ZipAppTest.test_content_of_copied_archiveŚwin32z*Windows does not support an executable bitc             C   sZ   | j d }| ”  |d  ”  | j d }tjt|t|dd |  | ” jtj	@ ” d S )Nr   z__main__.pyz
source.pyzrE   )rF   )
r   r   r   r   r   r   r   ŚstatŚst_modeŚS_IEXEC)r   r   r   r   r   r   Śtest_shebang_is_executable.  s    

z%ZipAppTest.test_shebang_is_executablec             C   sZ   | j d }| ”  |d  ”  | j d }tjt|t|d d |  | ” jtj	@ ” d S )Nr   z__main__.pyz
source.pyz)rF   )
r   r   r   r   r   r   ZassertFalserX   rY   rZ   )r   r   r   r   r   r   Ś!test_no_shebang_is_not_executable9  s    

z,ZipAppTest.test_no_shebang_is_not_executableN)#Ś__name__Ś
__module__Ś__qualname__Ś__doc__r   r   r   r&   r+   r2   r3   r   r4   r7   r9   r;   r>   r@   rD   rH   rK   rM   rN   rP   rQ   rR   rS   rT   rV   ŚunittestZskipIfŚsysŚplatformr[   r\   r   r   r   r   r      s:   					
			
r   c               @   s^   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dejddd Zdd ZdS )ŚZipAppCmdlineTestz$Test zipapp module command line API.c             C   s&   t  ” }|  |j” t |j”| _d S )N)r   r   r   r   r	   r
   r   r   )r   r   r   r   r   r   I  s    zZipAppCmdlineTest.setUpc             C   s8   | j d }| ”  |d  ”  | j d }t ||” |S )Nr   z__main__.pyz
source.pyz)r   r   r   r   r   )r   r   r   r   r   r   Śmake_archiveN  s    

zZipAppCmdlineTest.make_archivec             C   sN   | j d }| ”  |d  ”  t|g}t |” | d”}|  | ” ” d S )Nr   z__main__.pyz.pyz)	r   r   r   r   r   r8   Zwith_suffixr   r   )r   r   Śargsr   r   r   r   Śtest_cmdline_createW  s    



z%ZipAppCmdlineTest.test_cmdline_createc             C   s@   |   ” }| jd }t|dt|g}t |” |  | ” ” d S )Nz
target.pyzz-o)re   r   r   r   r8   r   r   )r   Śoriginalr   rf   r   r   r   Śtest_cmdline_copya  s
    

z#ZipAppCmdlineTest.test_cmdline_copyc          	   C   sV   |   ” }| jd }t|dt|g}|  t”}t |” W d Q R X |  |jj	” d S )Nz
target.pyzz-o)
re   r   r   r5   Ś
SystemExitr   r8   r   Ś	exceptionŚcode)r   rh   r   rf   Ścmr   r   r   Śtest_cmdline_copy_inplacei  s    
z+ZipAppCmdlineTest.test_cmdline_copy_inplacec          	   C   sZ   |   ” }| jd }t|dt|ddg}|  t”}t |” W d Q R X |  |jj	” d S )Nz
target.pyzz-oz-mzfoo:bar)
re   r   r   r5   rj   r   r8   r   rk   rl   )r   rh   r   rf   rm   r   r   r   Śtest_cmdline_copy_change_mains  s    
z/ZipAppCmdlineTest.test_cmdline_copy_change_mainz
sys.stdout)Znew_callablec          	   C   sX   |   ” }t|dg}|  t”}t |” W d Q R X |  |jjd” |  | 	” d” d S )Nz--infor   zInterpreter: <none>
)
re   r   r5   rj   r   r8   r0   rk   rl   rI   )r   Zmock_stdoutr   rf   rm   r   r   r   Śtest_info_command}  s    z#ZipAppCmdlineTest.test_info_commandc          	   C   sH   | j d }t|dg}|  t”}t |” W d Q R X |  |jj” d S )Nz	dummy.pyzz--info)	r   r   r5   rj   r   r8   r   rk   rl   )r   r   rf   rm   r   r   r   Śtest_info_error  s
    
z!ZipAppCmdlineTest.test_info_errorN)r]   r^   r_   r`   r   re   rg   ri   rn   ro   r   r   ŚStringIOrp   rq   r   r   r   r   rd   E  s   	


rd   Ś__main__)r`   r   r	   rX   rb   r   ra   r   r!   Ztest.supportr   Zunittest.mockr   ZTestCaser   rd   r]   r8   r   r   r   r   Ś<module>   s      8M