o
    ÷ÑÐbâ  ã                   @   sj   d 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	m
Z
mZmZ G dd„ dejƒZdS )z&
This script tests utility functions.
é    N)Úiter_tree_entriesÚiter_tree_filesÚRecursionErrorÚnormalize_filec                   @   sÒ   e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZe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d„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Ze ejd*k d+¡d,d-„ ƒZd.S )/ÚIterTreeTestzG
	The ``IterTreeTest`` class tests `pathspec.util.iter_tree_files()`.
	c              	   C   s*   |D ]}t  t j | j|  |¡¡¡ qdS )z'
		Create the specified directories.
		N)ÚosÚmkdirÚpathÚjoinÚtemp_dirÚospath)ÚselfÚdirsÚdir© r   úW/var/www/secure340b-portal/env/lib/python3.10/site-packages/pathspec/tests/test_util.pyÚ	make_dirs   ó   ÿzIterTreeTest.make_dirsc              	   C   s*   |D ]}|   tj | j|  |¡¡¡ qdS )z!
		Create the specified files.
		N)Úmkfiler   r	   r
   r   r   )r   ÚfilesÚfiler   r   r   Ú
make_files   r   zIterTreeTest.make_filesc              
   C   sB   |D ]\}}t  t j | j|  |¡¡t j | j|  |¡¡¡ qdS )z!
		Create the specified links.
		N)r   Úsymlinkr	   r
   r   r   )r   ÚlinksÚlinkÚnoder   r   r   Ú
make_links$   s   2ÿzIterTreeTest.make_linksc                 C   s0   t | dƒ	 W d  ƒ dS 1 sw   Y  dS )z
		Creates an empty file.
		ÚwbN)Úopen)r   r   r   r   r   +   s   "ÿzIterTreeTest.mkfilec                 C   s   t jj|  d¡Ž S )z1
		Convert the POSIX path to a native OS path.
		ú/)r   r	   r
   Úsplit©r	   r   r   r   r   3   s   zIterTreeTest.ospathc                 C   ó   | j rt d¡‚dS )zP
		Skips the test if `os.path.realpath` does not properly support
		symlinks.
		z`os.path.realpath` is broken.N)Úbroken_realpathÚunittestÚSkipTest©r   r   r   r   Úrequire_realpath:   s   
ÿzIterTreeTest.require_realpathc                 C   r"   )z6
		Skips the test if `os.symlink` is not supported.
		z`os.symlink` is not supported.N)Ú
no_symlinkr$   r%   r&   r   r   r   Úrequire_symlinkB   s   
ÿzIterTreeTest.require_symlinkc                 C   s   t  ¡ | _dS )z
		Called before each test.
		N)ÚtempfileÚmkdtempr   r&   r   r   r   ÚsetUpI   s   zIterTreeTest.setUpc                 C   s   t  | j¡ dS )z
		Called after each test.
		N)ÚshutilÚrmtreer   r&   r   r   r   ÚtearDownO   s   zIterTreeTest.tearDownc                 C   sJ   |   g d¢¡ |  g d¢¡ tt| jƒƒ}|  |tt| jg d¢ƒƒ¡ dS )ú-
		Tests to make sure all files are found.
		©ÚEmptyÚDirú	Dir/Inner©ÚaÚbúDir/cúDir/dúDir/Inner/eúDir/Inner/fN)r   r   Úsetr   r   ÚassertEqualÚmapr   ©r   Úresultsr   r   r   Útest_1_filesU   s    zIterTreeTest.test_1_filesc              	   C   sv   d}z3t j | jd¡}t j | jd¡}|  |¡ zt  ||¡ W n ttfy,   d}‚ w d}W || j_	dS || j_	w )z)
		Tests whether links can be created.
		Nr   r   TF)
r   r	   r
   r   r   r   ÚAttributeErrorÚNotImplementedErrorÚ	__class__r(   )r   r(   r   r   r   r   r   Útest_2_0_check_symlinkp   s   
þz#IterTreeTest.test_2_0_check_symlinkc                 C   s–   d}zC|   ¡  tj | jd¡}tj | jd¡}|  |¡ t ||¡ z|  tj |¡tj |¡¡ W n
 t	y<   d}‚ w d}W || j
_dS || j
_w )zD
		Tests whether `os.path.realpath` works properly with symlinks.
		Nr   r   TF)r)   r   r	   r
   r   r   r   r=   ÚrealpathÚAssertionErrorrD   r#   )r   r#   r   r   r   r   r   Útest_2_1_check_realpath†   s   
 þz$IterTreeTest.test_2_1_check_realpathc                 C   s^   |   ¡  |  dg¡ |  g d¢¡ |  g d¢¡ tt| jƒƒ}|  |tt| j	g d¢ƒƒ¡ dS )z=
		Tests to make sure links to directories and files work.
		r3   )r6   r7   r8   r9   ))Úaxr6   )Úbxr7   )úDir/cxr8   )úDir/dxr9   ©ÚDirXr3   )r6   rI   r7   rJ   r8   rK   r9   rL   zDirX/czDirX/cxzDirX/dzDirX/dxN©
r)   r   r   r   r<   r   r   r=   r>   r   r?   r   r   r   Útest_2_2_linksŸ   s   ÿ zIterTreeTest.test_2_2_linksc                 C   s^   |   ¡  |  ddg¡ |  dg¡ |  g d¢¡ tt| jƒƒ}|  |tt| j	g d¢ƒƒ¡ dS )zZ
		Tests to make sure the same directory can be encountered multiple
		times via links.
		r3   ú
