o
    Zh<                     @   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	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 ddlmZ ddlmZ ddlZddlmZ ddlZddlm  mZ ddlmZmZm Z  ddl!m"Z" dd	lm#Z# dd
l$mZ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0Z1ej2dddd Z3G dd dZ4e/dZ5G dd dZ6ej78dG dd dZ9ej2dd Z:ej7;dej7;dG d d! d!Z<ej2d"d# Z=ej7;d#G d$d% d%Z>ej2d&d' Z?G d(d) d)Z@G d*d+ d+ZAd,d- ZBd.d/ ZCd0d1 ZDd2d3 ZEd4d5eBddd6fd7d8ZFej7jGe
jHId9o=eJe
jKd:d;G d<d= d=ZLG d>d? d?ZMG d@dA dAZNG dBdC dCeZOdDdE ZPej7jQe
jHdFkdGd;dHdI ZRdS )JzEasy install Tests    N)Path)
NamedTuple)mockpath)Distributionnormalize_pathworking_set)sandbox)PthDistributions)r   	run_setup)fail_on_ascii)
MockServerpath_to_url   )contexts)DALST)Zautousec                 C   s   |  dd dS )z
    Important: Disable the default index for pip to avoid
    querying packages in the index and potentially resolving
    and installing packages there.
    PIP_NO_INDEXtrueN)setenv)monkeypatch r   Y/var/www/html/lang_env/lib/python3.10/site-packages/setuptools/tests/test_easy_install.pypip_disable_index)   s   r   c                   @      e Zd Zdd Zdd ZdS )FakeDistc                 C   s   |dkri S ddiS )Nconsole_scriptsnameepr   )selfgroupr   r   r   get_entry_map4   s   zFakeDist.get_entry_mapc                 C      dS )Nspecr   r    r   r   r   as_requirement9   s   zFakeDist.as_requirementN)__name__
__module____qualname__r"   r&   r   r   r   r   r   3   s    r   z3
    from setuptools import setup

    setup()
    c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	dd Z
edd Zej	dd Zedd Zej	dd Zejjejd dddd ZdS )TestEasyInstallTestc                 C   s   t j   }t }tt j|}t	
|d\}}||s#J d|v s)J d|v s/J d|v s5J tjd|tjds@J d S )N   z'spec'z'console_scripts'z'name'z^# EASY-INSTALL-ENTRY-SCRIPT)flags)eiCommandSpecbestfrom_environment	as_headerr   nextScriptWriterget_args	itertoolsislice
startswithresearch	MULTILINE)r    headerdistargs_namescriptr   r   r   test_get_script_argsG   s   z(TestEasyInstallTest.test_get_script_argsc                 C   s   t  }t|}dd |_d|_ddg|_tjt	
 d|_dg|_|  |jji ks/J t|}dd |_ddg|_tjt	
 d|_dg|_|  t|jj }|ddgks`J d S )Nc                   S   r#   NTr   r   r   r   r   <lambda>W       z8TestEasyInstallTest.test_no_find_links.<locals>.<lambda>Tlink1link2okc                   S   r#   rA   r   r   r   r   r   rB   a   rC   )r   r-   easy_installZcheck_pth_processingZno_find_links
find_linksosr   jointempfilemkdtempinstall_dirr=   ensure_finalizedZpackage_indexZscanned_urlssortedkeys)r    r<   cmdrP   r   r   r   test_no_find_linksR   s"   





z&TestEasyInstallTest.test_no_find_linksc                 C   sV   t  }t|}t |_ttj	j
 |  W d   dS 1 s$w   Y  dS )zS
        Test that `cant_write_to_target` is rendered as a DistutilsError.
        N)r   r-   rG   rI   getcwdrM   pytestraises	distutilserrorsDistutilsErrorZcant_write_to_target)r    r<   rQ   r   r   r   test_write_exceptioni   s   


"z(TestEasyInstallTest.test_write_exceptionc                    s:   t d  fdd}|jtd|dd  t v sJ dS )zh
        get_site_dirs should always return site dirs reported by
        site.getsitepackages.
        z/setuptools/test/site-packagesc                      s    gS Nr   r   r   r   r   mock_gspz   s   z8TestEasyInstallTest.test_all_site_dirs.<locals>.mock_gspgetsitepackagesFZraisingN)r   setattrsiter-   get_site_dirs)r    r   r[   r   r   r   test_all_site_dirss   s   z&TestEasyInstallTest.test_all_site_dirsc                 C   s    |j tddd t sJ d S )Nr\   Fr]   )delattrr_   r-   r`   )r    r   r   r   r   0test_all_site_dirs_works_without_getsitepackages   s   zDTestEasyInstallTest.test_all_site_dirs_works_without_getsitepackagesc                 C   sX   dt dfddg}d}|| }tt|d}|D ]
\}}||| q|  t|S )Nsetup.pya5  
                    import setuptools
                    setuptools.setup(
                        name="setuptools-test-unicode",
                        version="1.0",
                        packages=["mypkg"],
                        include_package_data=True,
                    )
                    zmypkg/__init__.py )u   mypkg/☃.txtrf   zsetuptools-test-unicode-1.0.zipw)r   zipfileZipFilestrwritestrcloser    tmpdirfiles
