U
    uio                  
   @  s  d dl m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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Zd dlZd dlZd dlmZ d dlmZ d dl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ ddl0m1Z1m2Z2 d dl3m4Z4 ej5ddgddd Z6eded7 dddededdd d!id"Z8d#Z9ej:j;ejd$kd%d&ej:<d'e8d(e9ie8gd)d* Z=d+d, Z>d-d. Z?G d/d0 d0Z@G d1d2 d2ZAd3d4 ZBG d5d6 d6ZCd7d8 ZDG d9d: d:ZEG d;d< d<ZFej:Gd=d>d? ZHd@dA ZIG dBdC dCZJG dDdE dEZKG dFdG dGZLdHdI ZMej:GdJdKdL ZNdMdN ZOdOdP ZPdQdR ZQdSdSdTdUdVZRdWdWdXdYdZZSdS )[    )annotationsN)deepcopy)import_module)EXTENSION_SUFFIXES)Path)dedent)Any)Mock)uuid4)	resources)	_DebuggingTips_encode_pth_find_namespaces_find_package_roots_find_virtual_namespaces_finder_template	_LinkTree_TopLevelFindereditable_wheel)Distribution)	Extension)SetuptoolsDeprecationWarning   )contexts
namespaces)	run_setupstrictlenient)paramsc                 C  s   | j dkrddgS g S )Nr   --config-settingseditable-mode=strict)param)request r#   J/tmp/pip-unpacked-wheel-9osl9suo/setuptools/tests/test_editable_install.pyeditable_opts+   s    
r%   a          [build-system]
        requires = ["setuptools"]
        build-backend = "setuptools.build_meta"

        [project]
        name = "mypkg"
        version = "3.14159"
        license = {text = "MIT"}
        description = "This is a Python package"
        dynamic = ["readme"]
        classifiers = [
            "Development Status :: 5 - Production/Stable",
            "Intended Audience :: Developers"
        ]
        urls = {Homepage = "https://github.com"}

        [tool.setuptools]
        package-dir = {"" = "src"}
        packages = {find = {where = ["src"]}}
        license-files = ["LICENSE*"]

        [tool.setuptools.dynamic]
        readme = {file = "README.rst"}

        [tool.distutils.egg_info]
        tag-build = ".post0"
        zr        global-include *.py *.txt
        global-exclude *.py[cod]
        prune dist
        prune build
        zThis is a ``README``z!---- placeholder MIT license ----mypkga                  import sys
                from importlib.metadata import PackageNotFoundError, version

                try:
                    __version__ = version(__name__)
                except PackageNotFoundError:
                    __version__ = "unknown"
                a                  from importlib.resources import read_text
                from . import __version__, __name__ as parent
                from .mod import x

                data = read_text(parent, "data.txt")
                print(__version__, data, x)
                zx = ''zHello World)__init__.pyz__main__.pymod.pyzdata.txt)pyproject.tomlMANIFEST.inz
README.rstzLICENSE.txtsrcz __import__('setuptools').setup()darwinpypa/setuptools#4328reasonfilessetup.pyc                 C  s   | d }|   tjj||d ddddddt|f|}t|| dddg}|| d	ksht|d
 j	ddd |d j	ddd || dkstd S )Nr&   prefixpython-mpipinstall--no-build-isolation-ez3.14159.post0 Hello Worldzsrc/mypkg/data.txtfoobarutf-8encodingzsrc/mypkg/mod.pyx = 42z3.14159.post0 foobar 42)
mkdirjaracopathbuildstrprintrunstripAssertionError
write_text)tmp_pathvenvr0   r%   projectcmdr#   r#   r$   test_editable_with_pyproject|   s&    	

rM   c                 C  s|   dt dddiddi}tjj|| d | d }dd	d
dddt|f|}t|| dddg}|| dksxtd S )Nr&   ac                  [build-system]
                requires = ["setuptools", "wheel"]
                build-backend = "setuptools.build_meta"

                [project]
                name = "mypkg"
                version = "3.14159"

                [tool.setuptools]
                packages = ["pkg"]
                py-modules = ["mod"]
                r'   za = 4b = 2)r)   pkgr(   r2   r4   r5   r6   r7   r8   r9   -cz$import pkg, mod; print(pkg.a, mod.b)z4 2	r   r@   rA   rB   rC   rD   rE   rF   rG   rI   rJ   r%   r0   rK   rL   r#   r#   r$   test_editable_with_flat_layout   s.    

