o
    fTU                     @   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y-   d dl	mZmZ Y nw zd dl
m
Z
 W n eyE   d dlm
Z
 Y nw 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	v rd
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   ]/var/www/html/mig_web/myenv/lib/python3.10/site-packages/Cython/Compiler/Tests/TestCmdLine.pypatched_exists   s   r   zos.path.exists)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 ]}||vr%| jt|||t||d| d qd S )Nzerror in option )msg)r   CompilationOptionsdefault_optionsobject__dict__keysr'   getattr)r#   optionsr(   r,   no_valuenamer   r   r   check_default_options0   s   "z'CmdLineParserTest.check_default_optionsc                 C   s   t g d\}}| | | |j | |j | |j | |j | |jdk | tj	 | tj
 | tj | |jd t g d\}}| | | t|dk | |j | |jd d S )N)-Vz-lz-+z-t-vr6   r6   z-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#   r1   sourcesr   r   r   test_short_options7   s    

z$CmdLineParserTest.test_short_optionsc                 C   s   t g d\}}| | | |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 )N)z	--versionz--create-listingz--cplus--embed--timestamps	--verboserJ   rJ   z--embed-positionsz--no-docstrings
--annotatez	--lenientmainr8   z--forcer   r	   )r   r9   r:   r;   r<   r=   r'   r   embedr>   r?   r@   rA   rB   error_on_unknown_nameserror_on_uninitializedrD   rE   r   r   r   test_long_optionsN   s&   


z#CmdLineParserTest.test_long_optionsc                 C   s   t g d\}}| |dg | tjd | |jg d | |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-dir/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=falser   huhu)z/test/include/dir1z/test/include/dir2rR   r   z/output/dirz/pre/importr8   zcov.xmlz/gdb/outdir
wraparoundF)r   r'   r   rM   include_pathworking_pathoutput_file
pre_importgenerate_cleanup_coder:   rB   annotate_coverage_xml	gdb_debug
output_dircompiler_directivesrE   r   r   r   test_options_with_valuesh   s   z*CmdLineParserTest.test_options_with_valuesc                 C   s0   t ddg\}}| |dg | tjd d S )NrH   r   rL   r   r'   r   rM   rE   r   r   r   test_embed_before_positional   s   
z.CmdLineParserTest.test_embed_before_positionalc                 C   0   t g d\}}| |dg | tjd d S )N)rH   rQ   r   r   rS   r_   rE   r   r   r   test_two_embeds      z!CmdLineParserTest.test_two_embedsc                 C   ra   )N)rQ   rH   r   r   rL   r_   rE   r   r   r   test_two_embeds2   rc   z"CmdLineParserTest.test_two_embeds2c                 C   s    t ddg\}}| tj d S )NrQ   r   )r   r9   r   rB   rE   r   r   r   test_no_annotate   s   
z"CmdLineParserTest.test_no_annotatec                 C   "   t ddg\}}| tjd d S )Nr7   r   defaultr   r'   r   rB   rE   r   r   r   test_annotate_short   
   
