B
    <\                 @   s~   d Z ddlZddlZddlmZ ddlZyddlmZ W n e	k
rP   dZY nX dd Z
dd Zdd	 Zd
d Zdd ZdS )zY
This plugin provides support for testing whether file-like objects are properly
closed.
    N)StrictVersionc             C   s&   | j dddd | jddddd	 d S )
Nz--open-files
store_truez"fail if any test leaves files open)actionhelpopen_files_ignorezwhen used with the --open-files option, allows specifying names of files that may be ignored when left open between tests--files in this list are matched may be specified by their base name (ignoring their full path) or by absolute pathargs )typedefault)Z	addoptionZaddini)parserr   r   6lib/python3.7/site-packages/pytest_openfiles/plugin.pypytest_addoption   s
    r   c             C   s   |  dd d S )NZmarkerszJopenfiles_ignore: Indicate that open files should be ignored for this test)getiniappend)configr   r   r   pytest_configure   s    
r   c                 s^   dd l } g }|  }td k	r*tt  ntdd t D   fdd| D }t|S )Nr   c             s   s   | ]}|d  V  qdS )r   Nr   ).0infor   r   r   	<genexpr>7   s    z&_get_open_file_list.<locals>.<genexpr>c                s   g | ]}|j  s|j qS r   )pathendswith)r   x)suffixesr   r   
<listcomp>9   s    z'_get_open_file_list.<locals>.<listcomp>)	psutilZProcessimportlib_machinerytupleall_suffixesimpZget_suffixes
open_filesset)r   filespr   )r   r   _get_open_file_list/   s    r#   c             C   sD   t tjt dk r| d}n
| d}| jdr@|s@t | _d S )Nz3.6Zopenfiles_ignorer   )	r   pytest__version__Z
get_markerZget_closest_markerr   getvaluer#   r   )itemignorer   r   r   pytest_runtest_setup>   s
    
r)   c             C   s  | j drt| dsd S | j}| `t }| jdkrPt|d t|ksLtd S t }| j 	d}xh|D ]`}d}x>|D ]6}t
j|st
j||krd}P qv||krvd}P qvW |rqh||krh|| qhW t|rdg}	x|D ]}
|	d|
 qW td	|	d S )
Nr   Ztest_open_file_detection   r   FTzFile(s) not closed:z  {0}
)r   r&   hasattrr   r#   namelenAssertionErrorr    r   osr   isabsbasenameaddr   formatjoin)r'   ZnextitemZstart_open_filesr   Z
not_closedr   filenamer(   Zignoredmsgr-   r   r   r   pytest_runtest_teardownL   s:    




r8   )__doc__r   r0   Zdistutils.versionr   r$   importlib.machinery	machineryr   ImportErrorr   r   r#   r)   r8   r   r   r   r   <module>   s   
