U
    eTU                     @   s   d dl Z d dlZd dlZd dlmZ zd dlmZmZ W n$ ek
r\   d dl	mZmZ Y nX zd dl
m
Z
 W n  ek
r   d dlm
Z
 Y nX ddlmZ ddlmZ dd	lmZmZmZ e jjZd
d ZedeeddG dd deZdS )    N)TestCase)patchMock)StringIO   )Options)parse_command_line   )backup_Optionsrestore_Optionscheck_global_optionsc              
   C   s:   | dt jddt jdddddddd	f	kr2d
S t| S )N
source.pyx	/work/dirmy_working_pathfile.pyx	file1.pyx	file2.pyx	file3.pyxfoo.pyxbar.pyxT)ospathjoinunpatched_exists)r    r   E/tmp/pip-unpacked-wheel-7k3cqui1/Cython/Compiler/Tests/TestCmdLine.pypatched_exists   s    r   zos.path.exists)Zside_effect)newc                   @   s  e Zd Zdd Zdd Zg fddZg f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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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9doS )pCmdLineParserTestc                 C   s   t  | _d S N)r
   _options_backupselfr   r   r   setUp'   s    zCmdLineParserTest.setUpc                 C   s   t | j d S r   )r   r    r!   r   r   r   tearDown*   s    zCmdLineParserTest.tearDownc                 C   s   |  t| j|d d S )N )assertEqualr   r    )r"   
white_listr   r   r   check_default_global_options-   s    z.CmdLineParserTest.check_default_global_optionsc                 C   sP   t t j}t }|j D ].}||kr| jt|||t||d| d qd S )Nzerror in option )msg)r   ZCompilationOptionsdefault_optionsobject__dict__keysr&   getattr)r"   optionsr'   r*   Zno_valuenamer   r   r   check_default_options0   s
    z'CmdLineParserTest.check_default_optionsc                 C   s   t ddddddddddd	g\}}| | | |j | |j | |j | |j | |jd
k | tj	 | tj
 | tj | |jd
 t dddg\}}| | | t|dk | |j | |jd d S )N-Vz-lz-+z-tz-vz-pz-D-az-3   z-fz-2r   r	   r   )r   assertFalse
assertTrueshow_versionuse_listing_filecplus
timestampsverboser   embed_pos_in_docstring
docstringsannotater&   language_levellenr"   r/   sourcesr   r   r   test_short_options7   s@              

  

z$CmdLineParserTest.test_short_optionsc                 C   s   t ddddddddddd	d
g\}}| | | |j | |j | |j | tjd | |j	 | |j
dk | tj | tj | tj | tj | tj t ddg\}}| | | t|dk | |j	 d S )Nz	--versionz--create-listingz--cplus--embed--timestampsz	--verbosez--embed-positionsz--no-docstrings
--annotatez	--lenientmainr4   z--forcer   r	   )r   r5   r6   r7   r8   r9   r&   r   embedr:   r;   r<   r=   r>   Zerror_on_unknown_namesZerror_on_uninitializedr@   rA   r   r   r   test_long_optionsN   sB             

 

z#CmdLineParserTest.test_long_optionsc                 C   s   t ddddddddd	d
dddg\}}| |dg | tjd | |jdddg | |jd | |jd | tjd | tjd | 	tj
 | tjd | 	|j | |jd | |jd d d S )N--embed=huhuz-I/test/include/dir1z --include-dir=/test/include/dir2z--include-dirz/test/include/dir3z--working=/work/dirr   z--output-file=/output/dirz--pre-import=/pre/importz--cleanup=3z--annotate-coverage=cov.xmlz--gdb-outdir=/gdb/outdirz--directive=wraparound=falsehuhuz/test/include/dir1z/test/include/dir2r   z/output/dirz/pre/importr4   zcov.xmlz/gdb/outdirZ
wraparoundF)r   r&   r   rH   Zinclude_pathworking_pathoutput_file
pre_importZgenerate_cleanup_coder6   r>   annotate_coverage_xml	gdb_debug
output_dircompiler_directivesrA   r   r   r   test_options_with_valuesh   s6      
z*CmdLineParserTest.test_options_with_valuesc                 C   s0   t ddg\}}| |dg | tjd d S )NrD   r   rG   r   r&   r   rH   rA   r   r   r   test_embed_before_positional   s    
z.CmdLineParserTest.test_embed_before_positionalc                 C   s2   t dddg\}}| |dg | tjd d S )NrD   rJ   r   rK   rT   rA   r   r   r   test_two_embeds   s     
z!CmdLineParserTest.test_two_embedsc                 C   s2   t dddg\}}| |dg | tjd d S )NrJ   rD   r   rG   rT   rA   r   r   r   test_two_embeds2   s     
z"CmdLineParserTest.test_two_embeds2c                 C   s    t ddg\}}| tj d S )NrJ   r   )r   r5   r   r>   rA   r   r   r   test_no_annotate   s
     
