U
    uiu;                     @  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oejj dkdddd Z!ejjejdd dkoNe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  s0   | t jkrt j|  tj| r,t|  d S N)syspathremoveosexists)self r   J/tmp/pip-unpacked-wheel-9osl9suo/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                   @  sV   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   )clsZ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|}dddg}t|d|ks2tddg}t|d|ksPtt|d|ksft|d	g ksxt|d
g kstdd l}t|}t|d|kst|dg kst|dg kstd S )Nr   r   r   r!    z	data2.datzmod2.pyzsubdir/Znonexistentznonexistent/)modpkg_resourcesZipProvidersortedresource_listdirAssertionErrormod2)r   r5   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sHtt|ddd}|d |  | j
 }t|||f ||d}t|dd}| dkstW 5 Q R X |  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    )r5   r6   ResourceManagerr7   get_resource_filenamedatetimefromtimestampr   statst_mtimer(   r:   openwriter,   	timestamputimereadcleanup_resources)r   r5   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(   classmethodr1   r3   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s8t|d S )Nfooz%Unexpected type from get_cache_path: )r6   r?   get_cache_pathstrtype
isinstancer:   )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 5 Q R X  j}t|dksxt|d tjdd  d	d
gkst|d tjdd  d	gkst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      )r6   rb   r\   r?   set_extraction_pathrU   r   sepr/   r   patchobjectrT   lenr:   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  sN   t j| }t | t| d}|| W 5 Q R X 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.
    wb)	r   r
   r^   mkdirrE   rF   listr6   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 5 Q R X |j
}t|}d}||ksvtd|||st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|   z`codec can't decode byte 0xe9 in position 1: invalid continuation byte in METADATA file at path: z
actual: {}N)r   r
   r/   rU   encoder   pytestraisesUnicodeDecodeErrorget_metadatavaluer:   format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<t|\}||fS )zN
    Create a distribution directory with no file containing the version.
    ztemp.txtrf   )
ensure_dirr/   Zensurery   r6   rz   rl   r:   )rn   basenamer}   r~   r   r   r   r   make_distribution_no_version   s    r   z-suffix, 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 |}tj||}tt}	|j W 5 Q R X t	|	j
}
||
ksntt	||
f|	j
j\}}||kst||kstt	||ft||kstdS )zI
    Test Distribution.version when the "Version" header is missing.
    zfoo.{}z2Missing 'Version:' header and/or {} file at path: N)r   r   r   r
   r/   r   r   
ValueErrorversionrU   r   r:   argsrV   )rn   r   Zexpected_filenameZexpected_dist_typer   r   r}   Zexpected_textr{   r   errmsgr   r   r   !test_distribution_version_missing   s    

r   c               	   C  sD   t d} tt}| j W 5 Q R X |jj\}} d}||ks@tdS )zl
    Test Distribution.version when the "Version" header is missing and
    the path can't be detected.
    z/foozJMissing 'Version:' header and/or PKG-INFO file at path: [could not detect]N)r   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*tdS )z5Test path containing a file with dist-info extension.zfoobar.dist-infoN)touchr6   dist_factoryrU   r:   )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 5 Q R X | dtjd	d  fd
d}tdtj|d.}tj	
  t	 ddgksttj	
  W 5 Q R X |  dS )z-Regression test for pkg_resources._macos_verszplatform.mac_ver)r4   r   r4   )Zreturn_value
fake.plistrw   ProductVersionz11.4zos.path.existsTc                   s    d f||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4N)builtinsrE   setattrr   ZMockplistlibdumprj   r6   Z_macos_verscache_clearr:   Z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dd	d
dg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dddg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   long_descriptionr   N)r   	distutilsr   r   commandinstall_egg_infoinitialize_optionsr   Zinstall_dirfinalize_optionsrun)r   r   r   Z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sBtd S )Nz1.11.0.dev0+2329eaezfoo>=1.9r   )	r   r6   Requirementparse
WorkingSetr   findr   r:   )r   r   r   reqr   r   r   r   #test_version_resolved_from_egg_infop  s
    zBTestDeepVersionLookupDistutils.test_version_resolved_from_egg_infozunnormalized, normalized)rS   rS   )zfoo/rS   )foo/barr   )zfoo/bar/r   c                 C  s$   t |}t |}||ks tdS )zeEnsure the trailing slash is cleaned for path comparison.

        See pypa/setuptools#1519.
        Nr6   normalize_pathr:   r   unnormalized
normalizedZ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  s$   t |}t |}||ks tdS )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.zunnormalized, expected)zforward/slashforward\slash)zforward/slash/r   )zbackward\slash\zbackward\slashc                 C  s   t |}||stdS )z;Ensure path seps are cleaned on backslash path sep systems.N)r6   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   Zfixturer   r   r   markparametrizer   Zskipifr   r
   normcaser   ri   r   r   r   r   r   r   P  sF   

	
	
r   )&
__future__r   r   rA   r   r   rC   rr   r	   r"   r$   Zunittestr   r   r6   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>   sR   Z6

