o
    ±ó®g X  ã                   @   s0  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	Z	ddl
mZmZmZmZ ddlmZmZmZ ddlZddlZddlmZ zddlZW n ey[   dZY nw e ejejkpie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 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Ú*skip_if_broken_multiprocessing_synchronize)Úcan_symlinkÚEnvironmentVarGuardÚrmtree)Úpatchz: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ÚPIPEÚcommunicateÚ
returncodeÚCalledProcessError)Úcmdr   ÚpÚoutÚerr© r   úD/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_venv.pyÚcheck_output$   s   ý
ÿr   c                   @   sB   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zddœdd„Z	dS )ÚBaseTestzBase class for venv tests.i   c                 C   s¢   t j t ¡ ¡| _t jdkrd| _d| _d| _	nd| _ddt
jd d…  f| _d	| _	t
j}t j |¡d
 | _t
jdkrLt j |¡rLt j |¡sLd| _d S d| _d S )NÚntÚScripts©ÚLibÚIncludeÚbinÚlibúpython%d.%dé   ÚincludeéÿÿÿÿÚwin32TF)ÚosÚpathÚrealpathÚtempfileÚmkdtempÚenv_dirÚnameÚbindirr!   r$   ÚsysÚversion_infoÚ_base_executableÚsplitÚexeÚplatformÚlexistsÚexistsÚcannot_link_exe)ÚselfÚ
executabler   r   r   ÚsetUp3   s"   


ÿ
þ

zBaseTest.setUpc                 C   s   t | jƒ d S ©N)r   r,   ©r8   r   r   r   ÚtearDownF   s   zBaseTest.tearDownc              	   O   sj   t ƒ "}tƒ }||i |¤Ž W d   ƒ n1 sw   Y  W d   ƒ n1 s(w   Y  | ¡ | ¡ fS r;   )r   r   Úgetvalue)r8   ÚfuncÚargsÚkwargsÚoutputÚerrorr   r   r   Úrun_with_captureI   s   ÿ€ÿzBaseTest.run_with_capturec                 G   s   t jj| jg|¢R Ž S r;   )r'   r(   Újoinr,   )r8   r@   r   r   r   Úget_env_fileO   s   zBaseTest.get_env_filezutf-8©r   c                G   sB   t | j|Ž d|d}| ¡ }W d   ƒ |S 1 sw   Y  |S )NÚrrG   )ÚopenrF   Úread)r8   r   r@   ÚfÚresultr   r   r   Úget_text_file_contentsR   s   

ÿþzBaseTest.get_text_file_contentsN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZmaxDiffr:   r=   rD   rF   rM   r   r   r   r   r   /   s    r   c                	   @   sB  e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zed
d„ ƒZ	e
jdkr$dZnd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e ejd)kd/¡d0d1„ ƒZe e
jd2kd3¡d4d5„ ƒZ dS )6Ú	BasicTestzTest venv module functionality.c                 G   s    | j |Ž }|  tj |¡¡ d S r;   )rF   Ú
assertTruer'   r(   Úisdir)r8   r@   Úfnr   r   r   rT   Z   s   
zBasicTest.isdirc                 C   s"  t | jƒ |  tj| j¡ |  | j¡ |  | j¡ | j| jŽ  |  	d¡}t
 d¡dko4tjdko4tjdk}|rA|  tj |¡¡ n	|  tj |¡¡ |  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Údarwinú
pyvenv.cfgz	home = %súContents of %r:ú    %rúFile %r should exist.N)r   r,   rD   ÚvenvÚcreaterT   r.   r$   r!   rF   ÚstructÚcalcsizer'   r-   r/   r4   rS   r(   ÚislinkÚassertFalser6   rM   r1   ÚdirnameÚassertInr3   ÚprintÚlistdir)r8   r   Z
conditionsÚdatar9   r(   rU   Úbdr   r   r   Útest_defaults^   s,   

ÿ
zBasicTest.test_defaultsc                 C   s0  t j | j¡d }t| jƒ t ¡ }|  |j| j¡ | 	| j¡}|  
d¡}|  |jd| ¡ |  d|¡ t| jƒ tjdd}|  |j| j¡ | 	| j¡}|  
d¡}|  |jd¡ |  d|¡ t| jƒ tjd	d}t j t  ¡ ¡}|  |j| j¡ | 	| j¡}|  
d¡}|  |jd| ¡ |  d
| |¡ d S )Né   rZ   z(%s) z	prompt = z	My prompt)Úpromptz(My prompt) zprompt = 'My prompt'
Ú.zprompt = '%s'
)r'   r(   r2   r,   r   r^   Ú
EnvBuilderrD   r_   Úensure_directoriesrM   ÚassertEqualrl   ZassertNotInre   ÚbasenameÚgetcwd)r8   Zenv_nameÚbuilderÚcontextrh   Úcwdr   r   r   Útest_promptz   s.   





