o
    Zhy>                     @   s  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mZ ddlm	Z	 ddl
mZmZ ddlmZ ddlZddlmZ ddlZddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ zddlZW n1 e y   ddl!Z!dZ"e!#e"ej$e"< zddlZW n e y   ddl%Zd\e_&e_'Y nw Y nw ej(Z)e* Z+ee+dZ,dd Z-dd Z.ej/dd Z0ej12dZ3ee3dd Z4ee3dd Z5ee3dd Z6e3dd Z7ee3dd Z8ee3d d! Z9e3d"d# Z:e3d$d% Z;e3d&d' Z<e3d(d) Z=d*d+ Z>ejd,d- Z?ejd.d/ Z@eje3d0d1 ZAe3d2d3 ZBed4d5 ZCG d6d7 d7ejDZEej1Fd8g d9d:d; ZGed<d= ZHG d>d? d?ejDZIdS )@zTests for IPython.utils.path.py    N)contextmanager)reload)abspathjoin)patch)TemporaryDirectory)paths)
decorators)onlyif_unicode_pathsskip_if_not_win32
skip_win32)make_tempfilepathwinreg)NNZhome_test_dirc                   C   s   t t jtd dS )zJSetup testenvironment for the module:

    - Adds dummy home dir tree
    ipythonN)osmakedirsr   r   HOME_TEST_DIR r   r   T/var/www/html/lang_env/lib/python3.10/site-packages/IPython/utils/tests/test_path.pysetup_module9   s   r   c                   C   s   t t dS )zOTeardown testenvironment for the module:

    - Remove dummy home dir tree
    N)shutilrmtreeTMP_TEST_DIRr   r   r   r   teardown_moduleC   s   r   c                  c   s    t  tjtjtjtj	t
 fad V  t\} t_t_t_t_	}t| tt tt D ]	}|| vr6t |= q-t |  ttdrCJ d S )Nfrozen)envcopyr   namesysplatformr   get_home_dirIPython__file__getcwdZoldstuffchdirr   listupdatehasattr)ZoldenvZold_wdkeyr   r   r   environmentO   s2   		

r+   c                 C   sB   t  }| jtdddd tttdt_t  }||ksJ dS )z1Testcase for py2exe logic, un-compressed lib
    r   TFZraisingzLib/IPython/__init__.pyN)	r   r"   setattrr    r   r   r   r#   r$   monkeypatchZunfrozenhome_dirr   r   r   test_get_home_dir_1r   s
   r1   c                 C   sH   t  }| jtdddd tttd t_	t d}||ks"J dS )z.Testcase for py2exe logic, compressed lib
    r   TFr,   zLibrary.zip/IPython/__init__.pyN)
r   r"   r-   r    r   r   r   lowerr#   r$   r.   r   r   r   test_get_home_dir_2   s   
r3   c                  C   s.   t td< td} | tjtd ksJ dS )z get_home_dir() uses $HOME if setHOMETN)r   r   r   r"   r   realpath)r0   r   r   r   test_get_home_dir_3   s   
r6   c                  C   s   dt v rt d= td} dS )z.get_home_dir() still works if $HOME is not setr4   FN)r   r   r"   )homer   r   r   test_get_home_dir_4   s   r8   c                 C   s4   t td td< | tdd ttjtj	d dS )z@raise HomeDirError if $HOME is specified, but not a writable dirgarbager4   r   posixTN)
r   r   r   r-   r   pytestraisesr   ZHomeDirErrorr"   r/   r   r   r   test_get_home_dir_5   s   r>   c              	   C   s   |  tdd dD ]}t|d q	G dd d}tjtd| d' tjtd	ttgd t	
 }W d   n1 s=w   Y  W d   n1 sLw   Y  |ttksYJ dS )
