a
    
dJ                     @   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r(e  dS )zTests for yapf.file_resources.    N)BytesIO)errors)file_resources)utilsc               	   c   s.   t  } zd V  W t |  nt |  0 d S N)osgetcwdchdir)curdir r   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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| nt| d| 0 d S )Nexists)getattrsetattr)moduleZmock_implementationZunmocked_existsr   r   r   _exists_mocked_in_module(   s
    
r   c                   @   sL   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   s   t  | _d S r   tempfilemkdtemptest_tmpdirselfr   r   r   setUp4   s    zGetExcludePatternsForDir.setUpc                 C   s   t | j d S r   shutilrmtreer   r   r   r   r   tearDown7   s    z!GetExcludePatternsForDir.tearDownc                 C   sr   t j| jd}ddg}t|d }|d| W d    n1 sH0    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 sH0    Y  | tj t	
| j W d    n1 s0    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 0 tj| jd}ddg}t|dH}|d |d |dd	d
 |D  |d W d    n1 s0    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       zZGetExcludePatternsForDir.test_get_exclude_file_patterns_from_pyproject.<locals>.<listcomp>])tomliImportErrorr   r$   r%   r   r&   writer'   r(   r)   r   r   r   r:   r+   r,   r-   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 0 tj| jd}g }t|d  | t	t
| jt	| d S )Nr   r2   r"   )r:   r;   r   r$   r%   r   r&   closer(   r)   r   r   )r   r:   r+   r,   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 0 tj| jd}g }t|d}|d W d    n1 s`0    Y  | t	t
| jt	| d S )Nr   r2   r"   r3   )r:   r;   r   r$   r%   r   r&   r<   r(   r)   r   r   r=   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   r,   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   r.   r1   r>   r@   rA   rB   r   r   r   r   r   2   s   
	r   c                   @   sL   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 )GetDefaultStyleForDirTestc                 C   s   t  | _d S r   r   r   r   r   r   r      s    zGetDefaultStyleForDirTest.setUpc                 C   s   t | j d S r   r   r   r   r   r   r      s    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 )NrG   zcustom-default)Zdefault_stylerI   rK   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"   rG   dir1)	r   r$   r%   r   r&   r?   r(   r   rJ   )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 sn0    Y  | |t| d S )Nz	setup.cfgr"   rP   rH   z[yapf]
)
r   r$   r%   r   r&   r?   r   rJ   r(   r<   )r   Zsetup_configtest_dirrL   r-   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 0 tj| jd}t|d  tj| jd}t	|}| 
|d t|d}|d W d    n1 s0    Y  | 
|t	| d S )Nr   r2   r"   rP   rH   z[tool.yapf]
)r:   r;   r   r$   r%   r   r&   r?   r   rJ   r(   r<   )r   r:   pyproject_tomlrS   rL   r-   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    n1 s0    Y  d S )NrP   Zdir2rO   c                    s   |  kS r   r   )r$   rQ   r   r   mock_exists_implementation   s    zVGetDefaultStyleForDirTest.test_local_style_at_root.<locals>.mock_exists_implementation)	r   r$   abspathsepr%   r   r   rJ   r(   )r   rootdirZtest_dir_at_rootZtest_dir_under_rootrX   Zdefault_style_at_rootZdefault_style_under_rootr   rW   r   test_local_style_at_root   s     z2GetDefaultStyleForDirTest.test_local_style_at_rootN)rC   rD   rE   r   r   rM   rN   rR   rT   rV   r\   r   r   r   r   rF      s   rF   c                 C   s   | D ]}t |d  qd S )Na)r&   r?   )	filenamesnamer   r   r   _touch_files   s    r`   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	   rb   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   r_   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)rf   r   r$   r%   r`   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 )Nrg   z	test1/foori   rj   F)Zcommand_line_file_listrl   rm   )
rf   r   r$   r%   r`   r/   r   r0   r   rn   ro   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 )	Nrg   test2/testinner/test3/foo/bar/bas/xxxri   rj   testfile3.pyTrk   
rf   r   r$   r%   r`   r(   r)   r   rn   r   r   rp   rq   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 )
Nrg   rv   rw   ri   rj   rx   Tz
*test*3.pyrk   ry   rz   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.3ri   rj   rx   T*.test1*rk   )
rf   r   r$   r%   r`   r   rn   r   r(   r)   r   rp   rq   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  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
   n1 s0    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   ri   rj   rx   Tr   rk   N)rf   r   r$   r%   r`   r   r	   r   r   rn   relpathr(   r)   basenamer   r   r   r   ,test_find_with_excluded_hidden_dirs_relativeI  s8    


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 )Nrg   rv   rw   ri   rj   rx   )rg   rh   test3Trk   z"test3/foo/bar/bas/xxx/testfile3.py/.r   z./test2/testinner/testfile2.py)rf   r   r$   r%   r`   r	   r   r)   r   rn   r(   replacerZ   )r   rp   rq   r{   r|   foundr   r   r   test_find_with_excluded_dirss  s>    



