o
    NZhB                     @  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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Zd dlZd dlmZmZmZ d dlZd dlZG dd deZG dd dZG d	d
 d
Zdd Zdd Zdd Zejdddefddefgejje	j dd dkoe	j j!dkdddd Z"ejje	j dd dkoe	j j!dkdddd Z#ejdd d!gd"d# Z$d$d% Z%G d&d' d'Z&G d(d) d)Z'dS )*    )annotationsN)mock)DistInfoDistributionDistributionEggInfoDistributionc                   @  s   e Zd Zdd ZdS )
EggRemoverc                 C  s4   | t jv rt j|  tj| rt|  d S d S N)syspathremoveosexists)self r   U/var/www/auris/lib/python3.10/site-packages/pkg_resources/tests/test_pkg_resources.py__call__   s
   
zEggRemover.__call__N)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @  sX   e Zd ZU g Zded< eddddddZ	 ed	d
 Zedd Z	dd Z
dd ZdS )TestZipProviderzlist[EggRemover]
finalizersi              r   c                 C  s  t jddd}t|d}t }d|_| j |_|	|d t }d|_| j |_|	|d t }d	|_| j |_|	|d
 t }d|_| j |_|	|d |
  |
  tj|j tj|jd}tj| | jt| | jt|j dS )z'create a zip egg and add it to sys.pathz.eggF)suffixdeletewmod.pyzx = 3
data.dathello, world!zsubdir/mod2.pyzx = 6
zsubdir/data2.datzgoodbye, world!subdirN)tempfileNamedTemporaryFilezipfileZipFileZipInfofilenameref_time	timetuple	date_timewritestrcloser	   r
   appendnamer   joinr   r   )clseggZzip_eggZzip_infor!   r   r   r   setup_class&   s2   zTestZipProvider.setup_classc                 C  s   | j D ]}|  qd S r   )r   )r0   	finalizerr   r   r   teardown_classD   s   
zTestZipProvider.teardown_classc                 C  s   dd l }t|}g d}t|d|ksJ ddg}t|d|ks'J t|d|ks2J |dg ks;J |d	g ksDJ dd l}t|}t|d|ksXJ |dg ksaJ |dg ksjJ d S )
Nr   )r   r   r!    z	data2.datzmod2.pyr!   zsubdir/Znonexistentznonexistent/)modpkg_resourcesZipProvidersortedresource_listdirmod2)r   r6   zpZexpected_rootZexpected_subdirr;   Zzp2r   r   r   test_resource_listdirI   s   

z%TestZipProvider.test_resource_listdirc                 C  s   ddl }t }t|}||d}tjt|j	}|| j
ks$J t|ddd}|d |  | j
 }t|||f ||d}t|dd}| dksVJ W d   n1 s`w   Y  |  dS )	a  
        If a previous call to get_resource_filename has saved the file, but
        the file has been subsequently mutated with different file of the
        same size and modification time, it should not be overwritten on a
        subsequent call to get_resource_filename.
        r   Nr   r   utf-8encodingzhello, world?r    )r6   r7   ResourceManagerr8   get_resource_filenamedatetimefromtimestampr   statst_mtimer(   openwriter,   	timestamputimereadcleanup_resources)r   r6   managerr<   r'   actualftsr   r   r   )test_resource_filename_rewrites_on_changea   s    


z9TestZipProvider.test_resource_filename_rewrites_on_changeN)r   r   r   r   __annotations__rC   r(   classmethodr2   r4   r=   rQ   r   r   r   r   r       s   
 

r   c                   @  s&   e Zd Zdd Zdd Z	 dd ZdS )TestResourceManagerc                 C  s<   t  }|d}tt|}d| }t|tsJ |d S )Nfooz%Unexpected type from get_cache_path: )r7   rA   get_cache_pathstrtype
isinstance)r   mgrr
   type_messager   r   r   test_get_cache_path{   s
   
z'TestResourceManager.test_get_cache_pathc                   s   t jf fdd	 g  _t  }|t| tjd}t	j
jt d d || W d    n1 s6w   Y   j}t|dksFJ |d tjdd  d	d
gksXJ |d tjdd  d	gksiJ d S )Nc                   s&    j |  || }|st|  |S r   )dirnamesr-   r   makedirs)dirnameZunpatched_isdirZwas_dirpatched_isdirr   r   rb      s
   
zCTestResourceManager.test_get_cache_path_race.<locals>.patched_isdir)rU   barZbazisdir)new   r   rU   rc      )r7   rd   r^   rA   set_extraction_pathrW   r   sepr/   r   patchobjectrV   lensplit)r   tmpdirrZ   archive_nameZcalled_dirnamesr   ra   r   test_get_cache_path_race   s   $&z,TestResourceManager.test_get_cache_path_racec                 C  s$   d}t jdd|g}t| dS )z
        In a separate Python environment, import pkg_resources and assert
        that action doesn't cause setuptools to be imported.
        )zimport pkg_resourcesz
import syszAassert "setuptools" not in sys.modules, "setuptools was imported"z-cz; N)r	   
executabler/   
subprocess
check_call)r   linescmdr   r   r   test_setuptools_not_imported   s   z0TestResourceManager.test_setuptools_not_importedN)r   r   r   r]   rr   rx   r   r   r   r   rT   z   s
    rT   c                 C  sb   t j| }t | t| d}|| W d   n1 s w   Y  tt|}|\}|S )a  
    Make a test Distribution object, and return it.

    :param metadata_path: the path to the metadata file that should be
        created. This should be inside a distribution directory that should
        also be created. For example, an argument value might end with
        "<project>.dist-info/METADATA".
    :param metadata: the desired contents of the metadata file, as bytes.
    wbN)	r   r
   r`   mkdirrG   rH   listr7   distributions_from_metadata)metadata_pathmetadatadist_dirrO   distsdistr   r   r   make_test_distribution   s   