z%CmdLineParserTest.test_annotate_shortc                 C   rf   )NrK   r   rg   rh   rE   r   r   r   test_annotate_long   rj   z$CmdLineParserTest.test_annotate_longc                 C   rf   )N--annotate-fullcr   fullcrh   rE   r   r   r   test_annotate_fullc   rj   z%CmdLineParserTest.test_annotate_fullcc                 C   8   t g d\}}| |jd |   | |dg d S )N)z-wr   r   r   rV   )r   r'   rV   r)   r4   rE   r   r   r   test_short_w      zCmdLineParserTest.test_short_wc                 C   ro   )N)z-o	my_outputr   rr   rW   )r   r'   rW   r)   r4   rE   r   r   r   test_short_o   rq   zCmdLineParserTest.test_short_oc                 C   8   t g d\}}| tjd | dg | | d S )N)z-zmy_preimportr   ru   rX   )r   r'   r   rX   r)   r4   rE   r   r   r   test_short_z      zCmdLineParserTest.test_short_zc                 C   8   t ddg\}}| tjd | dg | | d S )Nz--convert-ranger   Tconvert_range)r   r'   r   ry   r)   r4   rE   r   r   r   test_convert_range      
z$CmdLineParserTest.test_convert_rangec                 C   8   t ddg\}}| |jd |   | |dg d S )Nz--line-directivesr   Temit_linenums)r   r'   r}   r)   r4   rE   r   r   r   test_line_directives      
z&CmdLineParserTest.test_line_directivesc                 C   r|   )Nz--no-c-in-tracebackr   Fc_line_in_traceback)r   r'   r   r)   r4   rE   r   r   r   test_no_c_in_traceback   r   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   Tr[   r\   r   r'   r[   r\   r   curdirr)   r4   rE   r   r   r   test_gdb   s   
zCmdLineParserTest.test_gdbc                 C   r|   )Nz--3strr   3strrC   )r   r'   rC   r)   r4   rE   r   r   r   	test_3str   r   zCmdLineParserTest.test_3strc                 C   r|   )Nz--capi-reexport-cincludesr   Tcapi_reexport_cincludes)r   r'   r   r)   r4   rE   r   r   r   test_capi_reexport_cincludes   r   z.CmdLineParserTest.test_capi_reexport_cincludesc                 C   rx   )Nz--fast-failr   T	fast_fail)r   r'   r   r   r)   r4   rE   r   r   r   test_fast_fail  r{   z CmdLineParserTest.test_fast_failc                 C   rx   )Nz--cimport-from-pyxr   Tcimport_from_pyx)r   r'   r   r   r)   r4   rE   r   r   r   test_cimport_from_pyx  r{   z'CmdLineParserTest.test_cimport_from_pyxc                 C   rx   )Nz-Werrorr   Twarning_errorsr   r'   r   r   r)   r4   rE   r   r   r   test_Werror  r{   zCmdLineParserTest.test_Werrorc                 C   rx   )Nz--warning-errorsr   Tr   r   rE   r   r   r   test_warning_errors#  r{   z%CmdLineParserTest.test_warning_errorsc                 C   :   t ddg\}}| |jtj |   | |dg d S )Nz-Wextrar   r]   r   r'   r]   r   extra_warningsr)   r4   rE   r   r   r   test_Wextra,     
zCmdLineParserTest.test_Wextrac                 C   r   )N--warning-extrar   r]   r   rE   r   r   r   test_warning_extra5  r   z$CmdLineParserTest.test_warning_extrac                 C   rx   )Nz--old-style-globalsr   Told_style_globals)r   r'   r   r   r)   r4   rE   r   r   r   test_old_style_globals>  r{   z(CmdLineParserTest.test_old_style_globalsc                 C   N   t g d\}}| |jd d | |jd d |   | |dg d S )N)-Xcdivision=Truer   c_string_type=bytesr   	cdivisionTc_string_typebytesr]   r   r'   r]   r)   r4   r#   r1   sourcer   r   r   test_directive_multipleG  s
   z)CmdLineParserTest.test_directive_multiplec                 C   r   )N)r   z"cdivision=True,c_string_type=bytesr   r   Tr   r   r]   r   r   r   r   r   test_directive_multiple_v2R  
   z,CmdLineParserTest.test_directive_multiple_v2c                 C   <   t g d\}}| |jd d |   | |dg d S )N)r   zcdivision=YeSr   r   Tr]   r   r   r   r   r   test_directive_value_yes\     z*CmdLineParserTest.test_directive_value_yesc                 C   r   )N)r   zcdivision=nor   r   Fr]   r   r   r   r   r   test_directive_value_noe  r   z)CmdLineParserTest.test_directive_value_noc                 C      |  ttg d d S )N)r   zcdivision=sadfasdr   assertRaises
ValueErrorr   r"   r   r   r   test_directive_value_invalidn     z.CmdLineParserTest.test_directive_value_invalidc                 C   r   )N)r   abracadabrar   r   r"   r   r   r   test_directive_key_invalidt  r   z,CmdLineParserTest.test_directive_key_invalidc                 C   r   )N)r   r   r   r   r"   r   r   r   test_directive_no_valuez  r   z)CmdLineParserTest.test_directive_no_valuec                 C   r   )N)-E	MYSIZE=10r   MYSIZE
   compile_time_envr   r'   r   r)   r4   r   r   r   r   test_compile_time_env_short  r   z-CmdLineParserTest.test_compile_time_env_shortc                 C   r   )N)z--compile-time-envr   r   r   r   r   r   r   r   r   r   test_compile_time_env_long  r   z,CmdLineParserTest.test_compile_time_env_longc                 C   r   )N)r   r   r   z
