o
    ˷e                  
   @   sP  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d	 Zejd
d Zd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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d Z$G d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                  C   s   t d} | dsJ dS )z*Make sure we can find the full path to ls.lsN)r   endswithpath r   W/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/utils/tests/test_process.pytest_find_cmd_ls'   s   r   c                  C   s"   t d} |  dsJ | dS )zTry to find pythonw on Windows.pythonwzpythonw.exeN)r   lowerr   r   r   r   r   test_find_cmd_pythonw.   s   r   c                   C   s   t 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   r   r   test_find_cmd_fail5   s   r   zargstr, argvhizhello therehellothereu   hǎllozsomething "with quotes"	somethingz"with quotes"c                 C      t | |ksJ dS z?Ensure that argument lines are correctly split like in a shell.Nr   argstrargvr   r   r   test_arg_split:   s   r%   zargstr,argvzwith quotesc                 C   r   r    r!   r"   r   r   r   test_arg_split_win32L   s   r&   c                   @   sb   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	e
jdd Zdd Zdd ZdS )SubProcessTestCasec                 C   s   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linesr   r   r   setUp\   s   zSubProcessTestCase.setUpc                 C   s&   t t d| j d}| |d d S )N ""r   )r   pythonfnameassertEqualr+   statusr   r   r   test_systeme   s   zSubProcessTestCase.test_systemc                 C   s   t dt }| |d d S )Nz%s -c "import sys"r   )r   r0   r2   r3   r   r   r   test_system_quotesi   s   z%SubProcessTestCase.test_system_quotesc                 C   s   t  t  krtdttjtj dd }t j|d	  t

 }z| }W n	 ty3   Y nw t

 }| || dk d||   |S )z8
        Interrupt a subprocess after a second.
        z*Can't run this test if not in main thread.c                   S   s   t d t  d S )Ng      ?)timesleepr   r   r   r   r   	interrupty   s   

z7SubProcessTestCase.assert_interrupts.<locals>.interrupt)target   zProcess didn't die quickly: %s)	threadingmain_threadcurrent_threadr   skipsignalSIGINTdefault_int_handlerThreadstartr7   KeyboardInterrupt
assertTrue)r+   commandr9   rD   resultendr   r   r   assert_interruptsm   s    

z$SubProcessTestCase.assert_interruptsc                 C   s*   dd }|  |}| |dd|  dS )zr
        When interrupted in the way ipykernel interrupts IPython, the
        subprocess is interrupted.
        c                   S   s   t dt S )Nz"%s -c "import time; time.sleep(5)")r   r0   r   r   r   r   rG      s   z9SubProcessTestCase.test_system_interrupt.<locals>.commandr   z(The process wasn't interrupted. Status: N)rJ   assertNotEqual)r+   rG   r4   r   r   r   test_system_interrupt   s
   
z(SubProcessTestCase.test_system_interruptc                 C   sJ   t t d| j d}z	| |d W d S  ty$   | |d Y d S w )Nr.   r/   zon stderron stdoutzon stdouton stderr)r   r0   r1   r2   AssertionErrorr+   outr   r   r   test_getoutput   s   z!SubProcessTestCase.test_getoutputc                 C   s    t dt }| | d d S )Nz%s -c "print (1)"1r   r0   r2   striprN   r   r   r   test_getoutput_quoted   s   z(SubProcessTestCase.test_getoutput_quotedc                 C   s<   t dt }| | d t dt }| | d d S )Nz%s -c 'print (1)'rQ   z%s -c 'print ("1")'rR   rN   r   r   r   test_getoutput_quoted2   s   z)SubProcessTestCase.test_getoutput_quoted2c                 C   s6   t t d| j d\}}| |d | |d d S )Nr.   r/   	on stdout	on stderr)r   r0   r1   r2   )r+   rO   errr   r   r   test_getoutput_error   s   z'SubProcessTestCase.test_getoutput_errorc                 C   s~   dt  }t|\}}}| |d | |d | |d tt  d| j d\}}}| |d | |d | |d d S )	Nz%s -c "import sys; sys.exit(1)"    r.   r/   rV   rW   r   )r0   r	   r2   r1   )r+   
quiet_exitrO   rX   coder   r   r   test_get_output_error_code   s   z-SubProcessTestCase.test_get_output_error_codeN)__name__
__module____qualname__r-   r5   r6   rJ   rL   rP   rT   dec
skip_win32rU   rY   r^   r   r   r   r   r'   [   s    	
r'   )'__doc__sysr@   osr7   _threadr   r<   r   IPython.utils.processr   r   r   r   r   r   r	   IPython.utils.capturer
   IPython.testingr   rb   r   ttr   basename
executabler0   rc   r   skip_if_not_win32r   r   markparametrizer%   r&   TempFileMixinr'   r   r   r   r   <module>   sN    $





	