sdist_namesdistZ	sdist_zipfilenamecontentr   r   r   sdist_unicode   s   z!TestEasyInstallTest.sdist_unicodec                 C   Z   t ddgi}|d  }tj|t|dgd}|tjdt| |  || dS )zq
        The install command should execute correctly even if
        the package has unicode filenames.
        script_argsrG   targetxrM   r=   
PYTHONPATHN	r   
ensure_dirr-   rG   rj   setitemrI   environrN   )r    rt   rn   r   r<   rw   rQ   r   r   r   test_unicode_filename_in_sdist   s   z2TestEasyInstallTest.test_unicode_filename_in_sdistc                 C   sf   dt dfddt dfg}d}|| }tt|d}|D ]\}}|||d q|  t|S )	Nrd   ar  
                    import setuptools
                    setuptools.setup(
                        name="setuptools-test-unicode",
                        version="1.0",
                        packages=["mypkg"],
                        include_package_data=True,
                        scripts=['mypkg/unicode_in_script'],
                    )
                    re   zmypkg/unicode_in_scriptu   
                    #!/bin/sh
                    # á

                    non_python_fn() {
                    }
                z&setuptools-test-unicode-script-1.0.ziprg   utf-8)r   rh   ri   rj   rk   encoderl   rm   r   r   r   sdist_unicode_in_script   s&   z+TestEasyInstallTest.sdist_unicode_in_scriptc                 C   ru   )zr
        The install command should execute correctly even if
        the package has unicode in scripts.
        rv   rG   rw   rx   ry   rz   Nr{   )r    r   rn   r   r<   rw   rQ   r   r   r   test_unicode_content_in_sdist   s   z1TestEasyInstallTest.test_unicode_content_in_sdistc                 C   s6   dt dfdt dfg}d}t|| }t|| |S )Nrd   a  
                    import setuptools
                    setuptools.setup(
                        name="setuptools-test-script",
                        version="1.0",
                        scripts=["mypkg_script"],
                    )
                    mypkg_scriptzg
                     #/usr/bin/python
                     print('mypkg_script')
                     zsetuptools-test-script-1.0.zip)r   rj   
make_sdist)r    rn   ro   rp   rq   r   r   r   sdist_script   s   
z TestEasyInstallTest.sdist_scriptlinuxzTest can only be run on Linuxreasonc                 C   sj   t ddgi}|d  }tj|t|dgd}|tjdt| |  || |d 	 s3J dS )	z.
        Check scripts are installed.
        rv   rG   rw   rx   ry   rz   r   N)
r   r|   r-   rG   rj   r}   rI   r~   rN   exists)r    r   rn   r   r<   rw   rQ   r   r   r   test_script_install  s   
z'TestEasyInstallTest.test_script_installN)r'   r(   r)   r@   rR   rY   ra   rc   rT   fixturert   r   r   r   r   r   markskipifsysplatformr7   r   r   r   r   r   r*   F   s(    

#

)

r*   zignore:Unbuilt eggc                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestPTHFileWriterc                 C   s:   t dt g}|jrJ |tt  |jsJ dS )zba pth file manager should set dirty
        if a distribution is in site but also the cwd
        does-not_existN)r   rI   rS   dirtyaddPRDistribution)r    pthr   r   r   !test_add_from_cwd_site_sets_dirty'  s   
z3TestPTHFileWriter.test_add_from_cwd_site_sets_dirtyc                 C   s@   d}t |}td|g}|jrJ |t| |jrJ d S )Nz%/test/location/does-not-have-to-existr   )pkg_resourcesr   r   r   r   r   )r    locationr   r   r   r   test_add_from_site_is_ignored0  s   

z/TestPTHFileWriter.test_add_from_site_is_ignoredc                 C   s   | d}|  t| d}t|}t|}|j|j  kr'g ks,J d J d| d}|  t|}|j| |  |jsJJ d||jvsSJ d|  ||jv s`J d|jd |kskJ d	t|j|jkswJ d
dS )z
        If the pth file is modified under the hood, then PthDistribution
        will refresh its content before saving, merging contents when
        necessary.
        
pth_subdirz	file1.pthz6unless there would be some default added at some pointZ
src_subdirzEthe new_src_path added must still be present/valid in pth1 after savez:right before we save the entry should still not be presentzJthe new_src_path entry should have been added by pth2 with its save() callzVand it should match exactly on the last entry actually given we append to it in save()zXand we should have the exact same list at the end with a fresh PthDistributions instanceN)rJ   mkdirrj   r   pathsappendsave)r    rn   r   Zpth_pathZpth1Zpth2Znew_src_pathZnew_src_path_strr   r   r   *test_many_pth_distributions_merge_together>  s>   

