B
    ܇\                 @   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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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dlm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 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" 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' ddlm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. dd l/m0Z0 dd!l/m1Z1 dd"l2m3Z3 dd#l4m5Z5 dd$l6m7Z7 d%d& Z8d'd( Z9d)d* Z:d+d, Z;d-d. Z<e*d/d0d1d2 Z=d3d4 Z>d5d6 Z?d7d8 Z@e*d/d9d:d; ZAdsd<d=ZBG d>d? d?eCZDG d@dA dAeDZEG dBdC dCeEejFZGG dDdE dEejHeGZIG dFdG dGeIZJdtdHdIZKdJdK ZLdLdM ZMG dNdO dOeGZNG dPdQ dQeGZOG dRdS dSeEZPdTdU ZQdVdW ZRG dXdY dYeCZSG dZd[ d[ejTZUd\d] ZVd^d_ ZWd`da ZXdudbdcZYddde ZZdfdg Z[dhdi Z\djdk Z]dvdmdnZ^G dodp dpePej_ejTZ`G dqdr dre`ZadS )wz9 Python test discovery, setup and run of test functions.     )absolute_import)division)print_functionN)partial)dedent)
deprecated)fixtures)nodes)filter_traceback)ascii_escaped)enum)get_default_arg_names)get_real_func)getfslineno)	getimfunc)getlocation)is_generator)isclass)
isfunction)NoneType)NOTSET)
REGEX_TYPE)safe_getattr)safe_isclass)safe_str)STRING_TYPES)hookimpl)FSHookProxy)MARK_GEN)get_unpacked_marks)normalize_mark_list)fail)parts)PytestWarningc                s(    fdd}d   f }t|d d |S )Nc                s&   |  ttd }|d k	r"|jS d S )NZpytest)	getparentgetattr
__import__obj)selfnode)name -lib/python3.7/site-packages/_pytest/python.pyget3   s    zpyobj_property.<locals>.getz<python %s object this node was collected from (can be None).)lowerproperty)r*   r-   docr+   )r*   r,   pyobj_property2   s    r1   c             C   s   |  d}|jddddddd |jd	dd
ddd | jddg dd | jddddgdd | jdddgdd | jdddgdd |jddddgddd d S ) NZgeneralz
--fixturesz
--funcargs
store_trueshowfixturesFzishow available fixtures, sorted by plugin appearance (fixtures with leading '_' are only shown with '-v'))actiondestdefaulthelpz--fixtures-per-testshow_fixtures_per_testzshow fixtures per testZusefixturesargsz5list of default fixtures to be used with this project)typer6   r7   python_filesz	test_*.pyz	*_test.pyz9glob-style file patterns for Python test module discoverypython_classesZTestz6prefixes or glob names for Python test class discoverypython_functionsZtestzDprefixes or glob names for Python test function and method discoveryz--import-modeZprependappend
importmodezNprepend/append to sys.path when importing test modules, default is to prepend.)r6   choicesr5   r7   )ZgetgroupZ	addoptionZaddini)parsergroupr+   r+   r,   pytest_addoption>   sP    
rC   c             C   s,   | j jrt|  dS | j jr(t|  dS d S )Nr   )optionr3   r8   )configr+   r+   r,   pytest_cmdline_mains   s    rF   c             C   sl   dddg}x4|D ],}| j |rd}t|| jj|dd qW x&| j jddD ]}| j|j|j	 qPW d S )	NZparameterizeZparametriseZparameterisez4{0} has '{1}' mark, spelling should be 'parametrize'F)pytraceparametrize)r*   )

definitionZget_closest_markerr!   formatfunction__name__iter_markersrH   r9   kwargs)metafuncZalt_spellingsZ	mark_namemsgZmarkerr+   r+   r,   pytest_generate_tests|   s    

rQ   c             C   s   |  dd |  dd d S )NZmarkersa  parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see https://docs.pytest.org/en/latest/parametrize.html for more info and examples.zusefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/latest/fixture.html#usefixtures )Zaddinivalue_line)rE   r+   r+   r,   pytest_configure   s    	rR   T)Ztrylastc             C   sP   | j }|  r|| j  n2| j}i }x| jjD ]}|| ||< q.W |f | dS )NT)r'   _isyieldedfunction_argsfuncargs_fixtureinfoargnames)
pyfuncitemZtestfunctionrU   Ztestargsargr+   r+   r,   pytest_pyfunc_call   s    
rZ   c             C   sT   | j }|dkrP|j| s6t| |jddg s6d S |j| }|j| |dS d S )Nz.pyr;   z__init__.py)pathparent)extsession
isinitpathpath_matches_patternsrE   getinigethookproxypytest_pycollect_makemodule)r[   r\   r]   ihookr+   r+   r,   pytest_collect_file   s    re   c                s   t  fdd|D S )z^Returns True if the given py.path.local matches one of the patterns in the list of globs givenc             3   s   | ]}  |V  qd S )N)fnmatch).0pattern)r[   r+   r,   	<genexpr>   s    z(path_matches_patterns.<locals>.<genexpr>)any)r[   Zpatternsr+   )r[   r,   r`      s    r`   c             C   s   | j dkrt| |S t| |S )Nz__init__.py)basenamePackageModule)r[   r\   r+   r+   r,   rc      s    

rc   )Zhookwrapperc             c   s  d V }|  }|d k	rd S t|rB| ||r@|t|| d n| ||rt|d|}t|stt|st	|\}}t
jtd| d t||d d nnt|ddrt|rt|| d}tjj|d}|tjd	|d
 |t| nt| ||}|| d S )N)r\   __func__z/cannot collect %r because it is not a function.   )messagecategoryfilenamelineno__test__T)r*   F)runreason)Z