rS   c                 C  sv   dt dddi}tjj|| d | d }dddd	d
dt|f|}t|| dddg}|| dksrtd S )Nr&   a>                  [build-system]
                requires = ["setuptools", "wheel"]
                build-backend = "setuptools.build_meta"

                [project]
                name = "mod"
                version = "3.14159"

                [tool.setuptools]
                py-modules = ["mod"]
                rN   )r)   r(   r2   r4   r5   r6   r7   r8   r9   rP   zimport mod; print(mod.b)2rQ   rR   r#   r#   r$    test_editable_with_single_module   s,    

rU   c                   @  s8   e Zd Zdd Zejddejdddd Zd	S )
TestLegacyNamespacesc                 C  s   t jtdd}|d }|  d}|D ]v}tj||dd}|X | D}|| tddd	}	t	|	}
|

  |
d
j}|
|| W 5 Q R X W 5 Q R X q&t|d}t|t|kstd S )Nz .*namespace_packages parameter.*matchz.installation_dir)z	myns.pkgAz	myns.pkgBmyns.n.pkgAmyns.n.pkgB42)versionr1   config)
stop_afterZ	dist_infoz*-nspkg.pth)pytestwarnsr   r?   r   build_namespace_packagecontextchdirr   r   finalize_optionsget_finalized_commandnameZ_install_namespaceslistgloblenrG   )selfrI   monkeypatchdeprecationZinstallation_dirZexamplesrf   rO   ctxdistrL   Zeditable_namer0   r#   r#   r$   test_nspkg_file_is_unique   s$     
"z.TestLegacyNamespaces.test_nspkg_file_is_uniqueimpl)pkg_resourcesns)zmyns.nc           
   	   C  s   d}t j|| d|d}t j|| d|d}|d j|dd |d j|dd |dd }	|	d	 |d
dddt|f|	 |d
ddddt|f|	 |d
dd| d| dg |d
ddg dS )a>  
        Installing two packages sharing the same namespace, one installed
        naturally using pip or `--single-version-externally-managed`
        and the other installed in editable mode should leave the namespace
        intact and both packages reachable by import.
        (Ported from test_develop).
        zq        [build-system]
        requires = ["setuptools"]
        build-backend = "setuptools.build_meta"
        z.pkgA)rp   z.pkgBr)   r;   r<   Nr8   r4   r5   r6   r7   r9   rP   zimport z.pkgA; import zimport pkg_resources)r   ra   rH   appendrE   rC   )
rj   rJ   rI   rr   rp   r%   build_systempkg_Apkg_Boptsr#   r#   r$   !test_namespace_package_importable  s    
z6TestLegacyNamespaces.test_namespace_package_importableN)__name__
__module____qualname__ro   r_   markparametrizerx   r#   r#   r#   r$   rV      s   rV   c                   @  s$   e Zd Zdd Zdd Zdd ZdS )TestPep420Namespacesc              	   C  s|   t |d}t |d}|dd }|d |ddddt|f| |ddddd	t|f| |dd
dg dS )z
        Installing two packages sharing the same namespace, one installed
        normally using pip and the other installed in editable mode
        should allow importing both packages.
        rY   rZ   Nr8   r4   r5   r6   r7   r9   rP   z&import myns.n.pkgA; import myns.n.pkgB)r   build_pep420_namespace_packagers   rE   rC   )rj   rJ   rI   r%   ru   rv   rw   r#   r#   r$   rx   .  s    
z6TestPep420Namespaces.test_namespace_package_importablec           	   	   C  s   dt dddidi}tjj||d |d }t|d}t|d}|d	d	 }|d
 |ddddt|f| |dddddt|f| |dddddt|f| |dddg d	S )z@Currently users can create a namespace by tweaking `package_dir`pkgAat                      [build-system]
                    requires = ["setuptools", "wheel"]
                    build-backend = "setuptools.build_meta"

                    [project]
                    name = "pkgA"
                    version = "3.14159"

                    [tool.setuptools]
                    package-dir = {"myns.n.pkgA" = "src"}
                    r'   a = 1)r)   r+   r2   rZ   zmyns.n.pkgCNr8   r4   r5   r6   r7   r9   rP   z#from myns.n import pkgA, pkgB, pkgC)	r   r@   rA   rB   r   r   rs   rE   rC   )	rj   rJ   rI   r%   r0   ru   rv   Zpkg_Crw   r#   r#   r$   &test_namespace_created_via_package_dir=  s"    
