ó
;^c           @   s·   d  Z  e Z d d l Z d d l Z d d l m Z d d l m Z m	 Z	 y d d l
 m Z m Z Wn' e k
 r d d l m Z m Z n Xe j e ƒ Z d e f d „  ƒ  YZ d S(   sˆ	  
This plugin adds a test id (like #1) to each test name output. After
you've run once to generate test ids, you can re-run individual
tests by activating the plugin and passing the ids (with or
without the # prefix) instead of test names.

For example, if your normal test run looks like::

  % nosetests -v
  tests.test_a ... ok
  tests.test_b ... ok
  tests.test_c ... ok

When adding ``--with-id`` you'll see::

  % nosetests -v --with-id
  #1 tests.test_a ... ok
  #2 tests.test_b ... ok
  #3 tests.test_c ... ok

Then you can re-run individual tests by supplying just an id number::

  % nosetests -v --with-id 2
  #2 tests.test_b ... ok

You can also pass multiple id numbers::

  % nosetests -v --with-id 2 3
  #2 tests.test_b ... ok
  #3 tests.test_c ... ok
  
Since most shells consider '#' a special character, you can leave it out when
specifying a test id.

Note that when run without the -v switch, no special output is displayed, but
the ids file is still written.

Looping over failed tests
-------------------------

This plugin also adds a mode that will direct the test runner to record
failed tests. Subsequent test runs will then run only the tests that failed
last time. Activate this mode with the ``--failed`` switch::

 % nosetests -v --failed
 #1 test.test_a ... ok
 #2 test.test_b ... ERROR
 #3 test.test_c ... FAILED
 #4 test.test_d ... ok
 
On the second run, only tests #2 and #3 will run::

 % nosetests -v --failed
 #2 test.test_b ... ERROR
 #3 test.test_c ... FAILED

As you correct errors and tests pass, they'll drop out of subsequent runs.

First::

 % nosetests -v --failed
 #2 test.test_b ... ok
 #3 test.test_c ... FAILED

Second::

 % nosetests -v --failed
 #3 test.test_c ... FAILED

When all tests pass, the full set will run on the next invocation.

First::

 % nosetests -v --failed
 #3 test.test_c ... ok

Second::
 
 % nosetests -v --failed
 #1 test.test_a ... ok
 #2 test.test_b ... ok
 #3 test.test_c ... ok
 #4 test.test_d ... ok

.. note ::

  If you expect to use ``--failed`` regularly, it's a good idea to always run
  using the ``--with-id`` option. This will ensure that an id file is always
  created, allowing you to add ``--failed`` to the command line as soon as
  you have failing tests. Otherwise, your first run using ``--failed`` will
  (perhaps surprisingly) run *all* tests, because there won't be an id file
  containing the record of failed tests from your previous run.
  
iÿÿÿÿN(   t   Plugin(   t   srct   set(   t   dumpt   loadt   TestIdc           B   sƒ   e  Z d  Z d Z d Z e Z e Z	 d „  Z
 d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   s   
    Activate to add a test id (like #1) to each test name output. Activate
    with --failed to rerun failing tests only.
    t   idc         C   sg   t  j |  | | ƒ | j d d d d d d d d d	 d
 d ƒ| j d d d d d d t d
 d ƒd S(   s&   Register commandline options.
        s	   --id-filet   actiont   storet   destt
   testIdFilet   defaults   .noseidst   metavart   FILEt   helpsf   Store test ids found in test runs in this file. Default is the file .noseids in the working directory.s   --failedt
   store_truet   faileds/   Run the tests that failed in the last test run.N(   R    t   optionst
   add_optiont   False(   t   selft   parsert   env(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyR   x   s    c         C   sØ   t  j |  | | ƒ | j r> t |  _ t |  _ t j d ƒ n  t j	 j
 | j ƒ |  _ t j	 j |  j ƒ sŒ t j	 j | j |  j ƒ |  _ n  d |  _ i  |  _ i  |  _ g  |  _ g  |  _ i  |  _ | j d k |  _ d S(   s   Configure plugin.
        s   Looping on failed testsi   i   N(   R    t	   configureR   t   Truet   enabledt   loopOnFailedt   logt   debugt   ost   patht
   expanduserR
   t   idfilet   isabst   joint
   workingDirR   t   idst   testst   source_namest   _seent	   verbosityt   _write_hashes(   R   R   t   conf(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyR   †   s    			!						c         C   sÊ   | j  ƒ  r g  |  _ n  |  j r] t t t t |  j j ƒ  ƒ t |  j j ƒ  ƒ ƒ ƒ ƒ } n	 |  j	 } t
 |  j d ƒ } t i | d 6|  j d 6|  j d 6| ƒ | j ƒ  t j d | |  j |  j ƒ d S(   s&   Save new ids file, if needed.
        t   wbR$   R   R&   s#   Saved test ids: %s, failed %s to %sN(   t   wasSuccessfulR   t
   collectingt   dictt   listt   zipR%   t   valuest   keysR$   t   openR    R   R&   t   closeR   R   (   R   t   resultR$   t   fh(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt   finalize   s    	<	

	c         C   s¹  t  j d | | ƒ yt |  j d ƒ } t | ƒ } d | k rj | d |  _ | d |  _ | d |  _ n | |  _ g  |  _ | |  _ |  j rã t |  j ƒ d |  _	 t
 t t t |  j j ƒ  ƒ t |  j j ƒ  ƒ ƒ ƒ ƒ |  _ n	 d |  _	 t  j d |  j |  j |  j |  j |  j ƒ | j ƒ  WnR t k
 rS} t  j d |  j t | ƒ ƒ n$ t k
 rvt  j d	 |  j ƒ n X|  j r§|  j r§t |  _ |  j } g  |  _ n  g  } g  } g  } xF | D]> }	 |  j |	 ƒ }
 |
 |	 k rñ| j |
 ƒ qÀ| j |	 ƒ qÀW| r†t | ƒ } t |  j ƒ } t  j d
 | | ƒ g  | D] } | | k r=| ^ q=} | rt|  j j | ƒ n  | s| } qn	 t |  _ t  j d | | | ƒ d | | pµ| f S(   s   Translate ids in the list of requested names into their
        test addresses, if they are found in my dict of tests.
        s
   ltfn %s %st   rbR$   R   R&   i   s8   Loaded test ids %s tests %s failed %s sources %s from %ss   Error loading %s : %ss   IO error reading %ss   old: %s new: %ss&   translated: %s new sources %s names %sN(   R   R   R3   R    R   R$   R   R&   t   maxR   R.   R/   R0   R1   R2   R%   R4   t
   ValueErrort   strt   IOErrorR   R   R-   t   trt   appendR   t   extendt   None(   R   t   namest   moduleR6   t   datat   et
   translatedt
   new_sourcet
   really_newt   namet   transt   new_sett   old_sett   s(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt   loadTestsFromNames®   sd    				?	
				c         C   s^   t  j d | ƒ | \ } } } | d  k	 r: t | ƒ } n | } | d  k	 rZ d | | f S| S(   Ns   Make name %ss   %s:%s(   R   R   R@   R   (   R   t   addrt   filenameRB   t   callt   head(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt   makeNameö   s    c         C   s   | |  _  d S(   sB   Get handle on output stream so the plugin can print id #s
        N(   t   stream(   R   RS   (    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt   setOutputStream  s    c         C   s¶   | j  ƒ  } t j d | | |  j k ƒ | |  j k r | |  j k rV |  j d ƒ n% |  j d |  j | ƒ d |  j | <d S|  j |  j | <|  j d |  j ƒ |  j d 7_ d S(   s   Maybe output an id # before the test name.

        Example output::

          #1 test.test ... ok
          #2 test.test_two ... ok

        s   start test %s (%s)s      s   #%s i   N(   t   addressR   R   R%   R'   t   writeR   (   R   t   testt   adr(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt	   startTest  s    	c         C   si   | j  t k re y t |  j | j ƒ  ƒ } Wn t k
 r? qe X| |  j k re |  j j | ƒ qe n  d  S(   N(   t   passedR   R;   R%   RU   t   KeyErrorR   R>   (   R   RW   t   key(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt	   afterTest  s    c         C   sx   t  j d | ƒ y t | j d d ƒ ƒ } Wn t k
 r@ | SXt  j d | ƒ | |  j k rt |  j |  j | ƒ S| S(   Ns   tr '%s't   #t    s
   Got key %s(   R   R   t   intt   replaceR:   R$   RR   (   R   RH   R\   (    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyR=   (  s    c         C   s    |  j  r |  j j | ƒ n  d  S(   N(   R)   RS   RV   (   R   t   output(    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyRV   5  s    	N(   t   __name__t
   __module__t   __doc__RH   R@   R    R   R-   R   R   R   R   R7   RM   RR   RT   RY   R]   R=   RV   (    (    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyR   n   s   			H					(   Re   R   t   __test__t   loggingR   t   nose.pluginsR    t	   nose.utilR   R   t   cPickleR   R   t   ImportErrort   picklet	   getLoggerRc   R   R   (    (    (    s5   /tmp/pip-unpacked-wheel-IhimZu/nose/plugins/testid.pyt   <module>^   s   