z<TestPTHFileWriter.test_many_pth_distributions_merge_togetherN)r'   r(   r)   r   r   r   r   r   r   r   r   %  s    	r   c                 c   sr    | d j ddd}|t W d    n1 sw   Y  |   | V  W d    d S 1 s2w   Y  d S )Nrd   rg   r   encoding)openwriteSETUP_PYZas_cwd)rn   fr   r   r   setup_contextj  s   
"r   Zuser_overridec                   @   s   e Zd Zede Zedejeddedd Z	eddedd	 Z
ed
d Zdd Zejdd Zejdd Zdd Zejdd Zdd ZdS )TestUserInstallTestz;setuptools.command.easy_install.easy_install.check_site_dirz(setuptools.command.easy_install.__file__site.ENABLE_USER_SITETc                 C      |    d S rZ   assert_not_user_siter%   r   r   r   /test_user_install_not_implied_user_site_enabled~  s   zCTestUserInstallTest.test_user_install_not_implied_user_site_enabledFc                 C   r   rZ   r   r%   r   r   r   0test_user_install_not_implied_user_site_disabled  s   zDTestUserInstallTest.test_user_install_not_implied_user_site_disabledc                  C   s8   t  } d| _t| }dg|_|  |jrJ dd S )Nrd   pyzuser should not be implied)r   script_namer-   rG   r=   rN   user)r<   rQ   r   r   r   r     s   
z(TestUserInstallTest.assert_not_user_sitec                 C   s4   t d td}tjtjtjd |d d S )NmultiprocessingZtest_easy_install)levelstreamzthis should not break)	rT   Zimportorskiplogging	getLoggerbasicConfigINFOr   stderrinfo)r    logr   r   r   test_multiproc_atexit  s   

z)TestUserInstallTest.test_multiproc_atexitc                 C   sJ   |d }| d}|d W d    t|S 1 sw   Y  t|S )Nzfoo-1.0.egg-inforg   z
Name: foo
)r   r   rj   )r    rn   Zegg_filer   r   r   r   foo_package  s   
zTestUserInstallTest.foo_packagec              	   c   s    t |}tdtj|g 5 tjjtj}tjjtj	|d |V  W d    n1 s/w   Y  W d    d S W d    d S 1 sGw   Y  d S )Nzsys.pathrz   )
rj   r   patchr   r   rI   pathseprJ   dictr~   )r    rn   rw   Zpython_pathr   r   r   install_target  s   "z"TestUserInstallTest.install_targetc                 C   s~   t  }d|_t|}||_dg|_|  |j|g |d}t	j
t	j
|j}t	j
t	j
|}||ks=J dS )zd
        The local index must be used when easy_install locates installed
        packages.
        rd   ZfooN)r   r   r-   rG   rM   r=   rN   Zlocal_indexscanrI   r   normcaserealpathr   )r    r   r   r<   rQ   resactualexpectedr   r   r   test_local_index  s   

z$TestUserInstallTest.test_local_indexc                 o   sX    | j |i | ddlm  m} tj|_dV  W d   dS 1 s%w   Y  dS )zw
        Wrap sandbox.setup_context to patch easy_install in that context to
        appear as user-installed.
        r   N)orig_contextsetuptools.command.easy_installcommandrG   r_   	USER_SITE__file__)r    r=   kwargsr-   r   r   r   user_install_setup_context  s   "z.TestUserInstallTest.user_install_setup_contextc                 C   s   t j| _td| jS )Nz setuptools.sandbox.setup_context)r
   r   r   r   r   r   r%   r   r   r   patched_setup_context  s
   z)TestUserInstallTest.patched_setup_contextN)r'   r(   r)   r   r   MockZprev_sp_writer_   r   r   r   staticmethodr   r   rT   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r  s.    


	


r   c               	   c   s    t dd} tjtjd( tdddd}||  W d    n1 s&w   Y  d V  W d    d S 1 s9w   Y  d S )Nzfrom setuptools import setupz from distutils.core import setup)cdrd   rg   r   r   )r   replacer   tempdirrI   chdirr   r   )Zdistutils_setup_pyr   r   r   r   distutils_package  s   "r   c                   @      e Zd Zdd ZdS )TestDistutilsPackagec                 C   s   t ddg d S )Nrd   Z	bdist_eggr   r%   r   r   r   )test_bdist_egg_available_on_distutils_pkg  s   z>TestDistutilsPackage.test_bdist_egg_available_on_distutils_pkgN)r'   r(   r)   r   r   r   r   r   r     s    r   c                  C   s&   t  } | jdkrtd |   | S )Nr   zcould not find a valid port)r   Zserver_portrT   skipstart)Zp_indexr   r   r   
mock_index  s
   

r   c                   @   r   )TestInstallRequiresc                 C   s(  |d }|j dd |d }|j dd | | tjddddt|dt|d	t|d
t|dt|dt|dt|g}i tjd|ji}tj	|t||tj
tjddd}|jdks]J zddd |jD v sjJ tdd |j D sxJ W dS  ty   d|jv rttj d|j   w )z~
        When ``python setup.py install`` is called directly, it will use easy_install
        to fetch dependencies.
        projectT)exist_okinstall-c __import__("setuptools").setup()z--install-basez--install-libz--install-headersz--install-scriptsz--install-dataz--install-purelibz--install-platlibZ__EASYINSTALL_INDEXr   )cwdenvstdoutr   textr   r   /does-not-exist/c                 S      h | ]}|j qS r   r   .0rr   r   r   	<setcomp>      zOTestInstallRequires.test_setup_install_includes_dependencies.<locals>.<setcomp>c                 s   s$    | ]}d |v rd|v r|V  qdS )z"not find suitable distribution forzdoes-not-existNr   )r   liner   r   r   	<genexpr>  s    zOTestInstallRequires.test_setup_install_includes_dependencies.<locals>.<genexpr>zfailed to get random numbersz failure - N)r   create_projectr   
