o
    5h{                  
   @  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( d dl)m*Z* d dl+m,Z, d dl-m.Z. ddl/m0Z0m1Z1 d dl2m3Z3 ej4ddgddd Z5eded6 dddededdd d!id"Z7d#Z8ej9j:ejd$kd%d&ej9;d'i e7d(e8ie7gd)d* Z<d+d, Z=d-d. Z>G d/d0 d0Z?G d1d2 d2Z@d3d4 ZAG d5d6 d6ZBd7d8 ZCG d9d: d:ZDG d;d< d<ZEej9Fd=d>d? ZGej9jHd@dA ZIG dBdC dCZJG dDdE dEZKG dFdG dGZLdHdI ZMej9FdJdKdL ZNdMdN ZOdOdP ZPdQdR ZQd^dXdYZRd_d\d]ZSdS )`    )annotationsN)deepcopy)import_module)EXTENSION_SUFFIXES)Path)dedent)Any)Mock)uuid4)	resources)_encode_pth_find_namespaces_find_package_roots_find_virtual_namespaces_finder_template	_LinkTree_TopLevelFindereditable_wheel)Distribution)	Extension)SetuptoolsDeprecationWarning   )contexts
namespaces)	run_setupstrictZlenient)paramsc                 C  s   | j dkr	ddgS g S )Nr   --config-settingseditable-mode=strict)Zparam)Zrequest r   Flib/python3.10/site-packages/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|g|}t|| g d	}|| d
ks3J |d jddd |d jddd || dksPJ d S )Nr"   prefixpython-mpipinstall--no-build-isolation-e)r0   r1   r"   z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
write_text)tmp_pathvenvr,   r!   projectcmdr   r   r    test_editable_with_pyproject{   s&   	
rH   c                 C  sz   dt dddiddi}tjj|| d | d }dd	d
dddt|g|}t|| g d}|| dks;J 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$   r.   r0   r1   r2   r3   r4   r5   )r0   -cz$import pkg, mod; print(pkg.a, mod.b)z4 2r   r<   r=   r>   r?   r@   rA   rB   rD   rE   r!   r,   rF   rG   r   r   r    test_editable_with_flat_layout   s.   
rN   c                 C  st   dt dddi}tjj|| d | d }dddd	d
dt|g|}t|| g d}|| dks8J 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"]
                rI   )r%   r$   r.   r0   r1   r2   r3   r4   r5   )r0   rK   zimport mod; print(mod.b)2rL   rM   r   r   r     test_editable_with_single_module   s,   
rP   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 ]O}tj||dd}|; | '}|| tddd	}	t	|	}
|

  |
d
j}|
|| W d    n1 sNw   Y  W d    n1 s]w   Y  qt|d}t|t|kstJ d S )Nz .*namespace_packages parameter.*matchz.installation_dir)z	myns.pkgAz	myns.pkgBmyns.n.pkgAmyns.n.pkgB42)versionr-   config)Z
stop_afterZ	dist_infoz*-nspkg.pth)pytestZwarnsr   r;   r   build_namespace_packagecontextchdirr   r   Zfinalize_optionsget_finalized_commandnameZ_install_namespaceslistgloblen)selfrD   monkeypatchZdeprecationZinstallation_dirZexamplesr^   rJ   ctxdistrG   Zeditable_namer,   r   r   r    test_nspkg_file_is_unique   s*   
 z.TestLegacyNamespaces.test_nspkg_file_is_uniqueimpl)Z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|g|	 |d
