B
    2*™\äI  ã               @   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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ÚrmtreezTest not appropriate in a venvc             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   ú9C:\ALexclude\prg\programme\Python37\Lib\test\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   sx   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 | _d S )NÚntÚScripts)ÚLibÚIncludeÚbinÚlibzpython%d.%dé   ÚincludeÚ_base_executableéÿÿÿÿ)ÚosÚpathÚrealpathÚtempfileZmkdtempÚenv_dirÚnameÚbindirr   r   ÚsysÚversion_infoÚgetattrÚ
executableÚsplitÚexe)Úselfr,   r   r   r   ÚsetUp-   s    
zBaseTest.setUpc             C   s   t | jƒ d S )N)r   r&   )r/   r   r   r   ÚtearDown:   s    zBaseTest.tearDownc          
   O   s>   t ƒ "}tƒ }|||Ž W d Q R X W d Q R X | ¡ | ¡ fS )N)r   r   Úgetvalue)r/   ÚfuncÚargsÚkwargsÚoutputÚerrorr   r   r   Úrun_with_capture=   s    zBaseTest.run_with_capturec             G   s   t jj| jf|žŽ S )N)r"   r#   Újoinr&   )r/   r4   r   r   r   Úget_env_fileC   s    zBaseTest.get_env_filec          	   G   s(   t | j|Ž dƒ}| ¡ }W d Q R X |S )NÚr)Úopenr:   Úread)r/   r4   ÚfÚresultr   r   r   Úget_text_file_contentsF   s    zBaseTest.get_text_file_contentsN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZmaxDiffr0   r1   r8   r:   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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d+d,„ ZdS )-Ú	BasicTestzTest venv module functionality.c             G   s    | j |Ž }|  tj |¡¡ d S )N)r:   Ú
assertTruer"   r#   Úisdir)r/   r4   Úfnr   r   r   rG   N   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&   r8   ÚvenvÚcreaterG   r(   r   r   r:   ÚstructÚcalcsizer"   r'   r)   ÚplatformrF   r#   ÚislinkÚassertFalseÚexistsr@   r+   r,   ÚdirnameÚassertInr.   ÚprintÚlistdir)r/   r   Z
conditionsÚdatar,   r#   rH   Úbdr   r   r   Útest_defaultsR   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&   rM   Ú
EnvBuilderZensure_directoriesÚassertEqualr]   )r/   Zenv_nameÚbuilderÚcontextr   r   r   Útest_promptn   s    zBasicTest.test_promptc             C   s¼   |   tjtj¡ |   tjtj¡ 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)   rd   rc   re   Úexec_prefixr   r&   r8   rM   rN   r"   r#   r9   r(   r.   r   ÚstripÚencode)r/   Úenvpyr   rc   Zexpectedr   r   r   r   r   Útest_prefixesy   s    

zBasicTest.test_prefixesÚwin32))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#   r9   r&   Úmkdirr<   Úwrite)r/   ÚpathsÚfilenameÚsubdirsÚdrH   r>   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)rt   Úrbs   Still here?NT)Úclear)rs   ÚENV_SUBDIRSrM   rN   r&   r"   r#   r9   rF   rT   r<   r_   r=   r^   rS   )r/   rq   rH   r>   r`   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"   rX   r#   r9   rR   ÚisfileÚremoverG   r   )r/   r#   rH   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é   rl   ó    )rw   r"   r#   r9   r&   r<   rn   ZassertRaisesÚ
ValueErrorÚOSErrorrM   rN   r{   )r/   ro   rH   r>   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)rM   r^   r8   rN   r&   rG   r(   r   r   r:   r.   r"   r#   rT   rW   rX   rF   )r/   r   r`   rH   rZ   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)rv   Úsystem_site_packagesz
pyvenv.cfgz"include-system-site-packages = %s
N)rM   r^   rN   r&   r@   rV   )r/   ZsspÚsr`   rY   r   r   r   Útest_isolationä   s
    
zBasicTest.test_isolationzNeeds symlinksc             C   sR   xLdD ]D}t jd|d}| | j¡ |  | j| j¡}|r|  tj	 
|¡¡ qW dS )z3
        Test symlinking works as expected
        )FTT)rv   ÚsymlinksN)rM   r^   rN   r&   r:   r(   r.   rF   r"   r#   rR   )r/   Zuslr`   rH   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&   r8   rM   rN   r"   r#   r9   r$   r(   r.   r   r_   rg   rh   )r/   ri   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)rv   r‡   z-cz!import sys; print(sys.executable)N)r   r&   rM   r^   rN   r"   r#   r9   r$   r(   r.   r   r_   rg   rh   )r/   r`   ri   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)rv   zactivate.batú&z-czprint(0)Zoem)r
   Ú0N)r   r&   r"   r#   r9   r$   rM   r^   rN   r(   r.   r   r_   rg   )r/   r&   r`   Zactivateri   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-czSfrom multiprocessing import Pool; print(Pool(1).apply_async("Python".lower).get(3))ZpythonN)r   r&   r8   rM   rN   r"   r#   r9   r$   r(   r.   r   r_   rg   rh   )r/   ri   r   r   r   r   r   Útest_multiprocessing-  s    
zBasicTest.test_multiprocessing)rA   rB   rC   rD   rG   r[   rb   Ú
skipInVenvrj   r)   rQ   rw   r*   rs   rx   r{   r€   r‚   r†   ÚunittestÚ
skipUnlessr   rˆ   r‰   rŠ   r"   r'   r   rŽ   r   r   r   r   rE   K   s.   
	
rE   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#   r9   r$   r&   r(   r.   r   Údecoder_   rg   )r/   ri   r   r   r   r   r   Úassert_pip_not_installed=  s    

z&EnsurePipTest.assert_pip_not_installedc             C   s&   t | jƒ |  tj| j¡ |  ¡  d S )N)r   r&   r8   rM   rN   r•   )r/   r   r   r   Útest_no_pip_by_defaultJ  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&   r8   rM   rN   r•   )r/   r   r   r   Útest_explicit_no_pipO  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 )Nru   r}   Únul)
r<   r"   Údevnullr_   r=   ÚlowerrS   r#   rT   rF   )r/   r>   r   r   r   Útest_devnullT  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#   r9   rm   r<   rn   r8   rM   rN   r   r   r6   r”   ZfailÚformatr$   r(   r.   r   r_   Ú	ensurepipÚversionÚlenÚfsencoderV   ÚreÚsubÚ	MULTILINEÚrstripr•   )r/   r„   ÚenvvarsZhome_dirZ
bad_configZwin_locationZposix_locationrU   ZfnameZdirpathZfpathr>   ÚexcZdetailsÚmsgri   r   r   Zexpected_versionr&   r   r   r   Údo_test_with_pipb  s\    


8




zEnsurePipTest.do_test_with_pipzpip requires ctypesc             C   s   |   d¡ |   d¡ d S )NFT)r±   )r/   r   r   r   Útest_with_pip»  s    
zEnsurePipTest.test_with_pipN)rA   rB   rC   rD   r•   r–   r˜   rœ   r±   r   r‘   Úctypesr   r²   r   r   r   r   r’   :  s   Y
r’   Ú__main__)N) rD   r¦   r"   Zos.pathrª   rO   r   r)   r%   Ztest.supportr   r   r   r   r   r   Z	threadingr   rM   r³   ÚImportErrorZskipIfrc   rd   r   r   ZTestCaser   rE   r’   rA   Úmainr   r   r   r   Ú<module>   s6    

" p 
