o
    LK&hB                     @  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   Y/var/www/html/venv/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eggzip_egg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/nonexistentznonexistent/)modpkg_resourcesZipProvidersortedresource_listdirmod2)r   r9   zpexpected_rootexpected_subdirr>   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    )r9   r:   ResourceManagerr;   get_resource_filenamedatetimefromtimestampr   statst_mtimer(   openwriter,   	timestamputimereadcleanup_resources)r   r9   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__rI   r(   classmethodr4   r6   rC   rW   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: )r:   rG   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)dirnameunpatched_isdirwas_dirpatched_isdirr   r   rj      s
   
zCTestResourceManager.test_get_cache_path_race.<locals>.patched_isdir)r[   barbazisdir)new   r   r[   rk      )r:   rm   rd   rG   set_extraction_pathr]   r   sepr/   r   patchobjectr\   lensplit)r   tmpdirr`   archive_namecalled_dirnamesr   ri   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   rc   r|   r   r   r   r   r   rZ   z   s
    rZ   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
   rf   mkdirrM   rN   listr:   distributions_from_metadata)metadata_pathmetadatadist_dirrU   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/   r]   encoder   pytestraisesUnicodeDecodeErrorget_metadatavalueendswith)	ry   r'   r   r   r   excinfoexcrT   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.txtrq   )
ensure_dirr/   ensurer   r:   r   rw   )ry   basenamer   r   r   r   r   r   make_distribution_no_version   s   r   )r   expected_filenameexpected_dist_typezegg-infozPKG-INFOz	dist-infor   ro   )   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versionr]   r   argsr^   )ry   r   r   r   r   r   r   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)touchr:   dist_factoryr]   )tmp_pathr   	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)r7   r   r7   )return_value
fake.plistr   ProductVersionz11.4Nzos.path.existsTc                   s    d g|R i |S )Nr   r   )filer   kwargs	orig_openr   r   r   	fake_openE  s   z+test_macos_vers_fallback.<locals>.fake_openzbuiltins.open)side_effect114)builtinsrM   setattrr   Mockplistlibdumpru   r:   _macos_verscache_clearassert_called)monkeypatchr   	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   )r]   ).0rf   ry   r   r   	<genexpr>_  s    z5TestDeepVersionLookupDistutils.env.<locals>.<genexpr>)r]   chmodrK   S_IRWXUdictpathsr   mapr   r   values)r   ry   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! ❄r[   )r.   r   long_descriptionr   N)r   	distutilsr   r   commandinstall_egg_infoinitialize_optionsr   install_dirfinalize_optionsrun)r   r   r   ldattrsr   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   r:   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))r[   r[   )zfoo/r[   )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r:   normalize_pathr   r   r   result_from_unnormalized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)r:   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   fixturer   r   r   markparametrizer   skipifr   r
   normcaser   rt   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parentsrD   rE   r
   )r   FILESitemsparent
write_textinspectcleandocr   r	   r   fspath)r   r   r   
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  r:   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   rI   r  r   r   rK   r~   r	   r"   r$   unittestr   r   r:   r   r   r   "distutils.command.install_egg_infor   distutils.distr]   r   r   rZ   r   r   r   r   r   xfailversion_inforeleaselevelr   r   r   r   r   r  r   r   r   r   <module>   sX    Z6

^