z"CmdLineParserTest.test_no_annotatec                 C   s"   t ddg\}}| tjd d S )Nr3   r   defaultr   r&   r   r>   rA   r   r   r   test_annotate_short   s
    
z%CmdLineParserTest.test_annotate_shortc                 C   s"   t ddg\}}| tjd d S )NrF   r   rY   rZ   rA   r   r   r   test_annotate_long   s
    
z$CmdLineParserTest.test_annotate_longc                 C   s"   t ddg\}}| tjd d S )N--annotate-fullcr   fullcrZ   rA   r   r   r   test_annotate_fullc   s
    
z%CmdLineParserTest.test_annotate_fullcc                 C   s:   t dddg\}}| |jd |   | |dg d S )Nz-wr   r   rL   )r   r&   rL   r(   r1   rA   r   r   r   test_short_w   s     
zCmdLineParserTest.test_short_wc                 C   s:   t dddg\}}| |jd |   | |dg d S )Nz-oZ	my_outputr   rM   )r   r&   rM   r(   r1   rA   r   r   r   test_short_o   s     
zCmdLineParserTest.test_short_oc                 C   s:   t dddg\}}| tjd | dg | | d S )Nz-zZmy_preimportr   rN   )r   r&   r   rN   r(   r1   rA   r   r   r   test_short_z   s     
zCmdLineParserTest.test_short_zc                 C   s8   t ddg\}}| tjd | dg | | d S )Nz--convert-ranger   Tconvert_range)r   r&   r   rc   r(   r1   rA   r   r   r   test_convert_range   s    
z$CmdLineParserTest.test_convert_rangec                 C   s8   t ddg\}}| |jd |   | |dg d S )Nz--line-directivesr   Temit_linenums)r   r&   re   r(   r1   rA   r   r   r   test_line_directives   s    
z&CmdLineParserTest.test_line_directivesc                 C   s8   t ddg\}}| |jd |   | |dg d S )Nz--no-c-in-tracebackr   Fc_line_in_traceback)r   r&   rg   r(   r1   rA   r   r   r   test_no_c_in_traceback   s    
z(CmdLineParserTest.test_no_c_in_tracebackc                 C   sJ   t ddg\}}| |jd | |jtj |   | |ddg d S )N--gdbr   TrP   rQ   r   r&   rP   rQ   r   curdirr(   r1   rA   r   r   r   test_gdb   s    
zCmdLineParserTest.test_gdbc                 C   s8   t ddg\}}| |jd |   | |dg d S )Nz--3strr   Z3strr?   )r   r&   r?   r(   r1   rA   r   r   r   	test_3str   s    
zCmdLineParserTest.test_3strc                 C   s8   t ddg\}}| |jd |   | |dg d S )Nz--capi-reexport-cincludesr   Tcapi_reexport_cincludes)r   r&   rn   r(   r1   rA   r   r   r   test_capi_reexport_cincludes   s    
z.CmdLineParserTest.test_capi_reexport_cincludesc                 C   s8   t ddg\}}| tjd | dg | | d S )Nz--fast-failr   T	fast_fail)r   r&   r   rp   r(   r1   rA   r   r   r   test_fast_fail  s    
z CmdLineParserTest.test_fast_failc                 C   s8   t ddg\}}| tjd | dg | | d S )Nz--cimport-from-pyxr   Tcimport_from_pyx)r   r&   r   rr   r(   r1   rA   r   r   r   test_cimport_from_pyx  s    
z'CmdLineParserTest.test_cimport_from_pyxc                 C   s8   t ddg\}}| tjd | dg | | d S )Nz-Werrorr   Twarning_errorsr   r&   r   rt   r(   r1   rA   r   r   r   test_Werror  s    
zCmdLineParserTest.test_Werrorc                 C   s8   t ddg\}}| tjd | dg | | d S )Nz--warning-errorsr   Trt   ru   rA   r   r   r   test_warning_errors#  s    
z%CmdLineParserTest.test_warning_errorsc                 C   s:   t ddg\}}| |jtj |   | |dg d S )Nz-Wextrar   rR   r   r&   rR   r   extra_warningsr(   r1   rA   r   r   r   test_Wextra,  s    
zCmdLineParserTest.test_Wextrac                 C   s:   t ddg\}}| |jtj |   | |dg d S )N--warning-extrar   rR   rx   rA   r   r   r   test_warning_extra5  s    
z$CmdLineParserTest.test_warning_extrac                 C   s8   t ddg\}}| tjd | dg | | d S )Nz--old-style-globalsr   Told_style_globals)r   r&   r   r}   r(   r1   rA   r   r   r   test_old_style_globals>  s    
z(CmdLineParserTest.test_old_style_globalsc                 C   sT   t dddddg\}}| |jd d | |jd d |   | |d	g d S )
N-Xcdivision=Truec_string_type=bytesr   	cdivisionTc_string_typebytesrR   r   r&   rR   r(   r1   r"   r/   sourcer   r   r   test_directive_multipleG  s      
z)CmdLineParserTest.test_directive_multiplec                 C   sP   t dddg\}}| |jd d | |jd d |   | |dg d S )	Nr   z"cdivision=True,c_string_type=bytesr   r   Tr   r   rR   r   r   r   r   r   test_directive_multiple_v2R  s     
z,CmdLineParserTest.test_directive_multiple_v2c                 C   s>   t dddg\}}| |jd d |   | |dg d S )Nr   zcdivision=YeSr   r   TrR   r   r   r   r   r   test_directive_value_yes\  s     
z*CmdLineParserTest.test_directive_value_yesc                 C   s>   t dddg\}}| |jd d |   | |dg d S )Nr   zcdivision=nor   r   FrR   r   r   r   r   r   test_directive_value_noe  s     
z)CmdLineParserTest.test_directive_value_noc                 C   s   |  ttdddg d S )Nr   zcdivision=sadfasdr   assertRaises
ValueErrorr   r!   r   r   r   test_directive_value_invalidn  s
     z.CmdLineParserTest.test_directive_value_invalidc                 C   s   |  ttdddg d S )Nr   Zabracadabrar   r   r!   r   r   r   test_directive_key_invalidt  s
     z,CmdLineParserTest.test_directive_key_invalidc                 C   s   |  ttdddg d S )Nr   r   r   r   r!   r   r   r   test_directive_no_valuez  s
     z)CmdLineParserTest.test_directive_no_valuec                 C   s>   t dddg\}}| |jd d |   | |dg d S )N-E	MYSIZE=10r   MYSIZE
   compile_time_envr   r&   r   r(   r1   r   r   r   r   test_compile_time_env_short  s     
z-CmdLineParserTest.test_compile_time_env_shortc                 C   s>   t dddg\}}| |jd d |   | |dg d S )Nz--compile-time-envr   r   r   r   r   r   r   r   r   r   test_compile_time_env_long  s     
z,CmdLineParserTest.test_compile_time_env_longc                 C   sT   t dddddg\}}| |jd d | |jd d |   | |d	g d S )
Nr   r   z
ARRSIZE=11r   r   r   ARRSIZE   r   r   r   r   r   r   test_compile_time_env_multiple  s       
z0CmdLineParserTest.test_compile_time_env_multiplec                 C   sP   t dddg\}}| |jd d | |jd d |   | |dg d S )	Nr   zMYSIZE=10,ARRSIZE=11r   r   r   r   r   r   r   r   r   r   r   !test_compile_time_env_multiple_v2  s     
z3CmdLineParserTest.test_compile_time_env_multiple_v2c                 C   s"   t ddg\}}| |dg d S )Nr2   r   r   r&   rA   r   r   r   test_option_first  s    z#CmdLineParserTest.test_option_firstc                 C   s$   t dddg\}}| |dg d S )Nr2   r   r3   r   rA   r   r   r   test_file_inbetween  s    z%CmdLineParserTest.test_file_inbetweenc                 C   s"   t ddg\}}| |dg d S )Nr   r2   r   rA   r   r   r   test_option_trailing  s    z&CmdLineParserTest.test_option_trailingc                 C   s,   t dddddg\}}| |dddg d S )Nr   r2   r   r3   r   r   rA   r   r   r   test_multiple_files  s      
z%CmdLineParserTest.test_multiple_filesc                 C   sN   t dddg\}}ddlm} dD ]&}| t|||dk t||d q"d S )Nz--debug-disposal-codez--debug-coercionr   r   )
DebugFlags)debug_disposal_codeZdebug_temp_allocdebug_coercion)r   r   )r   ZCython.Compilerr   r&   r.   setattr)r"   r/   rB   r   r0   r   r   r   test_debug_flags  s     
z"CmdLineParserTest.test_debug_flagsc                 C   sL   t dddg\}}| |jd | |jtj |   | |ddg d S )N--gdb-outdir=my_dirri   r   TrP   rQ   rj   rA   r   r   r   test_gdb_overwrites_gdb_outdir  s     
z0CmdLineParserTest.test_gdb_overwrites_gdb_outdirc                 C   sJ   t dddg\}}| |jd | |jd |   | |ddg d S )Nri   r   r   TZmy_dirrP   rQ   )r   r&   rP   rQ   r(   r1   rA   r   r   r   test_gdb_first  s     
z CmdLineParserTest.test_gdb_firstc                 C   sJ   t dddg\}}| tjd | tjd | ddg | | d S )Nr]   --annotate-coverage=my.xmlr   Tmy.xmlr>   rO   r   r&   r   r>   rO   r(   r1   rA   r   r   r   #test_coverage_overwrites_annotation  s     
z5CmdLineParserTest.test_coverage_overwrites_annotationc                 C   sJ   t dddg\}}| tjd | tjd | ddg | | d S )Nr   r]   r   r^   r   r>   rO   r   rA   r   r   r   test_coverage_first  s     
z%CmdLineParserTest.test_coverage_firstc                 C   s:   t dddg\}}| tjd | dg | | d S )NrF   r]   r   r^   r>   r   r&   r   r>   r(   r1   rA   r   r   r    test_annotate_first_fullc_second  s     
z2CmdLineParserTest.test_annotate_first_fullc_secondc                 C   s:   t dddg\}}| tjd | dg | | d S )Nr]   rF   r   rY   r>   r   rA   r   r   r   test_annotate_fullc_first  s     
z+CmdLineParserTest.test_annotate_fullc_firstc                 C   sN   t ddddddg\}}| t|jttjd  |   | |dg d S )Nr   r   r{   r   r   r	   rR   )r   r6   r@   rR   r   ry   r(   r1   rA   r   r   r   !test_warning_extra_dont_overwrite  s      
z3CmdLineParserTest.test_warning_extra_dont_overwritec                 C   sh   t dg\}}| |jd  |   | | t dddg\}}| |jd |   | |dg d S )Nr   --module-namefoo.barmodule_name)r   r&   r   r(   r1   rA   r   r   r   test_module_name	  s    

 
z"CmdLineParserTest.test_module_namec                    s   d fdd	}|dgd |dgd |dgd |d	gd
 |dgd |dgd |dgd |ddgd |ddgd |ddgd |ddddgd |ddddgd d S )Nc                    s   t j}t  }t _z ttt|  W 5 |t _X | }d|	 } 