zBasicTest.test_promptc              	      sº   t  ¡ }tjdkrdnd‰ tjdkrdnd‰t ¡ 9‰‡ ‡‡‡fdd„}| ˆ¡}td|ƒ | |¡ W d   ƒ n1 s>w   Y  W d   ƒ d S W d   ƒ d S 1 sVw   Y  d S )	Nr&   r   r    z
python.exeÚpythonc              
      s*   ˆ  | tj ˆˆ ˆ¡ddddddg¡ d S )Nú-mÚpipZinstallz	--upgradeZ
setuptools)rp   r'   r(   rE   )r   ©Zbin_pathZfake_env_dirZ
python_exer8   r   r   Úpip_cmd_checkerœ   s   ùþz<BasicTest.test_upgrade_dependencies.<locals>.pip_cmd_checkerzvenv.subprocess.check_call)	r^   rn   r/   r4   r*   ÚTemporaryDirectoryro   r	   Zupgrade_dependencies)r8   rs   r{   Zfake_contextr   rz   r   Útest_upgrade_dependencies–   s   

ÿï"ïz#BasicTest.test_upgrade_dependenciesc                 C   s˜   t | jƒ |  tj| j¡ tj | j| j| j	¡}|ddg}d| jfd| jfdt
jfdt
jffD ]\}}d| |d< t|ƒ\}}|  | ¡ | ¡ ¡ q/dS )	z>
        Test that the prefix values are as expected.
        ú-cNÚprefixÚexec_prefixÚbase_prefixÚbase_exec_prefixzimport sys; print(sys.%s)r#   )r   r,   rD   r^   r_   r'   r(   rE   r.   r3   r/   r   r‚   r   rp   ÚstripÚencode)r8   Úenvpyr   r   Zexpectedr   r   r   r   r   Útest_prefixes®   s   

üùzBasicTest.test_prefixesr&   ))r   )r   r   )r   úsite-packages)r    )r$   )r!   r!   r"   Nr#   r‡   c              	   C   sp   |D ]3}t jj| jg|¢R Ž }t  |¡ t j ||¡}t|dƒ}| d¡ W d  ƒ q1 s0w   Y  qdS )zn
        Create some files in the environment which are unrelated
        to the virtual environment.
        Úwbó   Still here?N)r'   r(   rE   r,   ÚmkdirrI   Úwrite)r8   ÚpathsÚfilenameÚsubdirsÚdrU   rK   r   r   r   Úcreate_contentsÑ   s   
ÿüzBasicTest.create_contentsc              	   C   sà   |   | jd¡ t | j¡ | jD ]5}tjj| jg|d ¢R Ž }|  tj 	|¡¡ t
|dƒ}|  | ¡ d¡ W d  ƒ q1 s@w   Y  qtjdd}| | j¡ | jD ]}tjj| jg|d ¢R Ž }|  tj 	|¡¡ qUdS )zE
        Test creating environment in an existing directory.
        Úfoo)r‘   Úrbr‰   NT©Úclear)r   ÚENV_SUBDIRSr^   r_   r,   r'   r(   rE   rS   r6   rI   rp   rJ   rn   rc   )r8   rŽ   rU   rK   rs   r   r   r   Útest_overwrite_existingÝ   s   