get_resultr   istestclassZforce_resultClassistestfunctionr%   r   r   r   warningswarn_explicitr#   strr   Functionr   ZYIELD_TESTSrJ   Z
add_markerr   Zxfailwarnlist_genfunctions)	collectorr*   r'   Zoutcomeresrr   rs   rv   r+   r+   r,   pytest_pycollect_makeitem   s2    r   c             C   s   d S )Nr+   )rE   valargnamer+   r+   r,   pytest_make_parametrize_id   s    r   c               @   s$   e Zd ZedZedZedZdS )PyobjContextrm   rx   InstanceN)rL   
__module____qualname__r1   moduleclsinstancer+   r+   r+   r,   r      s   r   c                   sT   e Zd ZdZ fddZedd Zejdd Zdd Zdd
dZ	dd Z
  ZS )
PyobjMixinTc                s   t t| j|| d S )N)superr   __init__)r(   kkw)	__class__r+   r,   r      s    zPyobjMixin.__init__c             C   s>   t | dd}|dkr:|   | _}| jr:| jt| j |S )zUnderlying Python object._objN)r%   _getobjr   _ALLOW_MARKERSown_markersextendr   r'   )r(   r'   r+   r+   r,   r'      s    zPyobjMixin.objc             C   s
   || _ d S )N)r   )r(   valuer+   r+   r,   r'     s    c             C   s   t | jj| jS )zDGets the underlying Python object. May be overwritten by subclasses.)r%   r\   r'   r*   )r(   r+   r+   r,   r     s    zPyobjMixin._getobjFc             C   s   |   }|  g }xV|D ]N}t|tr*q|j}t|tr^tj|d }|r^|r\|	| P |	| qW |  d
|}|ddS )z7 return python path relative to the containing module. r   .z.[[)Z	listchainreverse
isinstancer   r*   rm   osr[   splitextr>   joinreplace)r(   ZstopatmoduleZincludemodulechainr"   r)   r*   sr+   r+   r,   
getmodpath
  s"    




zPyobjMixin.getmodpathc             C   sr   | j }t|dd }t|trFtj|j j}|dr@|d d }|}nt	|\}}| 
 }t|tsht|||fS )Ncompat_co_firstlinenoz.pyc)r'   r%   r   intsysmodulesr   __file__endswithr   r   AssertionError)r(   r'   r   fspathrs   Zmodpathr+   r+   r,   
reportinfo  s    

zPyobjMixin.reportinfo)TF)rL   r   r   r   r   r/   r'   setterr   r   r   __classcell__r+   r+   )r   r,   r      s   
r   c               @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )PyCollectorc             C   s   |  d|S )Nr=   )_matches_prefix_or_glob_option)r(   r*   r+   r+   r,   funcnamefilter0  s    zPyCollector.funcnamefilterc             C   s   t |dddkS )zg Look for the __test__ attribute, which is applied by the
        @nose.tools.istest decorator
        rt   FT)r   )r(   r'   r+   r+   r,   
isnosetest3  s    zPyCollector.isnosetestc             C   s   |  d|S )Nr<   )r   )r(   r*   r+   r+   r,   classnamefilter<  s    zPyCollector.classnamefilterc             C   sL   |  |s| |rDt|tr*t|dd}t|ddoBt|d kS dS d S )Nrn   F__call__)r   r   r   staticmethodr   r   getfixturemarker)r(   r'   r*   r+   r+   r,   ry   ?  s    
zPyCollector.istestfunctionc             C   s   |  |p| |S )N)r   r   )r(   r'   r*   r+   r+   r,   rw   K  s    zPyCollector.istestclassc             C   sP   xJ| j |D ]:}||r dS d|ks8d|ks8d|krt||rdS qW dS )zs
        checks if the given name matches the prefix or glob-pattern defined
        in ini configuration.
        T*?r   F)rE   ra   
startswithrf   )r(   Zoption_namer*   rD   r+   r+   r,   r   N  s    
z*PyCollector._matches_prefix_or_glob_optionc       	      C   s   t | jddsg S t | jdi g}x"t| jjD ]}||j q2W i }g }xh|D ]`}xZt| D ]J\}}||krxqfd||< | 	||}|d krqft
|ts|g}|| qfW qTW |jdd d |S )Nrt   T__dict__c             S   s   |   d d S )N   )r   )itemr+   r+   r,   <lambda>u  s    z%PyCollector.collect.<locals>.<lambda>)key)r%   r'   inspectZgetmror   r>   r   r   items	_makeitemr   r   sort)	r(   ZdictsZbaseclsseenvaluesZdicr*   r'   r   r+   r+   r,   collect_  s(    

zPyCollector.collectc             C   s   | j j| ||dS )N)r   r*   r'   )rd   r   )r(   r*   r'   r+   r+   r,   r   x  s    zPyCollector._makeitemc          
   c   s&  |  tj}|  t}|r |jp"d }| jj}t|| |d}||||}t||| j	||d}	g }
