a zeX@s*dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZmZmZmZddlZddlZddlmZz ddlZWneydZYn0eejejkpejejkdZdddZGdd d ej Z!Gd d d e!Z"eGd d d e!Z#e$dkr&e%dS)z| Test harness for the venv module. Copyright (C) 2011-2012 Vinay Sajip. Licensed to the PSF under a contributor agreement. N)captured_stdoutcaptured_stderr requires_zlib can_symlinkEnvironmentVarGuardrmtree import_module*skip_if_broken_multiprocessing_synchronize)patchz:cannot run venv.create from within a venv on this platformcCsBtj|tjtj|d}|\}}|jr:t|j|||||fS)N)stdoutstderrencoding) subprocessPopenPIPE communicate returncodeCalledProcessError)cmdr pouterrrB/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_venv.py check_output%s  rc@sBeZdZdZdZddZddZddZd d Zd d d dZ dS)BaseTestzBase class for venv tests.icCstjt|_tjdkr0d|_d|_d|_ n$d|_ddt j ddf|_d |_ t j }tj |d |_t jd krtj|rtj|sd |_nd |_dS)NntScriptsLibIncludebinlib python%d.%dincludewin32TF)ospathrealpathtempfilemkdtempenv_dirnamebindirr"r%sys version_info_base_executablesplitexeplatformlexistsexistscannot_link_exe)self executablerrrsetUp4s"    zBaseTest.setUpcCst|jdSN)rr-r9rrrtearDownGszBaseTest.tearDownc OsjtD}t}||i|Wdn1s20YWdn1sP0Y||fSr<)rrgetvalue)r9funcargskwargsoutputerrorrrrrun_with_captureJsJzBaseTest.run_with_capturecGstjj|jg|RSr<)r(r)joinr-)r9rArrr get_env_filePszBaseTest.get_env_filezutf-8r cGs@t|j|d|d}|}Wdn1s20Y|S)NrrH)openrGread)r9r rAfresultrrrget_text_file_contentsSs&zBaseTest.get_text_file_contentsN) __name__ __module__ __qualname____doc__ZmaxDiffr;r>rErGrNrrrrr0src @sBeZdZdZddZddZddZdd Zed d Z e j d krHd Z n4ddddde j ddfdde j dddffZ ddZddZddZddZddZd d!Zeed"d#d$Zed%d&Zeed"d'd(Zeejd)kd*d+d,Zed-d.Zeejd)kd/d0d1Zee j d2kd3d4d5Z dS)6 BasicTestzTest venv module functionality.cGs |j|}|tj|dSr<)rG assertTruer(r)isdir)r9rAfnrrrrU[s zBasicTest.isdircCs$t|j|tj|j||j||j|j|j| d}t ddkoht j dkohtjdk}|r|t j|n|t j||d}tj}t j|}|d||| |j|j}t j|s| |j}td|td t ||t j|d |d S) zB Test the create function with default arguments. Zlib64Pposixdarwin pyvenv.cfgz home = %sContents of %r: %rFile %r should exist.N)rr-rEvenvcreaterUr/r%r"rGstructcalcsizer(r.r0r5rTr)islink assertFalser7rNr2dirnameassertInr4printlistdir)r9rZ conditionsdatar:r)rVbdrrr test_defaults_s,         zBasicTest.test_defaultscCs0tj|jd}t|jt}||j|j| |j}| d}| |j d|| d|t|jtjdd}||j|j| |j}| d}| |j d|d|t|jtjd d}tjt}||j|j| |j}| d}| |j d||d ||dS) Nr[z(%s) z prompt = z My prompt)promptz (My prompt) zprompt = 'My prompt' .zprompt = '%s' )r(r)r3r-rr_ EnvBuilderrEr`ensure_directoriesrN assertEqualrmZ assertNotInrfbasenamegetcwd)r9Zenv_namebuildercontextricwdrrr test_prompt{s.             zBasicTest.test_promptc st}tjdkrdnd}tjtjd}t }tj tj |||tjdkrltj tj fdd}| |}td|||Wdn1s0YWdn1s0YdS)Nr'rr!rlc s2tj|d|d<|ddddddgdS)Nr-mpipinstallz --upgradeZ setuptools)r(r)normcaserq)rZ expect_exer9rrpip_cmd_checkersz.pip_cmd_checkerzvenv.subprocess.check_call)r_ror0r5r(r)r3r:r+TemporaryDirectoryr{rFr*rpr Zupgrade_dependencies)r9rtZbin_pathZ python_exeZ fake_env_dirr}Z fake_contextrr|rtest_upgrade_dependenciess    z#BasicTest.test_upgrade_dependenciescCst|j|tj|jtj|j|j|j }|ddg}d|jfd|jfdt j fdt j ffD]4\}}d||d<t |\}}|||q^dS) z> Test that the prefix values are as expected. -cNprefix exec_prefix base_prefixbase_exec_prefixzimport sys; print(sys.%s)r$)rr-rEr_r`r(r)rFr/r4r0rrrrqstripencode)r9envpyrrZexpectedrrrrr test_prefixess     zBasicTest.test_prefixesr'))r)r r)r site-packages)r!)r%)r"r"r#Nr$rc Csp|D]f}tjj|jg|R}t|tj||}t|d}|dWdq1s`0YqdS)zn Create some files in the environment which are unrelated to the virtual environment. wb Still here?N)r(r)rFr-mkdirrJwrite)r9pathsfilenamesubdirsdrVrLrrrcreate_contentss   zBasicTest.create_contentsc Cs||jdt|j|jD]j}tjj|jg|dR}|tj |t |d }| | dWdq 1s0Yq tj dd}||j|jD]0}tjj|jg|dR}|tj |qdS)zE Test creating environment in an existing directory. foo)rrbrNTclear)r ENV_SUBDIRSr_r`r-r(r)rFrTr7rJrqrKrord)r9rrVrLrtrrrtest_overwrite_existings   0   z!BasicTest.test_overwrite_existingcCsZt|D]J}tj||}tj|s4tj|r@t|q tj|r t|q dSr<) r(rhr)rFrcisfileremoverUr)r9r)rVrrrclear_directorys   zBasicTest.clear_directoryc Cs|jddD]p}tjj|jg|R}t|d}|dWdn1sR0Y|tt ft j |j| |jqdS)Nr) rr(r)rFr-rJrZ assertRaises ValueErrorOSErrorr_r`r)r9rrVrLrrrtest_unoverwritable_failss  (z#BasicTest.test_unoverwritable_failscCsdD]}tj|d}||j|j||j||j|j|j| |j|j }t j |s| |j}td|tdt ||t j |d|qdS)zC Test upgrading an existing environment directory. FT)upgrader\r]r^N)r_rorEr`r-rUr/r%r"rGr4r(r)r7rgrhrT)r9rrtrVrjrrr test_upgrades       zBasicTest.test_upgradecCsFdD]<\}}tjd|d}||j|d}|d||qdS)z: Test isolation from system site-packages ))Ttrue)FZfalseT)rsystem_site_packagesr[z"include-system-site-packages = %s N)r_ror`r-rNrf)r9Zsspsrtrirrrtest_isolations    zBasicTest.test_isolationzNeeds symlinkscCshdD]^}tjd|d}||j||j|j}|r|jrP|t j |q| t j |qdS)z3 Test symlinking works as expected rTrsymlinksN) r_ror`r-rGr/r4r8rdr(r)rcrT)r9ZuslrtrVrrrtest_symlinking&s zBasicTest.test_symlinkingcCsbt|j|tj|jtjtj|j|j |j }t |ddg\}}| | |dS)D Test that the sys.executable value is as expected. r!import sys; print(sys.executable)N)rr-rEr_r`r(r)rFr*r/r4rrqrrr9rrrrrrtest_executable>s  zBasicTest.test_executablecCslt|jtjddd}||jtjtj|j|j |j }t |ddg\}}| | |dS)rTrrrNrr-r_ror`r(r)rFr*r/r4rrqrrr9rtrrrrrrtest_executable_symlinksKs   z"BasicTest.test_executable_symlinksrzonly relevant on WindowscCst|jtjtj|jd}tjdd}||tj||j d}tj||j |j }t |d|j ddgdd \}}| | d d S) z0 Test handling of Unicode paths u ϼўТλФЙTrz activate.bat&rzprint(0)ZoemrH0N)rr-r(r)rFr*r_ror`r/r4rrqr)r9r-rtactivaterrrrrrtest_unicode_in_batch_fileYs    z$BasicTest.test_unicode_in_batch_filecCshtt|j|tj|jtjtj |j|j |j }t |ddg\}}| |ddS)zA Test that the multiprocessing is able to spawn. rzrfrom multiprocessing import Pool; pool = Pool(1); print(pool.apply_async("Python".lower).get(3)); pool.terminate()ZpythonN)r rr-rEr_r`r(r)rFr*r/r4rrqrrrrrrtest_multiprocessingjs  zBasicTest.test_multiprocessingznot relevant on WindowscCstd}|dur|dt|jtjdd}||jtj |j|j d}tj |jd}t |d"}| d|d Wdn1s0Yt||g\}}||d ||d dS) Nbashzbash required for this testTrrztest_strict.shwzset -euo pipefail source z deactivate )shutilwhichZskipTestrr-r_ror`r(r)rFr/rJrrrqr)r9rrtrZ test_scriptrLrrrrr%test_deactivate_with_strict_bash_optss      (z/BasicTest.test_deactivate_with_strict_bash_optsrZzonly relevant on macOScCsft|jt}||jtjtj|j|j |j }t |ddg\}}| | ddS)Nrz5import os; print("__PYVENV_LAUNCHER__" in os.environ)Falserrrrrtest_macos_envs   zBasicTest.test_macos_env)!rOrPrQrRrUrkrwrrequireVenvCreaterr0r5rr1rrrrrrunittest skipUnlessrrrrr(r.rrZskipIfrrrrrrrSXsF           rSc@sReZdZdZddZddZddZdd Zd d Ze e d e d dZ dS) EnsurePipTestz%Test venv module installation of pip.cCsdtjtj|j|j|j}t|ddg\}}|d}| |d|d}| | ddS)Nrz1try: import pip except ImportError: print("OK")latin-1rZOK) r(r)rFr*r-r/r4rdecoderqrrrrrassert_pip_not_installeds    z&EnsurePipTest.assert_pip_not_installedcCs&t|j|tj|j|dSr<rr-rEr_r`rr=rrrtest_no_pip_by_defaults z$EnsurePipTest.test_no_pip_by_defaultcCs*t|j|jtj|jdd|dS)NF)with_piprr=rrrtest_explicit_no_pips z"EnsurePipTest.test_explicit_no_pipcCsTttjd }||dWdn1s20Y|tjtjdS)Nrr)rJr(devnullrqrKrTr)r7)r9rLrrr test_devnulls.zEnsurePipTest.test_devnullc Cst|jt0}d|d<d|d<t}||d<d}d}d}|fD]b\}}tj||} t| tj| |} t | d } | |WdqH1s0YqHz|j t j |j|d d WnLtjy} z0| jjd d } d}||| | WYd} ~ n d} ~ 00Wdn1s*0YWdn1sJ0Ytjtj|j|j|j}t|ddddddg\}}|d}||d|d}dt}||dt||t|jd}|||t(}t|dddddg\}}Wdn1s&0Y|d}tjdd|tj d}||!d|d}|d||d||s|"dS)NeZPYTHONWARNINGS1ZPIP_NO_INSTALLHOMEz[global] no-install=1)ryzpip.ini)z.pipzpip.confrT)rrreplace)errorsz{} **Subprocess Output** {}z-Wzignore::DeprecationWarningz-Irxryz --versionrrzpip {}zensurepip._uninstallzl^(WARNING: )?The directory .* or its parent directory is not owned or is not writable by the current user.*$)flagszSuccessfully uninstalled pipz#Successfully uninstalled setuptools)#rr-rr+r~r(r)rFrrJrrEr_r`rrrCrZfailformatr*r/r4rrq ensurepipversionlenfsencoderfresub MULTILINErstripr)r9renvvarsZhome_dirZ bad_configZ win_locationZposix_locationreZfnamedirpathZfpathrLexcZdetailsmsgrrrZexpected_versionr-rrrdo_test_with_pipsf     *  h      *    zEnsurePipTest.do_test_with_pipzpip requires ctypescCs|d|ddS)NFT)rr=rrr test_with_pips zEnsurePipTest.test_with_pipN)rOrPrQrRrrrrrrrctypesrrrrrrrs  Z r__main__)N)&rRrr(Zos.pathrrrarr0r+Z test.supportrrrrrrrr rr_Z unittest.mockr r ImportErrorrrrr2r:rrZTestCaserrSrrOmainrrrrs@(       (H