U
    ½mœdN   ã                   @   sR  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m	Z	m
Z
 ddlmZ d dlmZ ddlmZmZmZ G dd	„ d	ƒZG d
d„ deejƒZG dd„ deejejƒZdd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dejejƒZG dd„ dejejƒZe jdd„ ƒZG dd„ dejƒZG dd„ dejƒZ e!dkrNe "¡  dS )é    Né   )Údata01)Ú	zipdata01Ú	zipdata02)Úutil)Úimport_module)Úimport_helperÚ	os_helperÚunlinkc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚResourceTestsc                 C   s"   t  | j¡d }|  | ¡ ¡ d S )Núbinary.file)Ú	resourcesÚfilesÚdataÚ
assertTrueÚis_file©ÚselfÚtarget© r   ú`/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/importlib_resources/tests/test_resource.pyÚtest_is_file_exists   s    z!ResourceTests.test_is_file_existsc                 C   s"   t  | j¡d }|  | ¡ ¡ d S )Nz
not-a-file)r   r   r   ÚassertFalser   r   r   r   r   Útest_is_file_missing   s    z"ResourceTests.test_is_file_missingc                 C   s0   t  | j¡d }|  | ¡ ¡ |  | ¡ ¡ d S )NÚsubdirectory)r   r   r   r   r   r   Úis_dirr   r   r   r   Útest_is_dir   s    zResourceTests.test_is_dirN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ ZdS )ÚResourceDiskTestsc                 C   s
   t | _d S ©N)r   r   ©r   r   r   r   ÚsetUp!   s    zResourceDiskTests.setUpN)r   r   r   r#   r   r   r   r   r        s   r    c                   @   s   e Zd ZdS )ÚResourceZipTestsN)r   r   r   r   r   r   r   r$   %   s   r$   c                 C   s   dd„ |   ¡ D ƒS )Nc                 S   s   h | ]
}|j ’qS r   ©Úname©Ú.0Úitemr   r   r   Ú	<setcomp>*   s     znames.<locals>.<setcomp>)Úiterdir)Ztraversabler   r   r   Únames)   s    r,   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚResourceLoaderTestsc                 C   s8   t jttjdddgd}|  tt |¡ƒdddh¡ d S )NÚAÚBÚC©ÚfileÚpathÚcontents)r   Úcreate_packager   Ú__file__ÚassertEqualr,   r   r   ©r   Úpackager   r   r   Útest_resource_contents.   s      ÿz*ResourceLoaderTests.test_resource_contentsc                 C   s:   t jttjdddddgd}|  t |¡ d¡ ¡ ¡ d S )Nr.   r/   r0   úD/EúD/Fr1   )	r   r5   r   r6   r   r   r   Újoinpathr   r8   r   r   r   Útest_is_file4   s      ÿz ResourceLoaderTests.test_is_filec                 C   s:   t jttjdddddgd}|  t |¡ d¡ ¡ ¡ d S )Nr.   r/   r0   r;   r<   r1   ÚD)	r   r5   r   r6   r   r   r   r=   r   r8   r   r   r   r   :   s      ÿzResourceLoaderTests.test_is_dirc                 C   s:   t jttjdddddgd}|  t |¡ d¡ ¡ ¡ d S )Nr.   r/   r0   r;   r<   r1   ÚZ)	r   r5   r   r6   r   r   r   r=   r   r8   r   r   r   Útest_resource_missing@   s      ÿz)ResourceLoaderTests.test_resource_missingN)r   r   r   r:   r>   r   rA   r   r   r   r   r-   -   s   r-   c                   @   s   e Zd Zdd„ ZdS )ÚResourceCornerCaseTestsc                 C   sX   t jttjdddgd}tƒ |_d|_|j|j_|j|j_|  	t
 |¡ d¡ ¡ ¡ dS )z°
        Test odd ball packages which:
        # 1. Do not have a ResourceReader as a loader
        # 2. Are not on the file system
        # 3. Are not in a zip file
        r.   r/   r0   r1   z/path/which/shall/not/be/namedN)r   r5   r   r6   ÚobjectÚ
__loader__Ú__spec__ÚloaderÚoriginr   r   r   r=   r   )r   Úmoduler   r   r   Ú#test_package_has_no_reader_fallbackH   s      ÿ

z;ResourceCornerCaseTests.test_package_has_no_reader_fallbackN)r   r   r   rI   r   r   r   r   rB   G   s   rB   c                   @   s8   e Zd ZeZdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Z	dS )ÚResourceFromZipsTest01c                 C   s&   t dƒ}|  t |¡ d¡ ¡ ¡ d S ©Núziptestdata.subdirectoryr   )r   r   r   r   r=   r   ©r   Ú	submoduler   r   r   Útest_is_submodule_resource^   s    z1ResourceFromZipsTest01.test_is_submodule_resourcec                 C   s   |   t d¡ d¡ ¡ ¡ d S rK   ©r   r   r   r=   r   r"   r   r   r   Ú$test_read_submodule_resource_by_nameb   s
    
ÿÿz;ResourceFromZipsTest01.test_read_submodule_resource_by_namec                 C   s&   t dƒ}|  tt |¡ƒddh¡ d S ©NrL   ú__init__.pyr   )r   r7   r,   r   r   rM   r   r   r   Útest_submodule_contentsi   s
     ÿz.ResourceFromZipsTest01.test_submodule_contentsc                 C   s   |   tt d¡ƒddh¡ d S rR   ©r7   r,   r   r   r"   r   r   r   Útest_submodule_contents_by_nameo   s    þz6ResourceFromZipsTest01.test_submodule_contents_by_namec              	   C   sn   t  t  d¡¡F}|jdks t‚| ¡ s,t‚| d¡ ¡ s>t‚tt| 	¡ ƒƒsRt‚W 5 Q R X |j
 ¡ rjt‚d S )NÚziptestdatar   )r   Úas_filer   r&   ÚAssertionErrorr   r=   ÚlenÚlistr+   ÚparentÚexists)r   r   r   r   r   Útest_as_file_directoryu   s    z-ResourceFromZipsTest01.test_as_file_directoryN)
r   r   r   r   Ú
ZIP_MODULErO   rQ   rT   rV   r^   r   r   r   r   rJ   [   s   rJ   c                   @   s   e Zd ZeZdd„ ZdS )ÚResourceFromZipsTest02c                 C   s8   |   tt d¡ƒddh¡ |   tt d¡ƒddh¡ dS )z…
        Test thata zip with two unrelated subpackages return
        distinct resources. Ref python/importlib_resources#44.
        zziptestdata.onerS   zresource1.txtzziptestdata.twozresource2.txtNrU   r"   r   r   r   Útest_unrelated_contents   s    þþz.ResourceFromZipsTest02.test_unrelated_contentsN)r   r   r   r   r_   ra   r   r   r   r   r`   ~   s   r`   c                 c   sÖ   t  tj¡}|j d¡}t  | ¡t ¡ › d }| | 	¡ ¡ t
j t|ƒ¡ tdƒ z
d V  W 5 t t¡ t
j t|ƒ¡ W 5 Q R X t t¡ t
jt|ƒ= t
jd= W 5 Q R X t t¡ t|ƒ W 5 Q R X X d S )Nzziptestdata.zipz.ziprW   )ÚpathlibÚPathr   r6   r\   r=   ÚuuidÚuuid4Úwrite_bytesÚ
read_bytesÚsysr3   ÚappendÚstrr   Ú
contextlibÚsuppressÚ
ValueErrorÚremoveÚKeyErrorÚpath_importer_cacheÚmodulesÚOSErrorr
   )ÚdirZ	data_pathZsource_zip_pathÚzip_pathr   r   r   Úzip_on_path   s    
ru   c                   @   sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Ze 	d
¡dd„ ƒZ
dd„ Zdd„ Zdd„ ZdS )ÚDeletingZipsTestz^Having accessed resources in a zip file should not keep an open
    reference to the zip.
    c                 C   sV   t  ¡ | _|  | jj¡ t ¡ }| jtjf|žŽ  | j t	 
¡ ¡}| j t|ƒ¡ d S r!   )rk   Ú	ExitStackZfixturesZ
addCleanupÚcloser   Zmodules_setupZmodules_cleanupÚenter_contextr	   Útemp_dirru   )r   rq   rz   r   r   r   r#   ¬   s    
zDeletingZipsTest.setUpc                 C   s   dd„ t  d¡ ¡ D ƒ d S )Nc                 S   s   g | ]
}|j ‘qS r   r%   r'   r   r   r   Ú
<listcomp>·   s     zDDeletingZipsTest.test_iterdir_does_not_keep_open.<locals>.<listcomp>rW   )r   r   r+   r"   r   r   r   Útest_iterdir_does_not_keep_open¶   s    z0DeletingZipsTest.test_iterdir_does_not_keep_openc                 C   s   t  d¡ d¡ ¡  d S ©NrW   r   ©r   r   r=   r   r"   r   r   r   Útest_is_file_does_not_keep_open¹   s    z0DeletingZipsTest.test_is_file_does_not_keep_openc                 C   s   t  d¡ d¡ ¡  d S )NrW   znot-presentr~   r"   r   r   r   Ú'test_is_file_failure_does_not_keep_open¼   s    z8DeletingZipsTest.test_is_file_failure_does_not_keep_openzDesired but not supported.c                 C   s   t  t  d¡d ¡ d S r}   )r   rX   r   r"   r   r   r   Útest_as_file_does_not_keep_open¿   s    z0DeletingZipsTest.test_as_file_does_not_keep_openc                 C   s   t  t  d¡d ¡ ¡  dS )zr
        Mimic what certifi does on import to make its bundle
        available for the process duration.
        rW   r   N)r   rX   r   Ú	__enter__r"   r   r   r   Ú$test_entered_path_does_not_keep_openÃ   s    z5DeletingZipsTest.test_entered_path_does_not_keep_openc                 C   s   t  d¡ d¡ ¡  d S r}   )r   r   r=   rg   r"   r   r   r   Ú#test_read_binary_does_not_keep_openÊ   s    z4DeletingZipsTest.test_read_binary_does_not_keep_openc                 C   s   t  d¡ d¡jdd d S )NrW   ú
utf-8.filezutf-8)Úencoding)r   r   r=   Ú	read_textr"   r   r   r   Ú!test_read_text_does_not_keep_openÍ   s    ÿz2DeletingZipsTest.test_read_text_does_not_keep_openN)r   r   r   Ú__doc__r#   r|   r   r€   ÚunittestÚskipr   rƒ   r„   rˆ   r   r   r   r   rv   §   s   

rv   c                   @   sT   e Zd Zee e¡jƒZe	dd„ ƒZ
e	dd„ ƒZdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚResourceFromNamespaceTest01c                 C   s   t j | j¡ d S r!   )rh   r3   ri   Úsite_dir©Úclsr   r   r   Ú
setUpClassÖ   s    z&ResourceFromNamespaceTest01.setUpClassc                 C   s   t j | j¡ d S r!   )rh   r3   rn   r   rŽ   r   r   r   ÚtearDownClassÚ   s    z)ResourceFromNamespaceTest01.tearDownClassc                 C   s"   |   t tdƒ¡ d¡ ¡ ¡ d S ©NÚnamespacedata01r   )r   r   r   r   r=   r   r"   r   r   r   rO   Þ   s
    ÿÿz6ResourceFromNamespaceTest01.test_is_submodule_resourcec                 C   s   |   t d¡ d¡ ¡ ¡ d S r’   rP   r"   r   r   r   rQ   å   s    ÿz@ResourceFromNamespaceTest01.test_read_submodule_resource_by_namec                 C   sL   t t tdƒ¡ƒ}z| d¡ W n tk
r4   Y nX |  |dddh¡ d S ©Nr“   Ú__pycache__r   r…   zutf-16.file)r,   r   r   r   rn   ro   r7   ©r   r4   r   r   r   rT   ê   s    z3ResourceFromNamespaceTest01.test_submodule_contentsc                 C   sH   t t d¡ƒ}z| d¡ W n tk
r0   Y nX |  |dddh¡ d S r”   )r,   r   r   rn   ro   r7   r–   r   r   r   rV   ò   s    z;ResourceFromNamespaceTest01.test_submodule_contents_by_nameN)r   r   r   rj   rb   rc   r6   r\   r   Úclassmethodr   r‘   rO   rQ   rT   rV   r   r   r   r   rŒ   Ó   s   

rŒ   Ú__main__)#rk   rh   rŠ   Zimportlib_resourcesr   rd   rb   Ú r   r   r   r   Ú	importlibr   Z_compatr   r	   r
   r   ZTestCaser    ZZipSetupr$   r,   r-   rB   ZZipSetupBaserJ   r`   Úcontextmanagerru   rv   rŒ   r   Úmainr   r   r   r   Ú<module>   s0   #
,(
