
    f>                     L   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 	 ddlZejP                  Z) ejT                         Z+ ee+d      Z,d Z-d Z.d Z/d Z0ejb                  d        Z2ejf                  ji                  d      Z5ee5d               Z6ee5d               Z7ee5d               Z8e5d        Z9ee5d               Z:ee5d               Z;e5d        Z<e5d        Z=e5d        Z>e5d        Z?d  Z@ej0                  d!        ZAej2                  d"        ZB G d# d$ej                        ZDe5d%        ZEed&        ZF G d' d(ej                        ZGejf                  j                  d)g d*      d+        ZIed,        ZJ G d- d.ej                        ZKy# e $ rM ddl!Z!dZ" e!jF                  e"      ejH                  e"<   	 ddlZn# e $ r ddl%Zd\  e_&        e_'        Y nw xY wY w xY w)/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)NNhome_test_dirc                  r    t        j                  t         j                  j                  t        d             y)zJSetup testenvironment for the module:

    - Adds dummy home dir tree
    ipythonN)osmakedirsr   r   HOME_TEST_DIR     X/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/utils/tests/test_path.pysetup_moduler   9   s     KK]I67r   c                  6    t        j                  t               y)zOTeardown testenvironment for the module:

    - Remove dummy home dir tree
    N)shutilrmtreeTMP_TEST_DIRr   r   r   teardown_moduler    C   s     MM,r   c                      t         j                         t        j                  t        j
                  t        j                  t        j                  t        j                         fay)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   setup_environmentr,   N   s:     