|dd|   
d|kd|  |r 
t||d||f  d S )NzMessage "{}"zusage: z %s does not start with "usage :"z	: error: z%s does not contain ": error :"z%s does not match search "%s")sysstderrr   r   
SystemExitr   listgetvalueformatstripr6   
startswithresearch)argsregexZ
old_stderrr   r)   err_msgr!   r   r   error  s&    
z,CmdLineParserTest.test_errors.<locals>.errorz-1zunknown option -1z-Iz0argument -I/--include-dir: expected one argumentz--version=-az5argument -V/--version: ignored explicit argument '-a'z--version=--annotate=truezBargument -V/--version: ignored explicit argument '--annotate=true'z	--workingz,argument -w/--working: expected one argumentz--verbose=1z4argument -v/--verbose: ignored explicit argument '1'z	--cleanupz)argument --cleanup: expected one argumentz --debug-disposal-code-wrong-namer   z/unknown option --debug-disposal-code-wrong-namer   r   zNeed at least one source filer   r   z5Only one source file allowed when using --module-namerE   z*Cannot use --module-name with --timestamps)Nr   )r"   r   r   r!   r   test_errors  sJ    zCmdLineParserTest.test_errorsN):__name__
__module____qualname__r#   r$   r(   r1   rC   rI   rS   rU   rV   rW   rX   r[   r\   r_   r`   ra   rb   rd   rf   rh   rl   rm   ro   rq   rs   rv   rw   rz   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   %   sn   						
									
				






		r   )r   r   r   Zunittestr   Zunittest.mockr   r   ImportErrorZmockr   ior%   r   ZCmdLiner   ZUtilsr
   r   r   r   existsr   r   r   r   r   r   r   <module>   s$   