B
    u9ašJ  ã               @   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
mZmZmZmZmZ ddlZddlZddlZyddlZW n ek
r    dZY nX e eedƒpºejejkd¡Zddd„ZG dd	„ d	ejƒZG d
d„ deƒZeG dd„ deƒƒZedkre  ¡  dS )z|
Test harness for the venv module.

Copyright (C) 2011-2012 Vinay Sajip.
Licensed to the PSF under a contributor agreement.
é    N)Úcaptured_stdoutÚcaptured_stderrÚrequires_zlibÚcan_symlinkÚEnvironmentVarGuardÚrmtreeÚ_base_executablez:cannot run venv.create from within a venv on this platformc             C   sB   t j| t jt j|d}| ¡ \}}|jr:t  |j| ||¡‚||fS )N)ÚstdoutÚstderrÚencoding)Ú
subprocessÚPopenÚPIPEZcommunicateÚ
returncodeÚCalledProcessError)Úcmdr   ÚpÚoutÚerr© r   ú/usr/lib/python3.7/test_venv.pyÚcheck_output"   s    r   c               @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )ÚBaseTestzBase class for venv tests.i   c             C   s¨   t j t ¡ ¡| _t jdkr0d| _d| _d| _	n$d| _ddt
jd d…  f| _d	| _	tt
d
t
jƒ}t j |¡d | _t
jdkržt j |¡ržt j |¡sžd| _nd| _d S )NÚntÚScripts)ÚLibÚIncludeÚbinÚlibzpython%d.%dé   Úincluder   éÿÿÿÿÚwin32TF)ÚosÚpathÚrealpathÚtempfileZmkdtempÚenv_dirÚnameÚbindirr   r    ÚsysÚversion_infoÚgetattrÚ
executableÚsplitÚexeÚplatformÚlexistsÚexistsÚcannot_link_exe)Úselfr-   r   r   r   ÚsetUp1   s    

zBaseTest.setUpc             C   s   t | jƒ d S )N)r   r'   )r4   r   r   r   ÚtearDownD   s    zBaseTest.tearDownc          
   O   s>   t ƒ "}tƒ }|||Ž W d Q R X W d Q R X | ¡ | ¡ fS )N)r   r   Úgetvalue)r4   ÚfuncÚargsÚkwargsÚoutputÚerrorr   r   r   Úrun_with_captureG   s    zBaseTest.run_with_capturec             G   s   t jj| jf|žŽ S )N)r#   r$   Újoinr'   )r4   r9   r   r   r   Úget_env_fileM   s    zBaseTest.get_env_filec          	   G   s(   t | j|Ž dƒ}| ¡ }W d Q R X |S )NÚr)Úopenr?   Úread)r4   r9   ÚfÚresultr   r   r   Úget_text_file_contentsP   s    zBaseTest.get_text_file_contentsN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZmaxDiffr5   r6   r=   r?   rE   r   r   r   r   r   -   s   r   c            	   @   s  e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZe	j
d
kr@dZn4ddddde	jdd…  fdde	jdd…  dffZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze eƒ d ¡d!d"„ ƒZed#d$„ ƒZe eƒ d ¡d%d&„ ƒZe ejd'kd(¡d)d*„ ƒZed+d,„ ƒZdS )-Ú	BasicTestzTest venv module functionality.c             G   s    | j |Ž }|  tj |¡¡ d S )N)r?   Ú
assertTruer#   r$   Úisdir)r4   r9   Úfnr   r   r   rL   X   s    
zBasicTest.isdirc             C   s,  t | jƒ |  tj| j¡ |  | j¡ |  | j¡ | j| jŽ  |  	d¡}t
 d¡dkohtjdkohtjdk}|r‚|  tj |¡¡ n|  tj |¡¡ |  d¡}ttdtjƒ}tj |¡}|  d| |¡ |  	| j| j¡}tj |¡s|  	| j¡}td	| ƒ td
t |¡ ƒ |  tj |¡d| ¡ dS )zB
        Test the create function with default arguments.
        Zlib64ÚPé   ÚposixÚdarwinz