ÿ
þz!BasicTest.test_overwrite_existingc                 C   sZ   t  |¡D ]%}t j ||¡}t j |¡st j |¡r t  |¡ qt j |¡r*t|ƒ qd S r;   )	r'   rg   r(   rE   rb   ÚisfileÚremoverT   r   )r8   r(   rU   r   r   r   Úclear_directoryï   s   €ûzBasicTest.clear_directoryc              	   C   s„   | j d d… D ]8}tjj| jg|¢R Ž }t|dƒ}| d¡ W d   ƒ n1 s)w   Y  |  tt	ft
j| j¡ |  | j¡ qd S )Né   rˆ   ó    )r•   r'   r(   rE   r,   rI   r‹   ZassertRaisesÚ
ValueErrorÚOSErrorr^   r_   r™   )r8   rŒ   rU   rK   r   r   r   Útest_unoverwritable_fails÷   s   ÿûz#BasicTest.test_unoverwritable_failsc                 C   s¬   dD ]Q}t j|d}|  |j| j¡ |  | j¡ |  | j¡ | j| jŽ  |  	| j| j
¡}tj |¡sG|  	| j¡}td| ƒ tdt |¡ ƒ |  tj |¡d| ¡ qdS )zC
        Test upgrading an existing environment directory.
        ©FT)Úupgrader[   r\   r]   N)r^   rn   rD   r_   r,   rT   r.   r$   r!   rF   r3   r'   r(   r6   rf   rg   rS   )r8   r    rs   rU   ri   r   r   r   Útest_upgrade   s   ôzBasicTest.test_upgradec                 C   sF   dD ]\}}t jd|d}| | j¡ |  d¡}|  d| |¡ qdS )z:
        Test isolation from system site-packages
        ))TÚtrue)FZfalseT)r”   Úsystem_site_packagesrZ   z"include-system-site-packages = %s
N)r^   rn   r_   r,   rM   re   )r8   ZsspÚsrs   rh   r   r   r   Útest_isolation  s   
üzBasicTest.test_isolationzNeeds symlinksc                 C   sh   dD ]/}t jd|d}| | j¡ |  | j| j¡}|r1| jr(|  t	j
 |¡¡ q|  t	j
 |¡¡ qdS )z3
        Test symlinking works as expected
        rŸ   T©r”   ÚsymlinksN)r^   rn   r_   r,   rF   r.   r3   r7   rc   r'   r(   rb   rS   )r8   Zuslrs   rU   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 )úD
        Test that the sys.executable value is as expected.
        r~   ú!import sys; print(sys.executable)N)r   r,   rD   r^   r_   r'   r(   rE   r)   r.   r3   r   rp   rƒ   r„   ©r8   r…   r   r   r   r   r   Útest_executable7  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 )r©   Tr¦   r~   rª   N©r   r,   r^   rn   r_   r'   r(   rE   r)   r.   r3   r   rp   rƒ   r„   ©r8   rs   r…   r   r   r   r   r   Útest_executable_symlinksD  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   Ï¼ÑžÐ¢Î»Ð¤Ð™Tr“   zactivate.batú&r~   zprint(0)ZoemrG   Ú0N)r   r,   r'   r(   rE   r)   r^   rn   r_   r.   r3   r   rp   rƒ   )r8   r,   rs   Úactivater…   r   r   r   r   r   Útest_unicode_in_batch_fileR  s   


þz$BasicTest.test_unicode_in_batch_filec                 C   sh   t ƒ  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.
        r~   zrfrom multiprocessing import Pool; pool = Pool(1); print(pool.apply_async("Python".lower).get(3)); pool.terminate()rw   N)r   r   r,   rD   r^   r_   r'   r(   rE   r)   r.   r3   r   rp   rƒ   r„   r«   r   r   r   Útest_multiprocessingc  s   
ÿ
ÿzBasicTest.test_multiprocessingznot relevant on Windowsc                 C   sÒ   t  d¡}|d u r|  d¡ t| jƒ tjdd}| | j¡ tj	 
| j| jd¡}tj	 
| jd¡}t|dƒ}| d|› d	¡ W d   ƒ n1 sJw   Y  t||gƒ\}}|  |d
 ¡ ¡ |  |d
 ¡ ¡ d S )NÚbashzbash required for this testTr“   r²   ztest_strict.shÚwzset -euo pipefail
source z
deactivate
Ú )ÚshutilÚwhichZskipTestr   r,   r^   rn   r_   r'   r(   rE   r.   rI   r‹   r   rp   r„   )r8   rµ   rs   r²   Ztest_scriptrK   r   r   r   r   r   Ú%test_deactivate_with_strict_bash_optsx  s    


ÿÿz/BasicTest.test_deactivate_with_strict_bash_optsrY   zonly relevant on macOSc                 C   sf   t | jƒ t ¡ }| | j¡ tj tj | j¡| j	| j
¡}t|ddgƒ\}}|  | ¡ d ¡ ¡ d S )Nr~   z5import os; print("__PYVENV_LAUNCHER__" in os.environ)ÚFalser­   r®   r   r   r   Útest_macos_env‹  s   
ÿ
ÿzBasicTest.test_macos_env)!rN   rO   rP   rQ   rT   rj   rv   r}   ÚrequireVenvCreater†   r/   r4   r•   r0   r   r–   r™   rž   r¡   r¥   ÚunittestÚ
skipUnlessr   r¨   r¬   r¯   r'   r-   r³   r´   ZskipIfrº   r¼   r   r   r   r   rR   W   sH    

