U
    k&p[:.                     @   s   d dl mZ zd dlmZ eZW n$ ek
rD   d dlmZmZ Y nX d dlZd dlZd dl	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jZG dd dejZdS )    )unicode_literals)StringIO)r   
TextIOBaseNc                 C   s   d S N )msgr   r   2/usr/lib/python3/dist-packages/cliapp/app_tests.pydevnull   s    r	   c                   @   s   e Zd Zdd Zdd ZdS )AppExceptionTestsc                 C   s   t d| _d S Nfoo)cliappAppExceptioneselfr   r   r   setUp%   s    zAppExceptionTests.setUpc                 C   s   |  dt| jk d S r   )
assertTruestrr   r   r   r   r   test_error_message_contains_foo(   s    z1AppExceptionTests.test_error_message_contains_fooN)__name__
__module____qualname__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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;S )<ApplicationTestsc                 C   s   t  | _d S r   r   Applicationappr   r   r   r   r   .   s    zApplicationTests.setUpc                 C   s   |  t| jjtj d S r   )r   
isinstancer   settingsr   ZSettingsr   r   r   r   test_creates_settings1   s    z&ApplicationTests.test_creates_settingsc                 C   sH   G dd dt j}| }| d|jk |jg d | d|jk d S )Nc                   @   s   e Zd Zdd Zdd ZdS )zAApplicationTests.test_calls_add_settings_only_in_run.<locals>.Fooc                 S   s   d S r   r   r   argsr   r   r   process_args8   s    zNApplicationTests.test_calls_add_settings_only_in_run.<locals>.Foo.process_argsc                 S   s   | j dgd d S Nr    )r   stringr   r   r   r   add_settings;   s    zNApplicationTests.test_calls_add_settings_only_in_run.<locals>.Foo.add_settingsN)r   r   r   r"   r&   r   r   r   r   Foo6   s   r'   r   r!   )r   r   ZassertFalser   runr   r   r'   r   r   r   r   #test_calls_add_settings_only_in_run4   s
    z4ApplicationTests.test_calls_add_settings_only_in_runc                 C   s>   G dd dt j}| }|jdgd | |jd dg d S )Nc                   @   s   e Zd Zdd Zdd ZdS )zIApplicationTests.test_run_uses_string_list_options_only_once.<locals>.Fooc                 S   s   | j dgd d S r#   )r   Zstring_listr   r   r   r   r&   G   s    zVApplicationTests.test_run_uses_string_list_options_only_once.<locals>.Foo.add_settingsc                 S   s   d S r   r   r    r   r   r   r"   J   s    zVApplicationTests.test_run_uses_string_list_options_only_once.<locals>.Foo.process_argsN)r   r   r   r&   r"   r   r   r   r   r'   E   s   r'   z--foo=yor(   r   Zyo)r   r   r)   assertEqualr   r*   r   r   r   +test_run_uses_string_list_options_only_onceC   s    z<ApplicationTests.test_run_uses_string_list_options_only_oncec                    sB   d _  fdd}| j_dd  j_ jg    j  d S )NFc                      s
   d _ d S NTcalledr   r   r   r   setupT   s    z8ApplicationTests.test_run_sets_up_logging.<locals>.setupc                 S   s   d S r   r   r(   r   r   r   <lambda>X       z;ApplicationTests.test_run_sets_up_logging.<locals>.<lambda>)r0   r   Zsetup_loggingr"   r)   r   )r   r1   r   r   r   test_run_sets_up_loggingQ   s    z)ApplicationTests.test_run_sets_up_loggingc                 C   s4   dd | j _| j jg dgd | | j jjd d S )Nc                 S   s   d S r   r   r(   r   r   r   r2   ]   r3   zGApplicationTests.test_run_sets_progname_from_sysargv0.<locals>.<lambda>r   )r!   Zsysargv)r   r"   r)   r,   r   Zprognamer   r   r   r   $test_run_sets_progname_from_sysargv0\   s    z5ApplicationTests.test_run_sets_progname_from_sysargv0c                    s>   d  _  fdd j_ jjddgd   j ddg d S )Nc                    s   t  d| S Nr0   setattrr(   r   r   r   r2   c   r3   z>ApplicationTests.test_run_calls_process_args.<locals>.<lambda>r   barr(   )r0   r   r"   r)   r,   r   r   r   r   test_run_calls_process_argsa   s    z,ApplicationTests.test_run_calls_process_argsc                 C   s:   g | _ | j j| j_| jjddgd | | j ddg d S )Nr   r9   r(   )Zinputsappendr   process_inputr)   r,   r   r   r   r   test_run_processes_input_filesg   s    z/ApplicationTests.test_run_processes_input_filesc                 C   s0   dd | j _| j jg d | | j jtj d S )Nc                 S   s   d S r   r   r(   r   r   r   r2   n   r3   zAApplicationTests.test_run_sets_output_attribute.<locals>.<lambda>r(   )r   r"   r)   r,   outputsysstdoutr   r   r   r   test_run_sets_output_attributem   s    z/ApplicationTests.test_run_sets_output_attributec                 C   s2   dd | j _| j jdgd | | j jjd d S )Nc                 S   s   d S r   r   r(   r   r   r   r2   s   r3   zWApplicationTests.test_run_sets_output_to_file_if_output_option_is_set.<locals>.<lambda>z--output=/dev/nullr(   	/dev/null)r   r"   r)   r,   r>   namer   r   r   r   4test_run_sets_output_to_file_if_output_option_is_setr   s    zEApplicationTests.test_run_sets_output_to_file_if_output_option_is_setc                    sf   G dd dt }d  _ fdd j_dd  j_|  jj_ jjddgd   jddg d S )	Nc                   @   s   e Zd Zdd ZdS )z@ApplicationTests.test_run_calls_parse_args.<locals>.DummyOptionsc                 S   s   d | _ d | _d S r   )r>   logr   r   r   r   __init__y   s    zIApplicationTests.test_run_calls_parse_args.<locals>.DummyOptions.__init__N)r   r   r   rF   r   r   r   r   DummyOptionsx   s   rG   c                    s   t  d| S r6   r7   )r!   kwr   r   r   r2   }   r3   z<ApplicationTests.test_run_calls_parse_args.<locals>.<lambda>c                 S   s   d S r   r   r(   r   r   r   r2   ~   r3   r   r9   r(   )	objectr0   r   
parse_argsr"   r   Zoptionsr)   r,   )r   rG   r   r   r   test_run_calls_parse_argsw   s    z*ApplicationTests.test_run_calls_parse_argsc                 C   s|   |  | jdd |  | jdd |  | jdd |  | jdd |  | jdd |  | jd	d d S )
Nr   ZFOOzfoo.pyzfoo barZFOO_BARfoo-barzfoo/barZBARZfoo_bar)r,   r   Zenvnamer   r   r   r   test_makes_envname_correctly   s    z-ApplicationTests.test_makes_envname_correctlyc                 C   s`   | j jdgd | j jdgd | j ddg | | j jd d | | j jd d d S )	Nr   zfoo helpr9   zbar helpz--foo=foovaluez--barZfoovalueT)r   r   r%   ZbooleanrJ   r,   r   r   r   r   test_parses_options   s
    z$ApplicationTests.test_parses_optionsc                    s>   i  G  fdddt j}| }|jg d |  d  d S )Nc                       s    e Zd Z fddZdd ZdS )z.ApplicationTests.test_calls_setup.<locals>.Appc                    s   d d< d S )NTsetup-calledr   r   contextr   r   r1      s    z4ApplicationTests.test_calls_setup.<locals>.App.setupc                 S   s   d S r   r   r    r   r   r   process_inputs   s    z=ApplicationTests.test_calls_setup.<locals>.App.process_inputsN)r   r   r   r1   rR   r   rP   r   r   App   s   rS   r(   rO   r   r   r)   r   r   rS   r   r   rP   r   test_calls_setup   s
    z!ApplicationTests.test_calls_setupc                    s>   i  G  fdddt j}| }|jg d |  d  d S )Nc                       s    e Zd Z fddZdd ZdS )z0ApplicationTests.test_calls_cleanup.<locals>.Appc                    s   d d< d S )NTcleanup-calledr   r   rP   r   r   cleanup   s    z8ApplicationTests.test_calls_cleanup.<locals>.App.cleanupc                 S   s   d S r   r   r    r   r   r   rR      s    z?ApplicationTests.test_calls_cleanup.<locals>.App.process_inputsN)r   r   r   rX   rR   r   rP   r   r   rS      s   rS   r(   rW   rT   rU   r   rP   r   test_calls_cleanup   s
    z#ApplicationTests.test_calls_cleanupc                    s6   d _  fdd}| j_ jg    j  d S )NFc                    s
   d _ d S r.   r/   r(   r   r   r   rR      s    zOApplicationTests.test_process_args_calls_process_inputs.<locals>.process_inputs)r0   r   rR   r"   r   )r   rR   r   r   r   &test_process_args_calls_process_inputs   s
    z7ApplicationTests.test_process_args_calls_process_inputsc                    s@   g  _  fdd}| j_ jddg   j ddg d S )Nc                    s    j |  d S r   r!   r;   argr   r   r   r<      s    z\ApplicationTests.test_process_inputs_calls_process_input_for_each_arg.<locals>.process_inputr   r9   r!   r   r<   rR   r,   r   r<   r   r   r   4test_process_inputs_calls_process_input_for_each_arg   s
    zEApplicationTests.test_process_inputs_calls_process_input_for_each_argc                    s:   g  _  fdd}| j_ jg    j dg d S )Nc                    s    j |  d S r   r[   r\   r   r   r   r<      s    zfApplicationTests.test_process_inputs_calls_process_input_with_dash_if_no_inputs.<locals>.process_input-r^   r_   r   r   r   >test_process_inputs_calls_process_input_with_dash_if_no_inputs   s
    zOApplicationTests.test_process_inputs_calls_process_input_with_dash_if_no_inputsc                 C   s2   | j d}| t|t | t|dd d S )NrB   moder)r   
open_inputr   r   r   r,   getattrr   fr   r   r   test_open_input_opens_file   s    z+ApplicationTests.test_open_input_opens_filec                 C   s&   | j jddd}| t|dd d S )NrB   rb)rc   rc   )r   re   r,   rf   rg   r   r   r   8test_open_input_opens_file_in_binary_mode_when_requested   s    zIApplicationTests.test_open_input_opens_file_in_binary_mode_when_requestedc                 C   s   |  | jdtj d S Nra   )r,   r   re   r?   stdinr   r   r   r   )test_open_input_opens_stdin_if_dash_given   s    z:ApplicationTests.test_open_input_opens_stdin_if_dash_givenc                    s8   d  _  fdd}| j_ jd   j d d S )Nc                    s   |  _ tdS )Nr$   )r0   r   rC   r   r   r   re      s    zHApplicationTests.test_process_input_calls_open_input.<locals>.open_inputr   )r0   r   re   r<   r,   )r   re   r   r   r   #test_process_input_calls_open_input   s
    z4ApplicationTests.test_process_input_calls_open_inputc                    sV   d_ fdd}td | _ fdd}|j_jjd d j d d S )	NFc                      s
   d _ d S r.   )closedr   r   r   r   close   s    zGApplicationTests.test_process_input_does_not_close_stdin.<locals>.closer$   c                    s   | dkr S d S rl   r   ro   )rh   r   r   re      s    zLApplicationTests.test_process_input_does_not_close_stdin.<locals>.open_inputra   )rm   )rq   r   rr   r   re   r<   r,   )r   rr   re   r   )rh   r   r   'test_process_input_does_not_close_stdin   s    z8ApplicationTests.test_process_input_does_not_close_stdinc                    sH   g  G  fdddt j}| }|jddgd |  dddd	g d S )
Nc                       s"   e Zd ZdddZ fddZdS )z8ApplicationTests.test_processes_input_lines.<locals>.FooNc                    s    t d fddtdD S )Nr$   c                 3   s   | ]}d  |f V  qdS z%s%d
Nr   .0iro   r   r   	<genexpr>   s   zVApplicationTests.test_processes_input_lines.<locals>.Foo.open_input.<locals>.<genexpr>   r   joinranger   rC   rc   r   ro   r   re      s    zCApplicationTests.test_processes_input_lines.<locals>.Foo.open_inputc                    s     | d S r   )r;   r   rC   linelinesr   r   process_input_line  s    zKApplicationTests.test_processes_input_lines.<locals>.Foo.process_input_line)Nr   r   r   re   r   r   r   r   r   r'      s   
r'   r   r9   r(   zfoo0
zfoo1
zbar0
zbar1
r   r   r)   r,   r*   r   r   r   test_processes_input_lines   s
    	z+ApplicationTests.test_processes_input_linesc                    sH   g  G  fdddt j}| }|jddgd |  dddd	g d S )
Nc                       s"   e Zd ZdddZ fddZdS )zIApplicationTests.test_process_input_line_can_access_counters.<locals>.FooNc                    s    t d fddtdD S )Nr$   c                 3   s   | ]}d  |f V  qdS rt   r   ru   ro   r   r   rx     s   zgApplicationTests.test_process_input_line_can_access_counters.<locals>.Foo.open_input.<locals>.<genexpr>ry   rz   r}   r   ro   r   re     s    zTApplicationTests.test_process_input_line_can_access_counters.<locals>.Foo.open_inputc                    s     | j| j| jf d S r   )r;   filenoZglobal_linenolinenor~   Zcountersr   r   r     s    z\ApplicationTests.test_process_input_line_can_access_counters.<locals>.Foo.process_input_line)Nr   r   r   r   r   r'     s   
r'   r   r9   r(   )   r   r   )r   ry   ry   )ry      r   )ry      ry   r   r*   r   r   r   +test_process_input_line_can_access_counters	  s    	z<ApplicationTests.test_process_input_line_can_access_countersc                 C   sD   dd }|| j _t }| jt| j jg |td | d| k d S )Nc                 S   s   t dd S Nxxx)r   r   r(   r   r   r   raise_error  s    zPApplicationTests.test_run_prints_out_error_for_appexception.<locals>.raise_errorstderrrE   r   	r   r"   r   assertRaises
SystemExitr)   r	   r   getvaluer   r   rh   r   r   r   *test_run_prints_out_error_for_appexception  s
    z;ApplicationTests.test_run_prints_out_error_for_appexceptionc                 C   sD   dd }|| j _t }| jt| j jg |td | d| k d S )Nc                 S   s   t dd S r   )	Exceptionr(   r   r   r   r   &  s    zZApplicationTests.test_run_prints_out_stack_trace_for_not_appexception.<locals>.raise_errorr   	Tracebackr   r   r   r   r   4test_run_prints_out_stack_trace_for_not_appexception%  s
    zEApplicationTests.test_run_prints_out_stack_trace_for_not_appexceptionc                 C   s2   dd }|| j _t }| jt| j jg |td d S )Nc                 S   s   t dd S )N{   )r   r(   r   r   r   r   .  s    zOApplicationTests.test_run_raises_systemexit_for_systemexit.<locals>.raise_errorr   r   r"   r   r   r   r)   r	   r   r   r   r   )test_run_raises_systemexit_for_systemexit-  s    z:ApplicationTests.test_run_raises_systemexit_for_systemexitc                 C   s2   dd }|| j _t }| jt| j jg |td d S )Nc                 S   s
   t  d S r   )KeyboardInterruptr(   r   r   r   r   5  s    zPApplicationTests.test_run_raises_systemexit_for_keyboardint.<locals>.raise_errorr   r   r   r   r   r   *test_run_raises_systemexit_for_keyboardint4  s    z;ApplicationTests.test_run_raises_systemexit_for_keyboardintN) r   r   r   r   r   r+   r-   r4   r5   r:   r=   rA   rD   rK   rM   rN   rV   rY   rZ   r`   rb   ri   rk   rn   rp   rs   r   r   r   r   r   r   r   r   r   r   r   ,   s:   