ddddt|g|	 |d
dd| d| dg |g d 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)rg   z.pkgBr%   r7   r8   Nr4   r0   r1   r2   r3   r5   rK   zimport z.pkgA; import )r0   rK   zimport pkg_resources)r   rZ   rC   appendrA   r?   )
rb   rE   rD   rh   rg   r!   Zbuild_systempkg_Apkg_Boptsr   r   r    !test_namespace_package_importable  s   
z6TestLegacyNamespaces.test_namespace_package_importableN)__name__
__module____qualname__rf   rY   markparametrizerm   r   r   r   r    rQ      s    rQ   c                   @  s$   e Zd Zdd Zdd Zdd ZdS )TestPep420Namespacesc              	   C  sz   t |d}t |d}|dd }|d |ddddt|g| |ddddd	t|g| |g d
 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.
        rT   rU   Nr4   r0   r1   r2   r3   r5   )r0   rK   z&import myns.n.pkgA; import myns.n.pkgB)r   build_pep420_namespace_packageri   rA   r?   )rb   rE   rD   r!   rj   rk   rl   r   r   r    rm   -  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|g| |dddddt|g| |dddddt|g| |g d 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'   r.   rU   zmyns.n.pkgCNr4   r0   r1   r2   r3   r5   )r0   rK   z#from myns.n import pkgA, pkgB, pkgC)	r   r<   r=   r>   r   rt   ri   rA   r?   )	rb   rE   rD   r!   r,   rj   rk   Zpkg_Crl   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|g| |g d}| dksAJ d}|ddt |g}d|v sSJ 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.
        ru   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 = 1rv   )r#   zpkgA.py)r#   zother.pynr&   )r%   r"   r&   r.   r4   r0   r1   r2   -vr3   r5   )r0   rK   z'from mypkg.n import pkgA; print(pkgA.a)1z}        try:
            import mypkg.other
        except ImportError:
            print("mypkg.other not defined")
        rK   zmypkg.other not definedN)r   EXAMPLEr<   r=   r>   rA   r?   rB   )rb   rE   rD   r,   rj   rl   outrG   r   r   r    0test_namespace_accidental_config_in_lenient_mode^  s,    zETestPep420Namespaces.test_namespace_accidental_config_in_lenient_modeN)rn   ro   rp   rm   rw   r~   r   r   r   r    rs   ,  s    "rs   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g	|}tj||d t dkrLdnd} | d }tj|g|d dS )z>
    Editable install to a prefix should be discoverable.
    r/   c                   s4   g | ]}d |v r| tjr t|tj qS )zsite-packages)
startswithsysr/   r   relative_to).0r=   r.   r   r    
<listcomp>  s
    z-test_editable_with_prefix.<locals>.<listcomp>T)parents)Z
PYTHONPATHr1   r2   r3   
--editablez--prefixr4   )envZWindowsZScriptsbinZsampleN)r   r=   r;   _addsitedirsdictosenvironpathsepjoinmapr?   
executable
subprocessZ
check_callplatformsystem)	rD   Zsample_projectr!   Zsite_packages_allspr   rG   r   Zexer   r.   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 )Nr3   )exec)rb   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 @ dD ]	}t	j
|d  q8| | td}td}td}|jdksZJ |jdksaJ t|d  }	t||	 W d    n1 sxw   Y  W d    d S W d    d S 1 sw   Y  d S )Npkg1rx   mod1.pyza = 42)r#   subpkgmod2.pyza = 43src1src2r.   z	src1/pkg1z	src2/mod2)r   mod2)r   pkg1.subpkgpkg1.subpkg.mod1r   r   r   r   *   +   zsrc1/pkg1/subpkg)r<   r=   r>   r?   r   r
   r   
save_pathssave_sys_modulesr   modulespopr   r   aresolveassert_path)
rb   rD   r,   mappingtemplatemodmod1r   r   expectedr   r   r    test_packages  s0   	


Pz 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 B dD ]	}t	j
|d  q0| | td}t|d	 }t|d  }	t||	 |jd
ks^J |jdddkshJ W d    n1 srw   Y  W d    d S W d    d S 1 sw   Y  d S )NrJ   a = 13abc)r#   text.txtr.   ns.othernamerh   rh   r   r      r7   r8   )r<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   r   importlib_resourcesr,   r   r   r   	read_text)
rb   rD   r,   r   r   r   r   rJ   textr   r   r   r    test_namespace  s    