t
|drv|
|j t
|dr|
| j |
r| jj|
t|	d n| jj|	d |	jst|| |dV  nRt| |	| |  x:|	jD ]0}d||jf }t|| ||||jdi|dV  qW d S )	N)r*   r\   callobj)r   r   rQ   )rO   )r\   fixtureinfoz%s[%s]T)r*   r\   callspecr   r   keywordsoriginalname)r$   rm   r'   rx   r^   _fixturemanagerFunctionDefinitiongetfixtureinfoMetafuncrE   hasattrr>   rQ   rd   Z
call_extradict_callsr}   r   Zadd_funcarg_pseudo_fixture_defZprune_dependency_treeid)r(   r*   funcobjr   Zclscolr   fmrI   r   rO   methodsr   subnamer+   r+   r,   r   |  s>    


zPyCollector._genfunctionsN)rL   r   r   r   r   r   ry   rw   r   r   r   r   r+   r+   r+   r,   r   /  s   	r   c                   s@   e Zd ZdZdd Z fddZdd Zdd	 Zd
d Z  Z	S )rm   z+ Collector for test classes and functions. c             C   s   |   S )N)_importtestmodule)r(   r+   r+   r,   r     s    zModule._getobjc                s,   |    |   | jj|  tt|  S )N)_inject_setup_module_fixture_inject_setup_function_fixturer^   r   parsefactoriesr   rm   r   )r(   )r   r+   r,   r     s    zModule.collectc                s|   t | jd  dkr t | jd t | jddkr@t | jd dkrTdkrTdS tjddd fd	d
}|| j_dS )a(  Injects a hidden autouse, module scoped fixture into the collected module object
        that invokes setUpModule/tearDownModule if either or both are available.

        Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
        other fixtures (#517).
        setUpModuleNsetup_moduletearDownModuleteardown_moduleTr   )autousescopec             3   s2    d k	rt  | j d V  d k	r.t | j d S )N)_call_with_optional_argumentr   )request)r   r   r+   r,   xunit_setup_module_fixture  s
    zGModule._inject_setup_module_fixture.<locals>.xunit_setup_module_fixture)_get_non_fixture_funcr'   r   fixtureZ_Module__pytest_setup_module)r(   r   r+   )r   r   r,   r     s    z#Module._inject_setup_module_fixturec                sT   t | jd t | jd dkr,dkr,dS tjddd fdd}|| j_dS )	a0  Injects a hidden autouse, function scoped fixture into the collected module object
        that invokes setup_function/teardown_function if either or both are available.

        Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
        other fixtures (#517).
        setup_functionteardown_functionNTrK   )r   r   c             3   sF   | j d k	rd V  d S  d k	r(t | j d V  d k	rBt| j d S )N)r   r   rK   )r   )r   r   r+   r,   xunit_setup_function_fixture  s    
zKModule._inject_setup_function_fixture.<locals>.xunit_setup_function_fixture)r   r'   r   r   Z_Module__pytest_setup_function)r(   r   r+   )r   r   r,   r     s    z%Module._inject_setup_function_fixturec          
   C   sJ  | j d}y| jj|d}W n tk
rN   | tjj	 j
ddY n | jjk
r   t d }| d|j Y n tk
r   ddlm} |	 }| j d	d
k r|jt|_|jr|j
ddn| }t|}| dj| j|dY n< tjjk
r6 } z|jr | dW d d }~X Y nX | j j| |S )Nz--import-mode)Zensuresyspathshort)stylero   zimport file mismatch:
imported module %r has this __file__ attribute:
  %s
