U
    puf                  
   @   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tdS )z*Make sure we can find the full path to ls.ZlsN)r   endswithAssertionErrorpath r   D/tmp/pip-unpacked-wheel-bxwx4lqe/IPython/utils/tests/test_process.pytest_find_cmd_ls'   s    r   c                  C   s"   t d} |  dst| dS )zTry to find pythonw on Windows.pythonwzpythonw.exeN)r   lowerr   r   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.ZasdfasdfN)pytestZraisesr   r   r   r   r   r   test_find_cmd_fail5   s    r   zargstr, argvhizhello thereZhelloZthereu   hǎllozsomething "with quotes"Z	somethingz"with quotes"c                 C   s   t | |kstdS z?Ensure that argument lines are correctly split like in a shell.Nr   r   Zargstrargvr   r   r   test_arg_split:   s    r   zargstr,argvzwith quotesc                 C   s   t | |kstdS 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"   dddddg}|  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)Z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   r(   r*   r+   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k
rh   Y nX 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startr/   KeyboardInterrupt
assertTrue)r#   commandr1   r<   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   r(   r   r   r   r   r?      s    z9SubProcessTestCase.test_system_interrupt.<locals>.commandr   z(The process wasn't interrupted. Status: N)rB   ZassertNotEqual)r#   r?   r,   r   r   r   test_system_interrupt   s    
  z(SubProcessTestCase.test_system_interruptc                 C   sL   t t d| j d}z| |d W n  tk
rF   | |d Y nX d S )Nr&   r'   zon stderron stdoutzon stdouton stderr)r   r(   r)   r*   r   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   r(   r*   striprD   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)'rG   z%s -c 'print ("1")'rH   rD   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   r(   r)   r*   )r#   rE   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'   rL   rM   r   )r(   r	   r*   r)   )r#   Z
quiet_exitrE   rN   coder   r   r   test_get_output_error_code   s    z-SubProcessTestCase.test_get_output_error_codeN)__name__
__module____qualname__r%   r-   r.   rB   rC   rF   rJ   dec
skip_win32rK   rO   rS   r   r   r   r   r    [   s   	
r    )'__doc__sysr8   osr/   _threadr   r4   r   ZIPython.utils.processr   r   r   r   r   r   r	   ZIPython.utils.capturer
   ZIPython.testingr   rW   r   ttr   basename
executabler(   rX   r   Zskip_if_not_win32r   r   markZparametrizer   r   ZTempFileMixinr    r   r   r   r   <module>   sL   $





	