BGGS\\43D3DgFVFVXZXaXaXcdHr   c                  F   t         \  } t        _        t        _        t
        _        t        _        }t        j                  |       t        t
               t        t              D ]  }|| vst        |=  t        j                  |        t        t        d      rt        `yy)zJRestore things that were remembered by the setup_environment function
    frozenN)r+   r   r$   r%   r&   r   r'   r(   r)   chdirr   listr"   updatehasattrr.   )oldenvold_wdkeys      r   teardown_environmentr6   X   sy     T\PVRWclD$5w7GHHV
4LCy fC JJvsHJ r   c               #   <   K   t                d  t                y wN)r,   r6   r   r   r   environmentr9   h   s     	s   r9   c                      t        j                         } dt        _        t	        t        t        d            t        _        t        j                         }|| k(  sJ y)z1Testcase for py2exe logic, un-compressed lib
    TzLib/IPython/__init__.pyN)	r   r'   r%   r.   r   r   r   r(   r)   unfrozenhome_dirs     r   test_get_home_dir_1r>   r   sL    
   "HCJ tM3LMNG  "Hxr   c                      t        j                         } dt        _        t	        t        t        d            j                         t        _	        t        j                  d      }|| k(  sJ y)z.Testcase for py2exe logic, compressed lib
    TzLibrary.zip/IPython/__init__.pyN)
r   r'   r%   r.   r   r   r   lowerr(   r)   r;   s     r   test_get_home_dir_2rA      sU    
   "HCJtM3TUV\\^G  &Hxr   c                      t         t        d<   t        j                  d      } | t        j                  j                  t        d         k(  sJ y)z get_home_dir() uses $HOME if setHOMETN)r   r"   r   r'   r   realpath)r=   s    r   test_get_home_dir_3rE      s>      CK  &Hrww''F4444r   c                  L    dt         v rt         d= t        j                  d      } y)z.get_home_dir() still works if $HOME is not setrC   FN)r"   r   r'   )homes    r   test_get_home_dir_4rH      s!     }#f+U#Dr   c                      t        t        dz         t        d<   dt        _        t        j                  t        j                  t        j                  d       y)z@raise HomeDirError if $HOME is specified, but not a writable dirgarbagerC   posixTN)
r   r   r"   r   r$   pytestraisesr   HomeDirErrorr'   r   r   r   test_get_home_dir_5rO      s:     -	12CKBG
MM$##T%6%6=r   c                     dt         _        dD ]  } t        j                  | d         G d d      } t	        j
                  t        d |              5  t	        j
                  t        dt        t              g      5  t        j                         }ddd       ddd       t        t              k(  sJ y# 1 sw Y   &xY w# 1 sw Y   *xY w)	z~Using registry hack for 'My Documents', os=='nt'

    HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
    nt)rC   	HOMESHARE	HOMEDRIVEHOMEPATHUSERPROFILENc                       e Zd Zd Zd Zd Zy) test_get_home_dir_8.<locals>.keyc                      y r8   r   selfs    r   	__enter__z*test_get_home_dir_8.<locals>.key.__enter__       r   c                      y r8   r   rY   s    r   Closez&test_get_home_dir_8.<locals>.key.Close   r\   r   c                       y r8   r   )argskwargss     r   __exit__z)test_get_home_dir_8.<locals>.key.__exit__   r\   r   N)__name__
__module____qualname__r[   r^   rb   r   r   r   r5   rW      s    			r   r5   OpenKey)return_valueQueryValueEx)r   r$   r"   popr   objectwregr   r   r   r'   )r5   r=   s     r   test_get_home_dir_8rl      s     BGL T  
dICE	: '	dN'-:P9Q	R'$$&' ' w}----' ' ' 's$   +C?B9C9C	>CCc                  p   t        t               d t        _        d t        _        dt        _        dt        _        t        j                  dd       t        j                  dd       t        j                  dd       t        j                         t        j                  j                  d	d
      k(  sJ y)z!test_get_xdg_dir_0, check xdg_dirc                      y)NTr   r   s    r   <lambda>z$test_get_xdg_dir_0.<locals>.<lambda>       r   c                       y)N	somewherer   r   r   r   ro   z$test_get_xdg_dir_0.<locals>.<lambda>   rp   r   rK   linux2IPYTHON_DIRN
IPYTHONDIRXDG_CONFIG_HOMErr   .config)r   r   _writable_dirr'   r   r$   r%   r&   r"   ri   get_xdg_dirr   r   r   r   test_get_xdg_dir_0rz      sz     4L*D,DBGCLGGM4 GGL$GGt$k9!EEEEr   c                     t        t               d t        _        dt        _        dt
        _        t        j                  dd       t        j                  dd       t        j                  dd       t        j                         J y)z-test_get_xdg_dir_1, check nonexistent xdg_dirc                      t         S r8   r   r   r   r   ro   z$test_get_xdg_dir_1.<locals>.<lambda>        r   rK   rs   rt   Nru   rv   )
r   r   r'   r   r$   r%   r&   r"   ri   ry   r   r   r   test_get_xdg_dir_1r      sa     4L.DBGCLGGM4 GGL$GGt$%%%r   c                     t        t               d t        _        dt        _        dt
        _        t        j                  dd       t        j                  dd       t        j                  dd       t        j                  j                  t        j                         d      } t        j                  j                  |       st        j                  |        t        j                         | k(  sJ y)	z6test_get_xdg_dir_2, check xdg_dir default to ~/.configc                      t         S r8   r}   r   r   r   ro   z$test_get_xdg_dir_2.<locals>.<lambda>   r~   r   rK   rs   rt   Nru   rv   rw   )r   r   r'   r   r$   r%   r&   r"   ri   r   existsr   ry   cfgdirs    r   test_get_xdg_dir_2r      s     4L.DBGCLGGM4 GGL$GGt$77<<))+Y7F77>>&!
F'''r   c                     t        t               d t        _        dt        _        dt
        _        t        j                  dd       t        j                  dd       t        j                  dd       t        j                  j                  t        j                         d      } t        j                  | d	
       t        j                         J y)z?test_get_xdg_dir_3, check xdg_dir not used on non-posix systemsc                      t         S r8   r}   r   r   r   ro   z$test_get_xdg_dir_3.<locals>.<lambda>   r~   r   rQ   win32rt   Nru   rv   rw   T)exist_ok)r   r   r'   r   r$   r%   r&   r"   ri   r   r   ry   r   s    r   test_get_xdg_dir_3r      s     4L.DBGCLGGM4 GGL$GGt$77<<))+Y7FKK&%%%r   c                      t        j                         } t        j                         }t	        j
                  | j                  |      }y)zVarious tests for filefindN)tempfileNamedTemporaryFiler	   get_ipython_dirr   filefindr$   )falt_dirsts      r   test_filefindr      s2    ##%A$$&Haffh'Ar   c                     t               5 } t        j                  j                  t        j                  |       d      }t        j
                  |       t        j                  j                  | d      }t        j                  |      }|j                         |j                         k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nzthis is my long path namezTHISIS~1)r   r   r   r   get_long_path_namer   r@   )tmpdir	long_path
short_pathevaluated_paths       r   test_get_long_path_name_win32r     s    		 
; GGLL!8!8!@B]^	
I WW\\&*5
00<##%)::::
; 
; 
;s   B!B66B?c                  <    t        j                  d      } | dk(  sJ y )Nz
/usr/local)r   r   )ps    r   test_get_long_path_namer     s     -Ar   c                   :    e Zd Zej                  ed               Zy)TestRaiseDeprecationc                    t        j                         }dt        _        t        j                  dd        t        j                  dd        t        j                  dd        |t        d<   t        j                  j                  |d      }t        j                  |d       	 t        t        j                  j                  |d      d	d
      j                          t        j                  d       | j                  t        d      5  t!        j"                         }d d d        t        j                  dd        y # t        $ r Y Uw xY w# 1 sw Y   /xY w)NrK   rt   ru   rv   rC   z.ipythonim  _foo_wutf-8encodingz0I can't create directories that I can't write tozis not a writable location)r   mkdtempr   r$   r"   ri   r   r   mkdiropencloserL   skipIOErrorassertWarnsRegexUserWarningr	   r   )rZ   r   ipdirs      r   test_not_writable_ipdirz,TestRaiseDeprecation.test_not_writable_ipdir  s     !!#t$d#!4(FVZ0
	LeW-sWEKKM KKJK"";0LM 	,))+E	,t$  			, 	,s   ":D; E
;	EE
EN)rc   rd   re   decr   with_environmentr   r   r   r   r   r     s     ^^%  %r   r   c                     t        j                  t               t        d      5  t	        j
                  d      dk(  sJ t	        j
                  d      dk(  sJ 	 d d d        t        d      5  t	        j
                  d      dk(  sJ t        j                  t        t        j
                  d       d d d        t        j                  t        t        j
                  d       t        j                  t        t        j
                  d       d} t        |       5  t	        j
                  d      | k(  sJ t	        j
                  d      | k(  sJ t        j                  t        t        j
                  d       t        j                  t        t        j
                  d       d d d        y # 1 sw Y   PxY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nzfoo.pyfoozfoo with spaces.pyzfoo with spacesz"foo with spaces.py"z'foo with spaces.py')	r   r/   r   r   r   get_py_filenamerL   rM   r   )true_fns    r   test_get_py_filenamer   6  sg   HH\	x	  7##H-999##E*h6667 
u	 ?##E*e333gt33X>? MM'4//7
MM'4//:"G	w	 M##$56'AAA##$89WDDDgt335KLgt335KL	M M7 7? ?M Ms%   5F).AF6BG)F36G Gc                  r    	 t        j                  d       y# t        $ r} t        |        Y d} ~ yd} ~ ww xY 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)exs    r   test_unicode_in_filenamer   H  s.    \* Bs    	616c                   r    e Zd Zed        Zed        Zeed               Zd Zd Z	e
d        Zed        Zy)	TestShellGlobc                    g d| _         g d| _        | j                   | j                  z   | _        t               | _        | j                  j
                  }| j                         5  | j                  D ]<  }t        t        j                  j                  ||      dd      j                          > 	 d d d        y # 1 sw Y   y xY w)N)a0a1a2)0b1b2br   r   r   )filenames_start_with_afilenames_end_with_b	filenamesr   tempdirr$   
in_tempdirr   r   r   r   r   )clstdfnames      r   
setUpClasszTestShellGlob.setUpClassX  s    %7"#5 22S5M5MM(*[[^^ 	M MRWW\\"e,cGDJJLM	M 	M 	Ms   &AB<<Cc                 8    | j                   j                          y r8   r   cleanup)r   s    r   tearDownClasszTestShellGlob.tearDownClasse  s    r   c              #      K   t        j                         }	 t        j                  | j                  j                         d  t        j                  |       y # t        j                  |       w xY wwr8   )r   r*   r/   r   r$   )r   saves     r   r   zTestShellGlob.in_tempdiri  sG      yy{	HHS[[%%&HHTNBHHTNs   A5-A A5A22A5c                     | j                         5  t        t        j                  |            t        |      k(  sJ 	 d d d        y # 1 sw Y   y xY wr8   )r   sortedr   	shellglobrZ   patternsmatchess      r   check_matchzTestShellGlob.check_matchs  sE    __ 	G$..23vgFFF	G 	G 	Gs   -AAc                     dg| j                   fdg| j                  fdgdgfg d| j                   | j                  z   | j                  z   dgz   fdg| j                  fgS )N*a**c)r   r   z*br   a[012])r   r   r   rY   s    r   common_caseszTestShellGlob.common_casesx  s    UDNN#VT001VdV$dnn(,(C(C'D(,(A(A'B *.'/ 0 Z445	
 		
r   c                     | j                         dgdgfddgdg| j                  z   fdgdgfgz   D ]  \  }}| j                  ||        y )N\*r   a\*r   a\[012]r   r   r   r   r   s      r   test_match_posixzTestShellGlob.test_match_posix  sl    #'#4#4#63% $$$*E*E!EFz*: $ 	0Xw
 Xw/	0r   c                     | j                         ddgdg| j                  z   fdgdgfgz   D ]  \  }}| j                  ||        y )Nr   r   r   r   r   s      r   test_match_windowsz TestShellGlob.test_match_windows  sb    #'#4#4#6 $&D,G,G!GH
|,: $ 	0Xw Xw/	0r   N)rc   rd   re   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   V  sy    
M 
M     G


 0 0 0 0r   r   zglobstr, unescaped_globstr))z
\*\[\!\]\?z*[!]?)z\\*r   )z\\\*r   )z\\a\a)r   r   c                 8    t        j                  |       |k(  sJ y r8   )r   unescape_glob)globstrunescaped_globstrs     r   test_unescape_globr     s     g&*;;;;r   c                     t               5 } t        j                  j                  | d      }t        j                  |       t        j                  j                  |      sJ t        j                  |       t        j                  j                  | d      }t        |dd      j                          t        j                  t              5  t        j                  |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nu   ∂iru   ƒiler   r   r   )r   r   r   r   ensure_dir_existsisdirr   r   rL   rM   r   )r   dr   s      r   test_ensure_dir_existsr     s    		 &GGLLW%q!ww}}Qq!GGLLW%Qg&,,.]]7# 	&""1%	&& &	& 	&& &s$   CC?C3"C?3C<	8C??Dc                       e Zd Zd Zd Zd Zd Zd Zd Ze	d        Z
e	d        Ze	d	        Ze	d
        Zed        Zd Zy)TestLinkOrCopyc                     t               | _        | j                  d      | _        t	        | j                  dd      5 }|j                  d       d d d        y # 1 sw Y   y xY w)Nsrcr   r   r   zHello, world!)r   r   dstr   r   write)rZ   r   s     r   setUpzTestLinkOrCopy.setUp  sN    )+88E?$((C'2 	%aGGO$	% 	% 	%s   AA"c                 8    | j                   j                          y r8   r   rY   s    r   tearDownzTestLinkOrCopy.tearDown  s    r   c                 j    t        j                  j                  | j                  j                  g| S r8   )r   r   r   r   r$   )rZ   r`   s     r   r   zTestLinkOrCopy.dst  s$    ww||DLL--555r   c                     t        j                  |      j                  t        j                  |      j                  k7  sJ |d|d       y )N and z do reference the same indoesr   statst_inorZ   abs      r   assert_inode_not_equalz%TestLinkOrCopy.assert_inode_not_equal  s;    GGAJ!2!22	=78!<	=2r   c                     t        j                  |      j                  t        j                  |      j                  k(  sJ |d|d       y )Nr   z! do not reference the same indoesr   r  s      r   assert_inode_equalz!TestLinkOrCopy.assert_inode_equal  s=    GGAJ!2!22	A;<a@	A2r   c                     t        |d      5 }t        |d      5 }|j                         |j                         k(  sJ 	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nrb)r   read)rZ   r  r  a_fb_fs        r   assert_content_equalz#TestLinkOrCopy.assert_content_equal  sa    !T] 	0ca 0#xxzSXXZ///0	0 	00 0	0 	0s!   A$AAA	AA%c                     | j                  d      }t        j                  | j                  |       | j	                  | j                  |       y Ntarget)r   r   link_or_copyr   r  rZ   r   s     r   test_link_successfulz#TestLinkOrCopy.test_link_successful  s8    hhx $((C(#.r   c                 :   | j                  d      }t        j                  |       t        j                  | j
                  |       | j                  dt        j                  j                  | j
                              }| j                  | j
                  |       y )Nsome_dir)r   r   r   r   r  r   basenamer  )rZ   r   expected_dsts      r   test_link_into_dirz!TestLinkOrCopy.test_link_into_dir  sf    hhz"