Pz!TestFinderTemplate.test_namespacec           
   	   C  sD  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 Y t < dD ]	}t	j
|d  qG| | td}td}t|d	  }	t||	 |jdksrJ |jdksyJ W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nrh   r   r#   r   r   b = 37r   r.   zsrc1/ns/pkg1zsrc2/ns)ns.pkgArh   r   r   )rh   r   ns.mod2r   r   r   %   )r<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   r   r   r   r   b)
rb   rD   r,   r   namespaces_r   r   ru   r   r   r   r   r    test_combine_namespaces  s(   




Pz*TestFinderTemplate.test_combine_namespacesc              	   C  s  dddiidddiid}t j }||0 t j| dd	d
dddgd}t|}t|tt }t	dd |
 D }W d   n1 sJw   Y  t x t [ |d D ]	}tj|d q]| | td}	td}
t|d  }tt|	j |ksJ t|d  }tt|
j |ksJ |	jdksJ |
jdksJ W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  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>.       zDTestFinderTemplate.test_combine_namespaces_nested.<locals>.<genexpr>Nr   zdifferent_name.my_modulez different_name.subpkg.my_module2zsrc/my_package/my_module.pyzsrc2/my_package2/my_module2.pyr   r   )r<   r=   ZDirectoryStackr[   r>   r   r   r?   r
   nextZget_implementationr   r   r   r   r   r   r   r   r   r   __file__r   r   )rb   rD   r,   stackattrsre   r   coder   r   r   r   r   r   r    test_combine_namespaces_nested  s:   



Pz1TestFinderTemplate.test_combine_namespaces_nestedc              
   C  s  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dg|R D ]	}t	j
|d  qM| | td}|jdksgJ tt td W d    n1 s{w   Y  t	jt|d  td}	|	jdksJ tt td W d    n1 sw   Y  t	jt|d  td}
|
jdksJ W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nparentchildzone.pyx = 1ztwo.pyx = 2zthree.pyzx = 3)Zproject1project2project3r.   zproject1/parentc                 s  s    | ]}d | V  qdS )zparent.child.Nr   )r   r^   r   r   r    r   M      zCTestFinderTemplate.test_dynamic_path_computation.<locals>.<genexpr>)onetwothreezparent.childzparent.child.oner   zparent.child.twor      zparent.child.threer      )r<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   r   xrY   raisesImportErrorri   )rb   rD   r,   r   r   r   modsr   r   r   r   r   r   r    test_dynamic_path_computationA  s8   


Pz0TestFinderTemplate.test_dynamic_path_computationc              
   C  s   dddii}t jj||d dt|d i}ttt |i }t L t / t	j
dd  | | tjtdd td W d    n1 sKw   Y  W d    n1 sZw   Y  W d    d S W d    d S 1 srw   Y  d S )NrJ   r#   zfrom . import pkgr.   rR   r<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   rY   r   r   r   rb   rD   r,   r   r   r   r   r    test_no_recursione  s    

Pz$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 S t 6 t	j
dd  t	j
dd  | | tjtdd td W d    n1 sUw   Y  W d    n1 sdw   Y  W d    d S W d    d S 1 s|w   Y  d S )	Nfoorx   r#   )r#   barr.   zfoo.barr6   rR   r   r   r   r   r    test_similar_namez  s&   

Pz$TestFinderTemplate.test_similar_namec              
   C  s   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 d    n1 sPw   Y  tjtdd	 td W d    n1 skw   Y  tjtdd	 td W d    n1 sw   Y  tjtdd	 td W d    n1 sw   Y  tjtdd	 td W d    n1 sw   Y  td}|jdksJ td}|jdksJ W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   rx   r   r   )r#   lowercase.py)r#   r   r   r.   z'FOO'rR   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<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   rY   r   r   r   r   )rb   rD   r,   r   r   r   r   r   r    test_case_sensitivity  sF   






