a
    3b                     @   s   d dl mZ d dl mZ d dl m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 d	lmZ erxd d
lmZ dZe
ddddZejeddddZeddddZG dd dZdS )    )List)Optional)TYPE_CHECKINGN)nodes)Config)Parser)Session)
TestReport)Cachezcache/stepwise)parserreturnc                 C   s:   |  d}|jddddddd |jd	d
ddddd d S )NZgeneralz--swz
--stepwise
store_trueFstepwisezBexit on test failure and continue from last failing test next time)actiondefaultdesthelpz	--sw-skipz--stepwise-skipstepwise_skipz_ignore the first failing test but stop on the next failing test.
implicitly enables --stepwise.)ZgetgroupZ	addoption)r   group r   /lib/python3.9/site-packages/_pytest/stepwise.pypytest_addoption   s"    
r   configr   c                 C   s0   | j jrd| j _| dr,| jt| d d S )NTr   Zstepwiseplugin)Zoptionr   r   	getoptionZpluginmanagerregisterStepwisePlugin)r   r   r   r   pytest_configure'   s    
r   sessionr   c                 C   s0   | j ds,| j jd usJ | j jtg  d S )Nr   )r   r   cachesetSTEPWISE_CACHE_DIRr   r   r   r   pytest_sessionfinish0   s    r$   c                   @   st   e Zd ZeddddZeddddZeeej	 ddd	d
Z
eddddZee dddZddddZdS )r   Nr   c                 C   sH   || _ d | _d| _|jd us J |j| _| jtd | _|d| _d S )N r   )	r   r   report_statusr    getr"   
lastfailedr   skip)selfr   r   r   r   __init__8   s    zStepwisePlugin.__init__r   c                 C   s
   || _ d S Nr#   )r*   r   r   r   r   pytest_sessionstartA   s    z"StepwisePlugin.pytest_sessionstart)r   itemsr   c                 C   s   | j sd| _d S d }t|D ]\}}|j| j kr|} q:q|d u rJd| _n2d| d| _|d | }|d |= |jj|d d S )Nz)no previously failed tests, not skipping.z/previously failed test not found, not skipping.z	skipping z already passed items.)r.   )r(   r&   	enumeratenodeidhookZpytest_deselected)r*   r   r.   Zfailed_indexindexitemZ
deselectedr   r   r   pytest_collection_modifyitemsD   s    
z,StepwisePlugin.pytest_collection_modifyitems)reportr   c                 C   sf   |j rF| jr&|j| jkrd | _d| _qb|j| _| jd us<J d| j_n|jdkrb|j| jkrbd | _d S )NFz0Test failed, continuing from this test next run.Zcall)Zfailedr)   r0   r(   r   Z
shouldstopZwhen)r*   r5   r   r   r   pytest_runtest_logreport\   s    
z'StepwisePlugin.pytest_runtest_logreport)r   c                 C   s&   | j ddkr"| jr"d| j S d S )Nverboser   z
stepwise: )r   r   r&   r*   r   r   r   pytest_report_collectionfinisht   s    z-StepwisePlugin.pytest_report_collectionfinishc                 C   s   | j t| j d S r,   )r    r!   r"   r(   r8   r   r   r   r$   y   s    z#StepwisePlugin.pytest_sessionfinish)__name__
__module____qualname__r   r+   r   r-   r   r   ZItemr4   r	   r6   r   strr9   r$   r   r   r   r   r   7   s   	
r   )typingr   r   r   ZpytestZ_pytestr   Z_pytest.configr   Z_pytest.config.argparsingr   Z_pytest.mainr   Z_pytest.reportsr	   Z_pytest.cacheproviderr
   r"   r   Zhookimplr   r$   r   r   r   r   r   <module>   s    