B
    ”Ü‡\k6  ã               @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlm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d
lmZ ddlmZ dZdZe	jG dd„ deƒƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zejdddd„ ƒZejdd„ ƒZ dd„ Z!d d!„ Z"dS )"z
merged implementation of the cache provider

the name cache was not chosen to ensure pluggy automatically
ignores the external pytest-cache
é    )Úabsolute_import)Údivision)Úprint_functionN)ÚOrderedDicté   )Ú_PY2)ÚPath)Úresolve_from_str)Úrmtreea'  # pytest cache directory #

This directory contains data from the pytest's cache plugin,
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.

**Do not** commit this to version control.

See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information.
sÂ   Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by pytest.
# For information about cache directory tags, see:
#	http://www.bford.info/cachedir/spec.html
c               @   sl   e Zd ZejddZejddZedd„ ƒZe	dd„ ƒZ
dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚCacheF)Úreprc             C   s:   |   |¡}| d¡r0| ¡ r0t|dd | ¡  | ||ƒS )NÚ
cacheclearT)Zforce)Úcache_dir_from_configÚ	getoptionÚexistsr
   Úmkdir)ÚclsÚconfigÚcachedir© r   ú4lib/python3.7/site-packages/_pytest/cacheprovider.pyÚ
for_config1   s
    
zCache.for_configc             C   s   t |  d¡| jƒS )NÚ	cache_dir)r	   ÚgetiniÚrootdir)r   r   r   r   r   9   s    zCache.cache_dir_from_configc             K   sB   ddl m} ddlm} |||r,|jf |Žn|ƒ| jjdd d S )Nr   )Ú_issue_warning_captured)ÚPytestWarningé   )Ú
stacklevel)Z_pytest.warningsr   Z_pytest.warning_typesr   ÚformatÚ_configÚhook)ÚselfZfmtÚargsr   r   r   r   r   Úwarn=   s    z
Cache.warnc             C   sF   t |ƒ}t|jƒdkrtdƒ‚| j d|¡}|jddd tj 	|¡S )aÀ   return a directory path object with the given name.  If the
        directory does not yet exist, it will be created.  You can use it
        to manage files likes e. g. store/retrieve database
        dumps across test sessions.

        :param name: must be a string not containing a ``/`` separator.
             Make sure the name contains your plugin or application
             identifiers to prevent clashes with other cache users.
        r   z.name is not allowed to contain path separatorsÚdT)Úexist_okÚparents)
r   ÚlenÚpartsÚ
ValueErrorÚ	_cachedirÚjoinpathr   ÚpyÚpathZlocal)r"   ÚnameZresr   r   r   ÚmakedirG   s    
zCache.makedirc             C   s   | j  dt|ƒ¡S )NÚv)r+   r,   r   )r"   Úkeyr   r   r   Ú_getvaluepathX   s    zCache._getvaluepathc          
   C   sJ   |   |¡}y | d¡}t |¡S Q R X W n tttfk
rD   |S X dS )aŽ   return cached value for the given key.  If no value
        was yet cached or the value cannot be read, the specified
        default is returned.

        :param key: must be a ``/`` separated value. Usually the first
             name is the name of your plugin or your application.
        :param default: must be provided in case of a cache-miss or
             invalid cache values.

        ÚrN)r3   ÚopenÚjsonÚloadr*   ÚIOErrorÚOSError)r"   r2   Údefaultr.   Úfr   r   r   Úget[   s    
z	Cache.getc          	   C   sÐ   |   |¡}y.|j ¡ rd}n
| j ¡ }|jjddd W n& ttfk
r^   | jd|d dS X y| 	t
rndnd¡}W n& ttfk
rœ   | jd|d Y n0X | tj||d	dd
 W dQ R X |sÌ|  ¡  dS )aP   save value for the given key.

        :param key: must be a ``/`` separated value. Usually the first
             name is the name of your plugin or your application.
        :param value: must be of any combination of basic
               python types, including nested types
               like e. g. lists of dictionaries.
        T)r&   r'   z"could not create cache path {path})r.   NÚwbÚwz!cache could not write path {path}é   )ÚindentZ	sort_keys)r3   ÚparentÚis_dirr+   r   r   r8   r9   r$   r5   ÚPY2r6   ÚdumpÚ_ensure_supporting_files)r"   r2   Úvaluer.   Zcache_dir_exists_alreadyr;   r   r   r   Úsetm   s"    	


z	Cache.setc             C   sn   | j  ¡ rj| j d }| ¡ s&| t¡ | j  d¡}| ¡ sLd}|j|dd | j  d¡}| ¡ sj| t¡ dS )zOCreate supporting files in the cache dir that are not really part of the cache.z	README.mdz
.gitignorez$# Created by pytest automatically.
*zUTF-8)ÚencodingzCACHEDIR.TAGN)r+   rB   Úis_fileZ
write_textÚREADME_CONTENTr,   Zwrite_bytesÚCACHEDIR_TAG_CONTENT)r"   Zreadme_pathZgitignore_pathÚmsgZcachedir_tag_pathr   r   r   rE   Š   s    


