U
    puf>                     @   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 nj e k
r6   ddl!Z!dZ"e!#e"ej$e"< zddlZW n* e k
r0   ddl%Zd\e_&e_'Y nX Y nX ej(Z)e* Z+ee+dZ,dd Z-dd Z.dd Z/dd Z0ej1dd Z2ej34dZ5ee5dd Z6ee5dd Z7ee5dd Z8e5d d! Z9ee5d"d# Z:ee5d$d% Z;e5d&d' Z<e5d(d) Z=e5d*d+ Z>e5d,d- Z?d.d/ Z@ejd0d1 ZAejd2d3 ZBG d4d5 d5ejCZDe5d6d7 ZEed8d9 ZFG d:d; d;ejCZGej3Hd<d=d>d?d@dAgdBdC ZIedDdE ZJG dFdG dGejCZKdS )Hz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
    ZipythonN)osmakedirsr   r   HOME_TEST_DIR r   r   A/tmp/pip-unpacked-wheel-bxwx4lqe/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S )aA  Setup testenvironment for some functions that are tested
    in this module. In particular this functions stores attributes
    and other things that we need to stub in some test functions.
    This needs to be done on a function level and not module level because
    each testfunction needs a pristine environment.
    N)envcopyr   namesysplatformr   get_home_dirIPython__file__getcwdoldstuffr   r   r   r   setup_environmentN   s    r%   c                  C   sb   t \} t_t_t_t_}t	| t