executablerj   rI   r~   url
subprocessrunPIPESTDOUT
returncoderequestsr2   r   
splitlines	ExceptionrT   xfailr   )r    tmp_pathr   Zproject_rootZinstall_rootrQ   r   cpr   r   r   (test_setup_install_includes_dependencies  sZ   
	
z<TestInstallRequires.test_setup_install_includes_dependenciesc                 C   s*   d}|d j t|dd |d   d S )Nz
        [metadata]
        name = project
        version = 42

        [options]
        install_requires = does-not-exist
        py_modules = mod
        	setup.cfgr   r   zmod.py)
write_textr   touch)r    rootconfigr   r   r   r   &  s   	z"TestInstallRequires.create_projectN)r'   r(   r)   r  r   r   r   r   r   r     s    8r   c                   @   s   e Zd Zdd Zeejdd ZdZe	j
dedd Ze	j
ded	d
 Ze	j
dedd Zdd Zdd Zdd Zdd Ze	j
dddd Zdd Zdd ZdS )TestSetupRequiresc                 C   s  | dd | dd | dd t [ t G}t 3}tj|d tjddd	d
|j	dd||g
}t
|  W d   n1 sGw   Y  W d   n1 sVw   Y  W d   n1 sew   Y  W d   n1 stw   Y  dd |jD dgksJ dS )z
        When easy_install installs a source distribution which specifies
        setup_requires, it should honor the fetch parameters (such as
        index-url, and find-links).
        PIP_RETRIES0PIP_TIMEOUTr   falser   r   r   rG   z--index-urlz--exclude-scriptsz--install-dirNc                 S   s   g | ]}|j qS r   r   r   r   r   r   
<listcomp>Q  r   zMTestSetupRequires.test_setup_requires_honors_fetch_params.<locals>.<listcomp>r   )r   r   quietr	  create_sdistr   environmentr   r   r   r   Popenwaitr   )r    r   r   Z	dist_fileZtemp_install_dirrQ   r   r   r   'test_setup_requires_honors_fetch_params5  s6   


z9TestSetupRequires.test_setup_requires_honors_fetch_paramsc                  c   sZ    t  } tj| d}t|dtdfdg |V  W d   dS 1 s&w   Y  dS )zl
        Return an sdist with a setup_requires dependency (of something that
        doesn't exist)
        z"setuptools-test-fetcher-1.0.tar.gzrd   a  
                    import setuptools
                    setuptools.setup(
                        name="setuptools-test-fetcher",
                        version="1.0",
                        setup_requires = ['does-not-exist'],
                    )
                r  rf   N)r   r   rI   r   rJ   r   r   )dir	dist_pathr   r   r   r  S  s   
"zTestSetupRequires.create_sdist)r   dependency_linkssetup_requires)r  r  use_setup_cfgc           	   
   C   s   t dddd}t| t d t G}t||d}tj	|d}t
 \}}t|dg W d   n1 s:w   Y  | }t|d	ksKJ |d
  dksUJ W d   n1 s_w   Y  W d   dS W d   dS 1 sww   Y  dS )a<  
        Regression test for distribution issue 323:
        https://bitbucket.org/tarek/distribute/issues/323

        Ensures that a distribution's setup_requires requirements can still be
        installed and used locally even if a conflicting version of that
        requirement is already on the path.
        zdoes-not-matterfoobar0.0project_nameversionr  rd   --nameNr   r   test_pkg)r   r	   r   r   save_pkg_resources_stater   create_setup_requires_packagerI   r   rJ   r  r   	readlineslenstrip)	r    r  Z	fake_disttemp_dirr$  test_setup_pyr   _stderrlinesr   r   r   .test_setup_requires_overrides_version_conflictx  s(   


"z@TestSetupRequires.test_setup_requires_overrides_version_conflictc                 C   s  t   t  }tj|d}t|dd tj|d}t| t	|}dd |_
|| W d   n1 s=w   Y  tjd| t|ddd	}t| td
}t|ddt||d}tj|d}	t  "\}
}zt|	dg W n tjy   | d Y nw W d   n1 sw   Y  d|
 vsJ |
 }t|dksJ |d  dksJ W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z
        Like ``test_setup_requires_overrides_version_conflict`` but where the
        ``setup_requires`` package is part of a namespace package that has
        *already* been imported.
        zfoo.bar-0.1.tar.gzzfoo.bar0.1zfoo.bar-0.1c                 S   s   | S rZ   r   )memberr   r   r   r   rB     rC   zFTestSetupRequires.test_setup_requires_override_nspkg.<locals>.<lambda>Nr   r  a                      import foo  # Even with foo imported first the
                                # setup_requires package should override
                    import setuptools
                    setuptools.setup(**%r)

                    if not (hasattr(foo, '__path__') and
                            len(foo.__path__) == 2):
                        print('FAIL')

                    if 'foo.bar-0.2' not in foo.__path__[0]:
                        print('FAIL')
                z0.2r"  rd   r#  z9Installing setup.py requirements caused a VersionConflictZFAILr   r   r$  )r   r%  r   rI   r   rJ   make_nspkg_sdistr   tarfiler   extraction_filter