which is not the same as the test file we want to collect:
  %s
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modulesr   )ExceptionInfoverboser   zImportError while importing test module '{fspath}'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
{traceback})r   	tracebackzUsing pytest.skip outside of a test is not allowed. To decorate a test function, use the @pytest.mark.skip or @pytest.mark.skipif decorators instead, and to skip a module use `pytestmark = pytest.mark.{skip,skipif}.)rE   	getoptionr   ZpyimportSyntaxErrorZCollectError_pytest_coder   Zfrom_currentZgetreprZImportMismatchErrorr   exc_infor9   ImportErrorZ_pytest._code.coder   filterr
   Zexconlyr   rJ   ZrunnerZSkippedZallow_module_levelpluginmanagerZconsider_module)r(   r?   moder   r   Zexc_reprZformatted_tbr+   r+   r,   r     s:    zModule._importtestmodule)
rL   r   r   __doc__r   r   r   r   r   r   r+   r+   )r   r,   rm     s   rm   c               @   sH   e Zd ZdddZdd Zdd Zdd	 ZdddZdd Zdd Z	dS )rl   Nc             C   s@   |j }tjj| |||||d |j| _|j| _|j| _|| _d S )N)r\   rE   r^   nodeid)	r^   r	   ZFSCollectorr   dirnamer*   Ztrace_norecursepatternsr   )r(   r   r\   rE   r^   r   r+   r+   r,   r   #  s    zPackage.__init__c             C   sx   t | jd}|d kr t | jd}|d k	r4t|| j t | jd}|d krTt | jd}|d k	rttt|| j}| | d S )Nr   r   r   r   )r   r'   r   r   Zaddfinalizer)r(   r   r   funcr+   r+   r,   setup-  s    zPackage.setupc             C   sl   |j dkrdS | | }|j|| jdr0d S x| jD ]}|j|dr8dS q8W | |}|j|| d dS )N__pycache__F)r[   rE   )rf   )r[   r\   T)rk   rb   dirpathpytest_ignore_collectrE   r   checkZpytest_collect_directory)r(   r  rd   Zpatr+   r+   r,   _recurse=  s    

zPackage._recursec             C   s<   | j j}||}|j|}|r0t|||}n| j j}|S )N)rE   r   Z_getconftestmodulesZ_conftest_plugins
differencer   hook)r(   r   ZpmZmy_conftestmodulesZremove_modsproxyr+   r+   r,   rb   J  s    
zPackage.gethookproxyTc             C   s   |  s(td|| | | f | |}| |sP|j|| jdrPdS |r| j	d}|s| jj
j}||krzdS || | j|kr| gS |j|| dS )Nz1%r is not a file (isdir=%r, exists=%r, islink=%r))r[   rE   r+   keepduplicates)r[   r\   )isfiler   isdirexistsislinkrb   r_   r  rE   r   r   Z_duplicatepathsaddr   re   )r(   r[   Zhandle_dupesrd   r  Zduplicate_pathsr+   r+   r,   _collectfileX  s&    




zPackage._collectfilec             C   s   || j jkS )N)r^   Z_initialpaths)r(   r[   r+   r+   r,   r_   r  s    zPackage.isinitpathc             #   s   | j  }|d}|jddr>t|| jdr>t|| V  t }x|j	| j
dddD ] }|rjdkr |krqXtj t fdd|D rqX|rxB| D ]
}|V  qW qX sqXqXdjddrX| qXW d S )	Nz__init__.pyro   )filer;   T)ZrecZbfr   c             3   s$   | ]}| ko| d kV  qdS )z__init__.pyN)r   )rg   Z
pkg_prefix)parts_r[   r+   r,   ri     s   z"Package.collect.<locals>.<genexpr>)r   r  r   r  r`   rE   ra   rm   setZvisitr  r	  rk   r"   Zstrpathrj   r  r
  r  )r(   Z	this_pathZinit_moduleZpkg_prefixesis_filexr+   )r  r[   r,   r   u  s.    



zPackage.collect)NNNN)T)
rL   r   r   r   r   r  rb   r  r_   r   r+   r+   r+   r,   rl   "  s   


rl   c                sV    dk	r n|  t | |dk	rRjj}tr<|d8 }|rN fddS S dS )a^  
    Return a callable to perform xunit-style setup or teardown if
    the function exists in the ``holder`` object.
    The ``param_obj`` parameter is the parameter which will be passed to the function
    when the callable is called without arguments, defaults to the ``holder`` object.
    Return ``None`` if a suitable callable is not found.
    Nro   c                  s    S )Nr+   r+   )	param_objresultr+   r,   r     s    z+_get_xunit_setup_teardown.<locals>.<lambda>)r   __code__co_argcountr   ismethod)ZholderZ	attr_namer  	arg_countr+   )r  r  r,   _get_xunit_setup_teardown  s    	

r  c             C   s2   | j j}t| r|d8 }|r(| | n|   dS )zxCall the given function with the given argument if func accepts one argument, otherwise
    calls func without argumentsro   N)r  r  r   r  )r   rY   r  r+   r+   r,   r     s    

r   c             C   s"   t | |d}t|dkr|S dS )zReturn the attribute from the given object to be used as a setup/teardown
    xunit-style function, but only if not marked as a fixture to
    avoid calling it twice.
    N)r%   r   r   )r'   r*   methr+   r+   r,   r     s    r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rx   z Collector for test methods. c             C   sx   t | jddsg S t| jr6| td| jj  g S t| jrZ| td| jj  g S |   |   t	d| dgS )Nrt   TzBcannot collect test class %r because it has a __init__ constructorzAcannot collect test class %r because it has a __new__ constructorz())r*   r\   )
r   r'   hasinitr~   r#   rL   hasnew_inject_setup_class_fixture_inject_setup_method_fixturer   )r(   r+   r+   r,   r     s"    

zClass.collectc                sX   t  jdt jdddkr.dkr.dS tjddd fdd}| j_dS )	a&  Injects a hidden autouse, class scoped fixture into the collected class object
        that invokes setup_class/teardown_class if either or both are available.

        Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
        other fixtures (#517).
        setup_classteardown_classNTclass)r   r   c             3   sB   d k	rt }t| j d V  d k	r>t }t| j d S )N)r   r   r'   )r   r   )r(   r   r!  r+   r,   xunit_setup_class_fixture  s    zDClass._inject_setup_class_fixture.<locals>.xunit_setup_class_fixture)r   r'   r%   r   r   Z_Class__pytest_setup_class)r(   r#  r+   )r(   r   r!  r,   r    s    
z!Class._inject_setup_class_fixturec                sV   t | jd t| jdd dkr.dkr.dS tjddd fdd}|| j_dS )	a+  Injects a hidden autouse, function scoped fixture into the collected class object
        that invokes setup_method/teardown_method if either or both are available.

        Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
        other fixtures (#517).
        setup_methodteardown_methodNTrK   )r   r   c             3   sH   |j } d k	r"t| d}t|| d V  d k	rDt| d}t|| d S )Nr$  r%  )rK   r%   r   )r(   r   methodr   )r$  r%  r+   r,   xunit_setup_method_fixture   s    