t ttD ]}|| kr2t|= q2t|  ttdr^t`dS )zJRestore things that were remembered by the setup_environment function
    frozenN)r$   r   r   r   r   r   r    r!   r"   chdirr   listr   updatehasattrr&   )ZoldenvZold_wdkeyr   r   r   teardown_environmentX   s    


r,   c                   c   s   t   d V  t  d S N)r%   r,   r   r   r   r   environmenth   s    r.   c                  C   s6   t  } dt_tttdt_t  }|| ks2t	dS )z1Testcase for py2exe logic, un-compressed lib
    TzLib/IPython/__init__.pyN)
r   r    r   r&   r   r   r   r!   r"   AssertionErrorZunfrozenhome_dirr   r   r   test_get_home_dir_1r   s
    r2   c                  C   s<   t  } dt_tttd t_	t d}|| ks8t
dS )z.Testcase for py2exe logic, compressed lib
    TzLibrary.zip/IPython/__init__.pyN)r   r    r   r&   r   r   r   lowerr!   r"   r/   r0   r   r   r   test_get_home_dir_2   s
    
r4   c                  C   s.   t td< td} | tjtd ks*tdS )z get_home_dir() uses $HOME if setHOMETN)r   r   r   r    r   realpathr/   )r1   r   r   r   test_get_home_dir_3   s    
r7   c                  C   s   dt krt d= td} dS )z.get_home_dir() still works if $HOME is not setr5   FN)r   r   r    )homer   r   r   test_get_home_dir_4   s     r9   c                   C   s,   t td td< dt_ttjtj	d dS )z@raise HomeDirError if $HOME is specified, but not a writable dirgarbager5   posixTN)
r   r   r   r   r   pytestraisesr   ZHomeDirErrorr    r   r   r   r   test_get_home_dir_5   s    r>   c               
   C   s   dt _dD ]} t| d q
G dd d} tjtd|  d0 tjtdttgd t	
 }W 5 Q R X W 5 Q R X |ttkstdS )	z~Using registry hack for 'My Documents', os=='nt'

    HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
    nt)r5   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   s   d S r-   r   selfr   r   r   	__enter__   s    z*test_get_home_dir_8.<locals>.key.__enter__c                 S   s   d S r-   r   rC   r   r   r   Close   s    z&test_get_home_dir_8.<locals>.key.Closec                  _   s   d S r-   r   )argskwargsr   r   r   __exit__   s    z)test_get_home_dir_8.<locals>.key.__exit__N)__name__
__module____qualname__rE   rF   rI   r   r   r   r   r+      s   r+   OpenKey)Zreturn_valueQueryValueEx)r   r   r   popr   objectwregr   r   r   r    r/   )r+   r1   r   r   r   test_get_home_dir_8   s    rR   c                   C   sj   t t dd t_dd t_dt_dt_t	dd t	dd t	d	d t
 tjd
dksftdS )z!test_get_xdg_dir_0, check xdg_dirc                 S   s   dS )NTr   r   r   r   r   <lambda>       z$test_get_xdg_dir_0.<locals>.<lambda>c                   S   s   dS )N	somewherer   r   r   r   r   rS      rT   r;   linux2IPYTHON_DIRN
IPYTHONDIRXDG_CONFIG_HOMErU   .config)r   r   Z_writable_dirr    r   r   r   r   r   rO   get_xdg_dirr   r/   r   r   r   r   test_get_xdg_dir_0   s    

r\   c                   C   sV   t t dd t_dt_dt_tdd tdd tdd t	 dksRt
dS )	z-test_get_xdg_dir_1, check nonexistent xdg_dirc                   S   s   t S r-   r   r   r   r   r   rS      rT   z$test_get_xdg_dir_1.<locals>.<lambda>r;   rV   rW   NrX   rY   )r   r   r    r   r   r   r   r   rO   r[   r/   r   r   r   r   test_get_xdg_dir_1   s    
r^   c                  C   s~   t t dd t_dt_dt_tdd tdd tdd tj	t d	} tj
| sjt|  t | ksztdS )
z6test_get_xdg_dir_2, check xdg_dir default to ~/.configc                   S   s   t S r-   r]   r   r   r   r   rS      rT   z$test_get_xdg_dir_2.<locals>.<lambda>r;   rV   rW   NrX   rY   rZ   )r   r   r    r   r   r   r   r   rO   r   existsr   r[   r/   Zcfgdirr   r   r   test_get_xdg_dir_2   s    

ra   c                  C   sv   t t dd t_dt_dt_tdd tdd tdd tj	t d	} tj
| d
d t dksrtdS )z?test_get_xdg_dir_3, check xdg_dir not used on non-posix systemsc                   S   s   t S r-   r]   r   r   r   r   rS      rT   z$test_get_xdg_dir_3.<locals>.<lambda>r?   win32rW   NrX   rY   rZ   T)exist_ok)r   r   r    r   r   r   r   r   rO   r   r   r[   r/   r`   r   r   r   test_get_xdg_dir_3   s    
rd   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    rj   c               	   C   s`   t  P} tjt| d}t| tj| d}t|}| | ksRtW 5 Q R X d S )Nzthis is my long path namezTHISIS~1)r   r   r   r   get_long_path_namer   r3   r/   )tmpdirZ	long_pathZ
short_pathZevaluated_pathr   r   r   test_get_long_path_name_win32  s    

rm   c                  C   s   t d} | dkstd S )Nz
/usr/local)r   rk   r/   )pr   r   r   test_get_long_path_name  s    
ro   c                   @   s   e Zd Zejedd ZdS )TestRaiseDeprecationc              	   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k
r   Y nX td | td t }W 5 Q R X tdd  d S )Nr;   rW   rX   rY   r5   z.ipythonim  Z_foo_wutf-8encodingz0I can't create directories that I can't write tozis not a writable location)re   mkdtempr   r   r   rO   r   r   mkdiropencloseIOErrorr<   skipZassertWarnsRegexUserWarningr   rg   )rD   rl   Zipdirr   r   r   test_not_writable_ipdir  s      
z,TestRaiseDeprecation.test_not_writable_ipdirN)rJ   rK   rL   decr   with_environmentr|   r   r   r   r   rp     s   rp   c               	   C   s   t t td* tddks&ttddks8tW 5 Q R X td( tddks^ttt	tjd W 5 Q R X tt	tjd tt	tjd d} t| J td| ksttd| ksttt	tjd tt	tjd W 5 Q R X 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=   ry   )Ztrue_fnr   r   r   test_get_py_filename6  s    



r   c               
   C   s>   zt d W n* tk
r8 }  zt|  W 5 d} ~ X Y nX dS )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   ry   str)exr   r   r   test_unicode_in_filenameH  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   sv   dddg| _ dddg| _| j | j | _t | _| jj}|  . | jD ] }ttj	
||ddd	  qFW 5 Q R X d S )
NZa0Za1Za20bZ1bZ2brq   rr   rs   )filenames_start_with_afilenames_end_with_b	filenamesr   tempdirr   
in_tempdirrw   r   r   r   rx   )clstdfnamer   r   r   
setUpClassX  s    

zTestShellGlob.setUpClassc                 C   s   | j   d S r-   r   cleanup)r   r   r   r   tearDownClasse  s    zTestShellGlob.tearDownClassc              	   c   s2   t  }zt | jj d V  W 5 t | X d S r-   )r   r#   r'   r   r   )r   saver   r   r   r   i  s
    
zTestShellGlob.in_tempdirc              	   C   s2   |     tt|t|ks$tW 5 Q R X d S r-   )r   sortedr   Z	shellglobr/   rD   patternsmatchesr   r   r   check_matchs  s    
zTestShellGlob.check_matchc                 C   sN   dg| j fdg| jfdgdgfddddg| j | j | j dg fdg| jfgS )N*a*z*cz*ba[012])r   r   r   rC   r   r   r   common_casesx  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 ]\}}| || q2d 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)rJ   rK   rL   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   V  s   


r   zglobstr, unescaped_globstr)z
\*\[\!\]\?z*[!]?)z\\*r   )z\\\*r   )z\\a\a)r   r   c                 C   s   t | |kstd S r-   )r   Zunescape_globr/   )ZglobstrZunescaped_globstrr   r   r   test_unescape_glob  s    r   c               
   C   s   t  x} tj| d}t| tj|s0tt| tj| d}t|ddd  t	
t t| W 5 Q R X W 5 Q R X d S )Nu   ∂iru   ƒilerq   rr   rs   )r   r   r   r   Zensure_dir_existsisdirr/   rw   rx   r<   r=   ry   )r   drh   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   s>   t  | _| d| _t| jddd}|d W 5 Q R X d S )Nsrcrq   rr   rs   zHello, world!)r   r   dstr   rw   write)rD   rh   r   r   r   setUp  s    zTestLinkOrCopy.setUpc                 C   s   | j   d S r-   r   rC   r   r   r   tearDown  s    zTestLinkOrCopy.tearDownc                 G   s   t jj| jjf| S r-   )r   r   r   r   r   )rD   rG   r   r   r   r     s    zTestLinkOrCopy.dstc                 C   s,   t |jt |jks(td||f d S )Nz&%r and %r do reference the same indoesr   statst_inor/   rD   abr   r   r   assert_inode_not_equal  s    
z%TestLinkOrCopy.assert_inode_not_equalc                 C   s,   t |jt |jks(td||f d S )Nz*%r and %r do not reference the same indoesr   r   r   r   r   assert_inode_equal  s    
z!TestLinkOrCopy.assert_inode_equalc              
   C   sD   t |d0}t |d}| | ks,tW 5 Q R X W 5 Q R X d S )Nrb)rw   readr/   )rD   r   r   Za_fZb_fr   r   r   assert_content_equal  s    z#TestLinkOrCopy.assert_content_equalc                 C   s*   |  d}t| j| | | j| d S Ntarget)r   r   link_or_copyr   r   rD   r   r   r   r   test_link_successful  s    
z#TestLinkOrCopy.test_link_successfulc                 C   sJ   |  d}t| t| j| |  dtj| j}| | j| d S )NZsome_dir)r   r   rv   r   r   r   basenamer   )rD   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   rq   rr   rs   )r   rw   rx   r   r   r   r   r   r   r   r   test_target_exists  s    
z!TestLinkOrCopy.test_target_existsc                 C   sP   t j}z<t `| d}t| j| | | j| | | j| W 5 |t _X d S r   )r   linkr   r   r   r   r   r   )rD   Z	real_linkr   r   r   r   test_no_link  s    
zTestLinkOrCopy.test_no_linkc                 C   s*   |  d}t| j| | | j| d S r   )r   r   r   r   r   r   r   r   r   test_windows  s    
zTestLinkOrCopy.test_windowsc                 C   sV   |  d}t| j| t| j| | | j| tt| jj	ddgksRt
d S )Nr   r   )r   r   r   r   r   r   r   listdirr   r   r/   r   r   r   r   test_link_twice  s
    
zTestLinkOrCopy.test_link_twiceN)rJ   rK   rL   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s"   




r   )L__doc__r   r   r   re   Zunittest
contextlibr   	importlibr   os.pathr   r   Zunittest.mockr   r<   r   r!   r   ZIPython.testingr	   r}   ZIPython.testing.decoratorsr
   r   r   ZIPython.testing.toolsr   ZIPython.utilsr   r   rQ   ImportErrortypesZwr_name
ModuleTypemodules_winregrM   rN   environr   ru   r   r   r   r   r%   r,   Zfixturer.   markZusefixturesr~   r2   r4   r7   r9   r>   rR   r\   r^   ra   rd   rj   rm   ro   ZTestCaserp   r   r   r   Zparametrizer   r   r   r   r   r   r   <module>   s   








	



C


