o
    Ďiz                     @   s   d Z ddl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m
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.    N)mock)core)tracec                   @   s0   e Zd ZdZejd	ddZejd
ddZdS )BaseTestCasez'Shared test case for Python Fire tests..*Tc           
   
   c   s<   t  }t  }zGtjtd|# tjtd| dV  W d   n1 s)w   Y  W d   n1 s8w   Y  W |sPtj|  tj	|  n|sdtj|  tj	|  w w d||fd||ffD ],\}}}| }	|du r|	rt
d||	f qot||	tjtjB st
d||	|f qod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)ioStringIOr   patchobjectsysr   writegetvaluer   AssertionErrorresearchDOTALL	MULTILINE)
selfr   r   capture	stdout_fp	stderr_fpnameregexpfpvalue r   J/home/jeff/fluffinator/venv/lib/python3.10/site-packages/fire/testutils.pyassertOutputMatches    sF   z BaseTestCase.assertOutputMatchesc                 c   s    | j |dQ | tj2 zdV  W n$ tjy8 } z|j|kr+td|j|f | |jtj  d}~ww W d   n1 sCw   Y  W d   dS W d   dS 1 s[w   Y  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   FireExitcoder   assertIsInstancer   	FireTrace)r   r"   r   excr   r   r   assertRaisesFireExitF   s&   

"z!BaseTestCase.assertRaisesFireExitN)r   r   T)r   )__name__
__module____qualname____doc__
contextlibcontextmanagerr   r&   r   r   r   r   r      s    %r   c              	   c   s8    t  }t |  z| V  W t | dS t | w )z>Context manager to mock a directory change and revert on exit.N)osgetcwdchdir)	directorycwdirr   r   r   ChangeDirectory`   s   
r2   )r*   r+   r	   r-   r   r   unittestr   firer   r   TestCaser   r,   r2   mainskipskipIfr   r   r   r   <module>   s    C

