B
    \2B                 @   s  d Z ddlZddlmZ ddlZddlmZ ddlZddlm	Z	m
Z
mZ ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddl m!Z!m"Z" ej#Z$ej%dde&dd ej%dde'dd ej%dde(dd ej%dde)dd ej%dde(dd ej%dde(dd ej%dde(dd ej%dde(dd ej%dde(dd ej%dde(dd ej%dde(dd ej%dde*dd edk rej%dd e)d!d ed"k rej%dd#e+dd ne,d$e* d%d& Z-d'd( Z.de.fd)d*Z/e/d+e/d,e/d-d.Z0d/d0d1d2d3d4gZ1G d5d6 d6e2Z3d7d8 e1D Z4e4d/ Z5e0d- se56d9 e56d: e0d+ se56d;f e56d< e4d1 Z5e56d= e0d, se56d> e56d? e56d@ e56dA e4d3 Z5ej7dBkre56dC e56dD e4d2 6dE e4d0 Z5e56dF e56dG e56dH e3dGdIdJge4dG< e18dG dKdL Z9G dMdN dNeZ:G dOdP dPe	Z;G dQdR dReZ<dSdT Z=e>dUkre=  dS )Vaw  IPython Test Suite Runner.

This module provides a main entry point to a user script to test IPython
itself from the command line. There are two ways of running this script:

1. With the syntax `iptest all`.  This runs our entire test suite by
   calling this script (with different arguments) recursively.  This
   causes modules and package to be tested in different processes, using nose
   or trial where appropriate.
2. With the regular nose syntax, like `iptest IPython -- -vvs`. In this form
   the script simply calls nose, but with special command line flags and
   plugins loaded. Options after `--` are passed to nose.

    N)BytesIO)ThreadLockEvent)Xunit)SkipTest)TestProgram)Plugin)safe_str)version_info)decode)import_item)IPythonDoctest)KnownFailuredecignorez).*Matplotlib is building the font cache.*z.*)messagecategorymoduleerrorz.*{'config': True}.*zIPy.*defaultz.*apply_wrapper.*z.*make_label_decz.*decorated_dummy.*z.*skip_file_no_x11.*z.*onlyif_any_cmd_exists.*z.*disable_gui.*z(.*ExceptionColors global is deprecated.*z(.*elementwise != comparison failed and.*)   z .*Please use assertEqual insteadz	IPython.*)   z.*Completer.complete.*zNCompleter.complete was pending deprecation and should be changed to Deprecatedc           
      sd   yt ddd   W n* tk
rB }  zt|  W d d } ~ X Y nX d fdd	}tjt_|t_d S )NTc               S   s   d S )N r   r   r   5lib/python3.7/site-packages/IPython/testing/iptest.py<lambda>V   s    z#monkeypatch_xunit.<locals>.<lambda>c                s.   t |d  r tf|dd   }| |||S )Nr      )
issubclassr   orig_addError)selftesterrZcapt)KnownFailureTestr   r   addErrorZ   s    z#monkeypatch_xunit.<locals>.addError)N)r   Zknownfailureif	Exceptiontyper   r#   r   )er#   r   )r"   r   monkeypatch_xunitT   s    r'   c             C   s   | j S )N)__version__)modr   r   r   extract_versione   s    r*   c          	   C   sF   yt | }W n ttfk
r$   dS X |r>|r6||}||kS dS dS )a  Test to see if item is importable, and optionally check against a minimum
    version.

    If min_version is given, the default behavior is to check against the
    `__version__` attribute of the item, but specifying `callback` allows you to
    extract the value you are interested in. e.g::

        In [1]: import sys

        In [2]: from IPython.testing.iptest import test_for

        In [3]: test_for('sys', (2,6), callback=lambda sys: sys.version_info)
        Out[3]: True

    FTN)r   ImportErrorRuntimeError)itemZmin_versioncallbackZcheckr   r   r   test_forh   s    r/   
matplotlibpygmentssqlite3)r0   r1   r2   core
extensionslibZterminalZtestingZutilsc               @   s0   e Zd Zdd Zdd Zdd Zedd Zd	S )
TestSectionc             C   s"   || _ || _g | _g | _d| _d S )NT)nameincludesexcludesdependenciesenabled)r   r7   r8   r   r   r   __init__   s
    zTestSection.__init__c             C   s6   | ds| jd d | }| j|dtj d S )NIPythonr   .)