z~Using registry hack for 'My Documents', os=='nt'

    HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
    r   nt)r4   Z	HOMESHARE	HOMEDRIVEHOMEPATHUSERPROFILENc                   @   s$   e Zd Zdd Zdd Zdd ZdS )z test_get_home_dir_8.<locals>.keyc                 S      d S Nr   selfr   r   r   	__enter__      z*test_get_home_dir_8.<locals>.key.__enter__c                 S   rC   rD   r   rE   r   r   r   Close   rH   z&test_get_home_dir_8.<locals>.key.Closec                  _   rC   rD   r   )argskwargsr   r   r   __exit__   rH   z)test_get_home_dir_8.<locals>.key.__exit__N)__name__
__module____qualname__rG   rI   rL   r   r   r   r   r*      s    r*   OpenKey)return_valueQueryValueEx)r-   r   r   popr   objectwregr   r   r   r"   )r/   r*   r0   r   r   r   test_get_home_dir_8   s   
 rV   c                 C   s   |  tddd  |  tddd  |  tdd |  tdd	 td
d tdd tdd t tjddks?J dS )z!test_get_xdg_dir_0, check xdg_dirZ_writable_dirc                 S      dS )NTr   r   r   r   r   <lambda>       z$test_get_xdg_dir_0.<locals>.<lambda>r"   c                   S   rW   )N	somewherer   r   r   r   r   rX      rY   r   r:   r!   linux2IPYTHON_DIRN
IPYTHONDIRXDG_CONFIG_HOMErZ   .config)r-   r   r   r    r   rS   get_xdg_dirr   r=   r   r   r   test_get_xdg_dir_0   s   ra   c                 C   sf   |  tddd  |  tdd |  tdd tdd	 td
d	 tdd	 t d	u s1J d	S )z-test_get_xdg_dir_1, check nonexistent xdg_dirr"   c                   S      t S rD   r   r   r   r   r   rX      rY   z$test_get_xdg_dir_1.<locals>.<lambda>r   r:   r!   r[   r\   Nr]   r^   )r-   r   r   r    r   rS   r`   r=   r   r   r   test_get_xdg_dir_1   s   rd   c                 C   s   |  tddd  |  tdd |  tdd tdd	 td
d	 tdd	 tjt d}tj|s=t	| t
 |ksEJ d	S )z6test_get_xdg_dir_2, check xdg_dir default to ~/.configr"   c                   S   rb   rD   rc   r   r   r   r   rX      rY   z$test_get_xdg_dir_2.<locals>.<lambda>r   r:   r!   r[   r\   Nr]   r^   r_   )r-   r   r   r    r   rS   r   r"   existsr   r`   r/   Zcfgdirr   r   r   test_get_xdg_dir_2   s   
rg   c                 C   s   |  tddd  |  tdd |  tdd tdd	 td
d	 tdd	 tjt d}tj|dd t	 d	u sAJ d	S )z?test_get_xdg_dir_3, check xdg_dir not used on non-posix systemsr"   c                   S   rb   rD   rc   r   r   r   r   rX      rY   z$test_get_xdg_dir_3.<locals>.<lambda>r   r?   r!   win32r\   Nr]   r^   r_   T)exist_ok)
r-   r   r   r    r   rS   r   r"   r   r`   rf   r   r   r   test_get_xdg_dir_3   s   rj   c                  C   s"   t  } t }t| j|}dS )zVarious tests for filefindN)tempfileNamedTemporaryFiler   get_ipython_dirr   Zfilefindr   )fZalt_dirstr   r   r   test_filefind   s   rp   c                  C   sv   t  .} tjt| d}t| tj| d}t|}| | ks)J W d    d S 1 s4w   Y  d S )Nzthis is my long path namezTHISIS~1)r   r   r   r   get_long_path_namer   r2   )tmpdirZ	long_pathZ
short_pathZevaluated_pathr   r   r   test_get_long_path_name_win32  s   