extractallr   insertr   r	   r   r   r&  r  r   r   VersionConflictZfailgetvaluer'  r(  r)  )r    r  r*  Zfoobar_1_archiveZfoobar_1_dirtfr<   templater$  r+  r   r,  r-  r   r   r   "test_setup_requires_override_nspkg  sZ   




	
"z4TestSetupRequires.test_setup_requires_override_nspkgc           	   
   C   s   dd }t  k t  N}t|tdd||d d}tj|d}t  \}}t	|dg W d    n1 s9w   Y  |
 }t|dksJJ |d	  d
ksTJ W d    n1 s^w   Y  W d    d S W d    d S 1 svw   Y  d S )Nc              	   S   s>   dt d|d|d|df|d t dfg}t| | d S )Nrd   zj
                    import setuptools
                    setuptools.setup(
                        name=z",
                        version=z&,
                        py_modules=[z-],
                    )
                    z.pyz6
                    version = 42
                    )r   r   )r  distnamer!  ro   r   r   r   make_dependency_sdist  s$   zVTestSetupRequires.test_setup_requires_with_attr_version.<locals>.make_dependency_sdistzattr: foobar.version)r!  )setup_attrsmake_packager  rd   	--versionr   r   42)r   r%  r   r&  r   rI   r   rJ   r  r   r'  r(  r)  )	r    r  r<  r*  r$  r+  r   r,  r-  r   r   r   %test_setup_requires_with_attr_version  s(   

"z7TestSetupRequires.test_setup_requires_with_attr_versionc              
   C   sT  | dd | dd | dd | d|j t p t \}t|ddtg d	d
}tj	|d}t
|ddd}|td W d    n1 sNw   Y  tj	|d}ttjj t|dg W d    n1 srw   Y  W d    n1 sw   Y  W d    n1 sw   Y  t|jdksJ |jd jdksJ d S )Nr
  r  r  r   r  PIP_INDEX_URLpython-xlib0.19r  r=  r  rg   r   r   z}
                        [easy_install]
                        index_url = https://pypi.org/legacy/
                        rd   r?  r   r   z/python-xlib/)r   r   r   r%  r   r&  r   rI   r   rJ   r   r   r   rT   rU   rV   rW   rX   r   r(  r   )r    r   r   r*  r$  test_setup_cfgfpr+  r   r   r   "test_setup_requires_honors_pip_env  s<   

	z4TestSetupRequires.test_setup_requires_honors_pip_envc              	   C   s   | dd | dd | d|j t J t 6}tj|d}t|dd t	|dd	}t
|d
dtd| dd}tj|d}t|dg W d    n1 sUw   Y  W d    n1 sdw   Y  t|jdksrJ d S )Nr
  r  r  rB  z
dep.tar.gz
dependencyr@  	localhost)	authorityrC  rD  zdependency @ r  rE  rd   r?  r   )r   r   r   r%  r   rI   r   rJ   make_trivial_sdistr   r&  r   r   r(  r   )r    r   r   r*  Z	dep_sdistZdep_urlr$  r+  r   r   r   #test_setup_requires_with_pep508_url#  s(   

z5TestSetupRequires.test_setup_requires_with_pep508_urlc              
   C   s   dt dt ddi}t I t 5}tj||d tt|dd}t	
tjj t|dg W d   n1 s=w   Y  W d   n1 sLw   Y  W d   n1 s[w   Y  t|jd	ksiJ dS )
z2The `allow-hosts` option in not supported anymore.r$  z~
                    from setuptools import setup
                    setup(setup_requires='python-xlib')
                    z\
                    [easy_install]
                    allow_hosts = *
                    rd   r  prefixrd   r?  Nr   )r   r   r%  r   r   buildrj   pathlibr   rT   rU   rV   rW   rX   r   r(  r   )r    r   ro   r*  setup_pyr   r   r   $test_setup_requires_with_allow_hosts7  s*   

z6TestSetupRequires.test_setup_requires_with_allow_hostsc              
   C   sh  | dd | dd | dd | dd d}tt|| }d}tt|| d	d
| d}tt|| }dtjj dtjj d}tt|| d	d| |d }	|	t	dj
||||||dd tt|	}
t $ tt|ddtd	|
gdd}tj|d}t|dg W d   n1 sw   Y  tttttj|d}|dgksJ dS )z#Check `python_requires` is honored.r
  r  r  r   1ZPIP_VERBOSEzdep-1.0.tar.gzz>=2.7dep1.0zdep-2.0.tar.gzz!=.z.*z2.0z