ARRSIZE=11r   r   r   ARRSIZE   r   r   r   r   r   r   test_compile_time_env_multiple  r   z0CmdLineParserTest.test_compile_time_env_multiplec                 C   r   )N)r   zMYSIZE=10,ARRSIZE=11r   r   r   r   r   r   r   r   r   r   r   !test_compile_time_env_multiple_v2  r   z3CmdLineParserTest.test_compile_time_env_multiple_v2c                 C   s"   t ddg\}}| |dg d S )Nr5   r   r   r'   rE   r   r   r   test_option_first     z#CmdLineParserTest.test_option_firstc                 C   s"   t g d\}}| |dg d S )N)r5   r   r7   r   r   rE   r   r   r   test_file_inbetween  r   z%CmdLineParserTest.test_file_inbetweenc                 C   s"   t ddg\}}| |dg d S )Nr   r5   r   rE   r   r   r   test_option_trailing  r   z&CmdLineParserTest.test_option_trailingc                 C   s$   t g d\}}| |g d d S )N)r   r5   r   r7   r   )r   r   r   r   rE   r   r   r   test_multiple_files  s   z%CmdLineParserTest.test_multiple_filesc                 C   sL   t g d\}}ddlm} dD ]}| t|||dv  t||d qd S )N)z--debug-disposal-codez--debug-coercionr   r   )
DebugFlags)debug_disposal_codedebug_temp_allocdebug_coercion)r   r   )r   Cython.Compilerr   r'   r0   setattr)r#   r1   rF   r   r3   r   r   r   test_debug_flags  s   z"CmdLineParserTest.test_debug_flagsc                 C   sJ   t g d\}}| |jd | |jtj |   | |ddg d S )N)--gdb-outdir=my_dirr   r   Tr[   r\   r   rE   r   r   r   test_gdb_overwrites_gdb_outdir  s
   z0CmdLineParserTest.test_gdb_overwrites_gdb_outdirc                 C   sH   t g d\}}| |jd | |jd |   | |ddg d S )N)r   r   r   Tmy_dirr[   r\   )r   r'   r[   r\   r)   r4   rE   r   r   r   test_gdb_first  s
   z CmdLineParserTest.test_gdb_firstc                 C   H   t g d\}}| tjd | tjd | ddg | | d S )N)rl   --annotate-coverage=my.xmlr   Tmy.xmlrB   rZ   r   r'   r   rB   rZ   r)   r4   rE   r   r   r   #test_coverage_overwrites_annotation  
   z5CmdLineParserTest.test_coverage_overwrites_annotationc                 C   r   )N)r   rl   r   rm   r   rB   rZ   r   rE   r   r   r   test_coverage_first  r   z%CmdLineParserTest.test_coverage_firstc                 C   rt   )N)rK   rl   r   rm   rB   r   r'   r   rB   r)   r4   rE   r   r   r    test_annotate_first_fullc_second  rw   z2CmdLineParserTest.test_annotate_first_fullc_secondc                 C   rt   )N)rl   rK   r   rg   rB   r   rE   r   r   r   test_annotate_fullc_first  rw   z+CmdLineParserTest.test_annotate_fullc_firstc                 C   sF   t g d\}}| t|jttjd  |   | |dg d S )N)r   r   r   r   r   r   r	   r]   )r   r:   rD   r]   r   r   r)   r4   rE   r   r   r   !test_warning_extra_dont_overwrite  s   z3CmdLineParserTest.test_warning_extra_dont_overwritec                 C   sf   t dg\}}| |jd  |   | | t g d\}}| |jd |   | |dg d S )Nr   )--module-namefoo.barr   r   module_name)r   r'   r   r)   r4   rE   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 |g dd |g dd d S )Nc                    s   t j}t  }t _z ttt|  W |t _n|t _w | }d|	 } 
|dd|   
d|v d|  |rN 
t||d||f  d S 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stripr:   
startswithresearch)argsregex
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   r   r   r   z5Only one source file allowed when using --module-name)r   r   rI   r   z*Cannot use --module-name with --timestampsr    r   )r#   r   r   r"   r   test_errors  sJ   zCmdLineParserTest.test_errorsN):__name__
__module____qualname__r$   r%   r)   r4   rG   rP   r^   r`   rb   rd   re   ri   rk   rn   rp   rs   rv   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   r   r   r   r   r   r   r   r   r   r   %   sp    						
									
				






		r   )r   r   r   unittestr   unittest.mockr   r   ImportErrormockr   ior&   r   CmdLiner   Utilsr
   r   r   r   existsr   r   r   r   r   r   r   <module>   s*    