startswithr8   r9   appendreplaceossep)r   r   r   r   r   exclude   s    
zTestSection.excludec             G   s   | j | d S )N)r:   extend)r   Zpackagesr   r   r   requires   s    zTestSection.requiresc             C   s   | j otdd | jD S )Nc             s   s   | ]}t | V  qd S )N)have).0pr   r   r   	<genexpr>   s    z'TestSection.will_run.<locals>.<genexpr>)r;   allr:   )r   r   r   r   will_run   s    zTestSection.will_runN)__name__
__module____qualname__r<   rD   rF   propertyrL   r   r   r   r   r6      s   r6   c             C   s   i | ]}t |d | g|qS )z
IPython.%s)r6   )rH   nr   r   r   
<dictcomp>   s    rR   ztests.test_historyhistoryZ
pylabtoolsztests.test_pylabtoolsZkernelztests.test_lexersZinputhookgtkZinputhookwxZ	inputhookZwin32zplugin.test_exampleipzplugin.dtexampleZconsoleZrmagicZ
autoreloadztests.test_autoreloadzIPython.extensions.autoreloadz(IPython.extensions.tests.test_autoreloadc              C   sx   ddl m}  ddlm} tj|  }xLtD ]D}x>|jD ]4}t	||}tj
|s8t|d s8|d|  q8W q,W d S )Nr   )get_ipython_package_dir)warnz.*zExcluding nonexistent file: %r)ZIPython.pathsrT   warningsrU   rB   pathdirnametest_sections
exclusionspjoinexistsglob)rT   rU   parentsecpatternZfullpathr   r   r   check_exclusions_exist   s    

ra   c                   sP   e Zd ZdZdZdZd fdd	ZejfddZ	d	d
 Z
dd Zdd Z  ZS )ExclusionPluginzEA nose plugin to effect our exclusions of files and directories.
    rZ   i  Nc                s   |pg | _ tt|   dS )z
        Parameters
        ----------

        exclude_patterns : sequence of strings, optional
          Filenames containing these patterns (as raw strings, not as regular
          expressions) are excluded from the tests.
        N)exclude_patternssuperrb   r<   )r   rc   )	__class__r   r   r<      s    	
zExclusionPlugin.__init__c             C   s   t | || d S )N)r	   options)r   parserenvr   r   r   rf     s    zExclusionPlugin.optionsc             C   s   t | || d| _d S )NT)r	   	configurer;   )r   rf   configr   r   r   ri     s    zExclusionPlugin.configurec                s    t  fdd| jD rdS dS )zGReturn whether the given filename should be scanned for tests.
        c             3   s   | ]}| kV  qd S )Nr   )rH   pat)filenamer   r   rJ     s    z+ExclusionPlugin.wantFile.<locals>.<genexpr>FN)anyrc   )r   rl   r   )rl   r   wantFile
  s    zExclusionPlugin.wantFilec                s    t  fdd| jD rdS dS )zHReturn whether the given directory should be scanned for tests.
        c             3   s   | ]}| kV  qd S )Nr   )rH   rk   )	directoryr   r   rJ     s    z0ExclusionPlugin.wantDirectory.<locals>.<genexpr>FN)rm   rc   )r   ro   r   )ro   r   wantDirectory  s    zExclusionPlugin.wantDirectory)N)rM   rN   rO   __doc__r7   Zscorer<   rB   environrf   ri   rn   rp   __classcell__r   r   )re   r   rb      s   rb   c                   sN   e Zd ZdZdZd fdd	Zdd Zdd Zd	d
 Zdd Z	dd Z
  ZS )StreamCapturerTFc                sF   t t|   || _g | _t | _t \| _	| _
t | _t | _d S )N)rd   rt   r<   echoZstreamsr   bufferrB   pipereadfdwritefdr   buffer_lockr   stop)r   ru   )re   r   r   r<     s    zStreamCapturer.__init__c          	   C   st   d| _ xP| j sVt| jd}| j | j| W d Q R X | j	rt
jt| qW t| j t| j d S )NTi   )startedr{   Zis_setrB   readrx   rz   rv   writeru   sysstdoutr   closery   )r   chunkr   r   r   run%  s    zStreamCapturer.runc          	   C   s.   | j  | jd | jd W d Q R X d S )Nr   )rz   rv   truncateseek)r   r   r   r   reset_buffer3  s    zStreamCapturer.reset_bufferc          	   C   s   | j  | j S Q R X d S )N)rz   rv   getvalue)r   r   r   r   
get_buffer8  s    zStreamCapturer.get_bufferc             C   s   | j s|   d S )N)r|   start)r   r   r   r   ensure_started<  s    zStreamCapturer.ensure_startedc             C   s.   | j s
dS | j  t| jd |   dS )zSafely stop the thread.N    )r|   r{   setrB   r~   ry   join)r   r   r   r   halt@  s
    
