a zeX@s&ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z mZmZmZmZddlZddlZddlmZz ddlZWneydZYn0eejejkpejejkdZdddZGdddejZ Gd d d e Z!eGd d d e Z"e#d kr"e$dS)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@s>eZdZdZddZddZddZdd Zd d d d ZdS)BaseTesticCstjt|_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__ZmaxDiffr;r>rErGrNrrrrr0s rc @s>eZdZddZddZddZddZed d Ze j d krDd Z n4d dddde j ddfdde j dddffZ ddZ ddZddZddZddZdd Zeed!d"d#Zed$d%Zeed!d&d'Zeejd(kd)d*d+Zed,d-Zeejd(kd.d/d0Zee j d1kd2d3d4ZdS)5 BasicTestcGs |j|}|tj|dSr<)rG assertTruer(r)isdir)r9rAfnrrrrT[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 |dS) NZlib64Pposixdarwin pyvenv.cfgz home = %sContents of %r: %rFile %r should exist.)rr-rEvenvcreaterTr/r%r"rGstructcalcsizer(r.r0r5rSr)islink assertFalser7rNr2dirnameassertInr4printlistdir)r9rZ conditionsdatar:r)rUbdrrr 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) NrZz(%s) z prompt = z My prompt)promptz (My prompt) zprompt = 'My prompt' .zprompt = '%s' )r(r)r3r-rr^ EnvBuilderrEr_ensure_directoriesrN assertEqualrlZ assertNotInrebasenamegetcwd)r9Zenv_namebuildercontextrhcwdrrr test_prompt{s.             zBasicTest.test_promptc st}tjdkrdnd}tjtjd}t }tj tj |||tjdkrltj tj fdd}| |}td|||Wdn1s0YWdn1s0YdS)Nr'rr!rkc s2tj|d|d<|ddddddgdS)Nr-mpipinstallz --upgradeZ setuptools)r(r)normcaserp)rZ expect_exer9rrpip_cmd_checkersz.pip_cmd_checkerzvenv.subprocess.check_call)r^rnr0r5r(r)r3r:r+TemporaryDirectoryrzrFr*ror Zupgrade_dependencies)r9rsZbin_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)N-cprefix exec_prefix base_prefixbase_exec_prefixzimport sys; print(sys.%s)r$)rr-rEr^r_r(r)rFr/r4r0rrrrpstripencode)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)Nwb Still here?)r(r)rFr-mkdirrJwrite)r9pathsfilenamesubdirsdrUrLrrrcreate_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)Nfoo)rrbrTclear)r ENV_SUBDIRSr^r_r-r(r)rFrSr7rJrprKrnrc)r9rrUrLrsrrrtest_overwrite_existings   0   z!BasicTest.test_overwrite_existingcCsZt|D]J}tj||}tj|s4tj|r@t|q tj|r t|q dSr<) r(rgr)rFrbisfileremoverTr)r9r)rUrrrclear_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)r9rrUrLrrrtest_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)NFT)upgrader[r\r])r^rnrEr_r-rTr/r%r"rGr4r(r)r7rfrgrS)r9rrsrUrirrr test_upgrades       zBasicTest.test_upgradecCsFdD]<\}}tjd|d}||j|d}|d||qdS)N))Ttrue)FZfalseT)rsystem_site_packagesrZz"include-system-site-packages = %s )r^rnr_r-rNre)r9Zsspsrsrhrrrtest_isolations    zBasicTest.test_isolationzNeeds symlinkscCshdD]^}tjd|d}||j||j|j}|r|jrP|t j |q| t j |qdS)NrTrsymlinks) r^rnr_r-rGr/r4r8rcr(r)rbrS)r9ZuslrsrUrrrtest_symlinking&s zBasicTest.test_symlinkingcCsbt|j|tj|jtjtj|j|j |j }t |ddg\}}| | |dS)Nr!import sys; print(sys.executable))rr-rEr^r_r(r)rFr*r/r4rrprrr9rrrrrrtest_executable>s  zBasicTest.test_executablecCslt|jtjddd}||jtjtj|j|j |j }t |ddg\}}| | |dS)NTrrrrr-r^rnr_r(r)rFr*r/r4rrprrr9rsrrrrrrtest_executable_symlinksKs   z"BasicTest.test_executable_symlinksrzonly relevant on WindowscCst|jtjtj|jd}tjdd}||tj||j d}tj||j |j }t |d|j ddgdd \}}| | d dS) Nu ϼўТλФЙTrz activate.bat&rzprint(0)ZoemrH0)rr-r(r)rFr*r^rnr_r/r4rrpr)r9r-rsactivaterrrrrrtest_unicode_in_batch_fileYs    z$BasicTest.test_unicode_in_batch_filecCshtt|j|tj|jtjtj |j|j |j }t |ddg\}}| |ddS)Nrzrfrom multiprocessing import Pool; pool = Pool(1); print(pool.apply_async("Python".lower).get(3)); pool.terminate()Zpython)r rr-rEr^r_r(r)rFr*r/r4rrprrrrrrtest_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^rnr_r(r)rFr/rJrrrpr)r9rrsrZ test_scriptrLrrrrr%test_deactivate_with_strict_bash_optss      (z/BasicTest.test_deactivate_with_strict_bash_optsrYzonly 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) rOrPrQrTrjrvr~requireVenvCreaterr0r5rr1rrrrrrunittest skipUnlessrrrrr(r.rrZskipIfrrrrrrrRXsD           rRc@sNeZdZddZddZddZddZd d Ze e d e d d Z dS) EnsurePipTestcCsdtjtj|j|j|j}t|ddg\}}|d}| |d|d}| | ddS)Nrz1try: import pip except ImportError: print("OK")latin-1rZOK) r(r)rFr*r-r/r4rdecoderprrrrrassert_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(devnullrprKrSr)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)rxzpip.ini)z.pipzpip.confrT)rrreplace)errorsz{} **Subprocess Output** {}z-Wzignore::DeprecationWarningz-Irwrxz --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/r4rrp ensurepipversionlenfsencodereresub MULTILINErstripr)r9renvvarsZhome_dirZ bad_configZ win_locationZposix_locationrdZfnamedirpathZfpathrLexcZdetailsmsgrrrZexpected_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) rOrPrQrrrrrrrctypesrrrrrrrs  Z r__main__)N)%rr(Zos.pathrrr`rr0r+Z test.supportrrrrrrrr rr^Z unittest.mockr r ImportErrorrrrr2r:rrZTestCaserrRrrOmainrrrrs>(       (H