a
    3ju!                     @   s,  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	m
Z
 d dlmZ d dl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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G dd dejZG dd dejZedkr(e  dS )    N   )data01)	zipdata01	zipdata02)util)	resourcesimport_module)Path)supportc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ResourceTestsc                 C   s   |  t| jd d S )Nbinary.file)
assertTruer   is_resourcedataself r   ]/www/server/python_manager/versions/3.9.10/lib/python3.9/test/test_importlib/test_resource.pytest_is_resource_good_path   s    z(ResourceTests.test_is_resource_good_pathc                 C   s   |  t| jd d S )Nz
not-a-fileassertFalser   r   r   r   r   r   r   test_is_resource_missing   s    z&ResourceTests.test_is_resource_missingc                 C   s   |  t| jd d S )Nsubdirectoryr   r   r   r   r   &test_is_resource_subresource_directory   s    z4ResourceTests.test_is_resource_subresource_directoryc                 C   sB   t t| j}|d |d |d | |h d d S )N__pycache__z__init__.pycz__init__.pyo>   
utf-8.filer   __init__.pyzutf-16.filer   )setr   contentsr   discardassertEqualr   r   r   r   r   test_contents   s
    


zResourceTests.test_contentsN)__name__
__module____qualname__r   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)   2   s   r)   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ResourceLoaderTestsc                 C   s4   t jttjg dd}| tt|h d d S )NABCfilepathr   >   r.   r-   r,   )r   create_packager   __file__r    r   r   r   r   packager   r   r   test_resource_contents7   s    z*ResourceLoaderTests.test_resource_contentsc                 C   s,   t jttjg dd}| t|d d S )Nr,   r-   r.   zD/EzD/Fr/   r-   )r   r2   r   r3   r   r   r   r4   r   r   r   test_resource_is_resource>   s
    z-ResourceLoaderTests.test_resource_is_resourcec                 C   s,   t jttjg dd}| t|d d S )Nr7   r/   Dr   r2   r   r3   r   r   r   r4   r   r   r   'test_resource_directory_is_not_resourceD   s
    z;ResourceLoaderTests.test_resource_directory_is_not_resourcec                 C   s,   t jttjg dd}| t|d d S )Nr7   r/   Zr:   r4   r   r   r   %test_resource_missing_is_not_resourceJ   s
    z9ResourceLoaderTests.test_resource_missing_is_not_resourceN)r#   r$   r%   r6   r8   r;   r=   r   r   r   r   r*   6   s   r*   c                   @   s   e Zd Zdd ZdS )ResourceCornerCaseTestsc                 C   sX   t jttjg dd}t |_d|_tjdkrB|j|j_	|j|j_
| t|d d S )Nr+   r/   z/path/which/shall/not/be/named)   r,   )r   r2   r   r3   object
__loader__sysversion_info__spec__loaderoriginr   r   r   )r   moduler   r   r   #test_package_has_no_reader_fallbackR   s    


z;ResourceCornerCaseTests.test_package_has_no_reader_fallbackN)r#   r$   r%   rH   r   r   r   r   r>   Q   s   r>   c                   @   s   e Zd ZeZdd ZdS )ResourceFromZipsTestc                 C   s8   |  ttdddh |  ttdddh d S )Nzziptestdata.oner   resource1.txtzziptestdata.twozresource2.txtr    r   r   r   r   r   r   r   test_unrelated_contentsf   s    z,ResourceFromZipsTest.test_unrelated_contentsN)r#   r$   r%   r   
ZIP_MODULErL   r   r   r   r   rI   c   s   rI   c                   @   s0   e Zd ZeZdd Zdd Zdd Zdd Zd	S )
 SubdirectoryResourceFromZipsTestc                 C   s   t d}| t|d d S Nziptestdata.subdirectoryr   )r   r   r   r   r   Z	submoduler   r   r   test_is_submodule_resourcew   s    