pyvenv.cfgr   z	home = %szContents of %r:z    %rzFile %r should exist.N)r   r'   r=   ÚvenvÚcreaterL   r)   r    r   r?   ÚstructÚcalcsizer#   r(   r*   r0   rK   r$   ÚislinkÚassertFalser2   rE   r,   r-   ÚdirnameÚassertInr/   ÚprintÚlistdir)r4   r   Z
conditionsÚdatar-   r$   rM   Úbdr   r   r   Útest_defaults\   s*    



zBasicTest.test_defaultsc             C   sb   t j | j¡d }t ¡ }| | j¡}|  |jd| ¡ tjdd}| | j¡}|  |jd¡ d S )Né   z(%s) z	My prompt)Úpromptz(My prompt) )	r#   r$   r.   r'   rR   Ú
EnvBuilderZensure_directoriesÚassertEqualr`   )r4   Zenv_nameÚbuilderÚcontextr   r   r   Útest_promptx   s    zBasicTest.test_promptc             C   sœ   t | jƒ |  tj| j¡ tj | j| j| j	¡}|ddg}x\d| jfd| jfdt
jfdt
jffD ]4\}}d| |d< t|ƒ\}}|  | ¡ | ¡ ¡ q`W dS )z>
        Test that the prefix values are as expected.
        z-cNÚprefixÚbase_prefixÚbase_exec_prefixzimport sys; print(sys.%s)r   )r   r'   r=   rR   rS   r#   r$   r>   r)   r/   r*   rg   rh   r   rb   ÚstripÚencode)r4   Úenvpyr   rf   Zexpectedr   r   r   r   r   Útest_prefixesƒ   s    

zBasicTest.test_prefixesr"   ))r   )r   )r   )r   zsite-packages)r   )r    )r   r   zpython%d.%dNr   zsite-packagesc          
   C   s^   xX|D ]P}t jj| jf|žŽ }t  |¡ t j ||¡}t|dƒ}| d¡ W dQ R X qW dS )zn
        Create some files in the environment which are unrelated
        to the virtual environment.
        Úwbs   Still here?N)r#   r$   r>   r'   ÚmkdirrA   Úwrite)r4   ÚpathsÚfilenameÚsubdirsÚdrM   rC   r   r   r   Úcreate_contents¦   s    

zBasicTest.create_contentsc          
   C   sÐ   |   | jd¡ t | j¡ x^| jD ]T}tjj| jf|d žŽ }|  tj 	|¡¡ t
|dƒ}|  | ¡ d¡ W dQ R X q"W tjdd}| | j¡ x8| jD ].}tjj| jf|d žŽ }|  tj 	|¡¡ qšW dS )zE
        Test creating environment in an existing directory.
        Úfoo)ru   Úrbs   Still here?NT)Úclear)rt   ÚENV_SUBDIRSrR   rS   r'   r#   r$   r>   rK   r2   rA   rb   rB   ra   rW   )r4   rr   rM   rC   rc   r   r   r   Útest_overwrite_existing²   s    z!BasicTest.test_overwrite_existingc             C   s^   xXt  |¡D ]J}t j ||¡}t j |¡s6t j |¡rBt  |¡ qt j |¡rt|ƒ qW d S )N)	r#   r[   r$   r>   rV   ÚisfileÚremoverL   r   )r4   r$   rM   r   r   r   Úclear_directoryÄ   s    zBasicTest.clear_directoryc          
   C   sr   xl| j d d… D ]Z}tjj| jf|žŽ }t|dƒ}| d¡ W d Q R X |  tt	ft
j| j¡ |  | j¡ qW d S )Né   rm   ó    )rx   r#   r$   r>   r'   rA   ro   ZassertRaisesÚ
ValueErrorÚOSErrorrR   rS   r|   )r4   rp   rM   rC   r   r   r   Útest_unoverwritable_failsÌ   s    z#BasicTest.test_unoverwritable_failsc             C   s°   xªdD ]¢}t j|d}|  |j| j¡ |  | j¡ |  | j¡ | j| jŽ  |  	| j| j
¡}tj |¡s|  	| j¡}td| ƒ tdt |¡ ƒ |  tj |¡d| ¡ qW dS )zC
        Test upgrading an existing environment directory.
        )FT)ÚupgradezContents of %r:z    %rzFile %r should exist.N)rR   ra   r=   rS   r'   rL   r)   r    r   r?   r/   r#   r$   r2   rZ   r[   rK   )r4   r‚   rc   rM   r]   r   r   r   Útest_upgradeÕ   s    
