o
    Zhg;                     @  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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%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   ]/var/www/html/lang_env/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_classC   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_listdirH   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   z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_change`   s    


z9TestZipProvider.test_resource_filename_rewrites_on_changeN)r   r   r   r   __annotations__rA   r(   classmethodr2   r4   r=   rO   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   r?   get_cache_pathstrtype
isinstance)r   mgrr
   type_messager   r   r   test_get_cache_pathz   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   r`      s
   
zCTestResourceManager.test_get_cache_path_race.<locals>.patched_isdir)rS   barZbazisdir)new   r   rS   ra      )r7   rb   r\   r?   set_extraction_pathrU   r   sepr/   r   patchobjectrT   lensplit)r   tmpdirrX   archive_nameZcalled_dirnamesr   r_   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[   rp   rv   r   r   r   r   rR   y   s
    rR   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^   mkdirrE   rF   listr7   distributions_from_metadata)metadata_pathmetadatadist_dirrM   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/   rU   encoder   pytestraisesUnicodeDecodeErrorget_metadatavalueendswith)	rn   r'   r{   r|   r   excinfoexcrL   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.txtrf   )
ensure_dirr/   Zensurery   r7   rz   rl   )rn   basenamer}   r~   r   r   r   r   make_distribution_no_version   s   r   )r   expected_filenameexpected_dist_typezegg-infozPKG-INFOz	dist-infor   rd   )   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versionrU   r   argsrV   )rn   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_factoryrU   )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.plistrw   ProductVersionz11.4Nzos.path.existsTc                   s    d g|R i |S )Nr   r   )filer   kwargsZ	orig_openr   r   r   	fake_openD  s   z+test_macos_vers_fallback.<locals>.fake_openzbuiltins.open)Zside_effectZ114)builtinsrE   setattrr   ZMockplistlibdumprj   r7   _macos_verscache_clearZassert_called)Zmonkeypatchr   Z	fake_filer   mr   r   r   test_macos_vers_fallback6  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   EnvironmentX  s    r   )homelibscriptsdatazegg-basec                 3  s     | ]}|t  | fV  qd S r   )rU   ).0r^   rn   r   r   	<genexpr>^  s    z5TestDeepVersionLookupDistutils.env.<locals>.<genexpr>)rU   chmodrC   S_IRWXUdictpathsry   mapr   rx   values)r   rn   r   envsubsr   r   r   r   Q  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! ❄rS   )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_pkgb  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_infop  s
   zBTestDeepVersionLookupDistutils.test_version_resolved_from_egg_info)unnormalized
normalized))rS   rS   )zfoo/rS   )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_sepz  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   ri   r   r   r   r   r   r   P  s:    


		
r   )&
__future__r   r   rA   r   r   rC   rr   r	   r"   r$   Zunittestr   r   r7   r   r   r   Z"distutils.command.install_egg_infor   distutils.distrU   r   r   rR   r   r   r   r   r   Zxfailversion_inforeleaselevelr   r   r   r   r   r   r   r   r   <module>   sT    Z6