zFClass._inject_setup_method_fixture.<locals>.xunit_setup_method_fixture)r   r'   r%   r   r   Z_Class__pytest_setup_method)r(   r'  r+   )r$  r%  r,   r    s    z"Class._inject_setup_method_fixtureN)rL   r   r   r   r   r  r  r+   r+   r+   r,   rx     s   rx   c                   s0   e Zd ZdZdd Z fddZdd Z  ZS )r   Fc             C   s
   | j  S )N)r\   r'   )r(   r+   r+   r,   r     s    zInstance._getobjc                s   | j j|  tt|  S )N)r^   r   r   r   r   r   )r(   )r   r+   r,   r     s    zInstance.collectc             C   s   |   | _| jS )N)r   r'   )r(   r+   r+   r,   newinstance  s    
zInstance.newinstance)rL   r   r   r   r   r   r(  r   r+   r+   )r   r,   r     s   r   c               @   s*   e Zd ZdZdd Zdd Zd	ddZdS )
FunctionMixinz: mixin for the code common to Function and Generator.
    c             C   s$   t | jtr | j  |  | _dS )z' perform setup for this test function. N)r   r\   r   r(  r   r'   )r(   r+   r+   r,   r   $  s    
zFunctionMixin.setupc             C   s   t | dr| jjjstjt| j}|j	|j
 }}|j}|j||d}||krv|j|d}||krv|t}|sv|}| |_| jjjdkrt|jdkrx |jdd D ]}|d qW d S )	Nr   )r[   firstlineno)r[   autor   ro   r   r   )r   rE   rD   Z	fulltracer   r   ZCoder   r'   r[   r*  r   Zcutr   r
   tbstylelenZset_repr_style)r(   excinfocoder[   r*  r   Z
ntracebackentryr+   r+   r,   _prunetraceback*  s     

zFunctionMixin._prunetracebackNc             C   s4   |d kst d| jjj}|dkr&d}| j||dS )NzXXX outerr usage is deprecatedr+  Zlong)r   )r   rE   rD   r,  Z_repr_failure_py)r(   r.  Zouterrr   r+   r+   r,   repr_failure?  s
    
zFunctionMixin.repr_failure)N)rL   r   r   r   r   r1  r2  r+   r+   r+   r,   r)     s   r)  c             C   s   t | dd }|r|tjkS d S )Nr   )r%   objectr   )r'   Zinitr+   r+   r,   r  G  s    r  c             C   s   t | dd }|r|tjkS d S )N__new__)r%   r3  r4  )r'   newr+   r+   r,   r  M  s    r  c               @   sH   e Zd Zdd Zdd Zdd Zdd Zed	d
 Zdd Z	dd Z
dS )	CallSpec2c             C   s:   || _ i | _g | _i | _t| _t| _i | _g | _i | _	d S )N)
rO   rU   _idlistparamsr   	_globalid_globalparam_arg2scopenummarksindices)r(   rO   r+   r+   r,   r   T  s    zCallSpec2.__init__c             C   sp   t | j}|j| j |j| j |j| j |j| j |j| j t	| j
|_
| j|_| j|_|S )N)r6  rO   rU   updater8  r<  r   r=  r;  r   r7  r9  r:  )r(   Zcsr+   r+   r,   copy_  s    
zCallSpec2.copyc             C   s&   || j ks|| jkr"td|f d S )Nzduplicate %r)r8  rU   
ValueError)r(   rY   r+   r+   r,   _checkargnotcontainedk  s    zCallSpec2._checkargnotcontainedc             C   s8   y
| j | S  tk
r2   | jtkr,t|| jS X d S )N)r8  KeyErrorr:  r   r@  )r(   r*   r+   r+   r,   getparamo  s    

zCallSpec2.getparamc             C   s   d tttd | jS )N-)r   mapr|   r   r7  )r(   r+   r+   r,   r   w  s    zCallSpec2.idc             C   sl   xJt ||D ]<\}}	| | || }
|	t| |
|< || j|< || j|< qW | j| | jt	| d S )N)
ziprA  r%   r=  r;  r7  r>   r<  r   r    )r(   valtypesrW   Zvalsetr   r<  scopenumparam_indexrY   r   Zvaltype_for_argr+   r+   r,   	setmulti2{  s    

zCallSpec2.setmulti2c             C   sr   x|D ]}|  | qW | j| |tk	r8| j| |tk	rT| jtksNt|| _x|D ]}tj	| j
|< qZW d S )N)rA  rU   r>  r   r7  r>   r:  r   r   Zscopenum_functionr;  )r(   rU   r   paramr  rY   r+   r+   r,   setall  s    

zCallSpec2.setallN)rL   r   r   r   r?  rA  rC  r/   r   rJ  rL  r+   r+   r+   r,   r6  S  s   
r6  c               @   s<   e Zd ZdZdddZdddZdd	 Zd
d Zdd ZdS )r   a-  
    Metafunc objects are passed to the :func:`pytest_generate_tests <_pytest.hookspec.pytest_generate_tests>` hook.
    They help to inspect a test function and to generate tests according to
    test configuration or values specified in the class or module where a
    test function is defined.
    Nc             C   s^   t |tst|jdkst|| _|| _|| _|j| _	|j
