3
sY]6                 @   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
 G dd dejZG dd dejZedkrej  dS )	z#Test harness for the zipapp module.    N)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
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ejejd,kd-d.d/ Zejejd,kd-d0d1 Zd2S )3
ZipAppTestz!Test zipapp module functionality.c             C   s&   t j }| j|j tj|j| _d S )N)tempfileTemporaryDirectory
addCleanupcleanuppathlibPathnametmpdir)selfr    r   !lib/python3.6/test/test_zipapp.pysetUp   s    zZipAppTest.setUpc             C   sN   | j d }|j  |d j  | j d }tjt|t| | j|j  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 }|j  |d j  | j d }tj|| | j|j  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 }|j  |d j  |d j  |d j  |d d j  tj }tjt|| |jd t	j
|d&}| jd|j  | jd	|j  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   sH   | j d }|j  |d j  tjt| | j d }| j|j  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_target8   s    

z-ZipAppTest.test_create_archive_default_targetc             C   sX   | j d }|j  |d j  | j d }| jtj tj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_mainA   s    

zZipAppTest.test_no_mainc             C   s\   | j d }|j  |d j  | j d }| j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   r'   r   r(   r   r   )r   r   r   r   r   r   test_main_and_main_pyJ   s    

z ZipAppTest.test_main_and_main_pyc             C   s   | j d }|j  |d j  | j d }tjt|t|dd tjt|d(}| jd|j	  | jd|j
d W d Q R X d S )	Nr   zfoo.pyz
source.pyzz
pkg.mod:fn)r*   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_writtenS   s    

zZipAppTest.test_main_writtenc             C   s   | j d }|j  |d j  |d j  | j d }tjt|t|dd tjt|d}| jd|j	 j
d	 W d Q R X d S )
Nr   zfoo.pyzbar.pyz
source.pyzz
pkg.mod:fn)r*   r      z__main__.py)r   r   r   r   r   r   r    r!   assertEqualr#   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  | j d }ddddddd	d
ddg
}xT|D ]L}| j|d6 | j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)r*   )r   r   ZsubTestr'   r   r(   r   r   )r   r   r   Zproblemsr*   r   r   r   test_main_validationo   s    


zZipAppTest.test_main_validationc          
   C   sh   | j d }|j  |d j  | j d }tjt|t| |jd}| j|jdd W d Q R X d S )Nr   z__main__.pyz
source.pyzrb   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 }|j  |d j  | j d }tjt|t|dd |jd(}| j|jdd | jd	|j	  W d Q R X d S )
Nr   z__main__.pyz
source.pyzpython)interpreterr4   r5   s   #!s   python
)
r   r   r   r   r   r   r6   r/   r,   readline)r   r   r   r7   r   r   r   test_custom_interpreter   s    

z"ZipAppTest.test_custom_interpreterc             C   sR   | j d }|j  |d j  tj }tjt||dd | j|j	 j
d d S )Nr   z__main__.pyr9   )r:   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 }|j  |d j  | j d }tjt|t|dd | jtjt|d d S )Nr   z__main__.pyz
source.pyzr9   )r:   )r   r   r   r   r   r   r/   get_interpreter)r   r   r   r   r   r   test_read_shebang   s    

zZipAppTest.test_read_shebangc             C   sV   | j d }|j  |d j  | j d }tjt|t| | jtjt|d  d S )Nr   z__main__.pyz
source.pyz)r   r   r   r   r   r   r/   r@   )r   r   r   r   r   r   test_read_missing_shebang   s    

z$ZipAppTest.test_read_missing_shebangc             C   s|   | j d }|j  |d j  | j d }tjt|t|dd | j d }tjt|t|dd | jtjt|d d S )Nr   z__main__.pyz
source.pyzr9   )r:   zchanged.pyzz	python2.7)r   r   r   r   r   r   r/   r@   )r   r   r   
new_targetr   r   r   test_modify_shebang   s    


zZipAppTest.test_modify_shebangc             C   st   | j d }|j  |d j  | j d }tjt|t|dd tj }tjt||dd | j|j	 j
d d S )Nr   z__main__.pyz
source.pyzr9   )r:   z	python2.7s   #!python2.7
)r   r   r   r   r   r   r   r   r   r=   r>   )r   r   r   rC   r   r   r   test_write_shebang_to_fileobj   s    

z(ZipAppTest.test_write_shebang_to_fileobjc             C   sh   | j d }|j  |d j  | j d }| j d }tj||dd tj||dd | jtj|d d S )Nr   z__main__.pyztarget1.pyzztarget2.pyzr9   )r:   z	python2.7)r   r   r   r   r   r/   r@   )r   r   Ztarget1Ztarget2r   r   r   test_read_from_pathobj   s    