zCache._ensure_supporting_filesN)Ú__name__Ú
__module__Ú__qualname__ÚattrZibr+   r    Úclassmethodr   Ústaticmethodr   r$   r0   r3   r<   rG   rE   r   r   r   r   r   ,   s   
r   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚLFPluginz< Plugin which implements the --lf (run last-failing) option c                sJ   ˆ | _ d}t‡ fdd„|D ƒƒ| _ˆ j di ¡| _d | _| j  d¡| _d S )N)ÚlfÚfailedfirstc             3   s   | ]}ˆ   |¡V  qd S )N)r   )Ú.0r2   )r   r   r   ú	<genexpr>¡   s    z$LFPlugin.__init__.<locals>.<genexpr>zcache/lastfailedÚlast_failed_no_failures)	r   ÚanyÚactiveÚcacher<   Ú
lastfailedÚ_previously_failed_countr   Ú_no_failures_behavior)r"   r   Zactive_keysr   )r   r   Ú__init__ž   s    zLFPlugin.__init__c             C   sd   | j r`| j d¡dkr`| js d S | jdkr.dnd}| j d¡rBdnd}d	j| j||d
}d| S d S )NÚverboser   r   ZfailureZfailuresrU   z firstÚ z%rerun previous {count} {noun}{suffix})ÚcountÚsuffixÚnounzrun-last-failure: %s)rZ   r   r   r]   r   )r"   rd   rc   Úmoder   r   r   Úpytest_report_collectionfinish¦   s    z'LFPlugin.pytest_report_collectionfinishc             C   s>   |j dkr|js|jr(| j |jd ¡ n|jr:d| j|j< d S )NZcallT)ZwhenÚpassedÚskippedr\   ÚpopÚnodeidZfailed)r"   Úreportr   r   r   Úpytest_runtest_logreport±   s    z!LFPlugin.pytest_runtest_logreportc             C   sR   |j dk}|rB|j| jkrN| j |j¡ | j dd„ |jD ƒ¡ nd| j|j< d S )N)rg   rh   c             s   s   | ]}|j d fV  qdS )TN)rj   )rV   Úitemr   r   r   rW   ¼   s    z0LFPlugin.pytest_collectreport.<locals>.<genexpr>T)Zoutcomerj   r\   ri   ÚupdateÚresult)r"   rk   rg   r   r   r   Úpytest_collectreport·   s    
zLFPlugin.pytest_collectreportc             C   s¸   | j r´| jrg }g }x.|D ]&}|j| jkr6| |¡ q| |¡ qW t|ƒ| _|sVd S | j d¡r~||d d …< |jj	|d q´|| |d d …< n$| j
dkr´|jj	|d g |d d …< d S )NrT   )ÚitemsÚnone)rZ   r\   rj   Úappendr(   r]   r   r   r!   Zpytest_deselectedr^   )r"   Úsessionr   rq   Zpreviously_failedZpreviously_passedrm   r   r   r   Úpytest_collection_modifyitemsÀ   s$    


z&LFPlugin.pytest_collection_modifyitemsc             C   sJ   | j }| d¡st|dƒrd S |j di ¡}|| jkrF|j d| j¡ d S )NÚ	cacheshowÚ
slaveinputzcache/lastfailed)r   r   Úhasattrr[   r<   r\   rG   )r"   rt   r   Zsaved_lastfailedr   r   r   Úpytest_sessionfinishØ   s    
zLFPlugin.pytest_sessionfinishN)
rM   rN   rO   Ú__doc__r_   rf   rl   rp   ru   ry   r   r   r   r   rS   ›   s   	rS   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚNFPluginz9 Plugin which implements the --nf (run new-first) option c             C   s$   || _ |jj| _|j dg ¡| _d S )Nzcache/nodeids)r   ÚoptionÚnewfirstrZ   r[   r<   Úcached_nodeids)r"   r   r   r   r   r_   å   s    
zNFPlugin.__init__c             C   s~   | j rjtƒ }tƒ }x.|D ]&}|j| jkr4|||j< q|||j< qW |  t |¡¡|  t |¡¡ |d d …< dd„ |D ƒ| _d S )Nc             S   s   g | ]}t |tjƒr|j‘qS r   )Ú
isinstanceÚpytestZItemrj   )rV   Úxr   r   r   ú
<listcomp>÷   s    z:NFPlugin.pytest_collection_modifyitems.<locals>.<listcomp>)rZ   r   rj   r~   Ú_get_increasing_orderÚsixZ
itervalues)r"   rt   r   rq   Z	new_itemsZother_itemsrm   r   r   r   ru   ê   s    