zStreamCapturer.halt)F)rM   rN   rO   Zdaemonr|   r<   r   r   r   r   r   rs   r   r   )re   r   rt     s   	rt   c               @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZeZ	dd Z
dS )SubprocessStreamCapturePluginZsubprocstreamsc             C   s.   t |  t | _tjdd| _| jt	_
d S )NZIPTEST_SUBPROC_STREAMScapture)r	   r<   rt   stream_capturerrB   rr   getdestinationget_write_filenonoseZiptest_stdstreams_fileno)r   r   r   r   r<   K  s    
z&SubprocessStreamCapturePlugin.__init__c             C   sD   | j dkr| j  | jjS | j dkr6ttjtjS tj	
 S d S )Nr   discard)r   r   r   ry   rB   opendevnullO_WRONLYr   
__stdout__fileno)r   r   r   r   r   S  s    


z.SubprocessStreamCapturePlugin.get_write_filenoc             C   s"   t | || | jdkrd| _d S )Nr   T)r	   ri   r   r;   )r   rf   rj   r   r   r   ri   \  s    
z'SubprocessStreamCapturePlugin.configurec             C   s   | j   d S )N)r   r   )r   r    r   r   r   	startTestb  s    z'SubprocessStreamCapturePlugin.startTestc             C   sL   |\}}}| j  dd}| rHt|}|d|dg}|d||fS |S )Nzutf-8rA   z&>> begin captured subprocess output <<z$>> end captured subprocess output <<
)r   r   r   stripr
   r   )r   r    r!   ZecZevtbZcapturedoutr   r   r   formatFailuref  s    
z+SubprocessStreamCapturePlugin.formatFailurec             C   s   | j   d S )N)r   r   )r   resultr   r   r   finalizeu  s    z&SubprocessStreamCapturePlugin.finalizeN)rM   rN   rO   r7   r<   r   ri   r   r   ZformatErrorr   r   r   r   r   r   I  s   	r   c              C   s  dt jkrttdst  t jd } | drV| drD| dd } | dd	t jd< t jd } | tkrt|  }|j	t jdd
< nJ| dr| dd tkrt| dd  }|j	t jdd
< nt
| | gd}t jddg }d|krd|kr|ddg }tjdkr|d t|jt t g}|jdrJ|t  |dddg dtjkrfttjd  d|jkrddlm} |  t||d dS )zRun the IPython test suite using nose.

    This function is called when this script is **not** called with the form
    `iptest all`.  It simply calls nose with appropriate command line flags
    and accepts all of the standard nose arguments.
    z--with-xunitr   r   zIPython/z.pyN/r>      zIPython.r   )r8   z--detailed-errorsz--exez-az-Az!crashz0.11z--traverse-namespace)r3   zIPython.corezIPython.utilsz--with-ipdoctestz--ipdoctest-testsz--ipdoctest-extension=txtZIPTEST_WORKING_DIRzkernel.inprocessr   )globalipapp)argvZ
addplugins)r   r   hasattrr   r'   r?   endswithrA   rY   r8   r6   r   r(   r@   rb   r9   r   r   r7   r   rE   rB   rr   chdirZIPython.testingr   Zstart_ipythonr   )Zarg1Zsectionr   Zpluginsr   r   r   r   
run_iptesty  sF    



	
	r   __main__)?rq   r]   ior   rB   os.pathrW   r   Z	threadingr   r   r   rV   Znose.plugins.builtinr   Znose.plugins.xunitr   r   Z	nose.corer   Znose.pluginsr	   Z	nose.utilr
   r=   r   ZIPython.utils.py3compatr   ZIPython.utils.importstringr   Z IPython.testing.plugin.ipdoctestr   ZIPython.external.decoratorsr   r   r   r[   filterwarningsUserWarningResourceWarningDeprecationWarningWarningFutureWarningPendingDeprecationWarningrU   r'   r*   r/   rG   Ztest_group_namesobjectr6   rY   r_   rD   platformr@   ra   rb   rt   r   r   rM   r   r   r   r   <module>   s   

"
















)00R