$((C(xx
BGG,<,<TXX,FG,7r   c                     | j                  d      }t        |dd      j                          t        j                  | j
                  |       | j                  | j
                  |       y )Nr  r   r   r   )r   r   r   r   r  r   r  r  s     r   test_target_existsz!TestLinkOrCopy.test_target_exists  sM    hhx S#(..0$((C(#.r   c                 :   t         j                  }	 t         `| j                  d      }t        j                  | j
                  |       | j                  | j
                  |       | j                  | j
                  |       |t         _        y # |t         _        w xY wr  )r   linkr   r   r  r   r  r  )rZ   	real_linkr   s      r   test_no_linkzTestLinkOrCopy.test_no_link  sn    GG		 ((8$Cdhh,%%dhh4''#6BGiBGs   A/B Bc                     | j                  d      }t        j                  | j                  |       | j	                  | j                  |       y r  )r   r   r  r   r  r  s     r   test_windowszTestLinkOrCopy.test_windows  s8    hhx $((C(!!$((C0r   c                 P   | j                  d      }t        j                  | j                  |       t        j                  | j                  |       | j	                  | j                  |       t        t        j                  | j                  j                              ddgk(  sJ y )Nr  r   )
r   r   r  r   r  r   r   listdirr   r$   r  s     r   test_link_twicezTestLinkOrCopy.test_link_twice  sy     hhx $((C($((C(#.bjj!2!2348IIIIr   N)rc   rd   re   r   r   r   r  r  r  r   r  r  r  r  r   r!  r$  r   r   r   r   r     s    %6=
A
0
 / /
 8 8 / / 	  	  1 1
Jr   r   )L__doc__r   r   r%   r   unittest
contextlibr   	importlibr   os.pathr   r   unittest.mockr   rL   r   r(   r	   IPython.testingr
   r   IPython.testing.decoratorsr   r   r   IPython.testing.toolsr   IPython.utilsr   r   rk   ImportErrortypeswr_name
ModuleTypemodules_winregrf   rh   environr"   r   r   r   r   r    r,   r6   fixturer9   markusefixturesr   r>   rA   rE   rH   rO   rl   rz   r   r   r   r   r   r   TestCaser   r   r   r   parametrizer   r   r   r   r   r   <module>r;     s   %
 
  
   %  !   '   - 
 0 :" 	jjx!\?3
8 e    ;;**=9  
   
  	   	  5  5 $ $ >  > .  .. F F 	& 	& ( ( & &( ; ;  
%8,, %4 M M" 
 
@0H%% @0F  	<	< 	& 	&JJX&& JJc  :G+5++G4CKK: : .:*t(	::s6   "G *H#<H H#HH#HH#"H#