U
    {±ËhÿV  ã                   @   s¢   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m	Z	 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)ÚStringIO)ÚTestCase)ÚpatchÚMocké   )Ú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-fhl22ezh/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„ Z9dodp„ Z:dqS )rÚ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_options*   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_options1   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_optionsH   sB             ý

 ÿ

z#CmdLineParserTest.test_long_optionsc                 C   sè   t d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¡ |  |j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=falsez--shared=foo.sharedÚhuhuz/test/include/dir1z/test/include/dir2r   z/output/dirz/pre/importr4   zcov.xmlz/gdb/outdirZ
wraparoundFz
foo.shared)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_directivesZshared_utility_qualified_namerA   r   r   r   Útest_options_with_valuesb   s:      ô
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   Ú3r?   )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&   ro   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   rq   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   rs   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   ru   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   Tru   rv   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   ry   rA   r   r   r   Útest_warning_extra1  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_multipleC  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_v2N  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_yesX  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_noa  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_invalidj  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_invalidp  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_valuev  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   rz   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                 C   s,   t dgƒ\}}|  |g ¡ |  |jd¡ d S )Nz--generate-shared=foo/shared.czfoo/shared.c)r   r&   Zshared_c_file_pathrA   r   r   r   Útest_generate_shared  s
    ÿ
z&CmdLineParserTest.test_generate_sharedc                    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dg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 --timestampsz--generate-shared=shared.cz4Source file not allowed when using --generate-sharedz--generate-sharedz1argument --generate-shared: expected one argument)Nr   )r"   rÇ   r   r!   r   Útest_errors  sV    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ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   rn   rp   rr   rt   rw   rx   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Â   Úior   Zunittestr   Zunittest.mockr   r   r%   r   ZCmdLiner   ZUtilsr
   r   r   r   Úexistsr   r   r   r   r   r   r   Ú<module>   s   