û	






rR   c                   @   sR   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 )Nr~   z1try:
 import pip
except ImportError:
 print("OK")úlatin-1r·   ZOK)r'   r(   rE   r)   r,   r.   r3   r   Údecoderp   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 r;   ©r   r,   rD   r^   r_   rÃ   r<   r   r   r   Útest_no_pip_by_default§  s   
z$EnsurePipTest.test_no_pip_by_defaultc                 C   s*   t | jƒ | jtj| jdd |  ¡  d S )NF)Úwith_piprÄ   r<   r   r   r   Útest_explicit_no_pip¬  s   
z"EnsurePipTest.test_explicit_no_pipc                 C   sT   t tjdƒ}|  | ¡ d¡ W d   ƒ n1 sw   Y  |  tj tj¡¡ d S )Nr’   r›   )rI   r'   Údevnullrp   rJ   rS   r(   r6   )r8   rK   r   r   r   Útest_devnull±  s   ÿzEnsurePipTest.test_devnullc                 C   sž  t | jƒ tƒ –}d|d< d|d< t ¡ z}||d< d}d}d}|fD ]1\}}tj ||¡}	t |	¡ tj |	|¡}
t	|
d	ƒ}| 
|¡ W d   ƒ q#1 sOw   Y  q#z| jtj| j|d
d W n% tjy‡ } z|jjdd}d}|  | ||¡¡ W Y d }~nd }~ww W d   ƒ n1 s’w   Y  W d   ƒ n1 s¡w   Y  tj tj | j¡| j| j¡}t|ddddddddg	ƒ\}}| d¡}|  |d¡ | d¡}d t ¡ ¡}|  |d t|ƒ… |¡ t | j¡ d¡}|  ||¡ tƒ }d|d< t|dddddddgƒ\}}W d   ƒ n	1 sw   Y  | d¡}tjdd|tj d}|  | !¡ d¡ | d¡}|  d|¡ |  d|¡ |sM|  "¡  d S d S )NÚignoreZPYTHONWARNINGSÚ1ZPIP_NO_INSTALLÚHOMEz[global]
no-install=1)ry   zpip.ini)z.pipzpip.confr¶   T)r£   rÆ   Úreplace)Úerrorsz{}

**Subprocess Output**
{}z-Wzignore::DeprecationWarningzignore::ImportWarningz-Irx   ry   z	--versionrÁ   r·   zpip {}zensurepip._uninstallzl^(WARNING: )?The directory .* or its parent directory is not owned or is not writable by the current user.*$)ÚflagszSuccessfully uninstalled pipz#Successfully uninstalled setuptools)#r   r,   r   r*   r|   r'   r(   rE   rŠ   rI   r‹   rD   r^   r_   r   r   rB   rÂ   ZfailÚformatr)   r.   r3   r   rp   Ú	ensurepipÚversionÚlenÚfsencodere   ÚreÚsubÚ	MULTILINEÚrstriprÃ   )r8   r£   ZenvvarsZhome_dirZ
bad_configZwin_locationZposix_locationrd   ÚfnameÚdirpathZfpathrK   ÚexcZdetailsÚmsgr…   r   r   Zexpected_versionr,   r   r   r   Údo_test_with_pip»  s~   


ÿ
þ€û€ë€ó)
þ

ýü

þ
ÿ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)rN   rO   rP   rQ   rÃ   rÅ   rÇ   rÉ   rÝ   r¾   r¿   Úctypesr   rÞ   r   r   r   r   rÀ   —  s    

_rÀ   Ú__main__r;   )&rQ   rÑ   r'   Zos.pathrÕ   r¸   r`   r   r/   r*   Ztest.supportr   r   r   r   Ztest.support.os_helperr   r   r   r¾   r^   Zunittest.mockr	   rß   ÚImportErrorr¿   r   r   r1   r9   r½   r   ZTestCaser   rR   rÀ   rN   Úmainr   r   r   r   Ú<module>   sH    ÿ
ý
(  B 	ÿ