z4GetCommandLineFilesTest.test_find_with_excluded_dirsc                 C   sB   |  tj" tjg ddgd W d    n1 s40    Y  d S )NFz./z)rm   )r/   r   r0   r   rn   r   r   r   r   #test_find_with_excluded_current_dir  s    z;GetCommandLineFilesTest.test_find_with_excluded_current_dirN)rC   rD   rE   r   r   rf   rt   ru   r}   r~   r   r   r   r   r   r   r   r   ra      s   *#ra   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S )IsPythonFileTestc                 C   s   t  | _d S r   r   r   r   r   r   r     s    zIsPythonFileTest.setUpc                 C   s   t | j d S r   r   r   r   r   r   r     s    zIsPythonFileTest.tearDownc                 C   s$   t j| jd}| t| d S )Nri   )r   r$   r%   r   
assertTruer   IsPythonFile)r   rr   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   rr   rs   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:0    Y  | t| t j| jd}t|d}|d W d    n1 s0    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   rr   r-   rs   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@0    Y  | t	| d S )Nr   r"   zlatin-1)modeencodingr   )
r   r$   r%   r   codecsr&   r<   r   r   r   r   rr   r-   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 sD0    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)
rC   rD   rE   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$   rZ   r   r   r   r   test_trailing_slash  s    z!IsIgnoredTest.test_trailing_slashN)rC   rD   rE   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     s    zBufferedByteStream.getvaluec                 C   s   | j S r   )r   r   r   r   r   buffer  s    zBufferedByteStream.bufferN)rC   rD   rE   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   s   t  | _d S r   r   clsr   r   r   
setUpClass  s    z#WriteReformattedCodeTest.setUpClassc                 C   s   t | j d S r   r   r   r   r   r   tearDownClass  s    z&WriteReformattedCodeTest.tearDownClassc              	   C   s   d}t j| jdf\}}tj||ddd |  t| }| | | W d    n1 s`0    Y  W d    n1 s~0    Y  d S )Nzfoobar
)dirnameTr   Zin_placer   )	r   ZNamedTempFiler   r   WriteReformattedCodeflushr&   r(   read)r   sr-   fnamef2r   r   r   test_write_to_file  s    
z+WriteReformattedCodeTest.test_write_to_filec                 C   sZ   d}t  }t|" tjd |ddd W d    n1 s<0    Y  | | | d S )NZfoobarFr   r   r   r   Zstdout_redirectorr   r   r(   r   r   r   r   r   r   r   test_write_to_stdout  s    $z-WriteReformattedCodeTest.test_write_to_stdoutc                 C   sZ   d}t  }t|" tjd |ddd W d    n1 s<0    Y  | | | d S )Nu-   ﻿# -*- coding: utf-8 -*-
result = "passed"
Fr   r   r   r   r   r   r   test_write_encoded_to_stdout  s    $z5WriteReformattedCodeTest.test_write_encoded_to_stdoutN)	rC   rD   rE   classmethodr   r   r   r   r   r   r   r   r   r     s   


r   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S )LineEndingTestc                 C   s"   ddg}t |}| |d d S )Nspam
r#   r   Z
LineEndingr(   r   linesr   r   r   r   test_line_ending_linefeed
  s    
z(LineEndingTest.test_line_ending_linefeedc                 C   s"   ddg}t |}| |d d S )Nspamr   r   r   r   r    test_line_ending_carriage_return  s    
z/LineEndingTest.test_line_ending_carriage_returnc                 C   s"   ddg}t |}| |d d S )Nspam
z
r   r   r   r   r   test_line_ending_combo  s    
z%LineEndingTest.test_line_ending_comboc                 C   s"   g d}t |}| |d d S )N)r   r   r   r   r#   r   r   r   r   r   test_line_ending_weighted  s    
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   s"   g d}t |}| |d d S )N)r   r   r   r   r#   r   r   r   r   r   test_line_ending_tie-  s    
z#LineEndingTest.test_line_ending_tieN)
rC   rD   rE   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   rF   r`   ra   r   r   objectr   r   r   rC   mainr   r   r   r   <module>   s4   

	OZ <+%0