Pz(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  q3| | td}t|d  }t|| |jdksZJ td}	|	jdkseJ td}
|
jdkspJ tjtdd td W d    n1 sw   Y  tjtdd td W d    n1 sw   Y  tjtdd td W d    n1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )NrJ   r   r   zc = 42)r#   zbar.py)r#   r   r.   r   rh   r   r   zns.othername.foor   zns.othername.foo.barr   z'NS'rR   zNS.othername.fooz'ns\.othername\.FOO\'zns.othername.FOOz'ns\.othername\.foo\.BAR\'zns.othername.foo.BAR)r<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   crY   r   r   )rb   rD   r,   r   r   r   r   rJ   r   r   r   r   r   r    test_namespace_case_sensitivity  sD   	




Pz2TestFinderTemplate.test_namespace_case_sensitivityc              
   C  s2  dddddddidii}t jj||d	 dt|d
 i}ttt |i }t e t H dD ]	}t	j
|d q2| | td}|jdksLJ td}|jdksWJ tt td W d   n1 skw   Y  W d   n1 szw   Y  W d   dS W d   dS 1 sw   Y  dS )z
        The finder should not import ``fullname`` if the intermediate segments
        don't exist (see pypa/setuptools#4019).
        r'   r"   rx   r   zb = 13	config.pyza = 37)r#   r   zhelloworld.py
componentsr.   z	src/mypkg)r"   zmypkg.configmypkg.helloworldzmypkg.componentsmypkg.components.configmypkg.components.helloworldNr   r   r   r   r   )r<   r=   r>   r?   r   r
   r   r   r   r   r   r   r   r   r   r   rY   r   r   )rb   rD   r,   r   r   r   rX   Z
helloworldr   r   r    test_intermediate_packages  s4   

Pz-TestFinderTemplate.test_intermediate_packagesN)rn   ro   rp   __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}g d}t||| }|t| d t| d t| d t| d t| d t| d dks\J ttt||}|ddhksmJ tt|}|h dks{J dS )r   r#   zab = 1rv   )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_anotherr.   r   r   r   )a.b.ca.b.c.x.y.z	m.n.o.p.q)r   a.br  	a.b.c.x.yr  r   zd.er   f.gzf.g.hr  r   r   r   )r   r  r  r   r   r  r  >   zm.n.o.pza.b.c.xr  mr  zm.nzm.n.oN)	r<   r=   r>   r   r?   setr   r   r   )rD   r,   r   r   rootsrh   r   r   r    test_pkg_roots  s4   





	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&   rx   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| g|R  \}}d}|ddt|g}d|v s"J d}	|ddt|	g}d|v s4J d}
|ddt|
g}d	|v sFJ t|d
}t|d}t|d}|jddd |jddd |jddd |ddt|	g}d|vsJ d|v sJ |ddt|
g}d	|vsJ d|v sJ d S )Nr"   zj        try:
            import otherfile
        except ImportError as ex:
            print(ex)
        r0   rK   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 = 17r7   r8   z	var = 781zresource 374z17 781)install_projectEXAMPLESrA   r   r   r`   rC   )rb   rD   rE   r  r!   rF   _cmd_import_errorr}   Zcmd_get_varscmd_get_resourcer   r   Zresource_filer   r   r    test_editable_install  s4   z*TestOverallBehaviour.test_editable_installN)rn   ro   rp   Z	PYPROJECTr   r|   r  __annotations__r  rY   rq   xfailr   r   rr   keysr  r   r   r   r    r  B  sX   
 





(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  sJ  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 u soJ t|dd d u s{J t|dd d u sJ t|dd d u sJ W d    d S 1 sw   Y  d S )Nr.   zmypkg-3.14159r   r   z.auxz.buildbuild_pyTr  zsrc/mypkg/mod1.pyz**/subpackager  r  z**/resource.not_in_manifest)r<   r=   r>   FILES_Pathr   Zparse_config_filesr	   r;   Zget_command_objZeditable_moder?   Z	build_libensure_finalizedZget_outputsZget_output_mappingr   r   r`   assert_link_to)rb   rD   r^   re   ZwheelZauxr>   r#  ZoutputsZoutput_mappingZ	make_treer   r   r   r   r    test_generated_tree  s4   



