U
    楡cw                     @   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k
r"   Y nX tj| j| _| jslt	d| j tj
j}tj
|||td| _| jds|t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AssertionError)clsZospd	__class__ 2/tmp/pip-unpacked-wheel-jjg5dubb/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}zXz | |W W HS  t	k
rp   t
jdt| j d j d|d d} Y nX W 5 |st  t| X 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   r1   r2   r   r0   r   with_rw_directory=   s    r5   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_globr0   r   r   r2   _   s    zwith_packs_rw.<locals>.wrapperr3   r4   r   r0   r   with_packs_rw[   s    r9    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   r7   m   s    r7   Fc              	   C   sx   t  | D ]h}|rfttdrftj|tj|}zt|| W qr tk
rb   t	|| Y qrX 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_dirr6   Zsrc_filetargetr   r   r   r8   t   s    r8   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    

rM   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    rS   c                 C   s   t | |}t|t|fS )z`:return: tuple(size_of_stream, stream)
    :param randomize: try to produce a very random stream)rM   rO   r   )rJ   rK   dr   r   r   make_memory_file   s    
rU   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)rW   rX   )r/   sizer   r   r   read   s    zDummyStream.readc                 C   s
   d| _ d S r\   )rY   rZ   r   r   r   close   s    zDummyStream.closec                 C   s   | j s
td S N)rW   r   rZ   r   r   r   _assert   s    zDummyStream._assertN)r   r   r   r[   r^   r_   ra   r   r   r   r   rV      s   rV   c                   @   s   e Zd Zdd Zdd ZdS )
DeriveTestc                 O   s   | d| _|| _d S )Nmyarg)poprc   args)r/   shar.   r]   streamre   kwargsr   r   r   r[      s    zDeriveTest.__init__c                 C   s   | j s
t| jstd S r`   )re   r   rc   rZ   r   r   r   ra      s    
zDeriveTest._assertN)r   r   r   r[   ra   r   r   r   r   rb      s   rb   )r:   )F)F)F)r   Zgitdbr   r+   rG   r   ior   r<   Zunittestr$   r(   r
   r'   r   	functoolsr   ZTestCaser   r5   r9   r7   r8   rM   rS   rU   rV   rb   r   r   r   r   <module>   s.   #