"rs   c                  C   s   t d} | dksJ d S )Nz
/usr/local)r   rq   )pr   r   r   test_get_long_path_name  s   
ru   c                  C   s   t  } dt_tdd  tdd  tdd  | td< tj| d}t|d zt	tj|dd	d
d
  W n	 tyC   Y nw td tjtdd t }W d    n1 s_w   Y  tdd  d S )Nr:   r\   r]   r^   r4   z.ipythonim  Z_foo_wutf-8encodingz0I can't create directories that I can't write tozis not a writable location)match)rk   mkdtempr   r   r   rS   r   r   mkdiropencloseIOErrorr;   skipZwarnsUserWarningr   rm   )rr   Zipdirr   r   r   test_not_writable_ipdir  s$    

r   c                  C   s6  t t td tddksJ tddksJ W d    n1 s&w   Y  td tddks9J tttjd W d    n1 sKw   Y  tttjd tttjd d} t| + td| kspJ td| ksyJ tttjd tttjd W d    d S 1 sw   Y  d S )Nzfoo.pyZfoozfoo with spaces.pyzfoo with spacesz"foo with spaces.py"z'foo with spaces.py')	r   r&   r   r   r   get_py_filenamer;   r<   r   )Ztrue_fnr   r   r   test_get_py_filename3  s$   



"r   c               
   C   s@   zt d W dS  ty }  zt|  W Y d} ~ dS d} ~ ww )zWhen a file doesn't exist, the exception raised should be safe to call
    str() on - i.e. in Python 2 it must only have ASCII characters.

    https://github.com/ipython/ipython/issues/875
    u
   fooéè.pyN)r   r   r   str)exr   r   r   test_unicode_in_filenameE  s   r   c                   @   s\   e Zd Zedd Zedd Zeedd Zdd Zd	d
 Z	e
dd Zedd ZdS )TestShellGlobc                 C   s   g d| _ g d| _| j | j | _t | _| jj}|   | jD ]}ttj	
||ddd  q!W d    d S 1 s=w   Y  d S )N)Za0Za1Za2)0bZ1bZ2brv   rw   rx   )filenames_start_with_afilenames_end_with_b	filenamesr   tempdirr   
in_tempdirr}   r   r   r   r~   )clstdfnamer   r   r   
setUpClassU  s   



"zTestShellGlob.setUpClassc                 C      | j   d S rD   r   cleanup)r   r   r   r   tearDownClassb  s   zTestShellGlob.tearDownClassc              	   c   s<    t  }zt | jj d V  W t | d S t | w rD   )r   r%   r&   r   r   )r   saver   r   r   r   f  s   zTestShellGlob.in_tempdirc                 C   sH   |    tt|t|ksJ W d    d S 1 sw   Y  d S rD   )r   sortedr   Z	shellglobrF   patternsmatchesr   r   r   check_matchp  s   
"zTestShellGlob.check_matchc                 C   sJ   dg| j fdg| jfdgdgfg d| j | j | j dg fdg| jfgS )N*a**c)r   r   z*br   a[012])r   r   r   rE   r   r   r   common_casesu  s   




zTestShellGlob.common_casesc                 C   sL   |   dgdgfddgdg| j fdgdgfg D ]
\}}| || qd S )N\*r   a\*r   a\[012]r   r   r   r   r   r   r   r   test_match_posix  s   

zTestShellGlob.test_match_posixc                 C   sB   |   ddgdg| j fdgdgfg D ]
\}}| || qd S )Nr   r   r   r   r   r   r   r   test_match_windows  s   
z TestShellGlob.test_match_windowsN)rM   rN   rO   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   S  s    


r   zglobstr, unescaped_globstr))z
\*\[\!\]\?z*[!]?)z\\*r   )z\\\*r   )z\\a\a)r   r   c                 C   s   t | |ks	J d S rD   )r   Zunescape_glob)ZglobstrZunescaped_globstrr   r   r   test_unescape_glob  s   r   c               	   C   s   t  T} tj| d}t| tj|sJ t| tj| d}t|ddd  t	t
 t| W d    n1 sBw   Y  W d    d S W d    d S 1 sZw   Y  d S )Nu   ∂iru   ƒilerv   rw   rx   )r   r   r   r   Zensure_dir_existsisdirr}   r~   r;   r<   r   )r   drn   r   r   r   test_ensure_dir_exists  s   

