o
    hq                     @   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
    GITDB_TEST_GIT_REPO_BASEc                    s~   zt    W n	 ty   Y nw tj| j| _| js5t	d| j tj
j}tj
|||td| _| jds=J 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_repogitrepopathlogginginfopathdirnamejoin__file__endswith)clsospd	__class__ B/var/www/vscode/kcb/lib/python3.10/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                        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}z2z | |W W |s"t  t| S S  t	y@   t
jdt| j d j d|d d} w |sMt  t| w w )N)prefixFzTest .z failed, output is at 
T)tempfilemktempr   r   mkdirgccollectshutilrmtree	Exceptionsysstderrwritetype)selfr   keepfuncr   r   wrapperA   s&   

(	z"with_rw_directory.<locals>.wrapperr   r6   r7   r   r5   r   with_rw_directory=   s   r:   c                    r#   )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)r3   r   src_pack_globr5   r   r   r7   _   s   
zwith_packs_rw.<locals>.wrapperr8   r9   r   r5   r   with_packs_rw[   s   r?    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 itselffixtures)r   r   r   r   r   )relapathr   r   r   r<   m   s   r<   Fc              	   C   st   t  | D ]2}|r1ttdr1tj|tj|}zt|| W q ty0   t	|| Y qw 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   basenamerC   OSErrorr,   copy)source_glob
target_dirr;   src_filetargetr   r   r   r=   t   s   r=   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	randomizeactual_sizeproducerar   r   r   
make_bytes   s   

rY   c                 C   s   dt | }|d| S )z0:return: bytes resembling an uncompressed objectzblob %i ascii)lenencode)r2   dataodatar   r   r   make_object   s   r_   c                 C   s   t | |}t|t|fS )z`:return: tuple(size_of_stream, stream)
    :param randomize: try to produce a very random stream)rY   r[   r   )rT   rU   dr   r   r   make_memory_file   s   
ra   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r3   r   r   r   __init__   s   
zDummyStream.__init__c                 C   s   d| _ || _d S NT)rc   rd   )r3   sizer   r   r   read   s   
zDummyStream.readc                 C   s
   d| _ d S rh   )re   rf   r   r   r   close   s   
zDummyStream.closec                 C   s   | j sJ d S N)rc   rf   r   r   r   _assert   s   zDummyStream._assertN)r   r   r   rg   rj   rk   rm   r   r   r   r   rb      s
    rb   c                   @   s   e Zd Zdd Zdd ZdS )
DeriveTestc                 O   s   | d| _|| _d S )Nmyarg)popro   args)r3   shar2   ri   streamrq   kwargsr   r   r   rg      s   
zDeriveTest.__init__c                 C   s   | j sJ | js
J d S rl   )rq   ro   rf   r   r   r   rm      s   
zDeriveTest._assertN)r   r   r   rg   rm   r   r   r   r   rn      s    rn   )r@   )F)r    gitdbr   r/   rQ   r   ior   rD   unittestr'   r,   r   r*   r   	functoolsr   TestCaser   r:   r?   r<   r=   rY   r_   ra   rb   rn   r   r   r   r   <module>   s0   #