r   c                   @   s   e Zd Zdd ZdS )DummySubcommandAppc                 C   s
   d| _ d S r.   )
foo_calledr    r   r   r   cmd_foo>  s    zDummySubcommandApp.cmd_fooN)r   r   r   r   r   r   r   r   r   <  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 )SubcommandTestsc                 C   s   t  | _t | _d S r   )r   r   r   trashr   r   r   r   r   D  s    zSubcommandTests.setUpc                 C   s   |  | j dg d S )Nr   )r,   r   Z_subcommand_methodnamesr   r   r   r   test_lists_subcommandsH  s    z&SubcommandTests.test_lists_subcommandsc                 C   s,   |  | jdd |  | jdd d S )Nr   r   rL   Zcmd_foo_bar)r,   r   Z_normalize_cmdr   r   r   r   test_normalizes_subcommandK  s    z*SubcommandTests.test_normalizes_subcommandc                 C   s   | j t| jjg | jtd d S )Nr   r   r   r   r)   r   r	   r   r   r   r   #test_raises_error_for_no_subcommandO  s     z3SubcommandTests.test_raises_error_for_no_subcommandc                 C   s    | j t| jjdg| jtd d S )Nzwhat?r   r   r   r   r   r   (test_raises_error_for_unknown_subcommandS  s     z8SubcommandTests.test_raises_error_for_unknown_subcommandc                 C   s(   | j jdg| jtd | | j j d S )Nr   r   )r   r)   r   r	   r   r   r   r   r   r   test_calls_subcommand_methodW  s    z,SubcommandTests.test_calls_subcommand_methodc                    sL   d _  fdd} jjd|dgd  jjdg jtd   j  d S )NFc                     s
   d _ d S r.   )