"r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	dd Z
e	dd Ze	dd Ze	dd Zedd Zdd ZdS )TestLinkOrCopyc                 C   sT   t  | _| d| _t| jddd}|d W d    d S 1 s#w   Y  d S )Nsrcrv   rw   rx   zHello, world!)r   r   dstr   r}   write)rF   rn   r   r   r   setUp  s
   "zTestLinkOrCopy.setUpc                 C   r   rD   r   rE   r   r   r   tearDown  s   zTestLinkOrCopy.tearDownc                 G   s   t jj| jjg|R  S rD   )r   r   r   r   r   )rF   rJ   r   r   r   r     s   zTestLinkOrCopy.dstc                 C   s,   t |jt |jksJ d||f d S )Nz&%r and %r do reference the same indoesr   statst_inorF   abr   r   r   assert_inode_not_equal     
z%TestLinkOrCopy.assert_inode_not_equalc                 C   s,   t |jt |jksJ d||f d S )Nz*%r and %r do not reference the same indoesr   r   r   r   r   assert_inode_equal  r   z!TestLinkOrCopy.assert_inode_equalc              	   C   s~   t |d0}t |d}| | ksJ W d    n1 s w   Y  W d    d S W d    d S 1 s8w   Y  d S )Nrb)r}   read)rF   r   r   Za_fZb_fr   r   r   assert_content_equal  s   "z#TestLinkOrCopy.assert_content_equalc                 C   *   |  d}t| j| | | j| d S Ntarget)r   r   link_or_copyr   r   rF   r   r   r   r   test_link_successful     
z#TestLinkOrCopy.test_link_successfulc                 C   sJ   |  d}t| t| j| |  dtj| j}| | j| d S )NZsome_dir)r   r   r|   r   r   r   basenamer   )rF   r   Zexpected_dstr   r   r   test_link_into_dir  s
   

z!TestLinkOrCopy.test_link_into_dirc                 C   s<   |  d}t|ddd  t| j| | | j| d S )Nr   rv   rw   rx   )r   r}   r~   r   r   r   r   r   r   r   r   test_target_exists  s   
z!TestLinkOrCopy.test_target_existsc                 C   sT   t j}z"t `| d}t| j| | | j| | | j| W |t _d S |t _w r   )r   linkr   r   r   r   r   r   )rF   Z	real_linkr   r   r   r   test_no_link  s   
zTestLinkOrCopy.test_no_linkc                 C   r   r   )r   r   r   r   r   r   r   r   r   test_windows  r   zTestLinkOrCopy.test_windowsc                 C   sV   |  d}t| j| t| j| | | j| tt| jj	ddgks)J d S )Nr   r   )
r   r   r   r   r   r   r   listdirr   r   r   r   r   r   test_link_twice  s
   
"zTestLinkOrCopy.test_link_twiceN)rM   rN   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s$    




r   )J__doc__r   r   r    rk   Zunittest
contextlibr   	importlibr   Zos.pathr   r   Zunittest.mockr   r;   r   r#   r   ZIPython.testingr	   decZIPython.testing.decoratorsr
   r   r   ZIPython.testing.toolsr   ZIPython.utilsr   r   rU   ImportErrortypesZwr_name
ModuleTypemodules_winregrP   rR   environr   r{   r   r   r   r   Zfixturer+   markZusefixturesZwith_environmentr1   r3   r6   r8   r>   rV   ra   rd   rg   rj   rp   rs   ru   r   r   r   ZTestCaser   Zparametrizer   r   r   r   r   r   r   <module>   s   








	



C


