
    `N`A                        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	 ddl
mZ ej        d             Zej        d             Z G d	 d
ej                  Z G d dej                  Zd Z G d dej                  Z G d dej                  Z G d dej                  Z G d de          Z G d dej                  Z G d dej                  Zedk    r ej                     dS dS )zTests for yapf.file_resources.    N)errors)file_resources)	py3compat)utilsc               #      K   t          j                    } 	 d V  t          j        |            d S # t          j        |            w xY wN)osgetcwdchdir)curdirs    =lib/python3.11/site-packages/yapftests/file_resources_test.py_restore_working_dirr      sK      9;;&	EEEHVBHVs	   1 Ac              #      K   t          | d          }t          | d|           	 d V  t          | d|           d S # t          | d|           w xY w)Nexists)getattrsetattr)modulemock_implementationunmocked_existss      r   _exists_mocked_in_moduler   '   sf      FH--/	&(/000/	EEEFHo.....GFHo....s	   < Ac                   &    e Zd Zd Zd Zd Zd ZdS )GetExcludePatternsForDirc                 6    t          j                    | _        d S r   tempfilemkdtemptest_tmpdirselfs    r   setUpzGetExcludePatternsForDir.setUp3       '))D    c                 8    t          j        | j                   d S r   shutilrmtreer   r   s    r   tearDownz!GetExcludePatternsForDir.tearDown6       
M$"#####r"   c                     t           j                            t           j                            | j        |                    }t          j        |           |S r   r	   pathnormpathjoinr   makedirsr   namefullpaths      r   _make_test_dirz'GetExcludePatternsForDir._make_test_dir9   ?    wT-=t D DEEHKOr"   c                    t           j                            | j        d          }ddg}t	          |d          5 }|                    d                    |                     d d d            n# 1 swxY w Y   |                     t          t          j	        | j                            t          |                     d S )Nz.yapfignoreztemp/**/*.pyz
temp2/*.pyw
)
r	   r+   r-   r   open
writelinesassertEqualsortedr   r   )r   local_ignore_fileignore_patternsfs       r   test_get_exclude_file_patternsz7GetExcludePatternsForDir.test_get_exclude_file_patterns>   s    T%5}EE%|4O		%	% /ll499_--.../ / / / / / / / / / / / / / / 	~6t7GHHII! ! ! ! !s   )A//A36A3N)__name__
__module____qualname__r    r'   r2   r>    r"   r   r   r   1   sP        * * *$ $ $  
! ! ! ! !r"   r   c                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
GetDefaultStyleForDirTestc                 6    t          j                    | _        d S r   r   r   s    r   r    zGetDefaultStyleForDirTest.setUpK   r!   r"   c                 8    t          j        | j                   d S r   r$   r   s    r   r'   z"GetDefaultStyleForDirTest.tearDownN   r(   r"   c                     t           j                            | j        d          }t	          j        |          }|                     |d           d S )Nfile.pypep8r	   r+   r-   r   r   GetDefaultStyleForDirr9   r   	test_file
style_names      r   test_no_local_stylez-GetDefaultStyleForDirTest.test_no_local_styleQ   sF    T-y99I5i@@JZ(((((r"   c                     t           j                            | j        d          }t	          j        |d          }|                     |d           d S )NrH   zcustom-default)default_stylerJ   rL   s      r   "test_no_local_style_custom_defaultz<GetDefaultStyleForDirTest.test_no_local_style_custom_defaultV   sT    T-y99I5!13 3 3JZ!122222r"   c                    t           j                            | j        d          }t	          |d                                           t           j                            | j        d          }|                     |t          j        |                     t           j                            | j        dd          }|                     |t          j        |                     d S )N.style.yapfr5   rH   dir1)	r	   r+   r-   r   r7   closer9   r   rK   )r   
style_filetest_filenames      r   test_with_local_stylez/GetDefaultStyleForDirTest.test_with_local_style\   s    d.>>JS!!!GLL!19==MZ#9-HHJ J J GLL!169EEMZ#9-HHJ J J J Jr"   c                    t           j                            | j        d          }t	          |d                                           t           j                            | j        d          }t          j        |          }|                     |d           t	          |d          5 }|	                    d           d d d            n# 1 swxY w Y   |                     |t          j        |                     d S )Nz	setup.cfgr5   rU   rI   z[yapf]
)
r	   r+   r-   r   r7   rV   r   rK   r9   write)r   setup_configtest_dirrN   r=   s        r   test_setup_configz+GetDefaultStyleForDirTest.test_setup_configi   s!   7<< 0+>>Ls!!###w||D,f55H5h??JZ((( 
lC	 	  Aggj              \#9(CCE E E E Es   'C		CCc                 *   	 dd l }n# t          $ r Y d S w xY wt          j                            | j        d          }t          |d                                           t          j                            | j        d          }t          j	        |          }| 
                    |d           t          |d          5 }|                    d           d d d            n# 1 swxY w Y   | 
                    |t          j	        |                     d S )Nr   zpyproject.tomlr5   rU   rI   z[tool.yapf]
)tomlImportErrorr	   r+   r-   r   r7   rV   r   rK   r9   r[   )r   r`   pyproject_tomlr]   rN   r=   s         r   test_pyproject_tomlz-GetDefaultStyleForDirTest.test_pyproject_tomlx   sV   kkkk   ff W\\$"24DEEN##%%%w||D,f55H5h??JZ((( 
nc	"	" aggo              ^#9(CCE E E E Es    
>C  C$'C$c                 H   t           j                            t           j        j                  }t           j                            |d          }t           j                            |dd          }t           j                            |d          fd}t          t          j         j        |          5  t          j        |          }|                     |           t          j        |          }|                     |           d d d            d S # 1 swxY w Y   d S )NrU   dir2rT   c                     | k    S r   rB   )r+   rW   s    r   mock_exists_implementationzVGetDefaultStyleForDirTest.test_local_style_at_root.<locals>.mock_exists_implementation   s    Zr"   )	r	   r+   abspathsepr-   r   r   rK   r9   )r   rootdirtest_dir_at_roottest_dir_under_rootrg   default_style_at_rootdefault_style_under_rootrW   s          @r   test_local_style_at_rootz2GetDefaultStyleForDirTest.test_local_style_at_root   sU   goobgk**Gw||GV44',,w?? g}55J          
"."3"8"<
> 
> = = -B
 
z#8999!/!E
" "
z#;<<<= = = = = = = = = = = = = = = = = =s   5ADDDN)r?   r@   rA   r    r'   rO   rR   rY   r^   rc   ro   rB   r"   r   rD   rD   I   s        * * *$ $ $) ) )
3 3 3J J JE E EE E E(= = = = =r"   rD   c                 T    | D ]$}t          |d                                           %d S )Na)r7   rV   )	filenamesr0   s     r   _touch_filesrs      s8      dsOO r"   c                   P    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )GetCommandLineFilesTestc                 f    t          j                    | _        t          j                    | _        d S r   )r   r   r   r	   r
   old_dirr   s    r   r    zGetCommandLineFilesTest.setUp   s$    '))D9;;DLLLr"   c                 j    t          j        | j                   t          j        | j                   d S r   )r	   r   rw   r%   r&   r   r   s    r   r'   z GetCommandLineFilesTest.tearDown   s-    HT\
M$"#####r"   c                     t           j                            t           j                            | j        |                    }t          j        |           |S r   r*   r/   s      r   r2   z&GetCommandLineFilesTest._make_test_dir   r3   r"   c                    |                      d          }|                      d          }t          j                            |d          }t          j                            |d          }t	          ||g           |                     t          j        ||gdd           ||g           |                     t          j        ||gdd           ||g           d S )Ntest1test2testfile1.pytestfile2.pyF	recursiveexcludeT)r2   r	   r+   r-   rs   r9   r   GetCommandLineFilesr   tdir1tdir2file1file2s        r   test_find_files_not_dirsz0GetCommandLineFilesTest.test_find_files_not_dirs   s    ((E((EGLL//EGLL//E%   *E5>5:37	9 	9 	9;@%.J J J 	*E5>5937	9 	9 	9;@%.J J J J Jr"   c                 Z   |                      d          }|                      d          }t          j                            |d          }t          j                            |d          }t	          ||g           |                     t          j        t          j	        |gdd            d S )Nr{   z	test1/foor}   r~   F)command_line_file_listr   r   )
r2   r	   r+   r-   rs   assertRaisesr   	YapfErrorr   r   r   s        r   test_nonrecursive_find_in_dirz5GetCommandLineFilesTest.test_nonrecursive_find_in_dir   s    ((E,,EGLL//EGLL//E%   * %w      r"   c           	         |                      d          }|                      d          }|                      d          }t          j                            |d          t          j                            |d          t          j                            |d          g}t	          |           |                     t          t          j        | j	        gdd                     t          |                     d S )	Nr{   test2/testinner/test3/foo/bar/bas/xxxr}   r~   testfile3.pyTr   
r2   r	   r+   r-   rs   r9   r:   r   r   r   r   r   r   tdir3filess        r   test_recursive_find_in_dirz2GetCommandLineFilesTest.test_recursive_find_in_dir   s    ((E 233E 788E
UN++
UN++
UN++E
 .0@/A9=7;= = =	> 	> @Fe}}	N N N N Nr"   c           
      t   |                      d          }|                      d          }|                      d          }t          j                            |d          t          j                            |d          t          j                            |d          g}t	          |           |                     t          t          j        | j	        gddg	                    t          t          j                            |d          t          j                            |d          g                     d S )
Nr{   r   r   r}   r~   r   Tz
*test*3.pyr   r   r   s        r   'test_recursive_find_in_dir_with_excludez?GetCommandLineFilesTest.test_recursive_find_in_dir_with_exclude   s    ((E 233E 788E
UN++
UN++
UN++E
 .0@/A9=8D~G G G	H 	H 	GLL//GLL//
 	 	    r"   c           
      x   |                      d          }|                      d          }|                      d          }t          j                            |d          t          j                            |d          t          j                            |d          g}t	          |           t          j        | j        gddg	          }|                     t          |          t          t          j                            |d          t          j                            |d          g                     d S )
N.test1test_2test.3r}   r~   r   T*.test1*r   )
r2   r	   r+   r-   rs   r   r   r   r9   r:   r   r   r   r   r   actuals         r   #test_find_with_excluded_hidden_dirsz;GetCommandLineFilesTest.test_find_with_excluded_hidden_dirs   s   ))E))E))E
UN++
UN++
UN++E
 /1A0B:>9CF F FF 	vGLL//GLL//
 	 	    r"   c                 2   |                      d          }|                      d          }|                      d          }t          j                            |d          t          j                            |d          t          j                            |d          g}t	          |           t                      5  t          j        | j                   t          j	        t          j        
                    | j                  gddg	          }|                     t          |          t          t          j                            t          j        
                    | j                  t          j                            |          d          t          j                            t          j        
                    | j                  t          j                            |          d          g                     d
d
d
           d
S # 1 swxY w Y   d
S )a  Test find with excluded hidden dirs.

    A regression test against a specific case where a hidden directory (one
    beginning with a period) is being excluded, but it is also an immediate
    child of the current directory which has been specified in a relative
    manner.

    At its core, the bug has to do with overzelous stripping of "./foo" so that
    it removes too much from "./.foo" .
    r   r   r   r}   r~   r   Tr   r   N)r2   r	   r+   r-   rs   r   r   r   r   r   relpathr9   r:   basenamer   s         r   ,test_find_with_excluded_hidden_dirs_relativezDGetCommandLineFilesTest.test_find_with_excluded_hidden_dirs_relative  s    ))E))E))E
UN++
UN++
UN++E
  
		  ht   17??4+,,
-,     f
 
..
gll'//$"233RW5E5Ee5L5L " " gll'//$"233RW5E5Ee5L5L " "	  	 	 	                 s   <EHHHc                    |                      d          }|                      d          }|                      d          }t          j                            |d          t          j                            |d          t          j                            |d          g}t	          |           t          j        | j                   t          t          j	        g ddddg	                    }| 
                    |d
                    dt          j        j                  g           t          t          j	        dgdddg	                    }| 
                    |d                    dt          j        j                  g           d S )Nr{   r   r   r}   r~   r   )r{   r|   test3Tr   z"test3/foo/bar/bas/xxx/testfile3.py/.r   z./test2/testinner/testfile2.py)r2   r	   r+   r-   rs   r   r   r:   r   r   r9   replaceri   )r   r   r   r   r   founds         r   test_find_with_excluded_dirsz4GetCommandLineFilesTest.test_find_with_excluded_dirs;  s   ((E 233E 788E
UN++
UN++
UN++E
 HT*+F+F+F59/6/A4-	. 	. 	./ /E 	4<<S"'+NNOQ Q Q *C559/6/64-	. 	. 	./ /E 	088bgkJJKM M M M Mr"   c                     |                      t          j                  5  t          j        g ddg           d d d            d S # 1 swxY w Y   d S )NFz./z)r   )r   r   r   r   r   r   s    r   #test_find_with_excluded_current_dirz;GetCommandLineFilesTest.test_find_with_excluded_current_dir^  s    			6+	,	, E E(UUGDDDDE E E E E E E E E E E E E E E E E Es   AA
A
N)r?   r@   rA   r    r'   r2   r   r   r   r   r   r   r   r   rB   r"   r   ru   ru      s          $ $ $  
J J J   N N N"  *  ,( ( (T!M !M !MFE E E E Er"   ru   c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	IsPythonFileTestc                 6    t          j                    | _        d S r   r   r   s    r   r    zIsPythonFileTest.setUpe  r!   r"   c                 8    t          j        | j                   d S r   r$   r   s    r   r'   zIsPythonFileTest.tearDownh  r(   r"   c                     t           j                            | j        d          }|                     t          j        |                     d S )Nr}   )r	   r+   r-   r   
assertTruer   IsPythonFile)r   r   s     r   test_with_py_extensionz'IsPythonFileTest.test_with_py_extensionk  s=    GLL)>::EOON/6677777r"   c                 6   t           j                            | j        d          }|                     t          j        |                     t           j                            | j        d          }|                     t          j        |                     d S )N	testfile1ztestfile1.rb)r	   r+   r-   r   assertFalser   r   )r   r   r   s      r   test_empty_without_py_extensionz0IsPythonFileTest.test_empty_without_py_extensiono  sv    GLL);77E^077888GLL)>::E^07788888r"   c                 *   t           j                            | j        d          }t	          |d          5 }|                    d           d d d            n# 1 swxY w Y   |                     t          j        |                     t           j                            | j        d          }t	          |d          5 }|                    d           d d d            n# 1 swxY w Y   |                     t          j        |                     d S )Nr   r5   z#!/usr/bin/python
ztestfile2.run#! /bin/python2
)	r	   r+   r-   r   r7   r[   r   r   r   )r   r   r=   r   s       r   test_python_shebangz$IsPythonFileTest.test_python_shebangu  s\   GLL);77E	eS		 &Qgg$%%%& & & & & & & & & & & & & & &OON/66777GLL)?;;E	eS		 $Qgg"###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $OON/6677777s#   AAA?C!!C%(C%c                 &   t           j                            | j        d          }t	          j        |dd          5 }|                    d           d d d            n# 1 swxY w Y   |                     t          j	        |                     d S )Nr   r5   zlatin-1)modeencodingr   )
r	   r+   r-   r   r   open_with_encodingr[   r   r   r   r   r   r=   s      r   test_with_latin_encodingz)IsPythonFileTest.test_with_latin_encoding  s    GLL);77E		%e#		J	J	J $agg"###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $OON/6677777s   AA#&A#c                 B   t           j                            | j        d          }t	          |d          5 }|                    d           |                    d           d d d            n# 1 swxY w Y   |                     t          j        |                     d S )Nr   r5   r   z# -*- coding: iso-3-14159 -*-
)	r	   r+   r-   r   r7   r[   r   r   r   r   s      r   test_with_invalid_encodingz+IsPythonFileTest.test_with_invalid_encoding  s    GLL);77E	eS		 2Qgg"###gg01112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 	^07788888s   +A--A14A1N)
r?   r@   rA   r    r'   r   r   r   r   r   rB   r"   r   r   r   c  s}        * * *$ $ $8 8 89 9 9	8 	8 	88 8 89 9 9 9 9r"   r   c                        e Zd Zd Zd Zd ZdS )IsIgnoredTestc                     |                      t          j        ddg                     |                     t          j        ddg                     d S )Nmediamedia/*)r   r   	IsIgnoredr   r   s    r   test_root_pathzIsIgnoredTest.test_root_path  sL    OON,Wwi@@AAA^-g	{CCDDDDDr"   c                     |                      t          j        ddg                     |                      t          j        ddg                     |                      t          j        ddg                     d S )Nzmedia/az*/azmedia/br   z	media/b/cz*/*/c)r   r   r   r   s    r   test_sub_pathzIsIgnoredTest.test_sub_path  sk    OON,Y@@AAAOON,YDDEEEOON,[7)DDEEEEEr"   c                     |                      t          j        ddg                     |                      t          j        ddt          j        j        z   g                     d S )Nz)r   r   r   r	   r+   ri   r   s    r   test_trailing_slashz!IsIgnoredTest.test_trailing_slash  sT    OON,S3%88999OON,S33D2EFFGGGGGr"   N)r?   r@   rA   r   r   r   rB   r"   r   r   r     sL        E E EF F F
H H H H Hr"   r   c                   0    e Zd Zd Zd Zed             ZdS )BufferedByteStreamc                 6    t          j                    | _        d S r   )r   BytesIOstreamr   s    r   __init__zBufferedByteStream.__init__  s    #%%DKKKr"   c                 Z    | j                                                             d          S )Nutf-8)r   getvaluedecoder   s    r   r   zBufferedByteStream.getvalue  s$    ;!!((111r"   c                     | j         S r   )r   r   s    r   bufferzBufferedByteStream.buffer  s
    ;r"   N)r?   r@   rA   r   r   propertyr   rB   r"   r   r   r     sM        & & &2 2 2   8  r"   r   c                   L    e Zd Zed             Zed             Zd Zd Zd ZdS )WriteReformattedCodeTestc                 6    t          j                    | _        d S r   r   clss    r   
setUpClassz#WriteReformattedCodeTest.setUpClass  s    &((COOOr"   c                 8    t          j        | j                   d S r   r$   r   s    r   tearDownClassz&WriteReformattedCodeTest.tearDownClass  s    
M#/"""""r"   c                 l   d}t          j        | j                  5 \  }}t          j        ||dd           |                                 t          |          5 }|                     |                                |           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 )Nzfoobar
)dirnameTr   in_placer   )	r   NamedTempFiler   r   WriteReformattedCodeflushr7   r9   read)r   sr=   fnamef2s        r   test_write_to_filez+WriteReformattedCodeTest.test_write_to_file  s/   A		T%5	6	6	6 '*1e)
TG5 5 5 5ggiii;; '"A&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' '' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's5   ?B))BB)B	B)B	B))B-0B-c                 <   d}t           j        rt                      nt          j                    }t	          j        |          5  t          j        d |dd           d d d            n# 1 swxY w Y   |                     |	                                |           d S )NfoobarFr   r   
r   PY3r   StringIOr   stdout_redirectorr   r   r9   r   r   r   r   s      r   test_write_to_stdoutz-WriteReformattedCodeTest.test_write_to_stdout  s    A%.]L!!!	8J8L8LF		 	(	( 5 5)
EG5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 	V__&&*****   A))A-0A-c                 <   d}t           j        rt                      nt          j                    }t	          j        |          5  t          j        d |dd           d d d            n# 1 swxY w Y   |                     |	                                |           d S )Nu-   ﻿# -*- coding: utf-8 -*-
result = "passed"
Fr   r   r   r   s      r   test_write_encoded_to_stdoutz5WriteReformattedCodeTest.test_write_encoded_to_stdout  s    <A%.]L!!!	8J8L8LF		 	(	( 5 5)
EG5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 	V__&&*****r   N)	r?   r@   rA   classmethodr   r   r   r   r   rB   r"   r   r   r     su        ) ) ;) # # ;#' ' '+ + ++ + + + +r"   r   c                   &    e Zd Zd Zd Zd Zd ZdS )LineEndingTestc                 b    ddg}t          j        |          }|                     |d           d S )Nspam
r6   r   
LineEndingr9   r   linesr   s      r   test_line_ending_linefeedz(LineEndingTest.test_line_ending_linefeed  8    x E&u--FVT"""""r"   c                 b    ddg}t          j        |          }|                     |d           d S )Nspamr  r  s      r    test_line_ending_carriage_returnz/LineEndingTest.test_line_ending_carriage_return  r  r"   c                 b    ddg}t          j        |          }|                     |d           d S )Nspam
z
r  r  s      r   test_line_ending_comboz%LineEndingTest.test_line_ending_combo  s8    $E&u--FVV$$$$$r"   c                 b    g d}t          j        |          }|                     |d           d S )N)r  r  r	  r  r6   r  r  s      r   test_line_ending_weightedz(LineEndingTest.test_line_ending_weighted  sA      E &u--FVT"""""r"   N)r?   r@   rA   r  r  r  r  rB   r"   r   r   r     sP        # # #
# # #
% % %
# # # # #r"   r   __main__)__doc__
contextlibr	   r%   r   unittestyapf.yapflibr   r   r   	yapftestsr   contextmanagerr   r   TestCaser   rD   rs   ru   r   r   objectr   r   r   r?   mainrB   r"   r   <module>r     s   % $     				          ' ' ' ' ' ' " " " " " "          / / /! ! ! ! !x0 ! ! !0W= W= W= W= W= 1 W= W= W=t  
xE xE xE xE xEh/ xE xE xEv(9 (9 (9 (9 (9x( (9 (9 (9VH H H H HH% H H H 
 
 
 
 
 
 
 
"+ "+ "+ "+ "+x0 "+ "+ "+J# # # # #X& # # #8 z(-///// r"   