
    &e>                        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nM# e $ rE ddl!Z!dZ" e!j#        e"          ej$        e"<   	 ddlZn# e $ r ddl%Zd\  e_&        e_'        Y nw xY wY nw xY wej(        Z) ej*                    Z+ ee+d          Z,d Z-d Z.d Z/d Z0ej1        d             Z2ej3        4                    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j        d!             ZAej        d"             ZB G d# d$ejC                  ZDe5d%             ZEed&             ZF G d' d(ejC                  ZGej3        H                    d)g d*          d+             ZIed,             ZJ G d- d.ejC                  ZKd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)NNhome_test_dirc                  t    t          j        t           j                            t          d                     dS )zJSetup testenvironment for the module:

    - Adds dummy home dir tree
    ipythonN)osmakedirsr   r   HOME_TEST_DIR     =lib/python3.11/site-packages/IPython/utils/tests/test_path.pysetup_moduler   9   s*     K]I6677777r   c                  8    t          j        t                     dS )zOTeardown testenvironment for the module:

    - Remove dummy home dir tree
    N)shutilrmtreeTMP_TEST_DIRr   r   r   teardown_moduler    C   s     M,r   c                      t                                           t          j        t          j        t          j        t          j	        t          j
                    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   setup_environmentr,   N   s6     