| _|| _g | _t | _|j| _d S )NZDefinitionMock)r   r   r:   rL   r   rI   rE   r   r'   rK   names_closurefixturenamesr   r   r  Z_idsname2fixturedefs_arg2fixturedefs)r(   rI   r   rE   r   r   r+   r+   r,   r     s    
zMetafunc.__init__Fc             C   s  ddl m} ddlm} |j||| j| j| jd\}}~|dkrNt|| j	|}| 
|| | ||}	| j|||| jd}||d| jjd}
g }xd| jpt| gD ]P}xJtt||D ]8\}\}}| }||	||j||j|
| || qW qW || _dS )	a   Add new invocations to the underlying test function using the list
        of argvalues for the given argnames.  Parametrization is performed
        during the collection phase.  If you need to setup expensive resources
        see about setting indirect to do it rather at test setup time.

        :arg argnames: a comma-separated string denoting one or more argument
                       names, or a list/tuple of argument strings.

        :arg argvalues: The list of argvalues determines how often a
            test is invoked with different argument values.  If only one
            argname was specified argvalues is a list of values.  If N
            argnames were specified, argvalues must be a list of N-tuples,
            where each tuple-element specifies a value for its respective
            argname.

        :arg indirect: The list of argnames or boolean. A list of arguments'
            names (subset of argnames). If True the list contains all names from
            the argnames. Each argvalue corresponding to an argname in this list will
            be passed as request.param to its respective argname fixture
            function so that it can perform more expensive setups during the
            setup phase of a test rather than at collection time.

        :arg ids: list of string ids, or a callable.
            If strings, each is corresponding to the argvalues so that they are
            part of the test id. If None is given as id of specific test, the
            automatically generated id for that argument will be used.
            If callable, it should take one argument (a single argvalue) and return
            a string or return None. If None, the automatically generated id for that
            argument will be used.
            If no ids are provided they will be generated automatically from
            the argvalues.

        :arg scope: if specified it denotes the scope of the parameters.
            The scope is used for grouping tests by parameter instances.
            It will also override any fixture-function defined scope, allowing
            to set a dynamic scope using test context or configuration.
        r   )scope2index)ParameterSet)Zfunction_definitionN)r   zparametrize() call in {})Zdescr)_pytest.fixturesrQ  _pytest.markrR  Z_for_parametrizerK   rE   rI   _find_parametrized_scoperP  _validate_if_using_arg_names_resolve_arg_value_types_resolve_arg_idsrJ   rL   r   r6  	enumeraterF  r?  rJ  r   r<  r>   )r(   rW   Z	argvaluesindirectidsr   rQ  rR  
parametersZarg_values_typesrH  Znewcallsr   rI  Zparam_idZ	param_setZnewcallspecr+   r+   r,   rH     s<    &zMetafunc.parametrizec       
      C   s   ddl m} d}t|r |}d}|r| jj}t|t|kr^d}t||t|t|dd xB|D ]:}	|	dk	rdt|	t	j
sdd}t||||	t|	dd qdW t||||| j|d}|S )	a  Resolves the actual ids for the given argnames, based on the ``ids`` parameter given
        to ``parametrize``.

        :param List[str] argnames: list of argument names passed to ``parametrize()``.
        :param ids: the ids parameter of the parametrized call (see docs).
        :param List[ParameterSet] parameters: the list of parameter values, same size as ``argnames``.
        :param Item item: the item that generated this parametrized call.
        :rtype: List[str]
        :return: the list of ids for each argname given
        r   )safereprNzDIn {}: {} parameter sets specified, with different number of ids: {}F)rG   z:In {}: ids must be list of strings, found: {} (type: {!r}))r   )Z_pytest._io.safereprr]  callablerK   rL   r-  r!   rJ   r   sixZstring_typesr:   idmakerrE   )
r(   rW   r[  r\  r   r]  idfn	func_namerP   Zid_valuer+   r+   r,   rX    s$    
zMetafunc._resolve_arg_idsc             C   s   i }|dkrt |d}nf|dkr0t |d}nPt|ttfrt |d}x4|D ],}||krttd| jj|dd d||< qPW |S )aw  Resolves if each parametrized argument must be considered a parameter to a fixture or a "funcarg"
        to the function, based on the ``indirect`` parameter of the parametrized() call.

        :param List[str] argnames: list of argument names passed to ``parametrize()``.
        :param indirect: same ``indirect`` parameter of ``parametrize()``.
        :rtype: Dict[str, str]
            A dict mapping each arg name to either:
            * "params" if the argname should be the parameter of a fixture of the same name.
            * "funcargs" if the argname should be a parameter to the parametrized test function.
        Tr8  FrU   z*In {}: indirect fixture '{}' doesn't exist)rG   )	r   fromkeysr   tupler   r!   rJ   rK   rL   )r(   rW   rZ  rG  rY   r+   r+   r,   rW  &  s    

