U
    ,c8                     @   s   d Z ddlmZ ddlm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	Z	ddl
mZ ddl
mZ ddlZddlZG dd	 d	e	jZejd
d Ze	jZe	jZe	jZdS )z"Utilities for Python Fire's tests.    )absolute_import)division)print_functionN)core)tracec                       s@   e Zd ZdZejd
ddZ fddZejddd	Z  Z	S )BaseTestCasez'Shared test case for Python Fire tests..*Tc           
   
   c   s   t  }t  }zBtj	td|( tj	td| dV  W 5 Q R X W 5 Q R X W 5 |sxtj|  tj|  X d||fd||ffD ]X\}}}| }	|dkr|	rt
d||	f qt||	tjtjB st
d||	|f qdS )a  Asserts that the context generates stdout and stderr matching regexps.

    Note: If wrapped code raises an exception, stdout and stderr will not be
      checked.

    Args:
      stdout: (str) regexp to match against stdout (None will check no stdout)
      stderr: (str) regexp to match against stderr (None will check no stderr)
      capture: (bool, default True) do not bubble up stdout or stderr

    Yields:
      Yields to the wrapped context.
    stdoutstderrNz%s: Expected no output. Got: %rz%s: Expected %r to match %r)sixStringIOsysr	   writegetvaluer
   mockpatchobjectAssertionErrorresearchDOTALL	MULTILINE)
selfr	   r
   captureZ	stdout_fpZ	stderr_fpnameregexpfpvalue r   2/tmp/pip-unpacked-wheel-1xt1w7un/fire/testutils.pyassertOutputMatches%   s,    z BaseTestCase.assertOutputMatchesc                    s4   t jjdkrtt| j||S tt| j||S d S )N   )r   version_infomajorsuperr   ZassertRaisesRegexpassertRaisesRegex)r   argskwargs	__class__r   r   r%   K   s    zBaseTestCase.assertRaisesRegexc                 c   s   | j |d| | tjd z
dV  W nR tjk
rx } z2|j|krVtd|j|f | |jtj  W 5 d}~X Y nX W 5 Q R X W 5 Q R X dS )a^  Asserts that a FireExit error is raised in the context.

    Allows tests to check that Fire's wrapper around SystemExit is raised
    and that a regexp is matched in the output.

    Args:
      code: The status code that the FireExit should contain.
      regexp: stdout must match this regex.

    Yields:
      Yields to the wrapped context.
    )r
   NzIncorrect exit code: %r != %r)	r    assertRaisesr   ZFireExitcoder   ZassertIsInstancer   Z	FireTrace)r   r+   r   excr   r   r   assertRaisesFireExitQ   s    

z!BaseTestCase.assertRaisesFireExit)r   r   T)r   )
__name__
__module____qualname____doc__
contextlibcontextmanagerr    r%   r-   __classcell__r   r   r(   r   r   "   s   %r   c              	   c   s.   t  }t |  z
| V  W 5 t | X dS )z>Context manager to mock a directory change and revert on exit.N)osgetcwdchdir)	directoryZcwdirr   r   r   ChangeDirectoryk   s
    

r9   )r1   
__future__r   r   r   r2   r5   r   r   ZunittestZfirer   r   r   r   ZTestCaser   r3   r9   mainskipZskipIfr   r   r   r   <module>   s$   I
