a
    ñ»jf$  ã                   @   s^   d Z dZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 e e¡ZG dd„ deƒZdS )	at	  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. Fé    N)ÚPlugin)Úsrc)ÚdumpÚloadc                   @   sr   e Zd ZdZdZdZdZdZdd„ Zdd	„ Z	d
d„ Z
ddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTestIdzuActivate to add a test id (like #1) to each test name output. Activate
    with --failed to rerun failing tests only.ÚidNTFc                 C   s<   t  | ||¡ |jddddddd� |jdd	d
ddd� dS )zRegister commandline options.z	--id-fileÚstoreÚ
testIdFilez.noseidsÚFILEzfStore test ids found in test runs in this file. Default is the file .noseids in the working directory.)ÚactionÚdestÚdefaultÚmetavarÚhelpz--failedÚ
store_trueÚfailedFz/Run the tests that failed in the last test run.)r   r   r   r   N)r   ÚoptionsÚ
add_option)ÚselfÚparserÚenv© r   ú9E:\soccer\ml_env\Lib\site-packages\nose/plugins/testid.pyr   o   s    ýþzTestId.optionsc                 C   s�   t  | ||¡ |jr*d| _d| _t d¡ tj 	|j
¡| _tj | j¡s\tj |j| j¡| _d| _i | _i | _g | _g | _i | _|jdk| _dS )zConfigure plugin.TzLooping on failed testsé   é   N)r   Ú	configurer   ÚenabledÚloopOnFailedÚlogÚdebugÚosÚpathÚ
expanduserr	   ÚidfileÚisabsÚjoinZ
workingDirr   ÚidsÚtestsÚsource_namesÚ_seenÚ	verbosityÚ_write_hashes)r   r   Zconfr   r   r   r   ~   s    
zTestId.configurec                 C   s„   |  ¡ rg | _| jr<tttt| j ¡ ƒt| j ¡ ƒƒƒƒ}n| j	}t
| jdƒ}t|| j| jdœ|ƒ | ¡  t d|| j| j¡ dS )zSave new ids file, if needed.Úwb)r&   r   r(   z#Saved test ids: %s, failed %s to %sN)ZwasSuccessfulr   Ú
collectingÚdictÚlistÚzipr'   ÚvaluesÚkeysr&   Úopenr#   r   r(   Úcloser   r   )r   Úresultr&   Úfhr   r   r   Úfinalize�   s$     ÿþþ
ÿzTestId.finalizec              
      s
  t  d||¡ z¾t| jdƒ}t|ƒ}d|v rL|d | _|d | _|d | _n|| _g | _|| _| jržt| jƒd | _	t
ttt| j ¡ ƒt| j ¡ ƒƒƒƒ| _nd| _	t  d| j| j| j| j| j¡ | ¡  W nZ t�y } z t  d| jt|ƒ¡ W Y d	}~n*d	}~0  t�y&   t  d
| j¡ Y n0 | j�rJ| j�rJd| _| j}g | _g }g }g }|D ]0}	|  |	¡}
|
|	k�r~| |
¡ n
| |	¡ �qZ|�rât|ƒ}t| jƒ‰ t  dˆ |¡ ‡ fdd„|D ƒ}|�rÖ| j |¡ |�sè|}nd| _t  d|||¡ d	|| �p|fS )zvTranslate ids in the list of requested names into their
        test addresses, if they are found in my dict of tests.z
ltfn %s %sÚrbr&   r   r(   r   z8Loaded test ids %s tests %s failed %s sources %s from %szError loading %s : %sNzIO error reading %sFzold: %s new: %sc                    s   g | ]}|ˆ vr|‘qS r   r   )Ú.0Ús©Zold_setr   r   Ú
<listcomp>×   ó    z-TestId.loadTestsFromNames.<locals>.<listcomp>z&translated: %s new sources %s names %s)r   r   r3   r#   r   r&   r   r(   Úmaxr   r.   r/   r0   r1   r2   r'   r4   Ú
ValueErrorÚstrÚIOErrorr   r-   ÚtrÚappendÚsetÚextend)r   ÚnamesÚmoduler6   ÚdataÚeZ
translatedZ
new_sourceZ
really_newÚnameZtransZnew_setr   r;   r   ÚloadTestsFromNames¢   sj    

 ÿý(


ÿzTestId.loadTestsFromNamesc                 C   sD   t  d|¡ |\}}}|d ur(t|ƒ}n|}|d ur@d||f S |S )NzMake name %sz%s:%s)r   r   r   )r   ÚaddrÚfilenamerG   ÚcallÚheadr   r   r   ÚmakeNameæ   s    

zTestId.makeNamec                 C   s
   || _ dS )z9Get handle on output stream so the plugin can print id #sN)Ústream)r   rQ   r   r   r   ÚsetOutputStreamñ   s    zTestId.setOutputStreamc                 C   sŒ   |  ¡ }t d||| jv ¡ || jv r^|| jv r<|  d¡ n|  d| j|  ¡ d| j|< dS | j| j|< |  d| j ¡ |  jd7  _dS )zƒMaybe output an id # before the test name.
        Example output:
          #1 test.test ... ok
          #2 test.test_two ... ok zstart test %s (%s)z   z#%s r   N)Úaddressr   r   r'   r)   Úwriter   )r   ÚtestZadrr   r   r   Ú	startTestõ   s    


zTestId.startTestc                 C   sN   |j du rJzt| j| ¡  ƒ}W n ty2   Y n0 || jvrJ| j |¡ d S )NF)Zpassedr@   r'   rS   ÚKeyErrorr   rC   )r   rU   Úkeyr   r   r   Ú	afterTest  s    

zTestId.afterTestc                 C   sb   t  d|¡ zt| dd¡ƒ}W n ty6   | Y S 0 t  d|¡ || jv r^|  | j| ¡S |S )Nztr '%s'ú#Ú z
Got key %s)r   r   ÚintÚreplacer?   r&   rP   )r   rJ   rX   r   r   r   rB     s    

z	TestId.trc                 C   s   | j r| j |¡ d S )N)r+   rQ   rT   )r   Úoutputr   r   r   rT      s    zTestId.write)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rJ   r#   r-   r   r   r   r7   rK   rP   rR   rV   rY   rB   rT   r   r   r   r   r   g   s   
Dr   )rb   Z__test__Úloggingr    Znose.pluginsr   Z	nose.utilr   Úpickler   r   Ú	getLoggerr_   r   r   r   r   r   r   Ú<module>   s   [