bar_calledr(   r   r   r   r9   ^  s    zCSubcommandTests.test_calls_subcommand_method_via_alias.<locals>.barr9   Zyoyo)aliasesr   )r   r   add_subcommandr)   r   r	   r   )r   r9   r   r   r   &test_calls_subcommand_method_via_alias[  s
    z6SubcommandTests.test_calls_subcommand_method_via_aliasc                 C   s,   | j jdg| jtd | d| j jk d S )Nr   r   help)r   r)   r   r	   r   subcommandsr   r   r   r   !test_adds_default_subcommand_helpe  s    z1SubcommandTests.test_adds_default_subcommand_helpN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   B  s   
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ExtensibleSubcommandTestsc                 C   s   t  | _d S r   r   r   r   r   r   r   l  s    zExtensibleSubcommandTests.setUpc                 C   s   |  | jji  d S r   )r,   r   r   r   r   r   r   test_lists_no_subcommandso  s    z3ExtensibleSubcommandTests.test_lists_no_subcommandsc                 C   s.   dd }| j d| | | j jd|i d S )Nc                 S   s   d S r   r   r\   r   r   r   help_callbacks  s    zEExtensibleSubcommandTests.test_adds_subcommand.<locals>.help_callbackr   )r   r   r,   r   )r   r   r   r   r   test_adds_subcommandr  s    z.ExtensibleSubcommandTests.test_adds_subcommandN)r   r   r   r   r   r   r   r   r   r   r   j  s   r   )Z
__future__r   r   filer   ImportErrorior?   Zunittestr   r	   ZTestCaser
   r   r   r   r   r   r   r   r   r   <module>   s    	  (