zBasicTest.test_upgradec             C   sJ   xDdD ]<\}}t jd|d}| | j¡ |  d¡}|  d| |¡ qW dS )z:
        Test isolation from system site-packages
        ))TÚtrue)FZfalseT)rw   Úsystem_site_packagesz
pyvenv.cfgz"include-system-site-packages = %s
N)rR   ra   rS   r'   rE   rY   )r4   ZsspÚsrc   r\   r   r   r   Útest_isolationê   s
    
zBasicTest.test_isolationzNeeds symlinksc             C   sl   xfdD ]^}t jd|d}| | j¡ |  | j| j¡}|r| jrR|  t	j
 |¡¡ q|  t	j
 |¡¡ qW dS )z3
        Test symlinking works as expected
        )FTT)rw   ÚsymlinksN)rR   ra   rS   r'   r?   r)   r/   r3   rW   r#   r$   rV   rK   )r4   Zuslrc   rM   r   r   r   Útest_symlinkingô   s    
zBasicTest.test_symlinkingc             C   sb   t | jƒ |  tj| j¡ tj tj | j¡| j	| j
¡}t|ddgƒ\}}|  | ¡ | ¡ ¡ dS )zD
        Test that the sys.executable value is as expected.
        z-cz!import sys; print(sys.executable)N)r   r'   r=   rR   rS   r#   r$   r>   r%   r)   r/   r   rb   ri   rj   )r4   rk   r   r   r   r   r   Útest_executable  s    
zBasicTest.test_executablec             C   sl   t | jƒ tjddd}| | j¡ tj tj | j¡| j	| j
¡}t|ddgƒ\}}|  | ¡ | ¡ ¡ dS )zD
        Test that the sys.executable value is as expected.
        T)rw   rˆ   z-cz!import sys; print(sys.executable)N)r   r'   rR   ra   rS   r#   r$   r>   r%   r)   r/   r   rb   ri   rj   )r4   rc   rk   r   r   r   r   r   Útest_executable_symlinks  s    
z"BasicTest.test_executable_symlinksr   zonly relevant on Windowsc             C   sŽ   t | jƒ tj tj | j¡d¡}tjdd}| |¡ tj || j	d¡}tj || j	| j
¡}t|d| j
ddgdd	\}}|  | ¡ d
¡ dS )z0
        Test handling of Unicode paths
        u   Ï¼ÑžÐ¢Î»Ð¤Ð™T)rw   zactivate.batú&z-czprint(0)Zoem)r   Ú0N)r   r'   r#   r$   r>   r%   rR   ra   rS   r)   r/   r   rb   ri   )r4   r'   rc   Zactivaterk   r   r   r   r   r   Útest_unicode_in_batch_file'  s    

z$BasicTest.test_unicode_in_batch_filec             C   sb   t | jƒ |  tj| j¡ tj tj | j¡| j	| j
¡}t|ddgƒ\}}|  | ¡ d ¡ ¡ dS )zA
        Test that the multiprocessing is able to spawn.
        z-czrfrom multiprocessing import Pool; pool = Pool(1); print(pool.apply_async("Python".lower).get(3)); pool.terminate()ZpythonN)r   r'   r=   rR   rS   r#   r$   r>   r%   r)   r/   r   rb   ri   rj   )r4   rk   r   r   r   r   r   Útest_multiprocessing8  s    
zBasicTest.test_multiprocessing)rF   rG   rH   rI   rL   r^   re   ÚrequireVenvCreaterl   r*   r0   rx   r+   rt   ry   r|   r   rƒ   r‡   ÚunittestÚ
skipUnlessr   r‰   rŠ   r‹   r#   r(   rŽ   r   r   r   r   r   rJ   U   s.   
	
rJ   c               @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	 
ed¡edd„ ƒƒZdS )ÚEnsurePipTestz%Test venv module installation of pip.c             C   sd   t j t j | j¡| j| j¡}t|ddgƒ\}}| d¡}|  	|d¡ | d¡}|  	| 
¡ d¡ d S )Nz-cz1try:
 import pip
