Format: application/x-python-code
⬇ Download File (12.3 KB)
   1  o
   2  
   3  ��h�6�@s�dZddlZddlmZddlZddlZddlZddlZddlm	Z	ddl
   4  mZddlm
   5  Z
   6  mZddlmZddlmZdd	lmZdd
   7  lmZddlmZmZddlmZd
   8  dlmZd
   9  dlmZej dkrjej!nej"Z#e#de$de%e$de%e$fdd��Z&dd�Z'de'_(dd�Z)dd�Z*d:dd�Z+d;d d!�Z,	"	d<d#d$�Z-Gd%d&�d&ej.�Z/d'Z0d(d)�Z1eZ2e3e�4d"��Z5d*Z6Gd+d,�d,e7�Z8d-Z9Gd.d/�d/e8�Z:e	d0d1��Z;e	d2d3��Z<d4d5�Z=d=d6d7�Z>d=d8d9�Z?dS)>zWGeneric testing tools.
  10  
  11  Authors
  12  -------
  13  - Fernando Perez <Fernando.Perez@berkeley.edu>
  14  �N)�Path)�contextmanager)�StringIO)�Popen�PIPE)�patch)�Config)�get_output_error_code)�list_strings)�temp_pyfile�Tee)�	py3compat�)�
  15  decorators)�skipdoctest�win32�	startPath�files�returncs0t|t�sJ�tj�|�d��fdd�|D�S)a�Make full paths for all the listed files, based on startPath.
  16  
  17      Only the base part of startPath is kept, since this routine is typically
  18      used with a script's ``__file__`` variable as startPath. The base of startPath
  19      is then prepended to all the listed files, forming the output list.
  20  
  21      Parameters
  22      ----------
  23      startPath : string
  24        Initial path to use as the base for the results.  This path is split
  25        using os.path.split() and only its first component is kept.
  26  
  27      files : list
  28        One or more files.
  29  
  30      Examples
  31      --------
  32  
  33      >>> full_path('/foo/bar.py',['a.txt','b.txt'])
  34      ['/foo/a.txt', '/foo/b.txt']
  35  
  36      >>> full_path('/foo',['a.txt','b.txt'])
  37      ['/a.txt', '/b.txt']
  38  
  39      rcsg|]	}tj��|��qS�)�os�path�join)�.0�f��baser�Kc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/testing/tools.py�
  40  <listcomp>Cszfull_path.<locals>.<listcomp>)�
  41  isinstance�listrr�split)rrrrr�	full_path&sr"cCs�t�d|tj�}|rt|�d��}d}||fSt�d|tj�}|r.d}t|�d��}||fSt�d|tj�}|rJt|�d��}t|�d��}||fSdS)aParse the output of a test run and return errors, failures.
  42  
  43      Parameters
  44      ----------
  45      txt : str
  46        Text output of a test run, assumed to contain a line of one of the
  47        following forms::
  48  
  49          'FAILED (errors=1)'
  50          'FAILED (failures=1)'
  51          'FAILED (errors=1, failures=1)'
  52  
  53      Returns
  54      -------
  55      nerr, nfail
  56        number of errors and failures.
  57      z^FAILED \(errors=(\d+)\)rrz^FAILED \(failures=(\d+)\)z(^FAILED \(errors=(\d+), failures=(\d+)\)�)rr)�re�search�	MULTILINE�int�group)�txtZerr_mZnerrZnfailZfail_mZboth_mrrr�parse_test_outputFs$�r*FcCsgd�S)zEReturn a valid default argv for creating testing instances of ipython)z--quickz--colors=NoColorz--no-term-titlez--no-bannerz--autocall=0rrrrr�default_argvtsr+cCsNt�}d|j_d|j_d|j_tjddd�}t|j	�|j
  58  _|��d|j
  59  _
  60  |S)z6Return a config object with good defaults for testing.ZNoColor�Frztest_hist.sqliteF)�suffix�deletei')r�TerminalInteractiveShell�colorsZ TerminalTerminalInteractiveShellZ
  61  term_titleZautocall�tempfile�NamedTemporaryFiler�nameZHistoryManagerZ	hist_file�closeZ
  62  db_cache_size)�configrrrr�default_config}sr6cCstjddg}|r
  63  d�|�}|S)aW
  64      Return appropriate IPython command line name. By default, this will return
  65      a list that can be used with subprocess.Popen, for example, but passing
  66      `as_string=True` allows for returning the IPython command as a string.
  67  
  68      Parameters
  69      ----------
  70      as_string: bool
  71          Flag to allow to return the command as a string.
  72      z-mZIPython� )�sys�
  73  executabler)�	as_string�ipython_cmdrrr�get_ipython_cmd�s
  74  r<rcCs�d}|durg}t�|}tj�t�}t�}tj�||�}||d|g}tj��}	d|	d<|	�	dd�|	�
  75  �D]\}
  76  }t|t�sFt
  77  |
  78  |�q8t|ttt|	d�}|jt�d�|��p[dd	�\}
  79  }t�|
  80  �t�|�}
  81  }|
  82  rtt�d
  83  d|
  84  �}
  85  |
  86  |fS)aUtility to call 'ipython filename'.
  87  
  88      Starts IPython with a minimal and safe configuration to make startup as fast
  89      as possible.
  90  
  91      Note that this starts IPython in a subprocess!
  92  
  93      Parameters
  94      ----------
  95      fname : str, Path
  96        Name of file to be executed (should have .py or .ipy extension).
  97  
  98      options : optional, list
  99        Extra command-line flags to be passed to IPython.
 100  
 101      commands : optional, list
 102        Commands to send in on stdin
 103  
 104      Returns
 105      -------
 106      ``(stdout, stderr)`` of ipython subprocess.
 107      TNz--�ignoreZPYTHONWARNINGSZPYCHARM_HOSTED)�stdout�stderr�stdin�env�
 108  )�inputz\x1b\[[^h]+h�)r+rr�dirname�__file__r<r�environ�copy�pop�itemsr�str�printrr�communicater
 109  �encode�decoder$�sub)�fname�options�commands�__tracebackhide__ZcmdargsZtest_dirr;Z
 110  full_fnameZfull_cmdrA�k�v�p�out�errrrr�ipexec�s*
 111  
 112  
 113  
 114  � rZrDcCs�d}t|||�\}}|r+|r#d�|�����d�|�����ks"J�ntd||f��d�|�����d�|�����ks?J�dS)a9Utility to call 'ipython filename' and validate output/error.
 115  
 116      This function raises an AssertionError if the validation fails.
 117  
 118      Note that this starts IPython in a subprocess!
 119  
 120      Parameters
 121      ----------
 122      fname : str, Path
 123        Name of the file to be executed (should have .py or .ipy extension).
 124  
 125      expected_out : str
 126        Expected stdout of the process.
 127  
 128      expected_err : optional, str
 129        Expected stderr of the process.
 130  
 131      options : optional, list
 132        Extra command-line flags to be passed to IPython.
 133  
 134      Returns
 135      -------
 136      None
 137      TrBz"Running file %r produced error: %rN)rZr�strip�
 138  splitlines�
 139  ValueError)rQZexpected_outZexpected_errrRrSrTrXrYrrr�ipexec_validate�s
 140  ��
 141  �r^c@s2eZdZdZddd�Zdd�Zdd�Zd	d
 142  �ZdS)
 143  �
 144  TempFileMixinzcUtility class to create temporary Python/IPython files.
 145  
 146      Meant as a mixin class for test cases.�.pycCs0t||�}t|d�s
 147  g|_|j�|�||_dS)zMake a valid python temp file.�tmpsN)r�hasattrra�appendrQ)�self�src�extrQrrr�mktmp
 148  s
 149  
 150  
 151  
 152  zTempFileMixin.mktmpcCs6t|d�r|jD]}zt�|�WqYqdSdS)Nra)rbrar�unlink)rdrQrrr�tearDowns
 153  
 154  �zTempFileMixin.tearDowncCs|S�Nr�rdrrr�	__enter__ szTempFileMixin.__enter__cCs|��dSrj)ri)rd�exc_type�	exc_value�	tracebackrrr�__exit__#szTempFileMixin.__exit__N)r`)�__name__�
 155  __module__�__qualname__�__doc__rgrirlrprrrrr_s
 156  r_z8Testing {0}
 157  
 158  In:
 159    {1!r}
 160  Expected:
 161    {2!r}
 162  Got:
 163    {3!r}
 164  cCsNd}t|dt|dd��}|D]\}}||�}||ks$Jt�||||���qdS)a�Utility function for the common case of checking a function with a
 165      sequence of input/output pairs.
 166  
 167      Parameters
 168      ----------
 169      func : callable
 170        The function to be tested. Should accept a single argument.
 171      pairs : iterable
 172        A list of (input, expected_output) tuples.
 173  
 174      Returns
 175      -------
 176      None. Raises an AssertionError if any output does not match the expected
 177      value.
 178      T�	func_namerqz	<unknown>N)�getattr�
 179  pair_fail_msg�format)�func�pairsrTr3�inp�expectedrXrrr�check_pairs.s�r}zEDid not find {0!r} in printed output (on {1}):
 180  -------
 181  {2!s}
 182  -------
 183  c@s*eZdZdZddd�Zdd�Zdd	�Zd
 184  S)�AssertPrintsz�Context manager for testing that code prints certain text.
 185  
 186      Examples
 187      --------
 188      >>> with AssertPrints("abc", suppress=False):
 189      ...     print("abcd")
 190      ...     print("def")
 191      ...
 192      abcd
 193      def
 194      r>TcCs0||_t|jttf�r|jg|_||_||_dSrj)�srrK�_re_type�channel�suppress)rdrr�r�rrr�__init__\s
 195  
 196  
 197  zAssertPrints.__init__cCsNtt|j�|_t�|_t|j|jd�|_tt|j|j	r!|j�dS|j�dS)N)r�)
 198  rvr8r��orig_stream�
 199  MyStringIO�bufferr�tee�setattrr�rkrrrrlcs&zAssertPrints.__enter__cCs�d}zP|durW|j��dS|j��tt|j|j�|j��}|j	D]&}t
 200  |t�r<|�|�s;Jt
 201  �|j|j|���q$||vsJJt
 202  �||j|���q$W|j��dS|j��w�NTF)r�r4�flushr�r8r�r�r��getvaluerrr�r%�notprinted_msgrx�pattern�rd�etype�valuerorTZprintedrrrrrpis
 203  �
 204  
 205  
 206  "zAssertPrints.__exit__N)r>T)rqrrrsrtr�rlrprrrrr~Ps
 207  
 208  r~z>Found {0!r} in printed output (on {1}):
 209  -------
 210  {2!s}
 211  -------
 212  c@seZdZdZdd�ZdS)�AssertNotPrintszcContext manager for checking that certain output *isn't* produced.
 213  
 214      Counterpart of AssertPrintscCs�d}zU|dur|j��W|j��dS|j��tt|j|j�|j��}|j	D]&}t
 215  |t�rA|�|�r@Jt
 216  �|j|j|���q)||vsOJt
 217  �||j|���q)W|j��dS|j��wr�)r�r4r�r�r8r�r�r�r�rrr�r%�printed_msgrxr�r�rrrrp�s&
 218  
 219  �
 220  
 221  
 222  
 223  ��zAssertNotPrints.__exit__N)rqrrrsrtrprrrrr��sr�ccs:�ddlm}|j}dd�|_z	dVW||_dS||_w)Nr)�warnc_sdSrjr)�a�kwrrr�<lambda>�szmute_warn.<locals>.<lambda>)�
 224  IPython.utilsr�)r�Z	save_warnrrr�	mute_warn�s�
 225  r�c	cs8�t|ddd���zdVWt�|�dSt�|�w)zGCreate an empty, named, temporary file for the duration of the context.�wzutf-8)�encodingN)�openr4rrh)r3rrr�
 226  make_tempfile�s
 227  �r�cs t|��d�fdd�	}td|�S)aTemporarily replace the input() function to return the given values
 228  
 229      Use as a context manager:
 230  
 231      with fake_input(['result1', 'result2']):
 232          ...
 233  
 234      Values are returned in order. If input() is called again after the last value
 235      was used, EOFError is raised.
 236      rDc
 237  s.zt��WSty}ztd�|�d}~ww)NzNo more inputs given)�next�
 238  StopIteration�EOFError)�prompt�e��itrr�
 239  mock_input�s
 240  
 241  ��zfake_input.<locals>.mock_inputzbuiltins.inputN�rD)�iterr)Zinputsr�rr�r�
 242  fake_input�s
 243  r�cC�Xt�|dg}t|�\}}}|dksJ|��d|vsJ�d|vs"J�d|vs(J�||fS)z)test that `ipython [subcommand] -h` worksz-hr�	Traceback�Options�
 244  --help-all�r<r	��
 245  subcommand�cmdrXrY�rcrrr�help_output_test��r�cCr�)z1test that `ipython [subcommand] --help-all` worksr�rr�r��Classr�r�rrr�help_all_output_test�r�r�r,)Nr)rDNrr�)@rtr�pathlibrr$r8r1Zunittest�
 246  contextlibr�ior�
 247  subprocessrrZ
 248  unittest.mockrZtraitlets.config.loaderrZIPython.utils.processr	ZIPython.utils.textr
 249  ZIPython.utils.iorrr�r
 250  rDr�decr�platformZskip_doctestZ	null_decoZdoctest_decorKr r"r*Z__test__r+r6r<rZr^ZTestCaser_rwr}r��type�compiler�r��objectr~r�r�r�r�r�r�r�rrrr�<module>s\ +	
 251  
 252  :
 253  �/",
 254  	
 255  
 256