BGS\43DgFVXZXaXcXcdHHHr   c                  x   t           \  } t          _        t          _        t
          _        t          _        }t          j	        |           t          t
                     t          t                    D ]}|| vrt          |= t                              |            t          t          d          r	t          `dS 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.   )oldenvold_wdkeys      r   teardown_environmentr6   X   s     T\PVRWclD$5w7GHV
4LLLCyy  fCJJvsH JJJ r   c               #   J   K   t                       d V  t                       d S N)r,   r6   r   r   r   environmentr9   h   s-      	EEEr   r9   c                      t          j                    } dt          _        t	          t          t          d                    t          _        t          j                    }|| k    sJ dS )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   s[    
  ""HCJ tM3LMMNNG ""Hxr   c                      t          j                    } dt          _        t	          t          t          d                                                    t          _	        t          j        d          }|| k    sJ 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;   s     r   test_get_home_dir_2rA      sg    
  ""HCJtM3TUUVV\\^^G &&Hxr   c                      t           t          d<   t          j        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)r=   s    r   test_get_home_dir_3rE      sH      CK &&Hrw''F44444444r   c                  P    dt           v rt           d= t          j        d          } dS )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##DDDr   c                      t          t          dz             t          d<   dt          _        t          j        t          j        t          j	        d           dS )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      sA     -	122CKBG
M$#T%6=====r   c                     dt           _        dD ]} t                              | d            G d d          } t	          j        t          d |                       5  t	          j        t          dt          t                    g          5  t          j
                    }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |t          t                    k    sJ dS )	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dS ) test_get_home_dir_8.<locals>.keyc                     d S r8   r   selfs    r   	__enter__z*test_get_home_dir_8.<locals>.key.__enter__       Dr   c                     d S r8   r   rY   s    r   Closez&test_get_home_dir_8.<locals>.key.Close   r\   r   c                      d S 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      sA        	 	 		 	 		 	 	 	 	r   r5   OpenKey)return_valueQueryValueEx)r   r$   r"   popr   objectwregr   r   r   r'   )r5   r=   s     r   test_get_home_dir_8rl      ss    BGL  T        
dICCEE	:	:	: ' '	dN'-:P:P9Q	R	R	R' '$&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' w}--------s6   0CB."C.B2	2C5B2	6CC	C	c                     t          t                     d t          _        d t          _        dt          _        dt          _        t          	                    dd           t          	                    dd           t          	                    dd           t          j
                    t          j                            d	d
          k    sJ dS )z!test_get_xdg_dir_0, check xdg_dirc                     dS )NTr   r   s    r   <lambda>z$test_get_xdg_dir_0.<locals>.<lambda>   s    d r   c                      dS )N	somewherer   r   r   r   ro   z$test_get_xdg_dir_0.<locals>.<lambda>   s     r   rK   linux2IPYTHON_DIRN
IPYTHONDIRXDG_CONFIG_HOMErq   .config)r   r   _writable_dirr'   r   r$   r%   r&   r"   ri   get_xdg_dirr   r   r   r   test_get_xdg_dir_0ry      s     4LLL**D,,DBGCLGGM4   GGL$GGt$$$k9!E!EEEEEEEr   c                  D   t          t                     d t          _        dt          _        dt
          _        t                              dd           t                              dd           t                              dd           t          j	                    J dS )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   rr   rs   Nrt   ru   )
r   r   r'   r   r$   r%   r&   r"   ri   rx   r   r   r   test_get_xdg_dir_1r~      s     4LLL..DBGCLGGM4   GGL$GGt$$$%%%%%r   c                     t          t                     d t          _        dt          _        dt
          _        t                              dd           t                              dd           t                              dd           t          j        	                    t          j                    d          } t          j        
                    |           st          j        |            t          j                    | k    sJ dS )	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   rr   rs   Nrt   ru   rv   )r   r   r'   r   r$   r%   r&   r"   ri   r   existsr   rx   cfgdirs    r   test_get_xdg_dir_2r      s     4LLL..DBGCLGGM4   GGL$GGt$$$7<<)++Y77F7>>&!! 
F''''''r   c                     t          t                     d t          _        dt          _        dt
          _        t                              dd           t                              dd           t                              dd           t          j        	                    t          j                    d          } t          j
        | d	
           t          j                    J dS )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   win32rs   Nrt   ru   rv   T)exist_ok)r   r   r'   r   r$   r%   r&   r"   ri   r   r   rx   r   s    r   test_get_xdg_dir_3r      s     4LLL..DBGCLGGM4   GGL$GGt$$$7<<)++Y77FK&&&&%%%%%r   c                      t          j                    } t          j                    }t	          j        | j        |          }dS )zVarious tests for filefindN)tempfileNamedTemporaryFiler	   get_ipython_dirr   filefindr$   )falt_dirsts      r   test_filefindr      s5    #%%A$&&Hafh''AAAr   c                     t                      5 } t          j                            t          j        |           d          }t          j        |           t          j                            | d          }t          j        |          }|                                |                                k    sJ 	 d d d            d S # 1 swxY w Y   d S )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    			 
; GLL!8!@!@B]^^	
I W\\&*55
0<<##%%):)::::::
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
;s   B'CCCc                  >    t          j        d          } | dk    sJ d S )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dS )TestRaiseDeprecationc                    t          j                    }dt          _        t                              dd            t                              dd            t                              dd            |t          d<   t          j                            |d          }t          j        |d           	 t          t          j                            |d          d	d
          
                                 t          j        d           n# t          $ r Y nw xY w|                     t          d          5  t!          j                    }d d d            n# 1 swxY w Y   t                              dd            d S )NrK   rs   rt   ru   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0
	LeW--sWEEEKKMMM KJKKKK  	 	 	D	 "";0LMM 	, 	,)++E	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,t$$$$$s%   1AD 
DD3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            n# 1 swxY w Y   t          d          5  t	          j        d          dk    sJ t          j        t          t          j        d           d d d            n# 1 swxY w Y   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            d S # 1 swxY w Y   d S )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  sm   H\	x	 	  7 7#H--9999#E**h666667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 
u		 ? ?#E**e3333gt3X>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? M'4/777
M'4/:::"G	w		 M M#$566'AAAA#$899WDDDDgt35KLLLgt35KLLL	M M M M M M M M M M M M M M M M M Ms7   5A++A/2A/A CCC7A?GG
Gc                  ~    	 t          j        d           dS # t          $ r} t          |            Y d} ~ dS 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  sT    \*****   Bs    
<7<c                       e Zd Zed             Zed             Zeed                         Zd Zd Z	e
d             Zed             ZdS )	TestShellGlobc                    g d| _         g d| _        | j         | j        z   | _        t                      | _        | j        j        }|                                 5  | j        D ]D}t          t          j	        
                    ||          dd                                           E	 d d d            d S # 1 swxY w Y   d S )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%7%7"#5#5#5 2S5MM(**[^^ 	M 	M M MRW\\"e,,cGDDDJJLLLLM	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	Ms   AB55B9<B9c                 8    | j                                          d S r8   r   cleanup)r   s    r   tearDownClasszTestShellGlob.tearDownClasse  s    r   c              #      K   t          j                    }	 t          j        | j        j                   d V  t          j        |           d S # t          j        |           w xY wr8   )r   r*   r/   r   r$   )r   saves     r   r   zTestShellGlob.in_tempdiri  sY       y{{	HS[%&&&EEEHTNNNNNBHTNNNNs   "A A%c                     |                                  5  t          t          j        |                    t          |          k    sJ 	 d d d            d S # 1 swxY w Y   d S r8   )r   sortedr   	shellglobrZ   patternsmatchess      r   check_matchzTestShellGlob.check_matchs  s    __ 	G 	G$.2233vgFFFFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	Gs   5A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  sv    UDN#VT01VdV$$$dn(,(C'D(,(A'B *.'/ 0 Z45	
 		
r   c                     |                                  dgdgfddgdg| j        z   fdgdgfgz   D ]\  }}|                     ||           d S )N\*r   a\*r   a\[012]r   r   r   r   r   s      r   test_match_posixzTestShellGlob.test_match_posix  s    #'#4#4#6#63% $$$*E!EFz*: $ 	0 	0Xw
 Xw////	0 	0r   c                     |                                  ddgdg| j        z   fdgdgfgz   D ]\  }}|                     ||           d S )Nr   r   r   r   r   s      r   test_match_windowsz TestShellGlob.test_match_windows  su    #'#4#4#6#6 $&D,G!GH
|,: $ 	0 	0Xw Xw////	0 	0r   N)rc   rd   re   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   V  s        
M 
M [
M   [   ^ [G G G


 

 

 0 0 Z0 0 0 0 0 0r   r   zglobstr, unescaped_globstr))z
\*\[\!\]\?z*[!]?)z\\*r   )z\\\*r   )z\\a\a)r   r   c                 :    t          j        |           |k    sJ d S r8   )r   unescape_glob)globstrunescaped_globstrs     r   test_unescape_globr     s'     g&&*;;;;;;;r   c                  8   t                      5 } t          j                            | d          }t          j        |           t          j                            |          sJ t          j        |           t          j                            | d          }t          |dd                                           t          j	        t                    5  t          j        |           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )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     sl   			 &GLLW%%q!!!w}}Qq!!!GLLW%%Qg&&&,,...]7## 	& 	&"1%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&& & & & & & & & & & & & & & & & & &s6   CDC7+D7C;	;D>C;	?DD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dS )TestLinkOrCopyc                     t                      | _        |                     d          | _        t	          | j        dd          5 }|                    d           d d d            d S # 1 swxY w Y   d S )Nsrcr   r   r   zHello, world!)r   r   dstr   r   write)rZ   r   s     r   setUpzTestLinkOrCopy.setUp  s    )++88E??$(C'222 	%aGGO$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   A((A,/A,c                 8    | j                                          d S r8   r   rY   s    r   tearDownzTestLinkOrCopy.tearDown  s    r   c                 D    t          j        j        | j        j        g|R  S r8   )r   r   r   r   r$   )rZ   r`   s     r   r   zTestLinkOrCopy.dst  s!    w|DL-55555r   c                     t          j        |          j        t          j        |          j        k    sJ |d|d            d S )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  sG    GAJJ!222278qq!!!< 32222r   c                     t          j        |          j        t          j        |          j        k    sJ |d|d            d S )Nr   z! do not reference the same indoesr   r  s      r   assert_inode_equalz!TestLinkOrCopy.assert_inode_equal  sG    GAJJ!2222;<11aaa@ 32222r   c                    t          |d          5 }t          |d          5 }|                                |                                k    sJ 	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nrb)r   read)rZ   r  r  a_fb_fs        r   assert_content_equalz#TestLinkOrCopy.assert_content_equal  s    !T]] 	0ca 0#xxzzSXXZZ/////0 0 0 0 0 0 0 0 0 0 0 0 0 0 0	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s4   A4-AA4A 	 A4#A 	$A44A8;A8c                     |                      d          }t          j        | j        |           |                     | j        |           d S Ntarget)r   r   link_or_copyr   r  rZ   r   s     r   test_link_successfulz#TestLinkOrCopy.test_link_successful  sF    hhx  $(C(((#.....r   c                 2   |                      d          }t          j        |           t          j        | j        |           |                      dt          j                            | j                            }|                     | j        |           d S )Nsome_dir)r   r   r   r   r  r   basenamer  )rZ   r   expected_dsts      r   test_link_into_dirz!TestLinkOrCopy.test_link_into_dir  sx    hhz""
$(C(((xx
BG,<,<TX,F,FGG,77777r   c                     |                      d          }t          |dd                                           t          j        | j        |           |                     | j        |           d S )Nr  r   r   r   )r   r   r   r   r  r   r  r  s     r   test_target_existsz!TestLinkOrCopy.test_target_exists  sg    hhx  S#(((..000$(C(((#.....r   c                 2   t           j        }	 t           `|                     d          }t          j        | j        |           |                     | j        |           |                     | j        |           |t           _        d S # |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  s    G		 ((8$$Cdh,,,%%dh444''#666BGGGiBGs   A,B Bc                     |                      d          }t          j        | j        |           |                     | j        |           d S r  )r   r   r  r   r  r  s     r   test_windowszTestLinkOrCopy.test_windows  sF    hhx  $(C(((!!$(C00000r   c                 4   |                      d          }t          j        | j        |           t          j        | j        |           |                     | j        |           t          t          j        | j        j	                            ddgk    sJ d S )Nr  r   )
r   r   r  r   r  r   r   listdirr   r$   r  s     r   test_link_twicezTestLinkOrCopy.test_link_twice  s     hhx  $(C((($(C(((#...bj!233448IIII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 6 6= = =
A A A
0 0 0
 / / Z/
 8 8 Z8 / / Z/ 	  	  Z	  1 1 1
J J J J 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   ry   r~   r   r   r   r   r   TestCaser   r   r   r   parametrizer   r   r   r   r   r   <module>r:     sR   % %
 
			  



   % % % % % %       ! ! ! ! ! ! ! !        ' ' ' ' ' '        - - - - - -         
 0 / / / / /      : : : :LLLG+5+G44CK: : : : .:*t(((	::  	jx!!\?33
8 8 8     e e e       ;**=99  
  
   
  	  	   	  5 5  5 $ $ $ > >  > . .  .. F F F 	& 	& 	& ( ( ( & & &( ( ( ; ; ;   
% % % % %8, % % %4 M M M" 
 
 
@0 @0 @0 @0 @0H% @0 @0 @0F    	 	< <	 	< 	& 	& 	&JJ JJ JJ JJ JJX& JJ JJ JJ JJ JJs6   "A' '#B1BB1B+(B1*B++B10B1