index.htmla  
            <!DOCTYPE html>
            <html><head><title>Links for dep</title></head>
            <body>
                <h1>Links for dep</h1>
                <a href="{dep_1_0_url}"data-requires-python="{dep_1_0_python_requires}">{dep_1_0_sdist}</a><br/>
                <a href="{dep_2_0_url}"data-requires-python="{dep_2_0_python_requires}">{dep_2_0_sdist}</a><br/>
            </body>
            </html>
            )dep_1_0_urldep_1_0_sdistdep_1_0_python_requiresdep_2_0_urldep_2_0_sdistdep_2_0_python_requiresr   rC  rD  )r  r  rE  rd   r?  Nz.eggszdep 1.0)r   r   rj   make_python_requires_sdistr   version_infomajorminorr  r   formatr   r%  r&  r   rI   r   rJ   r   listmapr   find_distributions)r    r   rn   rZ  rY  r[  r]  r\  r^  index	index_urlr$  r+  eggsr   r   r   (test_setup_requires_with_python_requiresQ  s^   
	z:TestSetupRequires.test_setup_requires_with_python_requires!with_dependency_links_in_setup_py)FTc           	   
   C   s  | dd | dd t  t }ttj|ddd tj|d}tj|d}tj|d	}t| t	|d
dd }|rNtj|dg}ng }|
tdj|d W d    n1 sew   Y  t	|d
dd}|
tdjtj|d|d W d    n1 sw   Y  t|dg W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr
  r  r  zpython-xlib-42.tar.gzrC  r@  r$  rd   r  rg   r   r   linksz
                        from setuptools import installer, setup
                        setup(setup_requires='python-xlib==42',
                        dependency_links={dependency_links!r})
                        r  z
                        [easy_install]
                        index_url = {index_url}
                        find_links = {find_links}
                        rg  )rh  rH   r?  )r   r   r%  r   rL  rI   r   rJ   r   r   r   r   rc  r   )	r    r   rk  r*  r$  r+  rF  rG  r  r   r   r   0test_setup_requires_with_find_links_in_setup_cfg  sN   


"zBTestSetupRequires.test_setup_requires_with_find_links_in_setup_cfgc              
   C   sn  t   t  }ttj|ddd tj|d}t| tjt	ddd|d t
tj|d	d
g t| tj|d}tj|d	}t| t|ddd}|t	d W d   n1 siw   Y  |dt| |dd |dd |dd t
|dg W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z
        Use case: installing a package with a build dependency on
        an already installed `dep[extra]`, which in turn depends
        on `extra_dep` (whose is not already installed).
        extra_dep-1.0.tar.gzZ	extra_deprW  rV  a  
                          import setuptools
                          setuptools.setup(
                              name='dep', version='2.0',
                              extras_require={'extra': ['extra_dep']},
                          )
                         rf   rN  rO  rd   	dist_infor$  rg   r   r   
                        from setuptools import installer, setup
                        setup(setup_requires='dep[extra]')
                        NPIP_FIND_LINKSr   rU  r
  r  r  r?  )r   r%  r   rL  rI   r   rJ   r   rQ  r   r   r	   	add_entryr   r   r   rj   r    r   r*  Zdep_pkgr$  r+  rG  r   r   r   4test_setup_requires_with_transitive_extra_dependency  sH   


	


"zFTestSetupRequires.test_setup_requires_with_transitive_extra_dependencyc              
   C   s  t   t  }ttj|ddtdfddg tj|d}t| tj	tddtd	d
|d t
tj|ddg t| tj|d}tj|d}t| t|ddd}|td W d   n1 srw   Y  |dt| |dd |dd |dd t
|dg W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )zk
        Use case: ensure build requirements' extras
        are properly installed and activated.
        rn  rd   a$  
                          import setuptools
                          setuptools.setup(
                              name='extra_dep',
                              version='1.0',
                              py_modules=['extra_dep'],
                          )
                          r  )zextra_dep.pyrf   Zepdepa(  
                          import setuptools
                          setuptools.setup(
                              name='dep', version='2.0',
                              py_modules=['epcmd'],
                              extras_require={'extra': ['extra_dep']},
                              entry_points='''
                                           [distutils.commands]
                                           epcmd = epcmd:epcmd [extra]
                                           ''',
                          )
                         rf   a  
                                     from distutils.command.build_py import build_py

                                     import extra_dep

                                     class epcmd(build_py):
                                         pass
                                     )rd   r  zepcmd.pyrO  ro  r$  rg   r   r   rp  Nrq  r   rU  r
  r  r  Zepcmd)r   r%  r   r   rI   r   rJ   r   r   rQ  r   r	   rr  r   r   r   rj   rs  r   r   r   .test_setup_requires_with_distutils_command_dep  s^   





"z@TestSetupRequires.test_setup_requires_with_distutils_command_depN)r'   r(   r)   r  r   r   r   r  r  rT   r   Zparametrizer.  r:  rA  rH  rM  rT  rj  rm  rt  ru  r   r   r   r   r	  4  s(    