z!Metafunc._resolve_arg_value_typesc             C   s   t t| j}| jj}xv|D ]n}|| jkr||krHtd||dd qt|tt	frh||krbdnd}n|rpdnd}td|||dd qW dS )a5  
        Check if all argnames are being used, by default values, or directly/indirectly.

        :param List[str] argnames: list of argument names passed to ``parametrize()``.
        :param indirect: same ``indirect`` parameter of ``parametrize()``.
        :raise ValueError: if validation fails.
        zCIn {}: function already takes an argument '{}' with a default valueF)rG   r   ZargumentzIn {}: function uses no {} '{}'N)
r  r   rK   rL   rN  r!   rJ   r   rd  r   )r(   rW   rZ  Zdefault_arg_namesrb  rY   r*   r+   r+   r,   rV  C  s    


z%Metafunc._validate_if_using_arg_names)NN)FNN)	rL   r   r   r   r   rH   rX  rW  rV  r+   r+   r+   r,   r     s   

R r   c                s|   ddl m} t|ttfr,t|t k}nt|}|rx|p>i } fdd| D }|rxxt|D ]}||krd|S qdW dS )a}  Find the most appropriate scope for a parametrized call based on its arguments.

    When there's at least one direct argument, always use "function" scope.

    When a test function is parametrized and all its arguments are indirect
    (e.g. fixtures), return the most narrow scope based on the fixtures used.

    Related to issue #1832, based on code posted by @Kingdread.
    r   )scopesc                s"   g | ]\}}| kr|d  j qS )r   )r   )rg   r*   
fixturedef)rW   r+   r,   
<listcomp>u  s   z,_find_parametrized_scope.<locals>.<listcomp>rK   )	rS  re  r   r   rd  r-  boolr   reversed)rW   Zarg2fixturedefsrZ  re  Zall_arguments_are_fixturesfixturedefsZused_scopesr   r+   )rW   r,   rU  a  s    

rU  c       
   
   C   s,  |r|y|| }|d k	r|} W q t k
rx } z>d}||j||}|dt|j|7 }tt|| W d d }~X Y qX n|r|jj	|| |d}	|	r|	S t
| trt| S t
| ttttfrt| S t
| trt| jS td k	rt
| tjrt| S t| s
t| rt| dr| jS t|t| S )NzO{}: error raised while trying to determine id of parameter '{}' at position {}
z	  {}: {}
)rE   r   r   rL   )	ExceptionrJ   r   r:   rL   r_  Z
raise_fromr@  r  r   r   r   r   floatr   rh  r   r|   r   rh   r   Enumr   r   r   )
r   r   idxra  r   rE   Zgenerated_idr   rP   Zhook_idr+   r+   r,   _idval  s4    $


 ro  c                sj   |j d k	r|j S |d ks0t|ks0| d krZ fddt|j|D }d|S t| S d S )Nc          
      s$   g | ]\}}t || d qS ))r   rE   )ro  )rg   r   r   )rE   ra  rn  r   r+   r,   rg    s   z_idvalset.<locals>.<listcomp>rD  )r   r-  rF  r   r   r   )rn  parametersetrW   ra  r[  r   rE   Zthis_idr+   )rE   ra  rn  r   r,   	_idvalset  s    
 
rq  c       
         s    fddt |D tttkrfddD }tdd }x@t D ]4\}}	|	|kr\|	t||	  |< ||	  d7  < q\W S )Nc                s&   g | ]\}}t || d qS ))rE   r   )rq  )rg   Zvalindexrp  )rW   rE   ra  r[  r   r+   r,   rg    s   zidmaker.<locals>.<listcomp>c                s   g | ]}  |d kr|qS )ro   )count)rg   testid)r[  r+   r,   rg    s    c               S   s   dS )Nr   r+   r+   r+   r+   r,   r     s    zidmaker.<locals>.<lambda>ro   )rY  r-  r  collectionsdefaultdictr|   )
rW   Zparametersetsra  r[  rE   r   
duplicatesZcountersindexrs  r+   )rW   rE   ra  r[  r   r,   r`    s    r`  c             C   s   ddl m} || tS )Nr   )wrap_session)_pytest.mainrx  _show_fixtures_per_test)rE   rx  r+   r+   r,   r8     s    r8   c                sx   dd l }|  tj  |j| | d fddfddfdd}x|jD ]}|| qdW d S )	Nr   r   c                s   t |  } |S )N)r   bestrelpath)r   loc)curdirr+   r,   get_best_relpath  s    
z1_show_fixtures_per_test.<locals>.get_best_relpathc                sx   | j }dkr|drd S dkr< | j}d||}n|}j|dd | jj}|rft| njddd d S )Nr   _z{} -- {}T)greenz    no docstring available)red)r   r   r   rJ   liner   write_docstring)Zfixture_defr   bestrelfuncargspecZfixture_doc)r~  twr   r+   r,   write_fixture  s    
z._show_fixtures_per_test.<locals>.write_fixturec                s   y
| j }W n tk
r   d S X |js*d S   dd| j dd | j x8t|j	 D ]&\}}|d k	st
|sqn|d  qnW d S )NrD  zfixtures used by {}z({})r   )rV   AttributeErrorrO  r  seprJ   r*   rK   sortedr   r   )r   infor  rj  )r~  r  r  r+   r,   
write_item  s    
z+_show_fixtures_per_test.<locals>.write_item)	_pytest.configperform_collectpyr[   localrE   create_terminal_writergetvaluer   )rE   r^   r   r  Zsession_itemr+   )r}  r~  r  r   r  r,   rz    s    