z!ZipAppTest.test_read_from_pathobjc             C   s~   | j d }|j  |d j  | j d }tj }tjt||dd tj }|jd tj||dd | j	|j
 jd d S )	Nr   z__main__.pyz
source.pyzr9   )r:   r   z	python2.7s   #!python2.7
)r   r   r   r   r   r   r   r   r   r   r=   r>   )r   r   r   Ztemp_archiverC   r   r   r   test_read_from_fileobj   s    


z!ZipAppTest.test_read_from_fileobjc             C   s|   | j d }|j  |d j  | j d }tjt|t|dd | j d }tjt|t|d d | jtjt|d  d S )Nr   z__main__.pyz
source.pyzr9   )r:   zchanged.pyz)r   r   r   r   r   r   r/   r@   )r   r   r   rC   r   r   r   test_remove_shebang   s    


zZipAppTest.test_remove_shebangc          
   C   s   | j d }|j  |d j  tj }tjt||dd tj }|jd tj||d d |jd t	j
|d}| jt|j dh W d Q R X d S )Nr   z__main__.pyr9   )r:   r   r   )r   r   r   r   r   r   r   r   r   r    r!   r/   setr#   )r   r   r   rC   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 }|j  |d j  | j d }tjt|t|dd | j|j jtj	@  d S )Nr   z__main__.pyz
source.pyzr9   )r:   )
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 }|j  |d j  | j d }tjt|t|d d | j|j jtj	@  d S )Nr   z__main__.pyz
source.pyz)r:   )
r   r   r   r   r   r   ZassertFalserL   rM   rN   )r   r   r   r   r   r   !test_no_shebang_is_not_executable  s    

z,ZipAppTest.test_no_shebang_is_not_executableN)__name__
__module____qualname____doc__r   r   r   r%   r&   r)   r+   r-   r1   r3   r8   r<   r?   rA   rB   rD   rE   rF   rG   rH   rJ   unittestZskipIfsysplatformrO   rP   r   r   r   r   r      s4   					
			
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 }| j|j tj|j| _d S )N)r   r   r   r   r   r	   r
   r   )r   r   r   r   r   r     s    zZipAppCmdlineTest.setUpc             C   s8   | j d }|j  |d j  | j d }tj|| |S )Nr   z__main__.pyz
source.pyz)r   r   r   r   r   )r   r   r   r   r   r   make_archive  s    

zZipAppCmdlineTest.make_archivec             C   sN   | j d }|j  |d j  t|g}tj| |jd}| j|j  d S )Nr   z__main__.pyz.pyz)	r   r   r   r   r   r*   Zwith_suffixr   r   )r   r   argsr   r   r   r   test_cmdline_create!  s    



z%ZipAppCmdlineTest.test_cmdline_createc             C   s@   | j  }| jd }t|dt|g}tj| | j|j  d S )Nz
target.pyzz-o)rY   r   r   r   r*   r   r   )r   originalr   rZ   r   r   r   test_cmdline_copy+  s
    

z#ZipAppCmdlineTest.test_cmdline_copyc          	   C   sV   | j  }| jd }t|dt|g}| jt}tj| W d Q R X | j|jj	 d S )Nz
target.pyzz-o)
rY   r   r   r'   
SystemExitr   r*   r   	exceptioncode)r   r\   r   rZ   cmr   r   r   test_cmdline_copy_inplace3  s    
z+ZipAppCmdlineTest.test_cmdline_copy_inplacec          	   C   sZ   | j  }| jd }t|dt|ddg}| jt}tj| W d Q R X | j|jj	 d S )Nz
target.pyzz-oz-mzfoo:bar)
rY   r   r   r'   r^   r   r*   r   r_   r`   )r   r\   r   rZ   ra   r   r   r   test_cmdline_copy_change_main=  s    
z/ZipAppCmdlineTest.test_cmdline_copy_change_mainz
sys.stdout)Znew_callablec          
   C   sX   | j  }t|dg}| jt}tj| W d Q R X | j|jjd | j|j	 d d S )Nz--infor   zInterpreter: <none>
)
rY   r   r'   r^   r   r*   r/   r_   r`   r=   )r   Zmock_stdoutr   rZ   ra   r   r   r   test_info_commandG  s    z#ZipAppCmdlineTest.test_info_commandc          	   C   sH   | j d }t|dg}| jt}tj| W d Q R X | j|jj d S )Nz	dummy.pyzz--info)	r   r   r'   r^   r   r*   r   r_   r`   )r   r   rZ   ra   r   r   r   test_info_errorR  s
    
z!ZipAppCmdlineTest.test_info_errorN)rQ   rR   rS   rT   r   rY   r[   r]   rb   rc   r   r   StringIOrd   re   r   r   r   r   rX     s   	


rX   __main__)rT   r   r   rL   rV   r   rU   r   r    Zunittest.mockr   ZTestCaser   rX   rQ   r*   r   r   r   r   <module>   s     M