ó
J9•Qc           @   sÒ  d  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 m	 Z	 d d l
 m Z m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z m Z m Z d d
 l m Z m Z d d l m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% d d l& m' Z' m( Z( m) Z) d d l
 m* Z* m+ Z+ e j, e- ƒ Z. e j/ j0 Z1 e j/ j2 Z3 e j/ j4 Z5 e j/ j6 Z7 e j/ j8 Z9 d d g Z: d e j; f d „  ƒ  YZ; e; Z< d S(   sß   
Test Loader
-----------

nose's test loader implements the same basic functionality as its
superclass, unittest.TestLoader, but extends it by more liberal
interpretations of what may be a test and how a test may be named.
iÿÿÿÿ(   t
   generatorsN(   t
   isfunction(   t   unbound_methodt   ismethod(   t   FunctionTestCaset   MethodTestCase(   t   Failure(   t   Config(   t   Importert   add_patht   remove_path(   t   defaultSelectort   TestAddress(
   t   func_linenot
   getpackaget   isclasst   isgeneratort	   ispackaget   regex_last_keyt   resolve_namet   transplant_funct   transplant_classt   test_address(   t   ContextSuiteFactoryt   ContextListt	   LazySuite(   t	   sort_listt
   cmp_to_keyt
   TestLoadert   defaultTestLoaderc           B   sæ   e  Z d  Z d Z d Z d Z d Z d Z d d d d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e d	 „ Z d e d
 „ Z d d „ Z d „  Z d „  Z d d „ Z d d „ Z d „  Z d „  Z RS(   sA  Test loader that extends unittest.TestLoader to:

    * Load tests from test-like functions and classes that are not
      unittest.TestCase subclasses
    * Find and load test modules in a directory
    * Support tests that are generators
    * Support easy extensions of or changes to that behavior through plugins
    c         C   s  | d k r t ƒ  } n  | d k r6 t d | ƒ } n  | d k rN | j } n  | d k ri t | ƒ } n t | ƒ r„ | | ƒ } n  | |  _ | |  _ t t	 | ƒ ƒ |  _ | |  _
 | j rÍ t | | ƒ n  t d | ƒ |  _ t g  ƒ |  _ t j j |  ƒ d S(   s  Initialize a test loader.

        Parameters (all optional):

        * config: provide a `nose.config.Config`_ or other config class
          instance; if not provided a `nose.config.Config`_ with
          default values is used.          
        * importer: provide an importer instance that implements
          `importFromPath`. If not provided, a
          `nose.importer.Importer`_ is used.
        * workingDir: the directory to which file and module names are
          relative. If not provided, assumed to be the current working
          directory.
        * selector: a selector class or instance. If a class is
          provided, it will be instantiated with one argument, the
          current config. If not provided, a `nose.selector.Selector`_
          is used.
        t   configN(   t   NoneR   R   t
   workingDirR   R   R   t   importert   op_normpatht
   op_abspatht   selectort   addPathsR	   R   t
   suiteClasst   sett   _visitedPathst   unittestR   t   __init__(   t   selfR   R!   R    R$   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR*   ;   s&    				c         C   sØ   |  j  j r t j j |  | ƒ S| |  j d „ } t | t | ƒ ƒ } xG | j D]< } x3 |  j | ƒ D]" } | | k rf | j	 | ƒ qf qf WqP W| r² t
 | d ƒ r² d g } n  |  j rÔ t | t |  j ƒ ƒ n  | S(   s_   Override to select with selector, unless
        config.getTestCaseNamesCompat is True
        c         S   sM   t  | |  d  ƒ } t | ƒ r0 t | | ƒ } n t | ƒ s@ t S| j | ƒ S(   N(   t   getattrR   R   R   R   t   Falset
   wantMethod(   t   attrt   clst   selt   item(    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   wantedl   s    t   runTest(   R   t   getTestCaseNamesCompatR)   R   t   getTestCaseNamesR$   t   filtert   dirt	   __bases__t   appendt   hasattrt   sortTestMethodsUsingR   R   (   R+   t   testCaseClassR3   t   casest   baset   case(    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR6   e   s    	c         C   s   | d  k r t S| |  j k S(   N(   R   R-   R(   (   R+   t   path(    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   _haveVisited   s    c         C   s#   | d  k	 r |  j j | ƒ n  d  S(   N(   R   R(   t   add(   R+   RA   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   _addVisitedPath‡   s    c         #   s™  t  j d | ƒ ˆ j j } | j | ƒ ˆ j j rJ t | ˆ j ƒ } n  t j | ƒ } t	 | t
 ˆ j j ƒ ƒ xa| D]Y} | j d ƒ r” qy n  t t | | ƒ ƒ ‰  t ˆ  ƒ } t } | rÜ t } ˆ j j ˆ  ƒ } n< t ˆ  ƒ } | r| j d ƒ rqy n  ˆ j j ˆ  ƒ } n  t ˆ  ƒ }	 | ry ˆ j ˆ  ƒ ry ˆ j ˆ  ƒ | r˜| j ƒ  | j d ƒ r}ˆ j ˆ  d t ƒVn ˆ j ˆ  ƒ V| j ƒ  qÒ|	 rµˆ j ˆ  d t ƒVqÒˆ j ‡  ‡ f d †  ƒ Vqy qy Wg  }
 x$ | j | ƒ D] } |
 j  | ƒ qìWy |
 rˆ j |
 ƒ Vn  Wn= t! t" f k
 r:‚  n$ ˆ j t# t$ j% ƒ  Œ  g ƒ Vn Xˆ j j rˆx | D] } t& | ƒ qqWn  | j' | ƒ d S(   sÚ   Load tests from the directory at path. This is a generator
        -- each suite of tests from a module or other file is yielded
        and is expected to be executed before the next file is
        examined.
        s   load from dir %st   .t   _s   .pyt
   discoveredc              s   ˆ j  ˆ  ƒ S(   N(   t   loadTestsFromDir(    (   t
   entry_pathR+   (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   <lambda>Å   s    N((   t   logt   debugR   t   pluginst   beforeDirectoryR%   R	   t   ost   listdirR   R   t	   testMatcht
   startswithR#   t   op_joint	   op_isfileR-   R$   t   wantFilet   op_isdirt   wantDirectoryR   RB   RD   t   beforeContextt   endswitht   loadTestsFromNamet   Truet   loadTestsFromFilet   afterContextR&   RH   R:   t   KeyboardInterruptt
   SystemExitR   t   syst   exc_infoR
   t   afterDirectory(   R+   RA   RM   t   paths_addedt   entriest   entryt   is_fileR3   t   is_dirt
   is_packaget   testst   testt   p(    (   RI   R+   s/   /sys/lib/python2.7/site-packages/nose/loader.pyRH   ‹   sd    
!c      	   C   sè   t  j d | ƒ yq g  |  j j j | ƒ D] } | ^ q) } | r] t d | ƒ } |  j | ƒ St | d ƒ j	 ƒ  t
 d | ƒ ‚ Wn` t t f k
 r ‚  nG t j ƒ  } |  j t | d | d | d d | d d f ƒg ƒ SXd S(	   s¸   Load tests from a non-module file. Default is to raise a
        ValueError; plugins may implement `loadTestsFromFile` to
        provide a list of tests loaded from the file.
        s   Load from non-module file %st   rs!   Unable to load tests from file %si    i   i   t   addressN(   RK   RL   R   RM   R\   R7   R   R&   t   opent   closet
   ValueErrorR^   R_   R`   Ra   R   (   R+   t   filenameRj   Ri   t   exc(    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR\   Ø   s"    %c            s1   ˆ  | ‡  ‡ f d † } ˆ j  | d ˆ  d t ƒS(   sº   Lazy-load tests from a generator function. The generator function
        may yield either:

        * a callable, or
        * a function name resolvable within the same module
        c         3   s¸   yb x[ |  ƒ  D]P } ˆ j  | ƒ \ } } t | ƒ sF t | | ƒ } n  t | d | d |  ƒVq WWnO t k
 rx ‚  n< t j ƒ  } t | d | d | d d t ˆ  ƒ ƒVn Xd  S(   Nt   argt
   descriptori    i   i   Rm   (	   t   parseGeneratedTestt   callableR,   R   R^   R`   Ra   R   R   (   t   gt   mRj   t	   test_funcRs   Rr   (   t	   generatorR+   (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   generateû   s    t   contextt	   can_split(   R&   R-   (   R+   Rz   t   moduleR{   (    (   Rz   R+   s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   loadTestsFromGeneratorô   s    c            sm   t  ˆ  d ƒ r ˆ  j } n  | ƒ  } ˆ  j } t | | ƒ ‰  ˆ  | ‡  ‡ f d † } ˆ j | d ˆ  d t ƒS(   sý   Lazy-load tests from a generator method.

        This is more complicated than loading from a generator function,
        since a generator method may yield:

        * a function
        * a bound or unbound method, or
        * a method name
        t   im_classc         3   s  y² x« |  ƒ  D]  } ˆ j  | ƒ \ } } t | ƒ sO t | t | | ƒ ƒ } n  t | ƒ ru t | d | d |  ƒVq t | ƒ r› t |  d | d | ƒVq t t d | ƒ Vq WWnO t	 k
 rÈ ‚  n< t
 j ƒ  } t | d | d | d d t ˆ  ƒ ƒVn Xd  S(	   NRs   Rt   Rj   s   %s is not a function or methodi    i   i   Rm   (   Ru   Rv   R   R,   R   R   R   R   t	   TypeErrorR^   R`   Ra   R   (   Rw   t   cRj   Ry   Rs   Rr   (   Rz   R+   (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR{     s$    R|   R}   (   R;   R€   t   __name__R,   R&   R-   (   R+   Rz   R0   t   instt   methodR{   (    (   Rz   R+   s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   loadTestsFromGeneratorMethod
  s    		c            s  t  j d ˆ  ƒ g  } g  } g  } | s; ˆ j j ˆ  ƒ rx… t ˆ  ƒ D]w } t ˆ  | d ƒ } t | ƒ r‘ ˆ j j | ƒ r¿ | j	 | ƒ q¿ qH t
 | ƒ rH ˆ j j | ƒ rH | j	 | ƒ qH qH Wt | d „  ƒ t | t ƒ t ‡  ‡ f d †  | | ƒ } n  t ˆ  d g  ƒ }	 | r/t j j | ƒ } n  x‡ |	 D] }
 t  j d |
 ƒ t  j d | |
 t j j |
 ƒ ƒ ˆ j j sœ| sœt j j |
 ƒ j | ƒ r6| j ˆ j |
 ƒ ƒ q6q6Wx- ˆ j j j ˆ  | ƒ D] } | j	 | ƒ qÒWˆ j t | d ˆ  ƒƒ S(	   sã   Load all tests from module and return a suite containing
        them. If the module has been discovered and is not test-like,
        the suite will be empty by default, though plugins may add
        their own tests.
        s   Load from module %sc         S   s   |  j  S(   N(   Rƒ   (   t   x(    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyRJ   L  s    c            s   ˆ j  |  d ˆ  ƒS(   Nt   parent(   t   makeTest(   t   t(   R~   R+   (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyRJ   N  s    t   __path__s   Load tests from module path %s?s!   path: %s os.path.realpath(%s): %sR|   N(   RK   RL   R$   t
   wantModuleR8   R,   R   R   t	   wantClassR:   R   t   wantFunctionR   R   t   mapRO   RA   t   realpathR   t   traverseNamespaceRR   t   extendRH   RM   t   loadTestsFromModuleR&   R   (   R+   R~   RA   RG   Ri   t   test_classest
   test_funcsR2   Rj   t   module_pathst   module_path(    (   R~   R+   s/   /sys/lib/python2.7/site-packages/nose/loader.pyR“   6  s:    	c            sh  t  j d | | ƒ ˆ j } ˆ j j j | | ƒ } | rD | | ƒ St | d ˆ j ƒ} | rD| j rt | j } n  ˆ j	 | | ƒ \ } } t
 | ƒ rê t | d d ƒ | j k rê t | t ƒ rê t | | j ƒ } t | | j ƒ } n  t  j d | | | ƒ t | t ƒ r| | g ƒ S| t ˆ j | | ƒ g d | ƒƒ Sn | j rby€ | j d k rqt | j ƒ } n[ ˆ j j j | j | j ƒ z ˆ j j | j | j ƒ } Wd ˆ j j j | j | j ƒ XWnZ t t f k
 ré‚  nA t j ƒ  }	 | t |	 d |	 d |	 d	 d
 | j ƒ  ƒg ƒ SX| j rFˆ j | j | ƒ Sˆ j | | j d | ƒSn| j r>| j ‰  | j rÚt  ˆ  ƒ }
 |
 d k rÄ| t t! d | j ˆ  f d
 | j ƒ  ƒg ƒ Sˆ j | j d |
 ƒSt" ˆ  ƒ rüt# ‡  ‡ f d †  ƒ St$ ˆ  ƒ rˆ j% ˆ  ƒ S| t t& d ˆ  d
 | j ƒ  ƒg ƒ Sn& | t t! d | d
 | j ƒ  ƒg ƒ Sd S(   sã   Load tests from the entity with the given name.

        The name may indicate a file, directory, module, or any object
        within a module. See `nose.util.split_test_name` for details on
        test name parsing.
        s   load from %s (%s)R    t
   __module__s   parent %s obj %s module %sR|   Ni    i   i   Rm   RG   s>   Can't find callable %s in file %s: file is not a python moduleR~   c              s   ˆ j  ˆ  ƒ S(   N(   RH   (    (   RA   R+   (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyRJ   Â  s    s   No such file %ss   Unresolvable test name %s('   RK   RL   R&   R   RM   RZ   R   R    t   callt   resolveR   R,   R   Rƒ   t
   isinstanceR   R   R   R‰   R~   Rq   R   t   beforeImportR!   t   importFromPatht   afterImportR^   R_   R`   Ra   t   totupleR“   R   Rp   RV   R   RT   R\   t   OSError(   R+   t   nameR~   RG   t   suitet
   plug_testst   addrRˆ   t   objRr   t   package(    (   RA   R+   s/   /sys/lib/python2.7/site-packages/nose/loader.pyRZ   d  s€    	

				
			c         C   sw   |  j  j j | | ƒ } | ra | \ } } | ra |  j |  j | ƒ t j j |  | | ƒ g ƒ Sn  t j j |  | | ƒ S(   sS   Load tests from all names, returning a suite containing all
        tests.
        (   R   RM   t   loadTestsFromNamesR&   R)   R   (   R+   t   namesR~   t   plug_resR¢   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR§   Ð  s    c         C   s”   g  } |  j  j } x$ | j | ƒ D] } | j | ƒ q" W| sU t t |  ƒ j | ƒ S| j g  t t |  ƒ j | ƒ D] } | ^ qt ƒ |  j | ƒ S(   s6   Load tests from a unittest.TestCase subclass.
        (   R   RM   t   loadTestsFromTestCaseR:   t   superR   R’   R&   (   R+   R=   R>   RM   R@   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyRª   Þ  s    )c         C   s•   | |  j  d „ } g  t | t | ƒ ƒ D]! } |  j t | | ƒ | ƒ ^ q( } x* |  j j j | ƒ D] } | j | ƒ qe W|  j	 t
 | d | ƒƒ S(   s!  Load tests from a test class that is *not* a unittest.TestCase
        subclass.

        In this case, we can't depend on the class's `__init__` taking method
        name arguments, so we have to compose a MethodTestCase for each
        method in the class that looks testlike.
        c         S   sM   t  | |  d  ƒ } t | ƒ r0 t | | ƒ } n t | ƒ s@ t S| j | ƒ S(   N(   R,   R   R   R   R   R-   R.   (   R/   R0   R1   R2   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR3   ÷  s    R|   (   R$   R7   R8   R‰   R,   R   RM   t   loadTestsFromTestClassR:   R&   R   (   R+   R0   R3   R@   R>   Rj   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR¬   ï  s    :c         C   s™   y |  j  | | ƒ SWn~ t t f k
 r0 ‚  ne t j ƒ  } y t | ƒ } Wn  t k
 re ‚  n d  } n Xt | d | d | d d | ƒSXd  S(   Ni    i   i   Rm   (   t	   _makeTestR^   R_   R`   Ra   R   R   R   (   R+   R¥   Rˆ   Rr   R¤   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR‰     s    
c         C   sl  g  } y t  | ƒ } Wn  t k
 r, ‚  n d } n Xx- |  j j j | | ƒ D] } | j | ƒ qR Wy | r |  j | ƒ SWnK t t f k
 rœ ‚  n2 t	 j
 ƒ  } t | d | d | d d | ƒSXt | ƒ r| rt | t j ƒ rt | | ƒ } n  t | t j ƒ r| St | ƒ rƒ| rT| j | j k rTt | | j ƒ } n  t | t j ƒ rs|  j | ƒ S|  j | ƒ Snå t | ƒ rï| d k r§| j } n  t | t j ƒ rÆ| | j ƒ St | ƒ râ|  j | | ƒ St | ƒ Sny t | ƒ rQ| r(| j | j k r(t | | j ƒ } n  t | ƒ rD|  j  | | ƒ St! | ƒ Sn t t" d | d | ƒSd S(   sV   Given a test object and its parent, return a test case
        or test suite.
        i    i   i   Rm   s   Can't make a test from %sN(#   R   R^   R   R   RM   R‰   R:   R&   R_   R`   Ra   R   R   R›   t   typest
   ModuleTypeR   R)   t   TestCaseR   R˜   Rƒ   R   t
   issubclassRª   R¬   R   t	   __class__R   R†   R   R   R   R   R   (   R+   R¥   Rˆ   R£   R¤   Rj   Rr   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR­     sX    
#%
c         C   sk   | } | j  d ƒ } x' | D] } | t | | d ƒ } } q W| d k ra t t d | ƒ } n  | | f S(   s#   Resolve name within module
        RE   s   No such test %sN(   t   splitR,   R   R   Rp   (   R+   R¡   R~   R¥   t   partst   partRˆ   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyRš   N  s    c         C   s‚   t  | t ƒ s" | t ƒ  } } nV t | ƒ d k rK | d t ƒ  } } n- t | ƒ d k sc t ‚ | d | d } } | | f S(   sŽ   Given the yield value of a test generator, return a func and args.

        This is used in the two loadTestsFromGenerator* methods.

        i   i    (   R›   t   tuplet   lent   AssertionError(   R+   Rj   Ry   Rs   (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyRu   Z  s    N(   Rƒ   R˜   t   __doc__R   R   R!   R    R$   R&   R*   R6   RB   RD   RH   R\   R   R†   R-   R“   RZ   R§   Rª   R¬   R‰   R­   Rš   Ru   (    (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyR   ,   s0   	)				M			,.l		;	(=   R¹   t
   __future__R    t   loggingRO   R`   R)   R®   t   inspectR   t   nose.pyversionR   R   t	   nose.caseR   R   t   nose.failureR   t   nose.configR   t   nose.importerR   R	   R
   t   nose.selectorR   R   t	   nose.utilR   R   R   R   R   R   R   R   R   R   t
   nose.suiteR   R   R   R   R   t	   getLoggerRƒ   RK   RA   t   normpathR"   t   abspathR#   t   joinRS   t   isdirRV   t   isfileRT   t   __all__R   R   (    (    (    s/   /sys/lib/python2.7/site-packages/nose/loader.pyt   <module>   s6   Fÿ ÿ ?