z;SubdirectoryResourceFromZipsTest.test_is_submodule_resourcec                 C   s   |  tdd d S rO   )r   r   r   r   r   r   r   $test_read_submodule_resource_by_name|   s    
zESubdirectoryResourceFromZipsTest.test_read_submodule_resource_by_namec                 C   s&   t d}| tt|ddh d S NrP   r   r   )r   r    r   r   r   rQ   r   r   r   test_submodule_contents   s
    z8SubdirectoryResourceFromZipsTest.test_submodule_contentsc                 C   s   |  ttdddh d S rT   rK   r   r   r   r   test_submodule_contents_by_name   s    z@SubdirectoryResourceFromZipsTest.test_submodule_contents_by_nameN)	r#   r$   r%   r   rM   rR   rS   rU   rV   r   r   r   r   rN   t   s
   rN   c                   @   s   e Zd Zdd ZdS )NamespaceTestc                 C   sv   t d}| t| | t dd | tt jdd | tt jdd | tt j	dd | tt j
dd d S )Nz$test.test_importlib.data03.namespacerJ   )r   r   r   listr   ZassertRaisesFileNotFoundErrorZ	open_textZopen_binary	read_textread_binaryr!   r   r   r   %test_namespaces_cannot_have_resources   s4    
z3NamespaceTest.test_namespaces_cannot_have_resourcesN)r#   r$   r%   r\   r   r   r   r   rW      s   rW   c                   @   s\   e Zd ZdZeZdd Zdd Zdd Zdd	 Z	d
d Z
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   s   t  }| jt jg|R   t| jj}|j}|d | _t	 d
t  | _| j| j  tjt| j td| _d S )Nzziptestdata.zipz{}.zipziptestdata)r
   Zmodules_setupZ
addCleanupZmodules_cleanupr	   rM   r3   parentZsource_zip_pathcwdformatuuidZuuid4zip_pathwrite_bytes
read_bytesrB   r1   appendstrr   r   )r   modulesZ	data_pathZdata_dirr   r   r   r(      s    
zDeletingZipsTest.setUpc                 C   s   zt jt| j W n ty(   Y n0 zt jt| j= t j| jj	= W n t
yZ   Y n0 zt| j W n ty~   Y n0 d S r'   )rB   r1   removerg   rc   
ValueErrorpath_importer_cacherh   r   r#   KeyErrorr
   unlinkOSErrorr   r   r   r   tearDown   s    zDeletingZipsTest.tearDownc                 C   s   t d}| j  d S )Nr^   )r   r   rc   rm   r   cr   r   r    test_contents_does_not_keep_open   s    
z1DeletingZipsTest.test_contents_does_not_keep_openc                 C   s   t dd}| j  d S Nr^   r   r   r   rc   rm   rp   r   r   r   #test_is_resource_does_not_keep_open   s    z4DeletingZipsTest.test_is_resource_does_not_keep_openc                 C   s   t dd}| j  d S )Nr^   znot-presentrt   rp   r   r   r   +test_is_resource_failure_does_not_keep_open   s    z<DeletingZipsTest.test_is_resource_failure_does_not_keep_openc                 C   s   t dd}| j  d S rs   )r   r1   rc   rm   rp   r   r   r   test_path_does_not_keep_open   s    z-DeletingZipsTest.test_path_does_not_keep_openc                 C   s   t dd }| j  d S rs   )r   r1   	__enter__rc   rm   rp   r   r   r   $test_entered_path_does_not_keep_open   s    z5DeletingZipsTest.test_entered_path_does_not_keep_openc                 C   s   t dd}| j  d S rs   )r   r[   rc   rm   rp   r   r   r   #test_read_binary_does_not_keep_open   s    z4DeletingZipsTest.test_read_binary_does_not_keep_openc                 C   s   t jdddd}| j  d S )Nr^   r   zutf-8)encoding)r   rZ   rc   rm   rp   r   r   r   !test_read_text_does_not_keep_open   s    z2DeletingZipsTest.test_read_text_does_not_keep_openN)r#   r$   r%   __doc__r   rM   r(   ro   rr   ru   rv   rw   ry   rz   r|   r   r   r   r   r]      s   r]   __main__)rB   Zunittestrb    r   r   r   r   	importlibr   r   pathlibr	   testr
   r   ZTestCaser&   ZZipSetupr)   r*   r>   ZZipSetupBaserI   rN   rW   r]   r#   mainr   r   r   r   <module>   s&    B
