B
    Ç S]l  ã               @   sž   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Zd dlZd dlZd dl	m
Z
 ej ej e¡d¡Zej ed¡ZG dd„ dejƒZedkrše 
¡  dS )é    N)ÚmainÚdatazpy2_test_grammar.pyc               @   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 )ÚTestMainc             C   s
   d | _ d S )N)Útemp_dir)Úself© r   ú(lib/python3.7/lib2to3/tests/test_main.pyÚsetUp   s    zTestMain.setUpc             C   s$   t jjd d …= | jr t | j¡ d S )N)ÚloggingÚrootZhandlersr   ÚshutilZrmtree)r   r   r   r   ÚtearDown   s    zTestMain.tearDownc             C   sJ   t j}t j}t j}|t _|t _|t _zt d|¡S |t _|t _|t _X d S )Nzlib2to3.fixes)ÚsysÚstdinÚstdoutÚstderrr   )r   ÚargsZ
in_captureZout_captureZerr_captureZ
save_stdinZsave_stdoutZsave_stderrr   r   r   Úrun_2to3_capture   s    zTestMain.run_2to3_capturec             C   st   t  d¡}t  ¡ }t d¡|ƒ}t  ¡ }|  dg|||¡}|  |d¡ | ¡  d¡}|  	d|¡ |  	d| ¡ ¡ d S )Nu   print 'nothing'
print u'Ã¼ber'
Úasciiú-r   z-print 'nothing'z9WARNING: couldn't encode <stdin>'s diff for your terminal)
ÚioÚStringIOÚBytesIOÚcodecsÚ	getwriterr   ÚassertEqualÚgetvalueÚdecodeÚassertIn)r   Zinput_streamÚoutZout_encÚerrÚretÚoutputr   r   r   Útest_unencodable_diff,   s    
zTestMain.test_unencodable_diffc          	   C   sê   t  ¡ | _tj | jd¡| _tj | jd¡| _t | j¡ t | j¡ g | _	t
tj | jd¡dƒ ¡  | j	 d¡ t t| j¡ | j	 tj t¡¡ tj | jd¡| _tj | jd¡| _t
| jdƒ}| d¡ W dQ R X | j	 d¡ dS )z5Setup a test source tree and output destination tree.Zpython2_projectZpython3_projectz__init__.pyÚwz
trivial.pyz#print 'I need a simple conversion.'N)ÚtempfileZmkdtempr   ÚosÚpathÚjoinÚpy2_src_dirÚpy3_dest_dirÚmkdirÚsetup_filesÚopenÚcloseÚappendr   ÚcopyÚPY2_TEST_MODULEÚbasenameÚtrivial_py2_fileÚinit_py2_fileÚwrite)r   Ztrivialr   r   r   Úsetup_test_source_trees8   s    
z TestMain.setup_test_source_treesc          
      s"  |   ¡  t ¡ }t ¡ }d‰ |  ddˆ ddd| j| jgt d¡||¡}|  |d¡ | ¡ }|  d	|¡ |  d
| j| jf |¡ |  t	‡ fdd„| j
D ƒƒt	t | j¡ƒ¡ x>| j
D ]4}|  dtj | j|¡tj | j|ˆ  ¡f |¡ q²W t tj¡}|  |d d|¡¡ |  |d d|¡¡ dS )z92to3 a single directory with a new output dir and suffix.ZTESTz-nz--add-suffixz--write-unchanged-filesz
--no-diffsz--output-dirÚ r   z implies -w.z6Output in %r will mirror the input directory %r layoutc             3   s   | ]}|ˆ  V  qd S )Nr   )Ú.0Úname)Úsuffixr   r   ú	<genexpr>\   s    zGTestMain.test_filename_changing_on_output_single_dir.<locals>.<genexpr>zWriting converted %s to %szNo changes to .*/__init__\.pyú/zNo changes to .*/trivial\.pyN)r6   r   r   r   r*   r)   r   r   r   Úsetr,   r&   Úlistdirr'   r(   ÚreÚescapeÚsepZassertRegexÚreplaceZassertNotRegex)r   r   r    r!   r   r9   rA   r   )r:   r   Ú+test_filename_changing_on_output_single_dirK   s4    
 z4TestMain.test_filename_changing_on_output_single_dirc             C   s¢   |   ¡  t ¡ }| j| jg}tdd„ |D ƒƒ}|  ddddd| jg| t d¡t ¡ |¡}|  |d	¡ | 	¡ }|  
d
| j| jf |¡ |  |tt | j¡ƒ¡ dS )z62to3 two files in one directory with a new output dir.c             s   s   | ]}t j |¡V  qd S )N)r&   r'   r2   )r8   r9   r   r   r   r;   m   s    zFTestMain.test_filename_changing_on_output_two_files.<locals>.<genexpr>z-nz-wz--write-unchanged-filesz
--no-diffsz--output-dirr7   r   z6Output in %r will mirror the input directory %r layoutN)r6   r   r   r3   r4   r=   r   r*   r   r   r   r)   r&   r>   )r   r    Z	py2_filesZexpected_filesr!   r   r   r   r   Ú*test_filename_changing_on_output_two_filesh   s    z3TestMain.test_filename_changing_on_output_two_filesc             C   s’   |   ¡  t ¡ }|  dddd| j| jgt d¡t ¡ |¡}|  |d¡ | ¡ }|  d| j| j	f |¡ |  t
tj | j¡gƒt
t | j¡ƒ¡ dS )	z)2to3 a single file with a new output dir.z-nz-wz
--no-diffsz--output-dirr7   r   z6Output in %r will mirror the input directory %r layoutN)r6   r   r   r   r*   r3   r   r   r   r)   r=   r&   r'   r2   r>   )r   r    r!   r   r   r   r   Ú,test_filename_changing_on_output_single_filey   s    z5TestMain.test_filename_changing_on_output_single_fileN)Ú__name__Ú
__module__Ú__qualname__r	   r   r   r#   r6   rC   rD   rE   r   r   r   r   r      s   r   Ú__main__)r   r   r
   r&   r?   r   r   r%   ZunittestZlib2to3r   r'   r(   ÚdirnameÚ__file__ZTEST_DATA_DIRr1   ZTestCaser   rF   r   r   r   r   Ú<module>   s   w