C
*<
+5r	  c              	   C   s(   t | dtd|d|dfdg dS )z\
    Create a simple sdist tarball at dist_path, containing just a simple
    setup.py.
    rd   zT             import setuptools
             setuptools.setup(
                 name=z,
                 version=z
             )
         r  N)r   r   )r  r;  r!  r   r   r   rL  C  s   rL  c           
   
      s   | d  d } fddtdt d D }td|d|d|d	|d
	}d}d|ftj|d|fg}|dd D ]}tjj| ddg  }	||	df q?t| | dS )z
    Make an sdist tarball with distname and version which also contains one
    package with the same name as distname.  The top-level package is
    designated a namespace package).
    rX  r   c                    s   g | ]}d   d| qS )rX  N)rJ   )r   idxpartsr   r   r  g  s    z$make_nspkg_sdist.<locals>.<listcomp>r   zE        import setuptools
        setuptools.setup(
            name=z,
            version=z,
            packages=z",
            namespace_packages=[z]
        )
    z7__import__('pkg_resources').declare_namespace(__name__)rd   z__init__.pyNrf   )	splitranger(  r   rI   r   rJ   r   r   )
r  r;  r!  Z	nspackagepackagesrS  initro   packagerr   r   rw  r   r1  ]  s(   
 r1  c                 C   s&   t | dtdj|||dfdg d S )Nrd   z                import setuptools
                setuptools.setup(
                  name={name!r},
                  version={version!r},
                  python_requires={python_requires!r},
                )
                )r   r!  python_requiresr  )r   r   rc  )r  r;  r!  r~  r   r   r   r_    s   	r_  c                 C   s   t |dksJ t| d3}|D ]'\}}t|d}tj|d}t | |_t	t

 |_|j||d qW d   dS 1 sDw   Y  dS )z
    Create a simple sdist tarball at dist_path, containing the files
    listed in ``files`` as ``(filename, content)`` tuples.
    r   zw:gzr   r   )fileobjN)r(  r2  r   ioBytesIOr   TarInfor7  sizeinttimemtimeaddfile)r  ro   r<   rr   rs   Z
file_bytes	file_infor   r   r   r     s   "r   r  r/  r   c                 C   s  dd| d| gt j| gd}|r|| t j| d}t | |rfg }	g }
|D ](}||}|d v r=|
}n|	}t|t	t
frKd|}|| d|  q-tdjd	|	d	|
d
}nd}tt j|dddd}|| W d   n1 sw   Y  |du rtd}tt j|dddd}|||  W d   n1 sw   Y  t j| | d| d}|||| |S )a  Creates a source tree under path for a trivial test package that has a
    single requirement in setup_requires--a tarball for that requirement is
    also created and added to the dependency_links argument.

    ``distname`` and ``version`` refer to the name/version of the package that
    the test package requires via ``setup_requires``.  The name of the test
    package itself is just 'test_pkg'.
    r$  r  z==)r   r!  r  r  zname version;z: zg
            [metadata]
            {metadata}
            [options]
            {options}
            
)optionsmetadatarf   r  rg   r   r   NzI            import setuptools
            setuptools.setup(**%r)
        rd   -z.tar.gz)rI   r   abspathupdaterJ   r   popry  
isinstancetuplerd  r   r   rc  r   r   )r   r;  r!  r>  Zsetup_py_templater=  r  Ztest_setup_attrsr$  r  r  r   valuesectionZtest_setup_cfg_contentsr   Zfoobar_pathr   r   r   r&    sR   



r&  javaz0Test cannot run under java when executable is shr   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )TestScriptHeaderu   /Users/José/bin/pythonz$C:\Program Files\Python36\python.exec                 C   8   dt tjtj d}t jd}||ksJ d S )N#!r  z#!/usr/local/bin/python	r-   Znt_quote_argrI   r   normpathr   r   r3   
get_headerr    r   r   r   r   r   test_get_script_header     z'TestScriptHeader.test_get_script_headerc                 C   r  )Nr   -x
z#!/usr/bin/python -xr  r  r   r   r   test_get_script_header_args  r  z,TestScriptHeader.test_get_script_header_argsc                 C   s0   t jjd| jd}d| j d}||ksJ d S )N#!/usr/bin/pythonr   r  r  )r-   r3   r  non_ascii_exer    r   r   r   r   r   $test_get_script_header_non_ascii_exe  s
   z5TestScriptHeader.test_get_script_header_non_ascii_exec                 C   s8   t jjdd| j d d}d| j d}||ksJ d S )Nr  "r  #!"z"
)r-   r3   r  exe_with_spacesr  r   r   r   &test_get_script_header_exe_with_spaces  s
   z7TestScriptHeader.test_get_script_header_exe_with_spacesN)	r'   r(   r)   r  r  r  r  r  r  r   r   r   r   r    s    r  c                   @   sR   e Zd Zdd Zdd Zedejej	e
jdd Zdd	 ZdddZdS )TestCommandSpecc                 C   s"   t ddg}| dksJ dS )zx
        Show how a custom CommandSpec could be used to specify a #! executable
        which takes parameters.
        z/usr/bin/envpython3z#!/usr/bin/env python3
N)r-   r.   r1   r    rQ   r   r   r   test_custom_launch_command  s   z*TestCommandSpec.test_custom_launch_commandc                 C   s(   t dg}t j|}||u sJ dS )zG
        from_param should return an instance of a CommandSpec
        pythonN)r-   r.   
