
^Yc           @` s  d  Z  d d l m Z m Z 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
 Z
 d d l Z d d l m Z e	 j d d k  r d d l m Z n e Z e Z e Z d e j j f d     YZ d" Z d# d$ d% d& f Z g  e D]= \ Z Z e e	 j k  r e d  e e  e e  f ^ q Z g  e D] Z  e e   ^ qCe Z e j! e d  e d  j" e   Z# [ [ [ e j! d  Z$ d   Z% d e& f d     YZ' e j( d    Z) d   Z* d   Z+ d   Z, d   Z- d  e& f d!     YZ. d S('   s'  
    report test results in JUnit-XML format,
    for use with Jenkins and build integration servers.


Based on initial code from Ross Lawley.

Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/
src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
i    (   t   absolute_importt   divisiont   print_functionN(   t   filename_argi   (   t   opent   Junitc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR      s   i	   i
   i   i    i~   i   i  i   i  i   i s   %s-%ss   [^%s]t    s   \.py$c         C` s1   d   } t  j j t j | t  j j |     S(   Nc         S` s>   t  |  j    } | d k r, t d  | St d  | Sd  S(   Ni   s   #x%02Xs   #x%04X(   t   ordt   groupt   unicode(   t   matchobjt   i(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   repl:   s    (   t   pyt   xmlt   rawt   illegal_xml_ret   subt   escape(   t   argR   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   bin_xml_escape9   s    	t   _NodeReporterc           B` s   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d d  Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   c         C` sR   | |  _  | |  _ |  j j |  _ d |  _ g  |  _ g  |  _ d  |  _ i  |  _ d  S(   Ni    (	   t   idR   t	   add_statst   durationt
   propertiest   nodest   Nonet   testcaset   attrs(   t   selft   nodeidR   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   __init__E   s    						c         C` s-   |  j  j t |  j  |  j j |  d  S(   N(   R   R   t   typeR   R   t   append(   R    t   node(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR$   P   s    c         C` s&   |  j  j t |  t |  f  d  S(   N(   R   R$   t   strR   (   R    t   namet   value(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   add_propertyT   s    c         C` sH   |  j  rD t j  g  |  j  D]$ \ } } t j d | d |  ^ q  Sd S(   sB   Return a Junit node containing custom properties, if any.
        R'   R(   R   (   R   R   t   property(   R    R'   R(   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   make_properties_nodeW   s
    	2c         C` s   |  j  s t  t | j  } | d  } |  j j rN | j d |  j j  n  i d j |  d 6t | d  d 6| j	 d d 6} | j	 d d  k	 r | j	 d | d <n  t | d	  r | j | d	 <n  | |  _ d  S(
   Nii    t   .t	   classnameR'   t   filei   t   linet   url(   R   t   AssertionErrort   mangle_test_addressR!   R   t   prefixt   insertt   joinR   t   locationR   t   hasattrR0   R   (   R    t
   testreportt   namest
   classnamesR   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   record_testreporta   s    
c         C` sS   t  j d |  j |  j  } | j |  j    x |  j D] } | j |  q8 W| S(   Nt   time(   R   R   R   R   R$   R+   R   (   R    R   R%   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   to_xmlr   s
    c         C` s/   t  |  } | | d | } |  j |  d  S(   Nt   message(   R   R$   (   R    t   kindR>   t   dataR%   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   _add_simpley   s    c         C` s]   xV d D]N } t  | d |  } | r t  t d |  } |  j | t |    q q Wd  S(   Nt   outt   errt   capstds   system-(   s   outs   err(   t   getattrR   R$   R   (   R    t   reportt   capnamet   contentt   tag(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   write_captured_output~   s
    c         C` s   |  j  d  d  S(   Nt   passed(   R   (   R    RF   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   append_pass   s    c         C` s   t  | d  r% |  j t j d  n t  | j d  rI | j j j } n3 t | j t t	 f  rm | j } n t	 | j  } t
 |  } t j d |  } | j t
 | j   |  j |  d  S(   Nt   wasxfails%   xfail-marked test passes unexpectedlyt	   reprcrashR>   (   R7   RA   R   t   skippedt   longreprRN   R>   t
   isinstanceR   R&   R   t   failureR$   (   R    RF   R>   t   fail(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   append_failure   s    
c         C` s)   |  j  t j t | j  d d  d  S(   NR>   s   collection failure(   R$   R   t   errorR   RP   (   R    RF   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   append_collect_error   s    c         C` s   |  j  t j d | j  d  S(   Ns   collection skipped(   RA   R   RO   RP   (   R    RF   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   append_collect_skipped   s    c         C` sD   t  | d d   d k r! d } n d } |  j t j | | j  d  S(   Nt   whent   teardowns   test teardown failures   test setup failure(   RE   R   RA   R   RU   RP   (   R    RF   t   msg(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   append_error   s
    	c         C` s   t  | d  r+ |  j t j d | j  nc | j \ } } } | j d  r_ t | d  } n  |  j t j d | | | f d d d |  |  j	 |  d  S(	   NRM   s   expected test failures	   Skipped: i	   s	   %s:%s: %sR#   s   pytest.skipR>   (
   R7   RA   R   RO   RM   RP   t
   startswithR   R$   RJ   (   R    RF   t   filenamet   linenot
   skipreason(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   append_skipped   s    
c         ` s;   |  j    j d d    |  j j     f d   |  _  d  S(   Nt   indenti    c           ` s   t  j j    S(   N(   R   R   R   (    (   R@   (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   <lambda>   s    (   R=   R   t   __dict__t   clear(   R    (    (   R@   s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   finalize   s    N(   R   R   R"   R$   R)   R+   R;   R=   R   RA   RJ   RL   RT   RV   RW   R[   R`   Re   (    (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR   D   s   				
									c         C` sg   |  j  j d d d d  t |  j d d  } | d k	 rV | j |  j  j  } | j Sd   } | Sd S(   s   Add extra xml properties to the tag for the calling test.
    The fixture is callable with ``(name, value)``, with value being automatically
    xml-encoded.
    t   codet   C3R>   s.   record_xml_property is an experimental featuret   _xmlc         S` s   d  S(   N(    (   R'   R(   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   add_property_noop   s    N(   R%   t   warnRE   t   configR   t   node_reporterR!   R)   (   t   requestR   Rl   Ri   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   record_xml_property   s    	c         C` s   |  j  d  } | j d d d d d d d d	 d
 t j t d d d d  d d | j d d d d d d d d  d d |  j d d d d d  S(   Ns   terminal reportings
   --junitxmls   --junit-xmlt   actiont   storet   destt   xmlpatht   metavart   pathR#   t   optnamet   defaultt   helps1   create junit-xml style report file at given path.s   --junitprefixs   --junit-prefixR&   s0   prepend prefix to classnames in junit-xml outputt   junit_suite_names    Test suite name for JUnit reportt   pytest(   t   getgroupt	   addoptiont	   functoolst   partialR   R   t   addini(   t   parserR
   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_addoption   s     		c         C` s`   |  j  j } | r\ t |  d  r\ t | |  j  j |  j d   |  _ |  j j |  j  n  d  S(   Nt
   slaveinputRx   (	   t   optionRr   R7   t   LogXMLt   junitprefixt   getiniRh   t   pluginmanagert   register(   Rk   Rr   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_configure   s    $c         C` s5   t  |  d d   } | r1 |  ` |  j j |  n  d  S(   NRh   (   RE   R   Rh   R   t
   unregister(   Rk   R   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_unconfigure   s    c         C` s   |  j  d  \ } } } | j d  } y | j d  Wn t k
 rK n X| d j d d  | d <t j d | d  | d <| d c | | 7<| S(	   Nt   [s   ::s   ()i    t   /R,   R   i(   t	   partitiont   splitt   removet
   ValueErrort   replacet
   _py_ext_reR   (   t   addressRt   t   possible_open_brackett   paramsR9   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR2      s    R   c           B` s   e  Z d  d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d	   Z
 d
   Z d   Z d   Z d   Z d   Z RS(   Ry   c         C` s   t  j j t  j j |   } t  j j t  j j |   |  _ | |  _ | |  _ t	 j
 d d d d g d  |  _ i  |  _ g  |  _ g  |  _ g  |  _ d |  _ d  S(   NRU   RK   RR   RO   i    (   t   osRt   t
   expandusert
   expandvarst   normpatht   abspatht   logfileR3   t
   suite_namet   dictt   fromkeyst   statst   node_reporterst   node_reporters_orderedt   global_propertiest   open_reportst   cnt_double_fail_tests(   R    R   R3   R   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR"     s    !						c         C` sY   t  | d |  } t  | d d   } |  j j | | f  } | d  k	 rU | j   n  d  S(   NR!   R%   (   RE   R   R   t   popRe   (   R    RF   R!   t	   slavenodet   reporter(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyRe     s
    c         C` sz   t  | d |  } t  | d d   } | | f } | |  j k rJ |  j | St | |   } | |  j | <|  j j |  | S(   NR!   R%   (   RE   R   R   R   R   R$   (   R    RF   R!   R   t   keyR   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyRl   !  s    c         C` s)   | |  j  k r% |  j  | c d 7<n  d  S(   Ni   (   R   (   R    R   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR   3  s    c         C` s    |  j  |  } | j |  | S(   N(   Rl   R;   (   R    RF   R   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   _opentestcase7  s    c         ` s  d }   j r@   j d k rB|  j    } | j    qBn  j r  j d k r t   d d   t   d d   t     f d   |  j D d  } | r |  j	 |  |  j
 d 7_
 q n  |  j    }   j d k r
| j    |  j j    qB| j    n(   j rB|  j    } | j    n  |  j      j d k r|  j    } | j    |  j	    t   d d   t   d d   t     f d   |  j D d  } | r|  j j |  qn  d S(	   s  handle a setup/call/teardown report, generating the appropriate
        xml tags as necessary.

        note: due to plugins like xdist, this hook may be called in interlaced
        order with reports from other nodes. for example:

        usual call order:
            -> setup node1
            -> call node1
            -> teardown node1
            -> setup node2
            -> call node2
            -> teardown node2

        possible call order in xdist:
            -> setup node1
            -> call node1
            -> setup node2
            -> call node2
            -> teardown node2
            -> teardown node1
        t   callRY   t	   worker_idt
   item_indexc         3` sW   |  ]M } | j    j  k r t | d  d   k r t | d d   k r | Vq d S(   R   R   N(   R!   RE   R   (   t   .0t   rep(   RF   t	   report_iit
   report_wid(    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pys	   <genexpr>^  s    i   c         3` sW   |  ]M } | j    j  k r t | d  d   k r t | d d   k r | Vq d S(   R   R   N(   R!   RE   R   (   R   R   (   RF   R   R   (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pys	   <genexpr>{  s    N(   R   RK   RX   R   RL   t   failedRE   t   nextR   Re   R   RT   R$   R[   RO   R`   t   update_testcase_durationRJ   R   (   R    RF   t   close_reportR   (    (   RF   R   R   s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_runtest_logreport<  sF    					c         C` s.   |  j  |  } | j t | d d  7_ d S(   s   accumulates total duration for nodeid from given report and updates
        the Junit.testcase with the new total if already created.
        R   g        N(   Rl   R   RE   (   R    RF   R   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR     s    c         C` sE   | j  sA |  j |  } | j r1 | j |  qA | j |  n  d  S(   N(   RK   R   R   RV   RW   (   R    RF   R   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_collectreport  s
    		c         C` sB   |  j  d  } | j j d d d d  | j t j d |  d  S(   Nt   internalR-   Ry   R'   s   internal error(   Rl   R   t   updateRA   R   RU   (   R    t   excreprR   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_internalerror  s    c         C` s   t  j    |  _ d  S(   N(   R<   t   suite_start_time(   R    (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_sessionstart  s    c         C` sK  t  j j t  j j |  j   } t  j j |  sC t  j |  n  t |  j d d d } t j   } | |  j	 } |  j
 d |  j
 d |  j
 d |  j
 d |  j } | j d  | j t j |  j   g  |  j D] } | j   ^ q d	 |  j d
 |  j
 d d |  j
 d d |  j
 d d | d d | j d d   | j   d  S(   Nt   wt   encodings   utf-8RK   RR   RO   RU   s&   <?xml version="1.0" encoding="utf-8"?>R'   t   errorst   failurest   skipst   testsR<   s   %.3fRa   i    (   R   Rt   t   dirnameR   R   t   isdirt   makedirsR   R<   R   R   R   t   writeR   t	   testsuitet   _get_global_properties_nodeR   R=   R   R   t   close(   R    R   R   t   suite_stop_timet   suite_time_deltat   numtestst   x(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_sessionfinish  s&    !+
	"	c         C` s   | j  d d |  j  d  S(   Nt   -s   generated xml file: %s(   t	   write_sepR   (   R    t   terminalreporter(    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   pytest_terminal_summary  s    	c         C` s&   |  j  j t |  t |  f  d  S(   N(   R   R$   R&   R   (   R    R'   R(   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   add_global_property  s    c         C` sH   |  j  rD t j g  |  j  D]$ \ } } t j d | d |  ^ q  Sd S(   sB   Return a Junit node containing custom properties, if any.
        R'   R(   R   (   R   R   R   R*   (   R    R'   R(   (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR     s
    	2(   R   R   R"   Re   Rl   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyR     s   					H							(   i	   i
   i   (   i    i~   (   i   i  (   i   i  (   i   i (/   t   __doc__t
   __future__R    R   R   R|   R   R   t   ret   sysR<   Ry   t   _pytest.configR   t   version_infot   codecsR   t   chrt   unichrR&   R   t   intt   longR   t	   NamespaceR   t   _legal_charst   _legal_rangest   lowt   hight
   maxunicodet   _legal_xml_reR   t   compileR5   R   R   R   t   objectR   t   fixtureRn   R   R   R   R2   R   (    (    (    s0   /tmp/pip-build-hU8Cw8/pytest/_pytest/junitxml.pyt   <module>
   sB   	G#(	|				