U
    xh                     @   s  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mZ ddlmZmZ ejdd ZG d	d
 d
ZG dd deejejZG dd deejejZG dd deejejZG dd deejejZG dd dZG dd dZG dd deejeejZG dd deejeejZG dd dZ G dd deeje ejZ!G dd  d eeje ejZ"e#d!kre$  dS )"    N   )Traversable   )util)	os_helperimport_helperc               	   c   s0   t jdd} t jdtd | V  W 5 Q R X d S )NT)recorddefault)category)warningscatch_warningssimplefilterDeprecationWarning)ctx r   H/tmp/pip-unpacked-wheel-jv8v9feh/importlib_resources/tests/test_files.pysuppress_known_deprecation   s    r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
FilesTestsc                 C   s*   t | j}|d }|dks&td S )N
utf-8.files   Hello, UTF-8 world!
)	resourcesfilesdatajoinpath
read_bytesAssertionErrorselfr   actualr   r   r   test_read_bytes   s    zFilesTests.test_read_bytesc                 C   s.   t | j}|djdd}|dks*td S )Nr   utf-8encodingzHello, UTF-8 world!
)r   r   r   r   	read_textr   r   r   r   r   test_read_text   s    zFilesTests.test_read_textc                 C   s   t t| jtstd S N)
isinstancer   r   r   r   r   r   r   r   r   test_traversable#   s    zFilesTests.test_traversablec                 C   s*   t | j}|dd}| |  d S )Nsubdirectoryzbinary.file)r   r   r   r   
assertTrueis_file)r   r   Zbinfiler   r   r    test_joinpath_with_multiple_args&   s    z+FilesTests.test_joinpath_with_multiple_argsc              	   C   s$   t   tj| jd W 5 Q R X dS )zx
        Files used to take a 'package' parameter. Make sure anyone
        passing by name is still supported.
        )packageN)r   r   r   r   r&   r   r   r   test_old_parameter+   s    zFilesTests.test_old_parameterN)__name__
__module____qualname__r   r#   r'   r+   r-   r   r   r   r   r      s
   r   c                   @   s   e Zd ZdS )OpenDiskTestsNr.   r/   r0   r   r   r   r   r1   4   s   r1   c                   @   s   e Zd ZdS )OpenZipTestsNr2   r   r   r   r   r3   8   s   r3   c                   @   s   e Zd ZdZdd ZdS )OpenNamespaceTestsnamespacedata01c                 C   s"   ddl }|jd t| dS )a  
        Non-path items in a namespace package's ``__path__`` are ignored.

        As reported in python/importlib_resources#311, some tools
        like Setuptools, when creating editable packages, will inject
        non-paths into a namespace package's ``__path__``, a
        sentinel like
        ``__editable__.sample_namespace-1.0.finder.__path_hook__``
        to cause the ``PathEntryFinder`` to be called when searching
        for packages. In that case, resources should still be loadable.
        r   Nz6__editable__.sample_namespace-1.0.finder.__path_hook__)r5   __path__appendr   r   )r   r5   r   r   r   test_non_paths_in_dunder_path?   s
    z0OpenNamespaceTests.test_non_paths_in_dunder_pathN)r.   r/   r0   MODULEr8   r   r   r   r   r4   <   s   r4   c                   @   s   e Zd ZdZdS )OpenNamespaceZipTestsr5   N)r.   r/   r0   Z
ZIP_MODULEr   r   r   r   r:   T   s   r:   c                   @   s   e Zd ZdZdZdd ZdS )
DirectSpeczI
    Override behavior of ModuleSetup to write a full spec directly.
    Zunusedc                 C   s   |  | j d S r$   )Ztree_on_pathspec)r   namer   r   r   load_fixture_   s    zDirectSpec.load_fixtureN)r.   r/   r0   __doc__r9   r>   r   r   r   r   r;   X   s   r;   c                   @   s   e Zd ZdddZdd ZdS )ModulesFiles resources are the best)zmod.pyres.txtc                 C   s6   ddl }t|djdd}|| jd ks2tdS )zK
        A module can have resources found adjacent to the module.
        r   NrC   r   r    )modr   r   r   r"   r<   r   )r   rD   r   r   r   r   test_module_resourcesi   s    z"ModulesFiles.test_module_resourcesN)r.   r/   r0   r<   rE   r   r   r   r   r@   c   s   r@   c                   @   s   e Zd ZdS )ModuleFilesDiskTestsNr2   r   r   r   r   rF   s   s   rF   c                   @   s   e Zd ZdS )ModuleFilesZipTestsNr2   r   r   r   r   rG   w   s   rG   c                   @   sb   e Zd Zedej  dZeeddeej ddddZdd	 Z	d
d Z
dd Zdd ZdS )ImplicitContextFilesz
        import zZ as res
        val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
        rB   )__init__.pyz	submod.pyrC   c_resources)rI   rC   )somepkg	frozenpkgc                 C   s   t djdkstdS )W
        Without any parameter, files() will infer the location as the caller.
        rK   rB   N	importlibimport_modulevalr   r&   r   r   r   test_implicit_files_package   s    z0ImplicitContextFiles.test_implicit_files_packagec                 C   s   t djdkstdS )rM   zsomepkg.submodrB   NrN   r&   r   r   r   test_implicit_files_submodule   s    z2ImplicitContextFiles.test_implicit_files_submodulec           
      C   s   | j t }t|d}ttjj}t	j
||dd d t|D ]D\}}}|D ]4}t|| }|d}	t||	 tj| qVqH| j t| dS )zO
        Make a compiled-only copy of the importlib resources package.
        rJ   c                  W   s   dgS )N__pycache__r   )_r   r   r   <lambda>       z9ImplicitContextFiles._compile_importlib.<locals>.<lambda>)ignorez.pycN)Zfixturesenter_contextr   temp_dirpathlibPathr   __file__parentshutilcopytreeoswalkwith_suffix
py_compilecompileunlinkr   ZDirsOnSysPath)
r   Zbin_siterJ   sourcesdirpathrU   	filenamesfilenamesource_pathcfiler   r   r   _compile_importlib   s    
z'ImplicitContextFiles._compile_importlibc                 C   s    |    tdjdkstdS )zk
        Caller detection works for compiled-only resources module.

        python/cpython#123085
        rL   rB   N)rm   rO   rP   rQ   r   r&   r   r   r   +test_implicit_files_with_compiled_importlib   s    z@ImplicitContextFiles.test_implicit_files_with_compiled_importlibN)r.   r/   r0   textwrapdedentr   Zset_valreplacer<   rR   rS   rm   rn   r   r   r   r   rH   {   s"   rH   c                   @   s   e Zd ZdS )ImplicitContextFilesDiskTestsNr2   r   r   r   r   rr      s   rr   c                   @   s   e Zd ZdS )ImplicitContextFilesZipTestsNr2   r   r   r   r   rs      s   rs   __main__)%ra   r[   rd   r_   ro   Zunittestr   rO   
contextlibZimportlib_resourcesr   abcr   rA   r   Zcompat.py39r   r   contextmanagerr   r   Z	DiskSetupZTestCaser1   ZZipSetupr3   r4   r:   r;   r@   rF   rG   rH   rr   rs   r.   mainr   r   r   r   <module>   sL   
:
   
   