rz  c             C   s   ddl m} || tS )Nr   )rx  )ry  rx  _showfixtures_main)rE   rx  r+   r+   r,   r3     s    r3   c          	   C   s  dd l }|  tj }|j| }| d}|j}g }t	 }x|j
 D ]z\}	}
|
d k	s`t|
sfqLx^|
D ]V}t|j|}|j|f|krql||j|f |t|j|jj|||j|f qlW qLW |  d }x|D ]\}}}}	}||kr|ds|  |dd|f  |}|dkr8|	d dkr8q|dkrPd|	|f }n|	}|j|dd	 t|j|}|jjpzd
}|rt|| q|jd|f dd qW d S )Nr   r   z_pytest.rD  zfixtures defined from %sr  z%s -- %sT)r   z    %s: no docstring available)r  )r  r  r  r[   r  rE   r  r  r   r  rP  r   r   r   r   r   r  r>   r-  baseidr   r{  r   r   r  r  r   r  )rE   r^   r   r}  r  r   r   Z	availabler   r   rj  rf  r|  Zcurrentmoduler  r   r  r  r0   r+   r+   r,   r     sT    




r      c             C   sx   |  }d|kr"|dd\}}n
|d }}| rF| ||   |rtx(t|dD ]}| || d  qZW d S )N
ro   r  )rstripsplitstripr  r   write)r  r0   indent	firstlinerestr  r+   r+   r,   r  7  s    
r  c                   sz   e Zd ZdZdZdddeddddf fdd	Zdd Zedd	 Z	d
d Z
edd Zdd Zdd Z fddZ  ZS )r}   z] a Function Item is responsible for setting up and executing a
    Python test function.
    FNc                s  t t j||||d | _|tk	r,| _ j jj  j	
t j |r| _x|jD ]}| j|j< q`W  j	
t|j |r j|  jt fdd  D d |	d kr jjj  j j   d}	|	 _|	j _   |
 _d S )N)rE   r^   c                s   g | ]}|j  jkr|j qS r+   )r*   r   )rg   mark)r(   r+   r,   rg  u  s   z%Function.__init__.<locals>.<listcomp>T)rU   )r   r}   r   rT   r   r'   r   r>  r   r   r   r   r   r<  r*   r    r   rc  rM   r^   r   r   r   rS   rV   rM  rN  _initrequestr   )r(   r*   r\   r9   rE   r   r   r   r^   r   r   r  )r   )r(   r,   r   N  s2    

zFunction.__init__c             C   s^   i | _ |  r"t| drNtdn,t| drN| j}|j r<tt|drN|j| _t| | _d S )Nr   z3yielded functions (deprecated) cannot have funcargsrK  )	rU   rS   r   r   r   rK  r   ZFixtureRequestZ_request)r(   r   r+   r+   r,   r    s    



zFunction._initrequestc             C   s
   t | jS )z#underlying python 'function' object)r   r'   )r(   r+   r+   r,   rK     s    zFunction.functionc             C   s2   | j }|d}|dkr$|d | }t| jj|S )Nr   r   )r*   findr%   r\   r'   )r(   r*   ir+   r+   r,   r     s
    
zFunction._getobjc             C   s   | S )z@(compatonly) for code expecting pytest-2.2 style request objectsr+   )r(   r+   r+   r,   _pyfuncitem  s    zFunction._pyfuncitemc             C   s   t | dd d k	S )NrT   )r%   )r(   r+   r+   r,   rS     s    zFunction._isyieldedfunctionc             C   s   | j j| d dS )z' execute the underlying test function. )rX   N)rd   rZ   )r(   r+   r+   r,   runtest  s    zFunction.runtestc                s   t t|   t|  d S )N)r   r}   r   r   Zfillfixtures)r(   )r   r+   r,   r     s    zFunction.setup)rL   r   r   r   r   r   r   r  r/   rK   r   r  rS   r  r   r   r+   r+   )r   r,   r}   F  s"   2r}   c               @   s   e Zd ZdZdd ZeZdS )r   zd
    internal hack until we get actual definition nodes instead of the
    crappy metafunc hack
    c             C   s   t dd S )Nz0function definitions are not supposed to be used)RuntimeError)r(   r+   r+   r,   r    s    zFunctionDefinition.runtestN)rL   r   r   r   r  r   r+   r+   r+   r,   r     s   r   )N)N)NNNN)r  )br   Z
__future__r   r   r   rt  rf   r   r   r   rz   	functoolsr   textwrapr   r  r_  r   r   r   r	   Z_pytest._coder
   Z_pytest.compatr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   ry  r   rT  r   Z_pytest.mark.structuresr   r    Z_pytest.outcomesr!   Z_pytest.pathlibr"   Z_pytest.warning_typesr#   r1   rC   rF   rQ   rR   rZ   re   r`   rc   r   r   r3  r   r   Z	Collectorr   ZFilerm   rl   r  r   r   rx   r   r)  r  r  r6  ZFuncargnamesCompatAttrr   rU  ro  rq  r`  r8   rz  r3   r  r  ZItemr}   r   r+   r+   r+   r,   <module>   s   5	%
?}vs

N'? P!!
47
p