z&NFPlugin.pytest_collection_modifyitemsc             C   s   t |dd„ ddS )Nc             S   s
   | j  ¡ S )N)ÚfspathÚmtime)rm   r   r   r   Ú<lambda>ú   s    z0NFPlugin._get_increasing_order.<locals>.<lambda>T)r2   Úreverse)Úsorted)r"   rq   r   r   r   rƒ   ù   s    zNFPlugin._get_increasing_orderc             C   s2   | j }| d¡st|dƒrd S |j d| j¡ d S )Nrv   rw   zcache/nodeids)r   r   rx   r[   rG   r~   )r"   rt   r   r   r   r   ry   ü   s    zNFPlugin.pytest_sessionfinishN)rM   rN   rO   rz   r_   ru   rƒ   ry   r   r   r   r   r{   â   s
   r{   c          	   C   s¸   |   d¡}|jdddddd |jdd	dd
dd |jdddddd |jddddd |jddddd d}dtjkrŒtj tjd |¡}| jd|dd |jdddddd d!d" d S )#NZgeneralz--lfz--last-failedÚ
store_truerT   zHrerun only the tests that failed at the last run (or all if none failed))ÚactionÚdestÚhelpz--ffz--failed-firstrU   zxrun all tests but run the last failures first.  This may re-order tests and thus lead to repeated fixture setup/teardownz--nfz--new-firstr}   zOrun tests from new files first, then the rest of the tests sorted by file mtimez--cache-showrv   z6show cache contents, don't perform collection or testsz--cache-clearr   z/remove all cache contents at start of test run.z.pytest_cacheZTOX_ENV_DIRr   zcache directory path.)r:   r   z--lfnfz--last-failed-no-failuresZstorerX   )Úallrr   rŽ   zxchange the behavior when no test failed in the last run or no information about the last failures was found in the cache)r‹   rŒ   Úchoicesr:   r   )ZgetgroupZ	addoptionÚosÚenvironr.   ÚjoinZaddini)ÚparserÚgroupZcache_dir_defaultr   r   r   Úpytest_addoption  sR    

r•   c             C   s"   | j jrddlm} || tƒS d S )Nr   )Úwrap_session)r|   rv   Z_pytest.mainr–   )r   r–   r   r   r   Úpytest_cmdline_main;  s    r—   T)Ztryfirstc             C   s4   t  | ¡| _| j t| ƒd¡ | j t| ƒd¡ d S )NZlfpluginZnfplugin)r   r   r[   ZpluginmanagerÚregisterrS   r{   )r   r   r   r   Úpytest_configureB  s    r™   c             C   s   | j jS )ag  
    Return a cache object that can persist state between testing sessions.

    cache.get(key, default)
    cache.set(key, value)

    Keys must be a ``/`` separated value, where the first part is usually the
    name of your plugin or application to avoid clashes with other cache users.

    Values can be any object handled by the json stdlib module.
    )r   r[   )Zrequestr   r   r   r[   I  s    r[   c             C   sV   | j js|  d¡dkrR| jj}y| | j¡}W n tk
rF   |}Y nX d |¡S dS )z6Display cachedir with --cache-show and if non-default.r   z.pytest_cachezcachedir: {}N)	r|   r`   r   r[   r+   Úrelative_tor   r*   r   )r   r   Zdisplaypathr   r   r   Úpytest_report_headerY  s    
r›   c             C   sX  ddl m} tj ¡ }| dt| jjƒ ¡ | jj 	¡ sF| d¡ dS t
ƒ }| jj}|d }| dd¡ x‚tdd	„ | d
¡D ƒƒD ]f}| |¡}| j ||¡}	|	|kr¶| d| ¡ q‚| d| ¡ x"||	ƒ ¡ D ]}
| d|
 ¡ qÒW q‚W |d }| 	¡ rTt| d
¡ƒ}| dd¡ x:|D ]2}| ¡ r| |¡}| d || ¡ j¡¡ qW dS )Nr   )Úpformatz
cachedir: zcache is emptyr1   ú-zcache valuesc             s   s   | ]}|  ¡ r|V  qd S )N)rI   )rV   r   r   r   r   rW   s  s    zcacheshow.<locals>.<genexpr>Ú*z/%s contains unreadable content, will be ignoredz%s contains:z  r%   zcache directoriesz{} is a file of length {:d})Zpprintrœ   r-   ÚioZTerminalWriterÚlineÚstrr[   r+   rB   ÚobjectÚsepr‰   Zrglobrš   r<   Ú
splitlinesrI   r   ÚstatÚst_size)r   rt   rœ   ZtwZdummyZbasedirZvdirZvalpathr2   Úvalr    ZddirÚcontentsÚpr   r   r   rv   g  s6    






rv   )#rz   Z
__future__r   r   r   r6   r   Úcollectionsr   rP   r-   r„   r€   Úcompatr   rC   Zpathlibr   r	   r
   rJ   rK   Úsr¢   r   rS   r{   r•   r—   Zhookimplr™   Zfixturer[   r›   rv   r   r   r   r   Ú<module>   s4   nG"7