r   c           	      C  s   d}t jt| d|}dd}t||d}tt}|	| W d   n1 s,w   Y  |j
}t|}d}||v sEJ d| ||sQJ d| dS )	zI
    Test a metadata file with bytes that can't be decoded as utf-8.
    METADATAzfoo.dist-infou   néez
iso-8859-1)r~   Nz`codec can't decode byte 0xe9 in position 1: invalid continuation byte in METADATA file at path: zactual: )r   r
   r/   rW   encoder   pytestraisesUnicodeDecodeErrorget_metadatavalueendswith)	rp   r'   r}   r~   r   excinfoexcrN   expectedr   r   r   test_get_metadata__bad_utf8   s   
r   c                 C  sJ   | | }|   |d  tt|}t|dksJ |\}||fS )zN
    Create a distribution directory with no file containing the version.
    ztemp.txtrh   )
ensure_dirr/   Zensurer{   r7   r|   rn   )rp   basenamer   r   r   r   r   r   make_distribution_no_version   s   r   )r   expected_filenameexpected_dist_typezegg-infozPKG-INFOz	dist-infor   rf   )   r   finalz/https://github.com/python/cpython/issues/103632reasonc                 C  s   d| }t | |\}}d| d}tj||}tt}	|j W d   n1 s,w   Y  t|	j	}
||
v sBJ t||
f|	j	j
\}}||v sNJ ||v sZJ t||ft||u sbJ dS )zI
    Test Distribution.version when the "Version" header is missing.
    zfoo.z!Missing 'Version:' header and/or z file at path: N)r   r   r
   r/   r   r   
ValueErrorversionrW   r   argsrX   )rp   r   r   r   r   r   r   Zexpected_textr}   r   errmsgr   r   r   !test_distribution_version_missing   s   


r   c                  C  sX   t d} tt}| j W d   n1 sw   Y  |jj\}} d}||ks*J dS )zl
    Test Distribution.version when the "Version" header is missing and
    the path can't be detected.
    z/fooNzJMissing 'Version:' header and/or PKG-INFO file at path: [could not detect])r   r   r   r   r   r   r   )r   r   r   r   r   r   r   1test_distribution_version_missing_undetected_path  s   r   onlyFTc                 C  s.   | d }|   tt| t||rJ dS )z5Test path containing a file with dist-info extension.zfoobar.dist-infoN)touchr7   dist_factoryrW   )tmp_pathr   Z	dist_infor   r   r   test_dist_info_is_not_dir/  s   r   c                   s   t j | dtjdd td d}tddi| W d   n1 s'w   Y  | d	tjd
d  fdd}tdtj|d}tj	
  t	 ddgksWJ tj	
  W d   n1 sfw   Y  |  dS )z-Regression test for pkg_resources._macos_verszplatform.mac_ver)r5   r   r5   )return_value
fake.plistry   ProductVersionz11.4Nzos.path.existsTc                   s    d g|R i |S )Nr   r   )filer   kwargsZ	orig_openr   r   r   	fake_openE  s   z+test_macos_vers_fallback.<locals>.fake_openzbuiltins.open)Zside_effectZ114)builtinsrG   setattrr   ZMockplistlibdumprl   r7   Z_macos_verscache_clearZassert_called)monkeypatchr   Z	fake_filer   mr   r   r   test_macos_vers_fallback7  s   
r   c                   @  s   e Zd Zejdd Zdd Zdd Zej	dg dd	d
 Z
ejjejdejdkddej	ddgdd Zejjejjdkddej	dg ddd ZdS )TestDeepVersionLookupDistutilsc                   sZ   G dd dt }| } tj d}t fdd|D |_tttj	|j
  |S )zq
        Create a package environment, similar to a virtualenv,
        in which packages are installed.
        c                   @  s   e Zd ZdS )z7TestDeepVersionLookupDistutils.env.<locals>.EnvironmentN)r   r   r   r   r   r   r   EnvironmentY  s    r   )homelibscriptsdatazegg-basec                 3  s     | ]}|t  | fV  qd S r   )rW   ).0r`   rp   r   r   	<genexpr>_  s    z5TestDeepVersionLookupDistutils.env.<locals>.<genexpr>)rW   chmodrE   S_IRWXUdictpathsr{   mapr   rz   values)r   rp   r   envsubsr   r   r   r   R  s   z"TestDeepVersionLookupDistutils.envc                 C  sT   d}t d||d}tj|}tjj|}|  |jd |_|	  |
  dS )zj
        Create a foo package installed (distutils-style) to env.paths['lib']
        as version.
        u&   This package has unicode metadata! ❄rU   )r.   r   Zlong_descriptionr   N)r   	distutilsr   r   commandZinstall_egg_infoZinitialize_optionsr   Zinstall_dirfinalize_optionsrun)r   r   r   ldattrsr   Ziei_cmdr   r   r   create_foo_pkgc  s   z-TestDeepVersionLookupDistutils.create_foo_pkgc                 C  sF   d}|  || tjd}t|jd g|}|j|ks!J d S )Nz1.11.0.dev0+2329eaezfoo>=1.9r   )r   r7   Requirementparse