z;TestPep420Namespaces.test_namespace_created_via_package_dirc              
   C  s   dt dddddddtd d	i}tjj||d
 |d }dg}|ddddddt|f| |dddg}| dkstd}|ddt |g}d|kstdS )a/  Sometimes users might specify an ``include`` pattern that ignores parent
        packages. In a normal installation this would ignore all modules inside the
        parent packages, and make them namespaces (reported in issue #3504),
        so the editable mode should preserve this behaviour.
        r   ar                      [build-system]
                    requires = ["setuptools", "wheel"]
                    build-backend = "setuptools.build_meta"

                    [project]
                    name = "pkgA"
                    version = "3.14159"

                    [tool.setuptools]
                    packages.find.include = ["mypkg.*"]
                     zb = 1r   )r'   zpkgA.py)r'   zother.pynr*   )r)   r&   r*   r2   r8   r4   r5   r6   -vr7   r9   rP   z'from mypkg.n import pkgA; print(pkgA.a)1z}        try:
            import mypkg.other
        except ImportError:
            print("mypkg.other not defined")
        zmypkg.other not definedN)	r   EXAMPLEr@   rA   rB   rE   rC   rF   rG   )rj   rJ   rI   r0   ru   rw   outrL   r#   r#   r$   0test_namespace_accidental_config_in_lenient_mode_  s,     zETestPep420Namespaces.test_namespace_accidental_config_in_lenient_modeN)ry   rz   r{   rx   r   r   r#   r#   r#   r$   r~   -  s   "r~   c           	   	     s   | d   fddt jD }|D ]}|jdd q t| ttjtjt	t
|d}t jddd	d
t
|dt
 df	|}tj||d t dkrdnd} | d }tj|g|d dS )z>
    Editable install to a prefix should be discoverable.
    r3   c                   s4   g | ],}d |kr| tjr t|tj qS )zsite-packages)
startswithsysr3   r   relative_to).0rA   r2   r#   r$   
<listcomp>  s    z-test_editable_with_prefix.<locals>.<listcomp>T)parents)
PYTHONPATHr5   r6   r7   z
--editablez--prefixr8   )envWindowsScriptsbinsampleN)r   rA   r?   _addsitedirsdictosenvironpathsepjoinmaprC   
executable
subprocess
check_callplatformsystem)	rI   Zsample_projectr%   Zsite_packages_allspr   rL   r   Zexer#   r2   r$   test_editable_with_prefix  s0    

r   c                   @  sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )TestFinderTemplateThis test focus in getting a particular implementation detail right.
    If at some point in time the implementation is changed for something different,
    this test can be modified or even excluded.
    c                 C  s   i }t ||| |d   d S )Nr7   )exec)rj   finderlocr#   r#   r$   install_finder  s    z!TestFinderTemplate.install_finderc           
   
   C  s   ddddididdid}t jj||d	 t|d
 t|d d}ttt |i }t  t v dD ]}t	j
|d  qp| | td}td}td}|jdkst|jdkstt|d  }	t||	 W 5 Q R X W 5 Q R X d S )Npkg1r   mod1.pyza = 42)r'   subpkgmod2.pyza = 43src1src2r2   z	src1/pkg1z	src2/mod2)r   mod2)r   pkg1.subpkgpkg1.subpkg.mod1r   r   r   r   *   +   zsrc1/pkg1/subpkg)r@   rA   rB   rC   r   r
   r   
save_pathssave_sys_modulesr   modulespopr   r   arG   resolveassert_path)
rj   rI   r0   mappingtemplatemodmod1r   r   expectedr#   r#   r$   test_packages  s.    	


z TestFinderTemplate.test_packagesc           
   
   C  s   ddddi}t jj||d dt|d i}dg i}ttt ||}t  t z dD ]}t	j
