

Nc              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 m Z d d l	 m
 Z
 d g Z d   Z d e j f d     YZ d	 e j f d
     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   s  
This module scans all ``*.rst`` files below ``docs/`` for example code. Example
code is discoved by checking for lines containing the ``.. literalinclude:: ``
directives.

An example consists of two consecutive literalinclude directives. The first
must include a ``*.py`` file and the second a ``*.out`` file. The ``*.py`` file
consists of the example code which is executed in a separate process. The
output of this process is compared to the contents of the ``*.out`` file.

iN(   s   TerminalRepr(   s
   _diff_texts   SimRTManual.rstc         C  sJ   |  j  d k r d Sx' t D] } t |    |  r9 d Sq Wt |  |  S(   sR   Checks if the file is a rst file and creates an :class:`ExampleFile`
    instance.s   .rstN(   t   extt	   blacklistt   strt   endswitht   ExampleFile(   t   patht   parentt   item(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   pytest_collect_file   s     s   ExampleFilec             s   e  Z d  Z d   Z RS(   s.   Collects all examples contained in a rst-file.c         c  s  g  } |  j     } } xs t |  D]e \ } } d | k rC q% n  |  d  d    } t j  |  j  j |  } | 	 | | f  q% WWd  QXx t
 t |  d  D]h } | | \ } }	 | | d \ }
 } |	  d  s q n  |  d  s q n  t |
 |	 | |   Vq Wd  S(   Ns   literalincludes   ::ii   s   .pys   .out(   t   fspatht   opent	   enumeratet   splitt   stript   ost   patht   joint   dirnamet   appendt   ranget   lent   endswitht   ExampleItem(   t   selft   literalincludest   datat   linenot   linet   filenamet   filepatht   idxt   example_linenot   examplet   output_linenot   output(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   collect)   s$      (   t   __name__t
   __module__t   __doc__t   collect(    (    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   ExampleFile&   s   s   ExampleItemc             s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s(   Executes an example found in a rst-file.c         C  sw   t  j  |  | |  | |  _ | |  _ | |  _ t j  |  j	 j
 |  j  |  _ t j  |  j	 j
 |  j  |  _ d  S(   N(   t   pytestt   Itemt   __init__t   linenot   examplet   outputt   ost   patht   joint   fspatht   dirnamet   examplefilet
   outputfile(   t   selft   linenot   examplet   outputt   parent(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   __init__B   s    			!c         C  s  t  |  j   } |    } Wd  QXd | k rk t  d  t t  d   d k rh t  d  n  n  t  |  j	   } |    } Wd  QXt
 t d  s t  d  n  t  d |  j g d t j } t | t  r |  d	  } n  | | k rt | |   n  d  S(
   Ns   expovariatei    g?s   Old exovariate implementation.s   check_outputs(   subprocess has no check_output() method.s   pythons   stderrs   utf8(   t   opent   examplefilet   readt   randomt   seedt   intt   expovariatet   pytestt   skipt
   outputfilet   hasattrt
   subprocesst   check_outputt   STDOUTt
   isinstancet   bytest   decodet
   ValueError(   t   selft   ft   srct   expectedt   output(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   runtestJ   s     c         C  s   |   t f  rR | j j \ } } t | |  } t |  j j |  j |  j	 |  S|   t
 f  r | j j t j k r t j  |  |  St |  j j |  j |  j |  S|   t j f  r t |  j j |  j |  j |  St j  |  |  Sd  S(   N(   t   errisinstancet
   ValueErrort   valuet   argst
   _diff_textt   ReprFailExamplet   fspatht   basenamet   linenot
   outputfilet   IOErrort   errnot   ENOENTt   pytestt   Itemt   repr_failuret   ReprFileNotFoundExamplet   examplefilet
   subprocesst   CalledProcessErrort   ReprErrorExample(   t   selft   exc_infot   expectedt   outputt   message(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   repr_failuree   s    c         C  s#   |  j  d d t j  |  j  f S(   s%   Returns a description of the example.Ns   [example %s](   t   fspatht   ost   patht   relpatht   examplefile(   t   self(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt
   reportinfo   s    (   t   __name__t
   __module__t   __doc__t   __init__t   runtestt   repr_failuret
   reportinfo(    (    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   ExampleItem?   s
   			s   ReprFailExamplec             sV   e  Z d  Z i  e d e  d 6e d e  d 6e d e  d 6Z d   Z d   Z RS(	   sC   Reports output mismatches in a nice and informative representation.s   greens   +s   reds   -s   bolds   ?c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   filenamet   linenot
   outputfilet   message(   t   selft   filenamet   linenot
   outputfilet   message(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   __init__   s    			c         C  sp   x: |  j  D]/ } t j  | d i   } | j | |  q
 W|  d |  j |  j t j 	 |  j
  f  d  S(   Ni    s    %s:%d (in %s): Unexpected output(   t   messaget   ReprFailExamplet   Markupt   gett   linet   filenamet   linenot   ost   patht   relpatht
   outputfile(   t   selft   twt   linet   markup(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt
   toterminal   s    
 (   t   __name__t
   __module__t   __doc__t   dictt   Truet   Markupt   __init__t
   toterminal(    (    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   ReprFailExample   s   	s   ReprErrorExamplec             s    e  Z d  Z d   Z d   Z RS(   s0   Reports failures in the execution of an example.c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   filenamet   linenot   examplefilet   exc_info(   t   selft   filenamet   linenot   examplefilet   exc_info(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   __init__   s    			c         C  s   |   d d t |  d  |   |  j j j d t d t |   d |  j |  j t j	 
 |  j  |  j j j f  d  S(   Ns"   Execution failed! Captured output:s   bolds   -s   reds'   %s:%d (%s) Example failed (exitcode=%d)(   t   linet   Truet   sept   exc_infot   valuet   outputt   filenamet   linenot   ost   patht   relpatht   examplefilet
   returncode(   t   selft   tw(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt
   toterminal   s    "(   t   __name__t
   __module__t   __doc__t   __init__t
   toterminal(    (    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   ReprErrorExample   s   	s   ReprFileNotFoundExamplec             s    e  Z d  Z d   Z d   Z RS(   sB   Reports concise error information in the case of a file not found.c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   filenamet   linenot   examplefilet   exc_info(   t   selft   filenamet   linenot   examplefilet   exc_info(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   __init__   s    			c         C  sR   |   |  j j d t d t |   d |  j |  j t j  |  j	  f  d  S(   Ns   reds   bolds   %s:%d (%s) Example failed(
   t   linet   exc_infot   valuet   Truet   filenamet   linenot   ost   patht   relpatht   examplefile(   t   selft   tw(    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt
   toterminal   s    (   t   __name__t
   __module__t   __doc__t   __init__t
   toterminal(    (    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   ReprFileNotFoundExample   s   	(   t   __doc__t   os.patht   ost
   subprocesst   errnot   randomt   pytestt   py._code.codet   TerminalReprt   _pytest.assertion.utilt
   _diff_textt	   blacklistt   pytest_collect_filet   Filet   ExampleFilet   Itemt   ExampleItemt   ReprFailExamplet   ReprErrorExamplet   ReprFileNotFoundExample(    (    (    t)   /Users/stefan/Code/simpy/docs/conftest.pyt   <module>   s   		F