WorkingSetr   findr   )r   r   r   reqr   r   r   r   #test_version_resolved_from_egg_infoq  s
   zBTestDeepVersionLookupDistutils.test_version_resolved_from_egg_info)unnormalized
normalized))rU   rU   )zfoo/rU   )foo/barr   )zfoo/bar/r   c                 C  $   t |}t |}||ksJ dS )zeEnsure the trailing slash is cleaned for path comparison.

        See pypa/setuptools#1519.
        Nr7   normalize_pathr   r   r   Zresult_from_unnormalizedZresult_from_normalizedr   r   r    test_normalize_path_trailing_sep{  s   

z?TestDeepVersionLookupDistutils.test_normalize_path_trailing_sepAaz%Testing case-insensitive filesystems.r   )z
MiXeD/CasEz
mixed/casec                 C  r   )z@Ensure mixed case is normalized on case-insensitive filesystems.Nr   r   r   r   r   test_normalize_path_normcase  s   

z;TestDeepVersionLookupDistutils.test_normalize_path_normcase\z5Testing systems using backslashes as path separators.)r   r   ))zforward/slashforward\slash)zforward/slash/r   )zbackward\slash\zbackward\slashc                 C  s   t |}||sJ dS )z;Ensure path seps are cleaned on backslash path sep systems.N)r7   r   r   )r   r   r   resultr   r   r   !test_normalize_path_backslash_sep  s   
z@TestDeepVersionLookupDistutils.test_normalize_path_backslash_sepN)r   r   r   r   Zfixturer   r   r   markparametrizer   Zskipifr   r
   normcaser   rk   r   r   r   r   r   r   Q  s:    


		
r   c                   @  s<   e Zd Zdd ZdddddZejdg d	d
d ZdS )TestWorkdirRequirec                 C  sj   |d }|   | j D ]\}}|| }|jj ddd |jt|dd q|td|g t	
|S )Nzsite-packagesT)exist_okparentsr>   r?   r
   )rz   FILESitemsparent
write_textinspectcleandocr   r	   r   fspath)r   r   r   Z
dist_filessite_packagesr   contentr
   r   r   r   fake_site_packages  s   
z%TestWorkdirRequire.fake_site_packagesze
            Metadata-Version: 2.4
            Name: pkg1.mod
            Version: 1.2.3
            zd
            Metadata-Version: 2.1
            Name: pkg2.mod
            Version: 0.42
            zE
            Name: pkg3.mod
            Version: 1.2.3.4
            zD
            Name: pkg4.mod
            Version: 0.42.1
            )z!pkg1_mod-1.2.3.dist-info/METADATAz pkg2.mod-0.42.dist-info/METADATAzpkg3_mod.egg-info/PKG-INFOzpkg4.mod.egg-info/PKG-INFO)r   requirement))z1.2.3zpkg1.mod>=1)z0.42zpkg2.mod>=0.4)z1.2.3.4zpkg3.mod<=2)z0.42.1zpkg4.mod>0.2,<1c           	      C  sp   |  ||| j}t|g}||ddfD ]}||\}|j|ks%J tj	tj
|j|g|s5J qd S )N.-)r   r   r7   r   replacerequirer   r   r
   samefile
commonpathlocation)	r   r   r   r   r   r   wsr   r   r   r   r    test_require_non_normalised_name  s   
z3TestWorkdirRequire.test_require_non_normalised_nameN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    	r   )(
__future__r   r   rC   r   r   r   rE   rt   r	   r"   r$   Zunittestr   r   r7   r   r   r   Z"distutils.command.install_egg_infor   distutils.distrW   r   r   rT   r   r   r   r   r   Zxfailversion_inforeleaselevelr   r   r   r   r   r   r   r   r   r   <module>   sX    Z6

^