o
    ?n0jJ                     @   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ZddlmZ ddl	m
Z
 ddl	mZ ddlmZ ejdd Zejd	d
 ZG dd dejZG dd dejZdd ZG dd dejZG dd dejZG dd dejZG dd deZG dd dejZG dd dejZedkre  dS dS )zTests for yapf.file_resources.    N)BytesIO)errors)file_resources)utilsc               	   c   s.    t  } zd V  W t |  d S t |  w N)osgetcwdchdir)curdir r   d/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/yapftests/file_resources_test.py_restore_working_dir   s
   r   c              
   c   s@    t | d}t| d| zd V  W t| d| d S t| d| w )Nexists)getattrsetattr)moduleZmock_implementationZunmocked_existsr   r   r   _exists_mocked_in_module(   s   
r   c                   @   L   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )GetExcludePatternsForDirc                 C      t  | _d S r   tempfilemkdtemptest_tmpdirselfr   r   r   setUp4      zGetExcludePatternsForDir.setUpc                 C      t | j d S r   shutilrmtreer   r   r   r   r   tearDown7      z!GetExcludePatternsForDir.tearDownc                 C   sr   t j| jd}ddg}t|d}|d| W d    n1 s$w   Y  | tt	| jt| d S )N.yapfignoretemp/**/*.py
temp2/*.pyw
)
r   pathjoinr   open
writelinesassertEqualsortedr   r   r   local_ignore_fileignore_patternsfr   r   r   .test_get_exclude_file_patterns_from_yapfignore:   s   zGGetExcludePatternsForDir.test_get_exclude_file_patterns_from_yapfignorec                 C   s   t j| jd}ddg}t|d}|d| W d    n1 s$w   Y  | tj t	
| j W d    d S 1 sAw   Y  d S )Nr$   r%   z./wrong/syntax/*.pyr'   r(   )r   r)   r*   r   r+   r,   assertRaisesr   	YapfErrorr   r   r/   r   r   r   @test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntaxD   s   "zYGetExcludePatternsForDir.test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntaxc                 C   s   zdd l }W n
 ty   Y d S w tj| jd}ddg}t|d$}|d |d |dd	d
 |D  |d W d    n1 sIw   Y  | 	t
t| jt
| d S )Nr   pyproject.tomlr%   r&   r'   [tool.yapfignore]
zignore_patterns=[z
,c                 S   s   g | ]}d  |qS )z"{}")format).0pr   r   r   
<listcomp>W   s    zZGetExcludePatternsForDir.test_get_exclude_file_patterns_from_pyproject.<locals>.<listcomp>])tomliImportErrorr   r)   r*   r   r+   writer,   r-   r.   r   r   r   r>   r0   r1   r2   r   r   r   -test_get_exclude_file_patterns_from_pyprojectM   s"   

zFGetExcludePatternsForDir.test_get_exclude_file_patterns_from_pyprojectc                 C   sd   zdd l }W n
 ty   Y d S w tj| jd}g }t|d  | t	t
| jt	| d S )Nr   r7   r'   )r>   r?   r   r)   r*   r   r+   closer-   r.   r   r   )r   r>   r0   r1   r   r   r   ?test_get_exclude_file_patterns_from_pyproject_no_ignore_section^   s   zXGetExcludePatternsForDir.test_get_exclude_file_patterns_from_pyproject_no_ignore_sectionc                 C   s   zdd l }W n
 ty   Y d S w tj| jd}g }t|d}|d W d    n1 s0w   Y  | t	t
| jt	| d S )Nr   r7   r'   r8   )r>   r?   r   r)   r*   r   r+   r@   r-   r.   r   r   rA   r   r   r   Btest_get_exclude_file_patterns_from_pyproject_ignore_section_emptyk   s   z[GetExcludePatternsForDir.test_get_exclude_file_patterns_from_pyproject_ignore_section_emptyc                 C   s$   g }|  tt| jt| d S r   )r-   r.   r   r   r   )r   r1   r   r   r   3test_get_exclude_file_patterns_with_no_config_filesy   s
   zLGetExcludePatternsForDir.test_get_exclude_file_patterns_with_no_config_filesN)__name__
__module____qualname__r   r"   r3   r6   rB   rD   rE   rF   r   r   r   r   r   2   s    
	r   c                   @   r   )GetDefaultStyleForDirTestc                 C   r   r   r   r   r   r   r   r      r   zGetDefaultStyleForDirTest.setUpc                 C   r   r   r   r   r   r   r   r"      r#   z"GetDefaultStyleForDirTest.tearDownc                 C   s*   t j| jd}t|}| |d d S )Nfile.pypep8r   r)   r*   r   r   GetDefaultStyleForDirr-   r   Z	test_file
style_namer   r   r   test_no_local_style   s   
z-GetDefaultStyleForDirTest.test_no_local_stylec                 C   s.   t j| jd}tj|dd}| |d d S )NrK   zcustom-default)default_stylerM   rO   r   r   r   "test_no_local_style_custom_default   s
   z<GetDefaultStyleForDirTest.test_no_local_style_custom_defaultc                 C   sh   t j| jd}t|d  t j| jd}| |t| t j| jdd}| |t| d S )N.style.yapfr'   rK   dir1)	r   r)   r*   r   r+   rC   r-   r   rN   )r   
style_fileZtest_filenamer   r   r   test_with_local_style   s   z/GetDefaultStyleForDirTest.test_with_local_stylec                 C   s   t j| jd}t|d  t j| jd}t|}| |d t|d}|	d W d    n1 s7w   Y  | |t| d S )Nz	setup.cfgr'   rU   rL   z[yapf]
)
r   r)   r*   r   r+   rC   r   rN   r-   r@   )r   Zsetup_configtest_dirrP   r2   r   r   r   test_setup_config   s   
z+GetDefaultStyleForDirTest.test_setup_configc                 C   s   zdd l }W n
 ty   Y d S w tj| jd}t|d  tj| jd}t	|}| 
|d t|d}|d W d    n1 sHw   Y  | 
|t	| d S )Nr   r7   r'   rU   rL   z[tool.yapf]
)r>   r?   r   r)   r*   r   r+   rC   r   rN   r-   r@   )r   r>   pyproject_tomlrX   rP   r2   r   r   r   test_pyproject_toml   s    
z-GetDefaultStyleForDirTest.test_pyproject_tomlc                    s   t jt jj}t j|d}t j|dd}t j|d  fdd}ttj j| t|}|  | t|}|  | W d    d S 1 sMw   Y  d S )NrU   Zdir2rT   c                    s   |  kS r   r   )r)   rV   r   r   mock_exists_implementation   s   zVGetDefaultStyleForDirTest.test_local_style_at_root.<locals>.mock_exists_implementation)	r   r)   abspathsepr*   r   r   rN   r-   )r   rootdirZtest_dir_at_rootZtest_dir_under_rootr]   Zdefault_style_at_rootZdefault_style_under_rootr   r\   r   test_local_style_at_root   s"   "z2GetDefaultStyleForDirTest.test_local_style_at_rootN)rG   rH   rI   r   r"   rQ   rS   rW   rY   r[   ra   r   r   r   r   rJ      s    rJ   c                 C   s   | D ]	}t |d  qd S )Na)r+   rC   )	filenamesnamer   r   r   _touch_files   s   re   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )GetCommandLineFilesTestc                 C   s   t  | _t | _d S r   )r   r   r   r   r   old_dirr   r   r   r   r      s   
zGetCommandLineFilesTest.setUpc                 C   s   t | j t| j d S r   )r   r	   rg   r    r!   r   r   r   r   r   r"      s   z GetCommandLineFilesTest.tearDownc                 C   s&   t jt j| j|}t | |S r   )r   r)   normpathr*   r   makedirs)r   rd   fullpathr   r   r   _make_test_dir   s   
z&GetCommandLineFilesTest._make_test_dirc                 C   s   |  d}|  d}tj|d}tj|d}t||g | tj||gdd d||g | tj||gdd d||g d S )Ntest1test2testfile1.pytestfile2.pyF	recursiveexcludeT)rk   r   r)   r*   re   r-   r   GetCommandLineFilesr   tdir1tdir2file1file2r   r   r   test_find_files_not_dirs   s&   



z0GetCommandLineFilesTest.test_find_files_not_dirsc                 C   sZ   |  d}|  d}tj|d}tj|d}t||g | jtjtj	|gdd d d S )Nrl   z	test1/foorn   ro   F)Zcommand_line_file_listrq   rr   )
rk   r   r)   r*   re   r4   r   r5   r   rs   rt   r   r   r   test_nonrecursive_find_in_dir   s   


z5GetCommandLineFilesTest.test_nonrecursive_find_in_dirc                 C   sv   |  d}|  d}|  d}tj|dtj|dtj|dg}t| | ttj| j	gdd dt| d S )	Nrl   test2/testinner/test3/foo/bar/bas/xxxrn   ro   testfile3.pyTrp   
rk   r   r)   r*   re   r-   r.   r   rs   r   r   ru   rv   tdir3filesr   r   r   test_recursive_find_in_dir  s"   



z2GetCommandLineFilesTest.test_recursive_find_in_dirc              	   C   s   |  d}|  d}|  d}tj|dtj|dtj|dg}t| | ttj| j	gddgd	ttj|dtj|dg d S )
Nrl   r{   r|   rn   ro   r}   Tz
*test*3.pyrp   r~   r   r   r   r   'test_recursive_find_in_dir_with_exclude  s(   



z?GetCommandLineFilesTest.test_recursive_find_in_dir_with_excludec              	   C   s   |  d}|  d}|  d}tj|dtj|dtj|dg}t| tj| jgddgd	}| t	|t	tj|dtj|dg d S )
N.test1test_2test.3rn   ro   r}   T*.test1*rp   )
rk   r   r)   r*   re   r   rs   r   r-   r.   r   ru   rv   r   r   actualr   r   r   #test_find_with_excluded_hidden_dirs3  s&   



z;GetCommandLineFilesTest.test_find_with_excluded_hidden_dirsc                 C   s   |  d}|  d}|  d}tj|dtj|dtj|dg}t| t H t| j tj	tj
| jgddgd	}| t|ttjtj
| jtj|dtjtj
| jtj|dg W d
   d
S 1 suw   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 overzealous stripping of "./foo" so that
    it removes too much from "./.foo" .
    r   r   r   rn   ro   r}   Tr   rp   N)rk   r   r)   r*   re   r   r	   r   r   rs   relpathr-   r.   basenamer   r   r   r   ,test_find_with_excluded_hidden_dirs_relativeI  s:   


"zDGetCommandLineFilesTest.test_find_with_excluded_hidden_dirs_relativec                 C   s   |  d}|  d}|  d}tj|dtj|dtj|dg}t| t| j ttj	g ddddgd	}| 
|d
dtjjg ttj	dgdddgd	}| 
|ddtjjg d S )Nrl   r{   r|   rn   ro   r}   )rl   rm   test3Trp   z"test3/foo/bar/bas/xxx/testfile3.py/.r   z./test2/testinner/testfile2.py)rk   r   r)   r*   re   r	   r   r.   r   rs   r-   replacer_   )r   ru   rv   r   r   foundr   r   r   test_find_with_excluded_dirss  s>   



z4GetCommandLineFilesTest.test_find_with_excluded_dirsc                 C   sD   |  tj tjg ddgd W d    d S 1 sw   Y  d S )NFz./z)rr   )r4   r   r5   r   rs   r   r   r   r   #test_find_with_excluded_current_dir  s   "z;GetCommandLineFilesTest.test_find_with_excluded_current_dirN)rG   rH   rI   r   r"   rk   ry   rz   r   r   r   r   r   r   r   r   r   r   rf      s    *#rf   c                   @   D   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )IsPythonFileTestc                 C   r   r   r   r   r   r   r   r     r   zIsPythonFileTest.setUpc                 C   r   r   r   r   r   r   r   r"     r#   zIsPythonFileTest.tearDownc                 C   s$   t j| jd}| t| d S )Nrn   )r   r)   r*   r   
assertTruer   IsPythonFile)r   rw   r   r   r   test_with_py_extension  s   z'IsPythonFileTest.test_with_py_extensionc                 C   sD   t j| jd}| t| t j| jd}| t| d S )N	testfile1ztestfile1.rb)r   r)   r*   r   assertFalser   r   )r   rw   rx   r   r   r   test_empty_without_py_extension  s   z0IsPythonFileTest.test_empty_without_py_extensionc                 C   s   t j| jd}t|d}|d W d    n1 sw   Y  | t| t j| jd}t|d}|d W d    n1 sGw   Y  | t| d S )Nr   r'   z#!/usr/bin/python
ztestfile2.run#! /bin/python2
)	r   r)   r*   r   r+   r@   r   r   r   )r   rw   r2   rx   r   r   r   test_python_shebang  s   z$IsPythonFileTest.test_python_shebangc                 C   s^   t j| jd}tj|ddd}|d W d    n1 s w   Y  | t	| d S )Nr   r'   zlatin-1)modeencodingr   )
r   r)   r*   r   codecsr+   r@   r   r   r   r   rw   r2   r   r   r   test_with_latin_encoding  s
   z)IsPythonFileTest.test_with_latin_encodingc                 C   sb   t j| jd}t|d}|d |d W d    n1 s"w   Y  | t| d S )Nr   r'   r   z# -*- coding: iso-3-14159 -*-
)	r   r)   r*   r   r+   r@   r   r   r   r   r   r   r   test_with_invalid_encoding  s   
z+IsPythonFileTest.test_with_invalid_encodingN)
rG   rH   rI   r   r"   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )IsIgnoredTestc                 C   s,   |  tddg | tddg d S )NZmediamedia/*)r   r   	IsIgnoredr   r   r   r   r   test_root_path  s   zIsIgnoredTest.test_root_pathc                 C   s@   |  tddg |  tddg |  tddg d S )Nzmedia/az*/azmedia/br   z	media/b/cz*/*/c)r   r   r   r   r   r   r   test_sub_path  s   zIsIgnoredTest.test_sub_pathc                 C   s4   |  tddg |  tddtjj g d S )Nz)r   r   r   r   r)   r_   r   r   r   r   test_trailing_slash  s    z!IsIgnoredTest.test_trailing_slashN)rG   rH   rI   r   r   r   r   r   r   r   r     s    r   c                   @   s(   e Zd Zdd Zdd Zedd ZdS )BufferedByteStreamc                 C   s   t  | _d S r   )r   streamr   r   r   r   __init__  s   zBufferedByteStream.__init__c                 C   s   | j  dS )Nutf-8)r   getvaluedecoder   r   r   r   r     r#   zBufferedByteStream.getvaluec                 C   s   | j S r   )r   r   r   r   r   buffer  s   zBufferedByteStream.bufferN)rG   rH   rI   r   r   propertyr   r   r   r   r   r     s
    r   c                   @   s<   e Zd Zedd Zedd Zdd Zdd Zd	d
 ZdS )WriteReformattedCodeTestc                 C   r   r   r   clsr   r   r   
setUpClass  s   z#WriteReformattedCodeTest.setUpClassc                 C   r   r   r   r   r   r   r   tearDownClass  s   z&WriteReformattedCodeTest.tearDownClassc              	   C   s   d}t j| jd<\}}tj||ddd |  t|}| | | W d    n1 s0w   Y  W d    d S W d    d S 1 sHw   Y  d S )Nzfoobar
)dirnameTr   Zin_placer   )	r   ZNamedTempFiler   r   WriteReformattedCodeflushr+   r-   read)r   sr2   fnamef2r   r   r   test_write_to_file  s   
"z+WriteReformattedCodeTest.test_write_to_filec                 C   Z   d}t  }t| tjd |ddd W d    n1 sw   Y  | | | d S )NZfoobarFr   r   r   r   Zstdout_redirectorr   r   r-   r   r   r   r   r   r   r   test_write_to_stdout     z-WriteReformattedCodeTest.test_write_to_stdoutc                 C   r   )Nu-   ﻿# -*- coding: utf-8 -*-
result = "passed"
Fr   r   r   r   r   r   r   test_write_encoded_to_stdout  r   z5WriteReformattedCodeTest.test_write_encoded_to_stdoutN)	rG   rH   rI   classmethodr   r   r   r   r   r   r   r   r   r     s    


r   c                   @   r   )LineEndingTestc                 C   "   ddg}t |}| |d d S )Nspam
r(   r   Z
LineEndingr-   r   linesr   r   r   r   test_line_ending_linefeed
     
z(LineEndingTest.test_line_ending_linefeedc                 C   r   )Nspamr   r   r   r   r    test_line_ending_carriage_return  r   z/LineEndingTest.test_line_ending_carriage_returnc                 C   r   )Nspam
z
r   r   r   r   r   test_line_ending_combo  r   z%LineEndingTest.test_line_ending_comboc                 C   "   g d}t |}| |d d S )N)r   r   r   r   r(   r   r   r   r   r   test_line_ending_weighted     
z(LineEndingTest.test_line_ending_weightedc                 C   s   g }t |}| |d d S )Nr(   r   r   r   r   r   test_line_ending_empty#  s   
z%LineEndingTest.test_line_ending_emptyc                 C   s    dg}t |}| |d d S )NZspamr(   r   r   r   r   r   test_line_ending_no_newline(  s   
z*LineEndingTest.test_line_ending_no_newlinec                 C   r   )N)r   r   r   r   r(   r   r   r   r   r   test_line_ending_tie-  r   z#LineEndingTest.test_line_ending_tieN)
rG   rH   rI   r   r   r   r   r   r   r   r   r   r   r   r     s    
r   __main__)__doc__r   
contextlibr   r    r   Zunittestior   Zyapf.yapflibr   r   Z	yapftestsr   contextmanagerr   r   ZTestCaser   rJ   re   rf   r   r   objectr   r   r   rG   mainr   r   r   r   <module>   s8   

	OZ <+%0