
NXc           @@  s  d  Z  d d l m 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	 m
 Z
 d d l m Z m Z y d d l j Z e Z Wn e k
 r e Z n Xd d l m Z d d l m Z d d	 l m Z d d
 l m Z m Z d d l m Z d d l  m! Z! d d l" m# Z$ d d l" m% Z% e j& d k rMe% j' n e$ j( Z) e) d    Z* d   Z+ e e+ _, d   Z- d   Z. e d  Z/ d d) d  Z1 d d d* d  Z2 d e3 f d     YZ4 d Z5 d   Z6 e j7 re
 Z8 n d e
 f d     YZ8 e9 e j: d   Z; d Z< d  e3 f d!     YZ= d" Z> d# e= f d$     YZ? e d%    Z@ e d&    ZA d d'  ZB d d(  ZC d S(+   sW   Generic testing tools.

Authors
-------
- Fernando Perez <Fernando.Perez@berkeley.edu>
i    (   t   absolute_importN(   t   contextmanager(   t   StringIO(   t   Popent   PIPE(   t   Config(   t   get_output_error_code(   t   list_strings(   t   temp_pyfilet   Tee(   t	   py3compat(   t   DEFAULT_ENCODINGi   (   t
   decorators(   t   skipdoctestt   win32c         C@  sH   t  |  } t j j |   d } g  | D] } t j j | |  ^ q) S(   sP  Make full paths for all the listed files, based on startPath.

    Only the base part of startPath is kept, since this routine is typically
    used with a script's ``__file__`` variable as startPath. The base of startPath
    is then prepended to all the listed files, forming the output list.

    Parameters
    ----------
    startPath : string
      Initial path to use as the base for the results.  This path is split
      using os.path.split() and only its first component is kept.

    files : string or list
      One or more files.

    Examples
    --------

    >>> full_path('/foo/bar.py',['a.txt','b.txt'])
    ['/foo/a.txt', '/foo/b.txt']

    >>> full_path('/foo',['a.txt','b.txt'])
    ['/a.txt', '/b.txt']

    If a single file is given, the output is still a list::

        >>> full_path('/foo','a.txt')
        ['/a.txt']
    i    (   R   t   ost   patht   splitt   join(   t	   startPatht   filest   baset   f(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt	   full_path9   s     c         C@  s   t  j d |  t  j  } | rC t | j d   } d } | | f St  j d |  t  j  } | r d } t | j d   } | | f St  j d |  t  j  } | r t | j d   } t | j d   } | | f Sd S(   s  Parse the output of a test run and return errors, failures.

    Parameters
    ----------
    txt : str
      Text output of a test run, assumed to contain a line of one of the
      following forms::
      
        'FAILED (errors=1)'
        'FAILED (failures=1)'
        'FAILED (errors=1, failures=1)'

    Returns
    -------
    nerr, nfail
      number of errors and failures.
    s   ^FAILED \(errors=(\d+)\)i   i    s   ^FAILED \(failures=(\d+)\)s(   ^FAILED \(errors=(\d+), failures=(\d+)\)i   (   i    i    (   t   ret   searcht	   MULTILINEt   intt   group(   t   txtt   err_mt   nerrt   nfailt   fail_mt   both_m(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   parse_test_output^   s"    


c           C@  s   d d d d d g S(   sE   Return a valid default argv for creating testing instances of ipythons   --quicks   --colors=NoColors   --no-term-titles   --no-banners   --autocall=0(    (    (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   default_argv   s    	c          C@  sq   t    }  d |  j _ t f |  j _ d |  j _ t j d d d t  } | j	 |  j
 _ | j   d |  j
 _ |  S(   s6   Return a config object with good defaults for testing.t   NoColori    t   suffixu   test_hist.sqlitet   deletei'  (   R   t   TerminalInteractiveShellt   colorst   Falset    TerminalTerminalInteractiveShellt
   term_titlet   autocallt   tempfilet   NamedTemporaryFilet   namet   HistoryManagert	   hist_filet   closet   db_cache_size(   t   configR   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   default_config   s    	
c         C@  s.   t  j d d g } |  r* d j |  } n  | S(   sW  
    Return appropriate IPython command line name. By default, this will return
    a list that can be used with subprocess.Popen, for example, but passing
    `as_string=True` allows for returning the IPython command as a string.

    Parameters
    ----------
    as_string: bool
        Flag to allow to return the command as a string.
    s   -mt   IPythont    (   t   syst
   executableR   (   t	   as_stringt   ipython_cmd(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   get_ipython_cmd   s    c      
   C@  sd  | d k r g  } n  d d d g } t   | | } t j j t  } t   } t j j | |   } | | | g } t j j	   }	 d |	 d <x7 |	 j
   D]) \ }
 } t | t  s |
 | f GHq q Wt | d t d t d t d	 |	 } | j d
 t j d j |   pd  \ } } t j |  t j |  } } | rZt j d d |  } n  | | f S(   s  Utility to call 'ipython filename'.

    Starts IPython with a minimal and safe configuration to make startup as fast
    as possible.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of file to be executed (should have .py or .ipy extension).

    options : optional, list
      Extra command-line flags to be passed to IPython.

    commands : optional, list
      Commands to send in on stdin

    Returns
    -------
    (stdout, stderr) of ipython subprocess.
    s   --PromptManager.in_template=""s   --PromptManager.in2_template=""s   --PromptManager.out_template=""t   ignoret   PYTHONWARNINGSt   stdoutt   stderrt   stdint   envt   inputs   
s   \x1b\[[^h]+ht    N(   t   NoneR$   R   R   t   dirnamet   __file__R=   R   t   environt   copyt   itemst
   isinstancet   strR   R   t   communicateR
   t   str_to_bytest   bytes_to_strR   t   sub(   t   fnamet   optionst   commandst   prompt_optst   cmdargst   test_dirR<   t
   full_fnamet   full_cmdRC   t   kt   vt   pt   outt   err(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   ipexec   s*     			
$0RE   c         C@  s   d d l  j } t |  | |  \ } } | r | rp | j d j | j   j    d j | j   j     q t d |  | f   n  | j d j | j   j    d j | j   j     d S(   s3  Utility to call 'ipython filename' and validate output/error.

    This function raises an AssertionError if the validation fails.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of the file to be executed (should have .py or .ipy extension).

    expected_out : str
      Expected stdout of the process.

    expected_err : optional, str
      Expected stderr of the process.

    options : optional, list
      Extra command-line flags to be passed to IPython.

    Returns
    -------
    None
    i    Ns   
s"   Running file %r produced error: %r(   t
   nose.toolst   toolsR_   t   assert_equalR   t   stript
   splitlinest
   ValueError(   RR   t   expected_outt   expected_errRS   RT   t   ntR]   R^   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   ipexec_validate   s    =t   TempFileMixinc           B@  s#   e  Z d  Z d d  Z d   Z RS(   sc   Utility class to create temporary Python/IPython files.

    Meant as a mixin class for test cases.s   .pyc         C@  s+   t  | |  \ } } | |  _ | |  _ d S(   s   Make a valid python temp file.N(   R   t   tmpfileRR   (   t   selft   srct   extRR   R   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   mktmp   s    	c         C@  sA   t  |  d  r= |  j j   y t j |  j  Wq= q= Xn  d  S(   NRk   (   t   hasattrRk   R3   R   t   unlinkRR   (   Rl   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   tearDown&  s    (   t   __name__t
   __module__t   __doc__Ro   Rr   (    (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyRj     s   s8   Testing {0}

In:
  {1!r}
Expected:
  {2!r}
Got:
  {3!r}
c         C@  so   t  |  d t  |  d d   } xJ | D]B \ } } |  |  } | | k s% t t j | | | |    q% Wd S(   s  Utility function for the common case of checking a function with a
    sequence of input/output pairs.

    Parameters
    ----------
    func : callable
      The function to be tested. Should accept a single argument.
    pairs : iterable
      A list of (input, expected_output) tuples.

    Returns
    -------
    None. Raises an AssertionError if any output does not match the expected
    value.
    t	   func_nameRs   s	   <unknown>N(   t   getattrt   AssertionErrort   pair_fail_msgt   format(   t   funct   pairsR0   t   inpt   expectedR]   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   check_pairs9  s    t
   MyStringIOc           B@  s   e  Z d    Z RS(   c         C@  s/   t  j | d t } t t |   j |  d  S(   Nt   encoding(   R
   t   cast_unicodeR   t   superR   t   write(   Rl   t   s(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyR   U  s    (   Rs   Rt   R   (    (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyR   T  s   sE   Did not find {0!r} in printed output (on {1}):
-------
{2!s}
-------
t   AssertPrintsc           B@  s/   e  Z d  Z d e d  Z d   Z d   Z RS(   s   Context manager for testing that code prints certain text.
    
    Examples
    --------
    >>> with AssertPrints("abc", suppress=False):
    ...     print("abcd")
    ...     print("def")
    ... 
    abcd
    def
    R@   c         C@  sL   | |  _  t |  j  t j t f  r6 |  j  g |  _  n  | |  _ | |  _ d  S(   N(   R   RL   R
   t   string_typest   _re_typet   channelt   suppress(   Rl   R   R   R   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   __init__m  s
    		c         C@  sh   t  t |  j  |  _ t   |  _ t |  j d |  j |  _ t t |  j |  j	 rZ |  j n |  j  d  S(   NR   (
   Rw   R9   R   t   orig_streamR   t   bufferR	   t   teet   setattrR   (   Rl   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt	   __enter__t  s    c         C@  s   z | d  k	 r t S|  j j   t t |  j |  j  |  j j	   } x} |  j
 D]r } t | t  r | j |  s t t j | j |  j |    qO | | k sO t t j | |  j |    qO Wt SWd  |  j j   Xd  S(   N(   RF   R*   R   t   flushR   R9   R   R   R   t   getvalueR   RL   R   R   Rx   t   notprinted_msgRz   t   patternR3   (   Rl   t   etypet   valuet	   tracebackt   printedR   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   __exit__z  s    3.(   Rs   Rt   Ru   t   TrueR   R   R   (    (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyR   a  s   	s>   Found {0!r} in printed output (on {1}):
-------
{2!s}
-------
t   AssertNotPrintsc           B@  s   e  Z d  Z d   Z RS(   sg   Context manager for checking that certain output *isn't* produced.
    
    Counterpart of AssertPrintsc         C@  s   z | d  k	 r  |  j j   t S|  j j   t t |  j |  j  |  j	 j
   } x~ |  j D]s } t | t  r | j |  s t t j | j |  j |    q\ | | k s\ t t j | |  j |    q\ Wt SWd  |  j j   Xd  S(   N(   RF   R   R3   R*   R   R   R9   R   R   R   R   R   RL   R   R   Rx   t   printed_msgRz   R   (   Rl   R   R   R   R   R   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyR     s    (   Rs   Rt   Ru   R   (    (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyR     s   c          c@  s?   d d l  m }  |  j } d   |  _ z	 d  VWd  | |  _ Xd  S(   Ni    (   t   warnc          _@  s   d  S(   N(   RF   (   t   at   kw(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   <lambda>  s    (   t   IPython.utilsR   (   R   t	   save_warn(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt	   mute_warn  s    		c         c@  s7   t  |  d  } | j   z	 d VWd t j |   Xd S(   sM    Create an empty, named, temporary file for the duration of the context.
    t   wN(   t   openR3   R   Rq   (   R0   R   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   make_tempfile  s
    
	c         C@  su   t    |  d g } t |  \ } } } t j | d |  t j d |  t j d |  t j d |  | | f S(   s)   test that `ipython [subcommand] -h` workss   -hi    t	   Tracebackt   Optionss
   --help-all(   R=   R   Rh   Rb   t   assert_not_int	   assert_in(   t
   subcommandt   cmdR]   R^   t   rc(    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   help_output_test  s    c         C@  su   t    |  d g } t |  \ } } } t j | d |  t j d |  t j d |  t j d |  | | f S(   s1   test that `ipython [subcommand] --help-all` workss
   --help-alli    R   R   s   Class parameters(   R=   R   Rh   Rb   R   R   (   R   R   R]   R^   R   (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   help_all_output_test  s    (    (    (D   Ru   t
   __future__R    R   R   R9   R.   t
   contextlibR   t   ioR   t
   subprocessR   R   R`   Ra   Rh   R   t   has_noset   ImportErrorR*   t   traitlets.config.loaderR   t   IPython.utils.processR   t   IPython.utils.textR   t   IPython.utils.ioR   R	   R   R
   t   IPython.utils.encodingR   RE   R   t   decR   t   platformt   skip_doctestt	   null_decot   doctest_decoR   R#   t   __test__R$   R6   R=   RF   R_   Ri   t   objectRj   Ry   R   t   PY3R   t   typet   compileR   R   R   R   R   R   R   R   R   (    (    (    sM   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/tools.pyt   <module>   s\   

!%	+				;+			.
