a
    (Dfq                     @   s   d Z ddlmZ ddlZddlZddlmZ 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mZ G dd de	jZd	d
 Zdd ZdddZdddZdddZdd Zd ddZG dd dZG dd deZdS )!zUtilities used in ODB testing    )OStreamN)array)BytesIO)wrapsc                       s(   e Zd ZdZdZe fddZ  ZS )TestBasezBase class for all tests

    TestCase providing access to readonly repositories using the following member variables.

    * gitrepopath

     * read-only base path of the git source repository, i.e. .../git/.git
    ZGITDB_TEST_GIT_REPO_BASEc                    s~   zt    W n ty    Y n0 tj| j| _| jsjt	d| j tj
j}tj
|||td| _| jdszJ d S )NzpYou can set the %s environment variable to a .git repository of your choice - defaulting to the gitdb repositoryz.git)super
setUpClassAttributeErrorosenvirongetk_env_git_repoZgitrepopathlogginginfopathdirnamejoin__file__endswith)clsZospd	__class__ [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/gitdb/test/lib.pyr   (   s    zTestBase.setUpClass)__name__
__module____qualname____doc__r   classmethodr   __classcell__r   r   r   r   r      s   
r   c                    s    fdd} j |_ |S )zCreate a temporary directory which can be written to, remove it if the
    test succeeds, but leave it otherwise to aid additional debuggingc                    s   t j jd}t| d}zz$ | |W W |sBt  t| S  t	y   t
jdt| j d j d|d d} Y n0 W |st  t| n|st  t| 0 d S )N)prefixFzTest .z failed, output is at 
T)tempfilemktempr   r
   mkdirgcZcollectshutilrmtree	Exceptionsysstderrwritetype)selfr   Zkeepfuncr   r   wrapperA   s(    


(
z"with_rw_directory.<locals>.wrapperr   r0   r1   r   r/   r   with_rw_directory=   s    r4   c                    s    fdd} j |_ |S )zFunction that provides a path into which the packs for testing should be
    copied. Will pass on the path to the actual function afterwardsc                    s    t d}t||dd  | |S )Nzpacks/*T)hard_link_ok)fixture_pathcopy_files_globbed)r.   r   Zsrc_pack_globr/   r   r   r1   _   s    zwith_packs_rw.<locals>.wrapperr2   r3   r   r/   r   with_packs_rw[   s    r8    c                 C   s   t jt jtd| S )z:return: absolute path into the fixture directory
    :param relapath: relative path into the fixtures directory, or ''
        to obtain the fixture directory itselfZfixtures)r
   r   r   r   r   )Zrelapathr   r   r   r6   m   s    r6   Fc              	   C   sv   t  | D ]f}|rdttdrdtj|tj|}zt|| W qp ty`   t	|| Y qp0 q
t	|| q
dS )zCopy all files found according to the given source glob into the target directory
    :param hard_link_ok: if True, hard links will be created if possible. Otherwise
        the files will be copiedlinkN)
globhasattrr
   r   r   basenamer:   OSErrorr'   copy)Zsource_glob
target_dirr5   Zsrc_filetargetr   r   r   r7   t   s    r7   c                 C   s8   | d }t |}|r&t|}t| td|}| S )zb:return: string with given size in bytes
    :param randomize: try to produce a very random stream   i)rangelistrandomshuffler   tobytes)size_in_bytes	randomizeZactual_sizeZproducerar   r   r   
make_bytes   s    

rL   c                 C   s   dt | }|d| S )z0:return: bytes resembling an uncompressed objectzblob %i ascii)lenencode)r-   dataodatar   r   r   make_object   s    rR   c                 C   s   t | |}t|t|fS )z`:return: tuple(size_of_stream, stream)
    :param randomize: try to produce a very random stream)rL   rN   r   )rI   rJ   dr   r   r   make_memory_file   s    
rT   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
DummyStreamc                 C   s   d| _ d| _d| _d S )NFr   )was_readbytesclosedr.   r   r   r   __init__   s    zDummyStream.__init__c                 C   s   d| _ || _d S NT)rV   rW   )r.   sizer   r   r   read   s    zDummyStream.readc                 C   s
   d| _ d S r[   )rX   rY   r   r   r   close   s    zDummyStream.closec                 C   s   | j s
J d S N)rV   rY   r   r   r   _assert   s    zDummyStream._assertN)r   r   r   rZ   r]   r^   r`   r   r   r   r   rU      s   rU   c                   @   s   e Zd Zdd Zdd ZdS )
DeriveTestc                 O   s   | d| _|| _d S )Nmyarg)poprb   args)r.   shar-   r\   streamrd   kwargsr   r   r   rZ      s    zDeriveTest.__init__c                 C   s   | j s
J | jsJ d S r_   )rd   rb   rY   r   r   r   r`      s    
zDeriveTest._assertN)r   r   r   rZ   r`   r   r   r   r   ra      s   ra   )r9   )F)F)F)r   Zgitdbr   r*   rF   r   ior   r;   Zunittestr#   r'   r
   r&   r   	functoolsr   ZTestCaser   r4   r8   r6   r7   rL   rR   rT   rU   ra   r   r   r   r   <module>   s.   #