"z TestLinkTree.test_generated_treec                 C  s   ddg}t d||| jg|R   |g d}d|v sJ d}|ddt|g}d	|v s/J d
}|ddt|g}d|v sAJ d|v sGJ d S )Nr   r   r"   r0   rK   z(import mypkg.mod1; print(mypkg.mod1.var)rV   zv        try:
            from mypkg import subpackage
        except ImportError as ex:
            print(ex)
        r0   rK   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$  rA   r   )rb   rD   rE   rl   r}   r  r  r   r   r    test_strict_install  s   	z TestLinkTree.test_strict_installN)
rn   ro   rp   r   r  r  r$  r   r(  r+  r   r   r   r    r!    s    
"r!  z9ignore:.*compat.*:setuptools.SetuptoolsDeprecationWarningc                 C  s   ddg}t jd }td|| |g|R   |g d}d|v s!J tt| }dD ]}t|dd	|g}||v s;J q)d
}|dd	t|g}d|v sNJ d S )Nr   zeditable-mode=compatr  r"   r)  rV   )z"import otherfile; print(otherfile)zimport other; print(other)zimport mypkg; print(mypkg)r0   rK   zc    try:
        from mypkg import subpackage;
    except ImportError as ex:
        print(ex)
    r*  )r  r  r  rA   comparable_pathr?   r   )rD   rE   rl   r,   r}   Zexpected_pathrG   r   r   r    test_compat_install  s   
r-  c                 C  sD   dddddd| g|}|j |tjd | g d}d	|v s J d
S )z3Ensure editable installs work with pbr, issue #3500r0   r1   r2   rz   r3   r   stderr)r0   rK   zimport mypkg.hellozHello world!N)rA   r   STDOUT)Zpbr_packagerE   r!   rG   r}   r   r   r    test_pbr_integration.  s   
r1  c                   @  s2   e Zd ZdZi 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  r-   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  sJ   t d||| j\}}d|v sJ d|v sJ |g d}d|v s#J dS )z>Ensure that errors in custom build_py are reported as warningsr"   r   zValueError: TEST_RAISEr)  rV   N)r  r$  rA   )rb   rD   rE   r  r}   r   r   r    test_safeguarded_from_errors_  s
   z.TestCustomBuildPy.test_safeguarded_from_errorsN)	rn   ro   rp   r   r  r  r   r$  r3  r   r   r   r    r2  @  s    r2  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 )NZ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)Z	plat_namer   superget_tag)rb   r  	__class__r   r    r6  o  s   