except ImportError:
 print("OK")zlatin-1Ú ZOK)r#   r$   r>   r%   r'   r)   r/   r   Údecoderb   ri   )r4   rk   r   r   r   r   r   Úassert_pip_not_installedK  s    

z&EnsurePipTest.assert_pip_not_installedc             C   s&   t | jƒ |  tj| j¡ |  ¡  d S )N)r   r'   r=   rR   rS   r–   )r4   r   r   r   Útest_no_pip_by_defaultX  s    
z$EnsurePipTest.test_no_pip_by_defaultc             C   s*   t | jƒ | jtj| jdd |  ¡  d S )NF)Úwith_pip)r   r'   r=   rR   rS   r–   )r4   r   r   r   Útest_explicit_no_pip]  s    
z"EnsurePipTest.test_explicit_no_pipc          	   C   sd   t tjdƒ}|  | ¡ d¡ W d Q R X tj ¡ dkrL|  tj tj¡¡ n|  	tj tj¡¡ d S )Nrv   r~   Znul)
rA   r#   Údevnullrb   rB   ÚlowerrW   r$   r2   rK   )r4   rC   r   r   r   Útest_devnullb  s
    zEnsurePipTest.test_devnullc             C   s:  t | jƒ tƒ ü}d|d< d|d< t ¡ Ø}||d< d}d}d}xX|fD ]N\}}tj ||¡}	t |	¡ tj |	|¡}
t	|
d	ƒ}| 
|¡ W d Q R X qHW y| jtj| j|d
d W nH tjk
rü } z(|jjdd}d}|  | ||¡¡ W d d }~X Y nX W d Q R X W d Q R X tj tj | j¡| j| j¡}t|ddddddgƒ\}}| d¡}|  |d¡ | d¡}d t ¡ ¡}|  |d t|ƒ… |¡ t | j¡ d¡}|  ||¡ tƒ }t|dddddgƒ\}}W d Q R X | d¡}tjdd|tj d}|  | !¡ d¡ | d¡}|  d|¡ |  d|¡ |s6|  "¡  d S )NÚeZPYTHONWARNINGSÚ1ZPIP_NO_INSTALLÚHOMEz[global]
no-install=1)Úpipzpip.ini)z.pipzpip.confÚwT)r…   r˜   Úreplace)Úerrorsz{}

**Subprocess Output**
{}z-Wzignore::DeprecationWarningz-Iz-mr    z	--versionzlatin-1r”   zpip {}zensurepip._uninstallzN^The directory .* or its parent directory is not owned by the current user .*$)ÚflagszSuccessfully uninstalled pipz#Successfully uninstalled setuptools)#r   r'   r   r&   ZTemporaryDirectoryr#   r$   r>   rn   rA   ro   r=   rR   rS   r   r   r;   r•   ZfailÚformatr%   r)   r/   r   rb   Ú	ensurepipÚversionÚlenÚfsencoderY   ÚreÚsubÚ	MULTILINEÚrstripr–   )r4   r…   ÚenvvarsZhome_dirZ
bad_configZwin_locationZposix_locationrX   ZfnameÚdirpathZfpathrC   ÚexcZdetailsÚmsgrk   r   r   Zexpected_versionr'   r   r   r   Údo_test_with_pipp  s\    


8




zEnsurePipTest.do_test_with_pipzpip requires ctypesc             C   s   |   d¡ |   d¡ d S )NFT)r²   )r4   r   r   r   Útest_with_pipÉ  s    
zEnsurePipTest.test_with_pipN)rF   rG   rH   rI   r–   r—   r™   rœ   r²   r‘   r’   Úctypesr   r³   r   r   r   r   r“   H  s   Y
r“   Ú__main__)N)!rI   r¦   r#   Zos.pathrª   rT   r   r*   r&   Ztest.supportr   r   r   r   r   r   Z	threadingr‘   rR   r´   ÚImportErrorr’   Úhasattrrf   rg   r   r   ZTestCaser   rJ   r“   rF   Úmainr   r   r   r   Ú<module>   s:    



( t 
