
^Yc           @   s   d  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 d d l m	 Z	 d e
 f d	     YZ d
   Z d e j f d     YZ d   Z d   Z e j j d    Z d   Z d e f d     YZ e j d    Z d S(   s   Coverage plugin for pytest.iN(   t   CoverageExceptioni   (   t   embed(   t   engine(   t   compatt   CoverageErrorc           B   s   e  Z d  Z RS(   s&   Indicates that our coverage is too low(   t   __name__t
   __module__t   __doc__(    (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyR      s   c   	      C   s   d d d g } d d g } d g } | | } |  j  d d  } | d	 } | | d
 g k r d j |  |  } t j |   n  t |  d k r | d  f S| d } | | k r | | k r | | f S| | k r d j |  |  } t j |   n  | S(   Nt   annotatet   htmlt   xmlt   terms   term-missings   skip-coveredt   :i   i    t    s'   invalid choice: "{}" (choose from "{}")s;   output specifier not supported for: "{}" (choose from "{}")(   t   splitt   formatt   argparset   ArgumentTypeErrort   lent   None(	   t   argt   file_choicest   term_choicest   term_modifier_choicest   all_choicest   valuest   report_typet   msgt   report_modifier(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   validate_report   s&    	




		t   StoreReportc           B   s   e  Z d d   Z RS(   c         C   s   | \ } } | | j  | <d  S(   N(   t
   cov_report(   t   selft   parsert	   namespaceR   t   option_stringR   t   file(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   __call__,   s    N(   R   R   R   R%   (    (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyR   +   s   c         C   s>  |  j  d d  } | j d d d d g  d d d	 d
 d t d d d d | j d d t d i  d d d t d d | j d d d d d d d d d | j d d d d t d d | j d d d d t d d | j d d d d d d t d d | j d d d d t d d  | j d! d d d d# d d" d# S($   s    Add options to control coverage.t   covs3   coverage reporting with distributed testing supports   --covt   actiont   appendt   defaultt   metavart   patht   nargst   ?t   constt   destt
   cov_sourcet   helps4   measure coverage for filesystem path (multi-allowed)s   --cov-reportt   types  type of report to generate: term, term-missing, annotate, html, xml (multi-allowed). term, term-missing may be followed by ":skip-covered". annotate, html and xml may be followed by ":DEST" where DEST specifies the output location. Use --cov-report= to not generate any output.s   --cov-configt   stores   .coveragercs.   config file for coverage, default: .coveragercs   --no-cov-on-failt
   store_trues8   do not report coverage if test run fails, default: Falses   --no-covsH   Disable coverage report completely (useful for debuggers) default: Falses   --cov-fail-undert   MINs,   Fail if the total coverage is less than MIN.s   --cov-appends<   do not delete coverage but append to current, default: Falses   --cov-branchs   Enable branch coverage.N(   t   getgroupt	   addoptiont   TrueR   R   t   Falset   intR   (   R!   t   group(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_addoption1   s*    c         C   s3   t  |  k r d Sg  |  D] } | t  k	 r | ^ q S(   s   
    Prepare cov_source so that:

     --cov --cov=foobar is equivalent to --cov (cov_source=None)
     --cov=foo --cov=bar is equivalent to cov_source=['foo', 'bar']
    N(   R8   R   (   R0   R+   (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   _prepare_cov_sourceT   s    c         C   s;   |  j  j r7 t |  j  |  j  } |  j j | d  n  d  S(   Nt   _cov(   t   known_args_namespaceR0   t	   CovPlugint   pluginmanagert   register(   t   early_configR!   t   argst   plugin(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_load_initial_conftests^   s    c         C   sY   |  j  d  rU |  j j d  sU t |  j |  j d t } |  j j | d  qU n  d S(   s(   Activate coverage plugin if appropriate.R0   R>   t   startN(   t   getvalueRA   t	   haspluginR@   t   optionR9   RB   (   t   configRE   (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_configuree   s    R@   c           B   s   e  Z d  Z e d  Z d d d  Z d   Z d   Z d   Z	 e e	 _
 d   Z e e _
 d   Z d   Z e j d	    Z d
   Z d   Z d   Z RS(   s   Use coverage package to produce code coverage reports.

    Delegates all work to a particular implementation based on whether
    this test process is centralised, a distributed master or a
    distributed slave.
    c         C   s]  d |  _ d |  _ d |  _ t j   |  _ d |  _ t |  _	 t |  _
 t |  _ | |  _ t | d t  p t | d t  p t | d d  d k } t | d t  r t |  _ d S|  j j s d g |  j _ n9 t |  j j  d k r	d	 |  j j k r	i  |  j _ n  t |  j j  |  j _ | r@| r@|  j t j  n | rY|  j t j  n  d S(
   s   Creates a coverage pytest plugin.

        We read the rc file that coverage uses to get the data file
        name.  This is needed since we give coverage through it's API
        the data file name.
        t   numprocessest   distloadt   distt   not   no_covNR   i   R   (   R   t   pidR&   t   cov_controllerR   t   StringIOR   t	   cov_totalR9   t   failedt   _startedt	   _disabledt   optionst   getattrR8   R   R=   R0   RG   R   t
   DistMastert   Central(   R    RY   RA   RG   t   is_dist(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   __init__u   s0    										*c            s   | d  k r4 d t f   f d     Y} |   } n  |   j j   j j   j j   j j   j j | |    _   j j	   t
   _   j j j }   j j d  k r t | d  r | j   j _ n  d  S(   Nt   Configc              s   e  Z   j Z RS(    (   R   R   RY   RJ   (    (   R    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyR_      s   t
   fail_under(   R   t   objectRY   R0   R   t
   cov_configt
   cov_appendt
   cov_branchRS   RG   R8   RW   R&   RK   t   cov_fail_undert   hasattrR`   (   R    t   controller_clsRK   t   nodeidR_   Rb   (    (   R    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyRG      s     						!c         C   s   t  | j d  S(   Nt
   slaveinput(   Rf   RK   (   R    t   session(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt	   _is_slave   s    c         C   s   |  j  j r t |  _ d St j   |  _ |  j |  rt | j j	 j
 d t | d   } |  j t j | j |  n |  j s |  j t j  n  d S(   sA   At session start determine our implementation and delegate to it.Nt   slaveidRh   (   RY   RQ   R8   RX   t   ost   getpidRR   Rk   RK   Ri   t   getRZ   RG   R   t	   DistSlaveRW   R\   (   R    Rj   Rh   (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_sessionstart   s    		c         C   s   |  j  j |  d S(   sl   Delegate to our implementation.

        Mark this hook as optional in case xdist is not installed.
        N(   RS   t   configure_node(   R    t   node(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_configure_node   s    c         C   s   |  j  j | |  d S(   sl   Delegate to our implementation.

        Mark this hook as optional in case xdist is not installed.
        N(   RS   t   testnodedown(   R    Rs   t   error(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_testnodedown   s    c         C   s   |  j  o |  j j S(   N(   RV   RY   t   no_cov_on_fail(   R    (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   _should_report   s    c         C   s%   |  j  j } | d  k	 o$ |  j | k  S(   N(   RY   Re   R   RU   (   R    Re   (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   _failed_cov_total   s    c         c   s   d  V|  j  r d  St j |  } t | j  |  _ |  j d  k	 rR |  j j   n  |  j	 |  r |  j
   r y |  j j |  j  |  _ Wn& t k
 r } t j d |   n X|  j d  k	 s t d   |  j   r | j d 7_ q n  d  S(   Ns   Failed to generate report: %s
s$   Test coverage should never be `None`i   (   RX   R   t   SessionWrappert   boolt   testsfailedRV   RS   R   t   finishRk   Ry   t   summaryR   RU   R    t   pytestt
   UsageErrort   AssertionErrorRz   (   R    Rj   t   compat_sessiont   exc(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_runtestloop   s     	c         C   s3  |  j  rI d } | j d | d t d t | j j d d d |  d  S|  j d  k r\ d  S|  j d  k ro d  S| j d |  j j	   d  |  j
 j d  k	 r/|  j
 j d	 k r/|  j |  j
 j k  r i t d 6t d 6} d
 |  j
 j |  j f } n& i t d 6} d |  j
 j |  j f } | j | |  n  d  S(   Ns&   Coverage disabled via --no-cov switch!s   WARNING: %s
t   redt   boldt   codes   COV-U1t   messages   
i    sH   FAIL Required test coverage of %d%% not reached. Total coverage: %.2f%%
t   greens?   Required test coverage of %d%% reached. Total coverage: %.2f%%
(   RX   t   writeR8   RK   t   warnRS   R   RU   R   RH   RY   Re   (   R    t   terminalreporterR   t   markup(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_terminal_summary   s&    	$c         C   s+   t  j   |  j k r' t j   |  _ n  d  S(   N(   Rm   Rn   RR   R   t   initR&   (   R    t   item(    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_runtest_setup  s    c         C   s/   |  j  d  k	 r+ t j |  j   d  |  _  n  d  S(   N(   R&   R   R   t   cleanup(   R    R   (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   pytest_runtest_teardown  s    N(   R   R   R   R8   R^   R   RG   Rk   Rq   Rt   t   optionalhookRw   Ry   Rz   R   t   hookwrapperR   R   R   R   (    (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyR@   m   s   '									 	c         C   sD   |  j  j j d  r@ |  j  j j d  } | j r@ | j j Sn  d S(   sE   A pytest fixture to provide access to the underlying coverage object.R>   N(   RK   RA   RI   t	   getpluginRS   R&   R   (   t   requestRE   (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyR&   %  s
    	(   R   Rm   R   R   t   coverage.miscR    R   R   R   R   t	   ExceptionR   R   t   ActionR   R<   R=   t   markt   tryfirstRF   RL   Ra   R@   t   fixtureR&   (    (    (    s5   /tmp/pip-build-hU8Cw8/pytest-cov/pytest_cov/plugin.pyt   <module>   s    		#	
	