|d  q`| | td}t|d	 }t|d  }	t||	 |jd
kst|jdddkstW 5 Q R X W 5 Q R X d S )NrO   a = 13abc)r'   text.txtr2   ns.othernamerr   rr   r   r      r;   r<   )r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r   importlib_resourcesr0   r   r   r   rG   	read_text)
rj   rI   r0   r   r   r   r   rO   textr   r#   r#   r$   test_namespace  s    

z!TestFinderTemplate.test_namespacec           
   
   C  s
  ddddiiidddiid}t jj||d t|d	 t|d
 d}dt|d t|d gi}ttt ||}t  t n dD ]}t	j
|d  q| | td}td}t|d	  }	t||	 |jdkst|jdkstW 5 Q R X W 5 Q R X d S )Nrr   r   r'   r   r   b = 37r   r2   zsrc1/ns/pkg1zsrc2/ns)ns.pkgArr   r   r   )rr   r   ns.mod2r   r   r   %   )r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r   r   r   r   rG   b)
rj   rI   r0   r   namespaces_r   r   r   r   r   r#   r#   r$   test_combine_namespaces  s&    




z*TestFinderTemplate.test_combine_namespacesc              
   C  sb  dddiidddiid}t j }||V t j| dd	d
dddgd}t|}t|tt }t	dd |
 D }W 5 Q R X t  t  |d D ]}tj|d q| | td}	td}
t|d  }tt|	j |kstt|d  }tt|
j |ks*t|	jdks:t|
jdksJtW 5 Q R X W 5 Q R X dS )z
        Users may attempt to combine namespace packages in a nested way via
        ``package_dir`` as shown in pypa/setuptools#4248.
        Z
my_packagezmy_module.pyr   Zmy_package2zmy_module2.pyr   )r+   r   	%PEP 517%zsrc/my_packagezsrc2/my_package2)different_namedifferent_name.subpkgr   r   )script_namepackage_dirpackagesc                 s  s    | ]\}}| d r|V  qdS )z.pyNendswith)r   kvr#   r#   r$   	<genexpr>/  s     
 zDTestFinderTemplate.test_combine_namespaces_nested.<locals>.<genexpr>r   Nzdifferent_name.my_modulez different_name.subpkg.my_module2zsrc/my_package/my_module.pyzsrc2/my_package2/my_module2.pyr   r   )r@   rA   ZDirectoryStackrb   rB   r   r   rC   r
   nextZget_implementationr   r   r   r   r   r   r   r   r   r   __file__rG   r   r   )rj   rI   r0   stackattrsrn   r   coder   r   r   r   r#   r#   r$   test_combine_namespaces_nested  s6    


 
z1TestFinderTemplate.test_combine_namespaces_nestedc                 C  sr  ddddiiiddddiiiddddiiid	}t jj||d
 i }dt|d gi}ttt ||}dd dD }t  t  d|D ]}t	j
|d  q| | td}|jdksttt td W 5 Q R X t	jt|d  td}	|	jdksttt td W 5 Q R X t	jt|d  td}
|
jdksZtW 5 Q R X W 5 Q R X d S )Nparentchildzone.pyx = 1ztwo.pyx = 2zthree.pyzx = 3)Zproject1project2project3r2   zproject1/parentc                 s  s   | ]}d | V  qdS )zparent.child.Nr#   )r   rf   r#   r#   r$   r   N  s     zCTestFinderTemplate.test_dynamic_path_computation.<locals>.<genexpr>)onetwothreeparent.childzparent.child.oner   zparent.child.twor      zparent.child.threer      )r   r   r   )r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r   xrG   r_   raisesImportErrorrs   )rj   rI   r0   r   r   r   Zmodsr   r   r   r   r#   r#   r$   test_dynamic_path_computationB  s2    
z0TestFinderTemplate.test_dynamic_path_computationc                 C  s   dddii}t jj||d dt|d i}ttt |i }t T t @ t	j
dd  | | tjtdd td W 5 Q R X W 5 Q R X W 5 Q R X d S )NrO   r'   zfrom . import pkgr2   rW   r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r_   r   r   r   rj   rI   r0   r   r   r#   r#   r$   test_no_recursionf  s      

z$TestFinderTemplate.test_no_recursionc                 C  s   ddddidi}t jj||d dt|d i}ttt |i }t b t N t	j
dd  t	j
dd  | | tjtdd td W 5 Q R X W 5 Q R X W 5 Q R X d S )	Nfoor   r'   )r'   barr2   zfoo.barr:   rW   r   r   r#   r#   r$   test_similar_name{  s$      

z$TestFinderTemplate.test_similar_namec                 C  sh  dddddddi}t jj||d dt|d i}ttt |i }t  t  t	j
dd  | | tjtdd	 td
 W 5 Q R X tjtdd	 td W 5 Q R X tjtdd	 td W 5 Q R X tjtdd	 td W 5 Q R X tjtdd	 td W 5 Q R X td}|jdks8ttd}|jdksPtW 5 Q R X W 5 Q R X d S )Nr   r   r   r   )r'   lowercase.py)r'   r   r   r2   z'FOO'rW   ZFOOz'foo\.LOWERCASE'zfoo.LOWERCASEz'foo\.bar\.Lowercase'zfoo.bar.Lowercasez
'foo\.BAR'zfoo.BAR.lowercasezFOO.bar.lowercasezfoo.lowercaser   zfoo.bar.lowercaser   )r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r_   r   r   r   r   rG   )rj   rI   r0   r   r   r   r#   r#   r$   test_case_sensitivity  s<    
 

z(TestFinderTemplate.test_case_sensitivityc                 C  s^  ddddddi}t jj||d dt|d i}d	g i}ttt ||}t  t  d
D ]}t	j
|d  qf| | td}t|d  }t|| |jdksttd}	|	jdksttd}
|
jdksttjtdd td W 5 Q R X tjtdd td W 5 Q R X tjtdd td W 5 Q R X W 5 Q R X W 5 Q R X d S )NrO   r   r   zc = 42)r'   zbar.py)r'   r   r2   r   rr   r   r   zns.othername.foor   zns.othername.foo.barr   z'NS'rW   zNS.othername.fooz'ns\.othername\.FOO\'zns.othername.FOOz'ns\.othername\.foo\.BAR\'zns.othername.foo.BAR)r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r   r   r   r   rG   r   cr_   r   r   )rj   rI   r0   r   r   r   r   rO   r   r   r   r#   r#   r$   test_namespace_case_sensitivity  s:    	

z2TestFinderTemplate.test_namespace_case_sensitivityc                 C  s   dddddddidii}t jj||d	 dt|d
 i}ttt |i }t  t r dD ]}t	j
|d qd| | td}|jdksttd}|jdksttt td W 5 Q R X W 5 Q R X W 5 Q R X dS )z
        The finder should not import ``fullname`` if the intermediate segments
        don't exist (see pypa/setuptools#4019).
        r+   r&   r   r   zb = 13	config.pyza = 37)r'   r   zhelloworld.py
componentsr2   z	src/mypkg)r&   zmypkg.configmypkg.helloworldzmypkg.componentsmypkg.components.configmypkg.components.helloworldNr  r   r  r   r  )r@   rA   rB   rC   r   r
   r   r   r   r   r   r   r   r   r   rG   r   r_   r   r   )rj   rI   r0   r   r   r   r]   Z
helloworldr#   r#   r$   test_intermediate_packages  s0     
z-TestFinderTemplate.test_intermediate_packagesN)ry   rz   r{   __doc__r   r   r   r   r   r   r   r   r   r   r  r#   r#   r#   r$   r     s    +$))r   c                 C  s  ddidddddiddd	dd
iiiddiddiddid}t jj|| d dddd}dddddddddddg}t||| }|t| d t| d t| d t| d t| d t| d dkstttt||}|ddhksttt	|}|dd dd!d"d#d$hkstd%S )&r   r'   zab = 1r   )r   r'   zd = 1zde = 1)r'   eghzfgh = 1zabc = 1z
abcxyz = 1z	mnopq = 1)r   dfotheranotheryet_anotherr2   r  r  r  )a.b.ca.b.c.x.y.z	m.n.o.p.qr   za.br  z	a.b.c.x.yr  r	  zd.er
  zf.gzf.g.hr  )r   r  r  r	  r
  r  za.b.c.xmzm.nzm.n.ozm.n.o.pN)
r@   rA   rB   r   rC   rG   setr   r   r   )rI   r0   r   r   rootsrr   r#   r#   r$   test_pkg_roots  sJ    





	r  c                
   @  s   e Zd ZU dZeeed ddddddddd	Zd
ed< eeeed dded ideeed ed dded d ded d deeed dded d ed d diddZe	j
jejdkdde	j
de dd ZdS )TestOverallBehaviourz        [build-system]
        requires = ["setuptools"]
        build-backend = "setuptools.build_meta"

        [project]
        name = "mypkg"
        version = "3.14159"
        r*   r   zvar = 42zvar = 13resource 39)r'   r   zresource_file.txt)r'   r   
subpackage)r)   r*   otherfile.pyr&   zdict[str, Any]FLAT_LAYOUTr&   )r)   r*   r  r+   z                [tool.setuptools]
                packages = ["mypkg", "mypkg.subpackage"]

                [tool.setuptools.package-dir]
                "mypkg.subpackage" = "other"
                r   )r'   r   r  )r)   r*   r  r&   r  )r   r  )flat-layout
src-layoutcustom-layout	namespacer,   r-   r.   layoutc                 C  s@  t d||| j| f| \}}d}|ddt|g}d|ksBtd}	|ddt|	g}d|ksftd}
|ddt|
g}d	|kstt|d
}t|d}t|d}|jddd |jddd |jddd |ddt|	g}d|kstd|kst|ddt|
g}d	|ks.td|ks<td S )Nr&   zj        try:
            import otherfile
        except ImportError as ex:
            print(ex)
        r4   rP   zNo module named 'otherfile'zy        import mypkg, mypkg.mod1, mypkg.subpackage.mod2
        print(mypkg.mod1.var, mypkg.subpackage.mod2.var)
        z42 13z        import mypkg.subpackage
        from setuptools._importlib import resources as importlib_resources
        text = importlib_resources.files(mypkg.subpackage) / "resource_file.txt"
        print(text.read_text(encoding="utf-8"))
        r  
**/mod1.py
**/mod2.py**/resource_file.txtzvar = 17r;   r<   z	var = 781zresource 374z17 781)install_projectEXAMPLESrE   r   rG   r   rh   rH   )rj   rI   rJ   r  r%   rK   _cmd_import_errorr   Zcmd_get_varscmd_get_resourcer   r   resource_filer#   r#   r$   test_editable_install  s:       
z*TestOverallBehaviour.test_editable_installN)ry   rz   r{   Z	PYPROJECTr   r   r  __annotations__r#  r_   r|   xfailr   r   r}   keysr(  r#   r#   r#   r$   r  C  sV   






(r  c                   @  sN   e Zd Zeejd Zed  ed7  < ded d d< dd	 Zd
d Z	dS )TestLinkTreer  r)   a[          [tool.setuptools]
        # Temporary workaround: both `include-package-data` and `package-data` configs
        # can be removed after #3260 is fixed.
        include-package-data = false
        package-data = {"*" = ["*.txt"]}

        [tool.setuptools.packages.find]
        where = ["src"]
        exclude = ["*.subpackage*"]
        r   r+   r&   resource.not_in_manifestc              	   C  s:  t jj| j|d t| d}tddi}|  t }|d }|d }|  |  |	d}d|_
t||_|  | }| }	t||||}
|
|||	 t|d	}|d
 }t|| t|dd d kstt|dd d kstt|dd d kstt|dd d ks,tW 5 Q R X d S )Nr2   zmypkg-3.14159r   r   z.auxz.buildbuild_pyTr  zsrc/mypkg/mod1.pyz**/subpackager   r!  z**/resource.not_in_manifest)r@   rA   rB   FILES_Pathr   parse_config_filesr	   r?   get_command_objZeditable_moderC   	build_libensure_finalizedget_outputsZget_output_mappingr   r   rh   assert_link_torG   )rj   rI   rf   rn   wheelZauxrB   r.  outputsZoutput_mappingZ	make_treer   r   r#   r#   r$   test_generated_tree  s2    


z TestLinkTree.test_generated_treec                 C  s   ddg}t d||| jf|  |dddg}d|ks:td}|ddt|g}d	|ks^td
}|ddt|g}d|kstd|kstd S )Nr   r    r&   r4   rP   (import mypkg.mod1; print(mypkg.mod1.var)r[   zv        try:
            from mypkg import subpackage
        except ImportError as ex:
            print(ex)
        cannot import name 'subpackage'a8          import mypkg
        from setuptools._importlib import resources as importlib_resources
        try:
            text = importlib_resources.files(mypkg) / "resource.not_in_manifest"
            print(text.read_text(encoding="utf-8"))
        except FileNotFoundError as ex:
            print(ex)
        zNo such file or directoryr-  )r"  r/  rE   rG   r   )rj   rI   rJ   rw   r   r%  r&  r#   r#   r$   test_strict_install  s    	z TestLinkTree.test_strict_installN)
ry   rz   r{   r   r  r#  r/  r   r9  r<  r#   r#   r#   r$   r,    s   
"r,  z9ignore:.*compat.*:setuptools.SetuptoolsDeprecationWarningc                 C  s   ddg}t jd }td|| |f|  |dddg}d|ksBttt| }d	D ]$}t|dd|g}||ksRtqRd
}|ddt|g}d|kstd S )Nr   zeditable-mode=compatr  r&   r4   rP   r:  r[   )z"import otherfile; print(otherfile)zimport other; print(other)zimport mypkg; print(mypkg)zc    try:
        from mypkg import subpackage;
    except ImportError as ex:
        print(ex)
    r;  )r  r#  r"  rE   rG   comparable_pathrC   r   )rI   rJ   rw   r0   r   Zexpected_pathrL   r#   r#   r$   test_compat_install  s    
r>  c              	   C  s   t dt dt ddddddid	}|d
ddddg tjdd td|| |f|  W 5 Q R X |d
ddg}d|kstdS )z3Ensure editable installs work with pbr, issue #3500z            [build-system]
            requires = ["setuptools"]
            build-backend = "setuptools.build_meta"
            z            __import__('setuptools').setup(
                pbr=True,
                setup_requires=["pbr"],
            )
            z~            [metadata]
            name = mypkg

            [files]
            packages =
                mypkg
            r   zprint('Hello world!'))r'   zhello.pyztest.txtzAnother file in here.)r)   r1   z	setup.cfgr&   r  r4   r5   r6   r7   Zpbrz0.42)ZPBR_VERSIONr&   rP   zimport mypkg.hellozHello world!N)r   rE   r   environmentr"  rG   )rI   rJ   r%   r0   r   r#   r#   r$   test_pbr_integration/  s&     r@  c                   @  s.   e Zd ZdZejd dediZdd ZdS )TestCustomBuildPya$  
    Issue #3501 indicates that some plugins/customizations might rely on:

    1. ``build_py`` not running
    2. ``build_py`` always copying files to ``build_lib``

    During the transition period setuptools should prevent potential errors from
    happening due to those assumptions.
    r  r1   af              import pathlib
            from setuptools import setup
            from setuptools.command.build_py import build_py as orig

            class my_build_py(orig):
                def run(self):
                    super().run()
                    raise ValueError("TEST_RAISE")

            setup(cmdclass={"build_py": my_build_py})
            c                 C  sL   t d||| j\}}d|ks td|ks,t|dddg}d|ksHtdS )	z>Ensure that errors in custom build_py are reported as warningsr&   r   zValueError: TEST_RAISEr4   rP   r:  r[   N)r"  r/  rG   rE   )rj   rI   rJ   r$  r   r#   r#   r$   test_safeguarded_from_errorsy  s
    z.TestCustomBuildPy.test_safeguarded_from_errorsN)	ry   rz   r{   r  r  r#  r   r/  rB  r#   r#   r#   r$   rA  Z  s    rA  c                   @  s   e Zd Zdd Zdd ZdS )TestCustomBuildWheelc                 C  s(   | d}G dd d|}||jd< d S )Nbdist_wheelc                      s   e Zd Z fddZ  ZS )zETestCustomBuildWheel.install_custom_build_wheel.<locals>.MyBdistWheelc                   s   | j drd}t  S )Nzmacosx-zmacOS platform)	plat_namer   superget_tag)rj   r$  	__class__r#   r$   rG    s    zMTestCustomBuildWheel.install_custom_build_wheel.<locals>.MyBdistWheel.get_tag)ry   rz   r{   rG  __classcell__r#   r#   rH  r$   MyBdistWheel  s   rK  )get_command_classcmdclass)rj   rn   Zbdist_wheel_clsrK  r#   r#   r$   install_custom_build_wheel  s    
z/TestCustomBuildWheel.install_custom_build_wheelc                 C  sj   t jddi t }d|_|  | | t|}|  |	  t
tt d}d|ksftd S )N	module.pyr>   r1   
dist/*.whleditable)r@   rA   rB   r   r   set_defaultsrN  r   r4  rE   rC   r   r   rh   rG   )rj   
tmpdir_cwdrn   rL   
wheel_filer#   r#   r$   test_access_plat_name  s    
z*TestCustomBuildWheel.test_access_plat_nameN)ry   rz   r{   rN  rU  r#   r#   r#   r$   rC    s   rC  c                   @  s2   e Zd Zdd Zejjejdkdddd Z	dS )	TestCustomBuildExtc                 C  s*   ddl m} G dd d|}||jd< d S )Nr   )	build_extc                   @  s   e Zd ZdS )zITestCustomBuildExt.install_custom_build_ext_distutils.<locals>.MyBuildExtN)ry   rz   r{   r#   r#   r#   r$   
MyBuildExt  s   rX  rW  )Zdistutils.command.build_extrW  rM  )rj   rn   Zbuild_ext_clsrX  r#   r#   r$   "install_custom_build_ext_distutils  s    z5TestCustomBuildExt.install_custom_build_ext_distutilslinuxz(compilers may fail without correct setupr.   c                   s   t jddi dtddggi}t|}d|_|  | | t|}|	  |
  ttt d}d|ksztdd	 t d
D }t|dkst|d j t fddtD std S )Nzmodule.cr   ext_modulesmoduler1   rP  rQ  c                 S  s   g | ]}|j d kr|qS )z.c)suffix)r   pr#   r#   r$   r     s     
 zITestCustomBuildExt.test_distutils_leave_inplace_files.<locals>.<listcomp>zmodule.*r   r   c                 3  s   | ]}  |V  qd S Nr   )r   extrf   r#   r$   r     s     zHTestCustomBuildExt.test_distutils_leave_inplace_files.<locals>.<genexpr>)r@   rA   rB   r   r   r   rR  rY  r   r4  rE   rC   r   r   rh   rG   ri   rf   anyr   )rj   rS  r   rn   rL   rT  r0   r#   ra  r$   "test_distutils_leave_inplace_files  s"     

z5TestCustomBuildExt.test_distutils_leave_inplace_filesN)
ry   rz   r{   rY  r_   r|   Zskipifr   r   rc  r#   r#   r#   r$   rV    s    rV  c              
   C  s   t jddi t }d|_|  t|}|  tdt	fi }t
| d}||d| d}t|( tjt|d |  W 5 Q R X W 5 Q R X d	S )
z7Make sure to display useful debugging tips to the user.rO  r>   r1   SimulatedErr)Zside_effectre   z-following steps are recommended to help debugrW   N)r@   rA   rB   r   r   rR  r   r4  type	Exceptionr	   setattrr_   r   r`   r   rE   )rS  rk   rn   rL   rd  Zsimulated_failureZexpected_msgr#   r#   r$   test_debugging_tips  s    rh  errorc                  C  s   t d} t| tstdS )z>Ensure _encode_pth function does not produce encoding warningsu   tkmilan_ç_utf8N)r   
isinstancebytesrG   )contentr#   r#   r$   test_encode_pth  s    rm  c              	   G  sV   ||  }|   tjj||d |d
}|jddddddt|f|tjd	}||fS )Nr2   r8   r4   r5   r6   r   r7   r9   )stderr)r8   )r?   r@   rA   rB   rE   rC   r   STDOUT)rf   rJ   rI   r0   rw   rK   r   r#   r#   r$   r"    s    r"  c                 C  s6   d dtdd | D  }| d d j|dd d	S )
a5  To use this function, it is necessary to insert new_dir in front of sys.path.
    The Python process will try to import a ``sitecustomize`` module on startup.
    If we manipulate sys.path/PYTHONPATH, we can force it to run our code,
    which invokes ``addsitedir`` and ensure ``.pth`` files are loaded.
    
)zimport sitec                 s  s    | ]}d t |dV  qdS )zsite.addsitedir()N)r   fspath)r   Znew_dirr#   r#   r$   r     s     z_addsitedirs.<locals>.<genexpr>r   zsitecustomize.pyr;   r<   N)r   tuplerH   )Znew_dirsrl  r#   r#   r$   r     s    r   c                 C  s6   | j r2tt| j d }|r2tt| |ks2td S r_  )__path__r   iterrC   r   r   rG   )rO   r   rA   r#   r#   r$   r     s    r   r   )filer  c                 C  sj   |   r&t|  t| ksftn@|  }| }|tj |tj ksNt|tj |tj ksftd S r_  )
is_symlinkrC   r   rG   statST_INOST_DEV)rv  r  Z	file_statZ
other_statr#   r#   r$   r6    s    r6  rC   )str_with_pathreturnc                 C  s   |   tjdddS )N/z//)lowerreplacer   sep)r{  r#   r#   r$   r=    s    r=  )T
__future__r   r   r   rx  r   r   copyr   	importlibr   Zimportlib.machineryr   pathlibr   textwrapr   typingr   Zunittest.mockr	   uuidr
   Zjaraco.envsr@   Zjaraco.pathr_   rA   r0  Zsetuptools._importlibr   r   Z!setuptools.command.editable_wheelr   r   r   r   r   r   r   r   r   Zsetuptools.distr   Zsetuptools.extensionr   Zsetuptools.warningsr   r   r   r   distutils.corer   Zfixturer%   rF   r   ZSETUP_SCRIPT_STUBr|   r*  r}   rM   rS   rU   rV   r~   r   r   r  r  r,  filterwarningsr>  r@  rA  rC  rV  rh  rm  r"  r   r   r6  r=  r#   r#   r#   r$   <module>   s   ,

G
(&Be(  Z1xS

 +* 