zMTestCustomBuildWheel.install_custom_build_wheel.<locals>.MyBdistWheel.get_tag)rn   ro   rp   r6  __classcell__r   r   r7  r    MyBdistWheeln  s    r:  )Zget_command_classcmdclass)rb   re   Zbdist_wheel_clsr:  r   r   r    install_custom_build_wheelk  s   
z/TestCustomBuildWheel.install_custom_build_wheelc                 C  sj   t jddi t }d|_|  | | t|}|  |	  t
tt d}d|v s3J d S )N	module.pyr:   r-   
dist/*.whleditable)r<   r=   r>   r   r   set_defaultsr<  r   r&  rA   r?   r   r   r`   )rb   
tmpdir_cwdre   rG   
wheel_filer   r   r    test_access_plat_namex  s   
z*TestCustomBuildWheel.test_access_plat_nameN)rn   ro   rp   r<  rC  r   r   r   r    r4  j  s    r4  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)rn   ro   rp   r   r   r   r    
MyBuildExt  s    rF  rE  )Zdistutils.command.build_extrE  r;  )rb   re   Zbuild_ext_clsrF  r   r   r    "install_custom_build_ext_distutils  s   z5TestCustomBuildExt.install_custom_build_ext_distutilsZ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|v s=J dd	 t d
D }t|dksPJ |d j t fddtD sbJ d S )Nzmodule.crx   Zext_modulesmoduler-   r>  r?  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r^   r   r    r     r   zHTestCustomBuildExt.test_distutils_leave_inplace_files.<locals>.<genexpr>)r<   r=   r>   r   r   r   r@  rG  r   r&  rA   r?   r   r   r`   ra   r^   anyr   )rb   rA  r   re   rG   rB  r,   r   rM  r    "test_distutils_leave_inplace_files  s    

z5TestCustomBuildExt.test_distutils_leave_inplace_filesN)
rn   ro   rp   rG  rY   rq   Zskipifr   r   rO  r   r   r   r    rD    s    
rD  c                 C  s   t jddi t }d|_|  t|}|  tdt	fi }t
| d}||d| t|}|  W d   n1 sBw   Y  tdd	 |jjD sTJ dS )
z7Make sure to display useful debugging tips to the user.r=  r:   r-   SimulatedErr)Zside_effectr]   Nc                 s  s    | ]}d |v V  qdS )zdebugging-tipsNr   )r   Znoter   r   r    r     s    z&test_debugging_tips.<locals>.<genexpr>)r<   r=   r>   r   r   r@  r   r&  type	Exceptionr	   setattrrY   r   rA   rN  valueZ	__notes__)rA  rc   re   rG   rP  Zsimulated_failurerd   r   r   r    test_debugging_tips  s   
rU  errorc                  C  s   t d} t| tsJ dS )z>Ensure _encode_pth function does not produce encoding warningsu   tkmilan_ç_utf8N)r   
isinstancebytes)contentr   r   r    test_encode_pth  s   rZ  c              	   G  sZ   ||  }|   tjj||d g |d}|jddddddt|g|tjd	}||fS )
Nr.   r4   r0   r1   r2   rz   r3   r5   r.  )r;   r<   r=   r>   rA   r?   r   r0  )r^   rE   rD   r,   rl   rF   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     r   z_addsitedirs.<locals>.<genexpr>r   zsitecustomize.pyr7   r8   N)r   tuplerC   )Znew_dirsrY  r   r   r    r     s   r   c                 C  s>   | j rtt| j d }|rtt| |ksJ d S d S d S rK  )__path__r   iterr?   r   r   )rJ   r   r=   r   r   r    r     s   r   filer   r   returnNonec                 C  sl   |   rt|  t| ksJ d S |  }| }|tj |tj ks(J |tj |tj ks4J d S rK  )
is_symlinkr?   r   statST_INOST_DEV)ra  r   Z	file_statZ
other_statr   r   r    r'    s    r'  str_with_pathr?   c                 C  s   |   tjdddS )N/z//)lowerreplacer   sep)rh  r   r   r    r,    s   r,  )ra  r   r   r   rb  rc  )rh  r?   rb  r?   )TZ
__future__r   r   r   re  r   r   copyr   	importlibr   importlib.machineryr   pathlibr   textwrapr   typingr   Zunittest.mockr	   Zuuidr
   Zjaraco.envsr<   Zjaraco.pathrY   r=   r%  Zsetuptools._importlibr   r   Z!setuptools.command.editable_wheelr   r   r   r   r   r   r   r   Zsetuptools.distr   Zsetuptools.extensionr   Zsetuptools.warningsr   rx   r   r   Zdistutils.corer   Zfixturer!   rB   r|   ZSETUP_SCRIPT_STUBrq   r  rr   rH   rN   rP   rQ   rs   r   r   r
  r  r!  filterwarningsr-  Zuses_networkr1  r2  r4  rD  rU  rZ  r  r   r   r'  r,  r   r   r   r    <module>   s    (


G(&Be(  Z1x
S
 
* 



