
    &e              
       D   d Z ddlZddlZddlZddlZddlmZ ddlZddlZddl	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ej                            ej                  Zej        d             Zej        d	             Zd
 Z ej        ej!        "                    dddgfdddgfddgfdddgfg          d                         Z#ej        ej!        "                    dddgfdddgfddgfdddgfg          d                         Z$ G d dej%                  Z&dS )z
Tests for platutils.py
    N)interrupt_main)find_cmdFindCmdError	arg_splitsystem	getoutputgetoutputerrorget_output_error_code)capture_output)
decorators)toolsc                  R    t          d          } |                     d          sJ dS )z*Make sure we can find the full path to ls.lsN)r   endswithpaths    @lib/python3.11/site-packages/IPython/utils/tests/test_process.pytest_find_cmd_lsr   '   s-     D>>D==    c                      t          d          } |                                                     d          s
J |             dS )zTry to find pythonw on Windows.pythonwzpythonw.exeN)r   lowerr   r   s    r   test_find_cmd_pythonwr   .   sA     ID::<<  //5555555r   c                  F    t          j        t          t          d           dS )z?Make sure that FindCmdError is raised if we can't find the cmd.asdfasdfN)pytestraisesr   r    r   r   test_find_cmd_failr   5   s    
M,*55555r   zargstr, argvhizhello therehellothereu   hǎllozsomething "with quotes"	somethingz"with quotes"c                 0    t          |           |k    sJ dS z?Ensure that argument lines are correctly split like in a shell.Nr   argstrargvs     r   test_arg_splitr*   :   s#     V$$$$$$r   zargstr,argvzwith quotesc                 0    t          |           |k    sJ dS r%   r&   r'   s     r   test_arg_split_win32r,   L   s#     V$$$$$$r   c                   d    e Zd Zd Zd Zd Zd Zd Zd Zd Z	e
j        d             Zd	 Zd
 ZdS )SubProcessTestCasec                 ^    g d}|                      d                    |                     dS )zMake a valid python temp file.)z
import sysz+print('on stdout', end='', file=sys.stdout)z+print('on stderr', end='', file=sys.stderr)zsys.stdout.flush()zsys.stderr.flush()
N)mktmpjoin)selfliness     r   setUpzSubProcessTestCase.setUp\   s9    ' ' '
 	

499U##$$$$$r   c                 p    t          t           d| j         d          }|                     |d           d S )N ""r   )r   pythonfnameassertEqualr3   statuss     r   test_systemzSubProcessTestCase.test_systeme   s>    622TZ22233#####r   c                 `    t          dt          z            }|                     |d           d S )Nz%s -c "import sys"r   )r   r9   r;   r<   s     r   test_system_quotesz%SubProcessTestCase.test_system_quotesi   s1    ,v566#####r   c                    t          j                    t          j                    k    rt          j        d          t          j        t
          j        t
          j                   d }t          j        |          	                                 t          j
                    }	  |            }n# t          $ r Y nw xY wt          j
                    }|                     ||z
  dk     d||z
  z             |S )z8
        Interrupt a subprocess after a second.
        z*Can't run this test if not in main thread.c                  J    t          j        d           t                       d S )Ng      ?)timesleepr   r   r   r   	interruptz7SubProcessTestCase.assert_interrupts.<locals>.interrupty   s!    JsOOOr   )target   zProcess didn't die quickly: %s)	threadingmain_threadcurrent_threadr   skipsignalSIGINTdefault_int_handlerThreadstartrC   KeyboardInterrupt
assertTrue)r3   commandrE   rP   resultends         r   assert_interruptsz$SubProcessTestCase.assert_interruptsm   s      ""i&>&@&@@@+JKKK
 	fmV%?@@@	 	 	
 		***00222		WYYFF  	 	 	D	 ikk%K!O=uM	
 	
 	
 s   $
B/ /
B<;B<c                 j    d }|                      |          }|                     |dd|            dS )zr
        When interrupted in the way ipykernel interrupts IPython, the
        subprocess is interrupted.
        c                  0    t          dt          z            S )Nz"%s -c "import time; time.sleep(5)")r   r9   r   r   r   rS   z9SubProcessTestCase.test_system_interrupt.<locals>.command   s    >GHHHr   r   z(The process wasn't interrupted. Status: N)rV   assertNotEqual)r3   rS   r=   s      r   test_system_interruptz(SubProcessTestCase.test_system_interrupt   s[    
	I 	I 	I ''00AJ&JJ	
 	
 	
 	
 	
r   c                     t          t           d| j         d          }	 |                     |d           d S # t          $ r |                     |d           Y d S w xY w)Nr7   r8   zon stderron stdoutzon stdouton stderr)r   r9   r:   r;   AssertionErrorr3   outs     r   test_getoutputz!SubProcessTestCase.test_getoutput   s    622TZ22233	8S"677777 	8 	8 	8S"6777777	8s   9  AAc                     t          dt          z            }|                     |                                d           d S )Nz%s -c "print (1)"1r   r9   r;   stripr]   s     r   test_getoutput_quotedz(SubProcessTestCase.test_getoutput_quoted   s9    +f455c*****r   c                    t          dt          z            }|                     |                                d           t          dt          z            }|                     |                                d           d S )Nz%s -c 'print (1)'ra   z%s -c 'print ("1")'rb   r]   s     r   test_getoutput_quoted2z)SubProcessTestCase.test_getoutput_quoted2   sj    +f455c***/&899c*****r   c                     t          t           d| j         d          \  }}|                     |d           |                     |d           d S )Nr7   r8   	on stdout	on stderr)r	   r9   r:   r;   )r3   r^   errs      r   test_getoutput_errorz'SubProcessTestCase.test_getoutput_error   sX    !V"<"<tz"<"<"<==Sk***k*****r   c                    dt           z  }t          |          \  }}}|                     |d           |                     |d           |                     |d           t          t            d| j         d          \  }}}|                     |d           |                     |d           |                     |d           d S )	Nz%s -c "import sys; sys.exit(1)"    r7   r8   rh   ri   r   )r9   r
   r;   r:   )r3   
quiet_exitr^   rj   codes        r   test_get_output_error_codez-SubProcessTestCase.test_get_output_error_code   s    6?
.z::S$b!!!b!!!q!!!.&/I/IDJ/I/I/IJJS$k***k***q!!!!!r   N)__name__
__module____qualname__r5   r>   r@   rV   rZ   r_   rd   dec
skip_win32rf   rk   rq   r   r   r   r.   r.   [   s        % % %$ $ $$ $ $  <
 
 
8 8 8+ + +
 	^+ + ^++ + +
	" 	" 	" 	" 	"r   r.   )'__doc__sysrL   osrC   _threadr   rH   r   IPython.utils.processr   r   r   r   r   r	   r
   IPython.utils.capturer   IPython.testingr   ru   r   ttr   basename
executabler9   rv   r   skip_if_not_win32r   r   markparametrizer*   r,   TempFileMixinr.   r   r   r   <module>r      sw    


  				  " " " " " "     : : : : : : : : : : : : : : : : : : 1 0 0 0 0 0 - - - - - - ' ' ' ' ' '			#.	)	)    6 6 66 6 6
 	v	'*+ 
~&	"[/$BC % %  %
 	v	'*+	~&	"[-$@A	 % %  %
_" _" _" _" _") _" _" _" _" _"r   