Dir/TargetúDir/Target/file))ÚAxr3   )ÚBxr3   )ZCxrQ   )ZDxrQ   )zDir/ExrQ   )zDir/FxrQ   )z
Ax/Ex/filez
Ax/Fx/filezAx/Target/filez
Bx/Ex/filez
Bx/Fx/filezBx/Target/filezCx/filezDx/filezDir/Ex/filezDir/Fx/filerR   NrO   r?   r   r   r   Útest_2_3_sideways_linksÄ   s   þÿ z$IterTreeTest.test_2_3_sideways_linksc                 C   s–   |   ¡  |  ¡  |  dg¡ |  dg¡ |  dg¡ |  t¡}tt| j	ƒƒ W d  ƒ n1 s1w   Y  |  
|jjd¡ |  
|jj|  d¡¡ dS )z)
		Tests detection of recursive links.
		r3   zDir/file)úDir/Selfr3   NrV   )r)   r'   r   r   r   ÚassertRaisesr   r<   r   r   r=   Ú	exceptionÚ
first_pathÚsecond_pathr   ©r   Úcontextr   r   r   Útest_2_4_recursive_linksè   s    ÿÿÿÿz%IterTreeTest.test_2_4_recursive_linksc                 C   s¸   |   ¡  |  ¡  |  g d¢¡ |  g d¢¡ |  g d¢¡ |  t¡}tt| j	ƒƒ W d  ƒ n1 s4w   Y  |  
|jjd¡ |  |jj|  d¡|  d¡|  d¡dœ|jj ¡ dS )z:
		Tests detection of recursion through circular links.
		)ÚAÚBÚC)zA/dzB/ezC/f))zA/Bxr_   )zB/Cxr`   )zC/Axr^   Nz
A/Bx/Cx/Axz
B/Cx/Ax/Bxz
C/Ax/Bx/Cx)r)   r'   r   r   r   rW   r   r<   r   r   ZassertInrX   rY   r=   rZ   r   r[   r   r   r   Ú!test_2_5_recursive_circular_linksü   s    ÿ
ý
üz.IterTreeTest.test_2_5_recursive_circular_linksc                 C   sn   dd„ }|   ¡  |  dg¡ |  t¡}tt| j|dƒ W d  ƒ n1 s'w   Y  |  |jj	t	j
¡ dS )z+
		Tests that broken links are detected.
		c                 S   s   | ‚)Nr   )Úer   r   r   Úreraise  s   z:IterTreeTest.test_2_6_detect_broken_links.<locals>.reraise©r^   ZDOES_NOT_EXIST)Zon_errorN)r)   r   rW   ÚOSErrorr<   r   r   r=   rX   ÚerrnoÚENOENT)r   rc   r\   r   r   r   Útest_2_6_detect_broken_links  s   ÿÿz)IterTreeTest.test_2_6_detect_broken_linksc                 C   s4   |   ¡  |  dg¡ tt| jƒƒ}|  |tƒ ¡ dS )z*
		Tests that broken links are ignored.
		rd   N)r)   r   r<   r   r   r=   r?   r   r   r   Útest_2_7_ignore_broken_links)  s   ÿz)IterTreeTest.test_2_7_ignore_broken_linksc                 C   sb   |   ¡  |  dg¡ |  g d¢¡ |  g d¢¡ tt| jddƒ}|  |tt| j	g d¢ƒƒ¡ dS )z8
		Tests to make sure directory links can be ignored.
		r3   )r^   r_   úDir/CúDir/D))rS   r^   )rT   r_   )úDir/Cxrj   )úDir/Dxrk   rM   F)Zfollow_links)	r^   rS   r_   rT   rj   rl   rk   rm   rN   NrO   r?   r   r   r   Útest_2_8_no_follow_links4  s   ÿ z%IterTreeTest.test_2_8_no_follow_linksc                 C   sP   |   g d¢¡ |  g d¢¡ dd„ t| jƒD ƒ}|  |tt| jg d¢ƒƒ¡ dS )r0   r1   r5   c                 S   s   h | ]}|j ’qS r   r!   )Ú.0Úentryr   r   r   Ú	<setcomp>g  s    z.IterTreeTest.test_3_entries.<locals>.<setcomp>)	r6   r7   r3   r8   r9   r4   r:   r;   r2   N)r   r   r   r   r=   r<   r>   r   r?   r   r   r   Útest_3_entriesV  s    zIterTreeTest.test_3_entries)é   é   z$pathlib entered stdlib in Python 3.4c                 C   s0   ddl m} t|dƒƒ}tdƒ}|  ||¡ dS )z-
		Tests passing pathlib.Path as argument.
		r   )ÚPathza.txtN)Úpathlibru   r   r=   )r   ru   Z
first_specZsecond_specr   r   r   Útest_4_normalizing_pathlib_patht  s   z,IterTreeTest.test_4_normalizing_pathlib_pathN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ústaticmethodr   r   r'   r)   r,   r/   rA   rE   rH   rP   rU   r]   ra   rh   ri   rn   rr   r$   ZskipIfÚsysÚversion_inforw   r   r   r   r   r      s4    

%$"r   )r{   rf   r   Úos.pathr-   r}   r*   r$   Zpathspec.utilr   r   r   r   ZTestCaser   r   r   r   r   Ú<module>   s   