from_param)r    rQ   Zcmd_newr   r   r   .test_from_param_for_CommandSpec_is_passthrough  s   z>TestCommandSpec.test_from_param_for_CommandSpec_is_passthroughzsys.executablec                 C   s>   t jdd  tj }t|dksJ | dsJ d S )N__PYVENV_LAUNCHER__r   r  )	rI   r~   r  r-   r.   r0   r(  r1   r7   r  r   r   r   /test_from_environment_with_spaces_in_executable&  s   
z?TestCommandSpec.test_from_environment_with_spaces_in_executablec                 C   s0   t jd}t|dksJ d| vsJ dS )z
        In order to support `executable = /usr/bin/env my-python`, make sure
        from_param invokes shlex on that input.
        z/usr/bin/env my-pythonr+   r  N)r-   r.   r  r(  r1   r  r   r   r   "test_from_simple_string_uses_shlex.  s   z2TestCommandSpec.test_from_simple_string_uses_shlexreturnNc                 C   sT   t t}tjt  W d   n1 sw   Y  t|jdks(J |jdS )zc
        from_param should raise its own TypeError when the argument's type is unsupported
        Nz1Argument has an unsupported type <class 'object'>)	rT   rU   	TypeErrorr-   r.   r  objectrj   r  )r    exc_infor   r   r   %test_from_param_raises_expected_error7  s   z5TestCommandSpec.test_from_param_raises_expected_error)r  N)r'   r(   r)   r  r  r   r   r  r  r   rI   r~   r  r  r  r   r   r   r   r    s    	r  c                   @   r   )TestWindowsScriptWriterc                 C   sN   t jd}|dsJ |dsJ |d}|d}|dr%J d S )Nrf   r  r  z\")r-   ZWindowsScriptWriterr  r7   endswithlstriprstrip)r    hdrr   r   r   test_headerC  s   

z#TestWindowsScriptWriter.test_headerN)r'   r(   r)   r  r   r   r   r   r  B  s    r  c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )VersionStubra  rb  microreleaselevelserialN)r'   r(   r)   r  __annotations__rj   r   r   r   r   r  M  s   
 r  c              	   C   st  t  }|d}dg|_d|_d|_t| |_d |_|d}t| |_d |_| J}t	
 6 t	d d}tdd	d
dd}|dd |d| |d| ||dt  |  W d    n1 siw   Y  W d    n1 sxw   Y  ttdrdnd}	|j }
t|	d |
rt|	d |
sJ |jd dksJ |jd dksJ |jd dksJ d S )NrG   rF   r   Tr   ignorez:3.10.1 (main, Dec 21 2021, 09:17:12) [GCC 10.2.1 20210110]   
   r   finalr   zsys.versionzsys.version_infoZcreate_home_pathpypy_version_infopypyr  z3\.?1z3\.?1\d
py_versionz3.10.1py_version_shortz3.10py_version_nodotZ310)r   Zget_command_objr=   optimizer   rj   Zinstall_userbaseZinstall_usersitecontextwarningscatch_warningssimplefilterr  r^   r   r   finalize_optionshasattrr   rM   lowerr8   r9   Zconfig_vars)rn   Z
tmpdir_cwdr   r<   rQ   Zinstall_cmdr   r!  r   r   rM   r   r   r   &test_use_correct_python_version_stringU  s:   





 

	r  darwinzDhttps://github.com/pypa/setuptools/pull/4716#issuecomment-2447624418c                 C   s6  | dd |dt|d  |dt|d  |dt|d  | dd | d	d tt }|jd
d
d |d }|jd
d
d | dt| d}|d j|dd t|	dg ksfJ t|	dg ksqJ t
dddg t|	dg ksJ dd |	dD }|dh }|dhksJ dS )zC`setup.py develop` should honor `--user` even under build isolationr   FHOMEz.homeUSERPROFILEAPPDATAzsite.USER_BASENzsite.USER_SITET)parentsr   z.sys_prefixz
sys.prefixzF__import__('setuptools').setup(name='aproj', version=42, packages=[])
rd   r   r   *developz--userc                 S   r   r   r  )r   r   r   r   r   r     r   z9test_editable_user_and_build_isolation.<locals>.<setcomp>zeasy-install.pthzaproj.egg-link)r^   r   rj   r   r_   getusersitepackagesr   r  rd  globr   )r   r   r  	user_siteZ
sys_prefixZsetup_script	installedr   r   r   &test_editable_user_and_build_isolation  s*   

r  )S__doc__r   r  r5   r   rI   rR  r8   r_   r   r   r2  rK   r  r  rh   r   typingr   Zunittestr   rT   Zjaracor   r   r   r   rG   r-   r   r   r   r	   
setuptoolsr
   r   Zsetuptools.distZsetuptools.sandboxr   Zsetuptools.testsr   Zsetuptools.tests.serverr   r   rf   r   textwrapr   Zdistutils.errorsrV   r   r   r   r   r*   r   filterwarningsr   r   Zusefixturesr   r   r   r   r   r	  rL  r1  r_  r   r&  r   r   r7   Zis_shr   r  r  r  r  r  r   r  r   r   r   r   <module>   s    

	
	 
`D


[


G    "
L-.