ó
VüęIc           @   s>  d  Z  d d d d d d d d d	 d
 d d d g Z y  d d l Z e j d g  Wn e k
 rf n Xd 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 Z d d l Z d d l Z d d l Z d   Z d e j f d     YZ d e j f d     YZ e d k r:e e j d  n  d S(   sl  Run a set of PyUnit-based regression tests.

This will find all modules whose name is "test_*.py" in the test
directory, and run them.  Various command line options provide
additional facilities.

Command line options:

--help        -- show usage info
-g;--generate -- write the output file for a test instead of comparing it.
                 The name of the  test to write the output for must be
                 specified.
<test_name>   -- supply the name of one (or more) tests to be run.
                 The .py file extension is optional.
doctest       -- run the docstring tests.
By default, all tests are run.
s   Bio.Seqs   Bio.SeqRecords	   Bio.SeqIOs   Bio.SeqIO.QualityIOs   Bio.SeqUtilss   Bio.Align.Generics   Bio.AlignIOs   Bio.AlignIO.StockholmIOs   Bio.KEGG.Compounds   Bio.KEGG.Enzymes   Bio.Wises   Bio.Wise.psws	   Bio.Motifi’’’’Ns   Bio.Statistics.lowessc         C   s  t  j d p d } t j j d |  } t  j j d |  t j j d | t j j   t  j d  f  } t j	 | t j
  r t  j j d |  n  y( t j |  d d d	 d
 g  \ } } Wn! t j k
 rį } | GHt GHd SXxČ | D]Ą \ } } | d k r
t GHd S| d k s"| d k ré t |  d k rBd GHt GHd St |  d k rbd GHt GHd S| d d d k r| d d  | d <n  t | d  }	 |	 j   d Sqé WxC t t |   D]/ }
 | |
 d d k rĄ| |
 d  | |
 <qĄqĄWt |  } | j   d  S(   Ni    t   .s   %s/..i   s   %s/../build/lib.%s-%si   t   gt   generatet   doctestt   helpi   s   --helps   -gs
   --generates5   Only one argument (the test name) needed for generates.   No test name specified to generate output for.iż’’’s   .py(   t   syst   patht   ost   abspatht   insertt	   distutilst   utilt   get_platformt   versiont   accesst   F_OKt   getoptt   errort   __doc__t   lent   ComparisonTestCaset   generate_outputt   ranget
   TestRunnert   run(   t   argvt	   test_patht   source_patht
   build_patht   optst   argst   msgt   ot   at   testt   arg_numt   runner(    (    s   run_tests.pyt   main:   sH    #(
R   c           B   s5   e  Z d  Z d d  Z d   Z d   Z d   Z RS(   sQ   Run a print-and-compare test and compare its output against expected output.
    c         C   s&   t  j j |   | |  _ | |  _ d S(   sõ   Initialize with the test to run.

        Arguments:
        o name - The name of the test. The expected output should be
          stored in the file output/name.
        o output - The output that was generated when this test was run.
        N(   t   unittestt   TestCaset   __init__t   namet   output(   t   selfR)   R*   (    (    s   run_tests.pyR(   w   s    	c         C   s   |  j  S(   N(   R)   (   R+   (    (    s   run_tests.pyt   shortDescription   s    c         C   s  t  j j t j d  } t  j j | |  j  } y t | d  } Wn+ t k
 rp |  j d | |  j f  n X|  j	 j
 d  | j   j   } | |  j k s» t d |  j | f   xÖ t r| j   } |  j	 j   } | sķ | r)| d k s	t d |   | d k s%t d |   Pn  | j d	  } | j d	  } t j d
  j |  rbq¾ | | k s¾ t d t |  t |  f   q¾ Wd  S(   NR*   t   rs"   Warning: Can't open %s for test %si    s   
Output:   %s
Expected: %st    s
   Unread: %ss   Extra output: %ss   
s   ^Ran [0-9]+ tests? in s   
Output  : %s
Expected: %s(   R   R   t   joinR   t   testdirR)   t   opent   IOErrort   failR*   t   seekt   readlinet   stript   AssertionErrort   Truet   ret   compilet   matcht   repr(   R+   t	   outputdirt
   outputfilet   expectedt   expected_testt   expected_linet   output_line(    (    s   run_tests.pyt   runTest   s0    	c         C   s   t  j j t j d  } t  j j | |  j  } t | d  } | j |  j d  t j	 } z | t _	 t
 |  j  Wd | j   | t _	 Xd S(   s;   Generate the golden output for the specified test.
        R*   t   ws   
N(   R   R   R/   R   R0   R)   R1   t   writeR   t   stdoutt
   __import__t   close(   R+   R=   R>   t   output_handlet   save_stdout(    (    s   run_tests.pyR   ³   s    		
N(   t   __name__t
   __module__R   t   NoneR(   R,   RC   R   (    (    (    s   run_tests.pyR   s   s
   		-R   c           B   sc   e  Z e  d  k r" e j d Z n e Z e j j e  p@ e j	 Z
 g  d  Z d   Z d   Z RS(   t   __main__i    c         C   s  | |  _  |  j  s t j t j  } xE | D]= } | d  d k r+ | d d k r+ |  j  j | d   q+ q+ W|  j  j   |  j  j d  n  d |  j  k rä |  j  j d  t j	 d  d k  rŃ t j
 j d  qä |  j  j t  n  t j   } t j j |  | d	 d
 d  S(   Ni   t   test_iż’’’s   .pyR   i   i   s,   Skipping doctests which require Python 2.4+
t	   verbosityi    (   i   i   (   t   testsR   t   listdirR   R0   t   appendt   sortt   removeR   t   version_infot   stderrRE   t   extendt   DOCTEST_MODULESt	   cStringIOt   StringIOR&   t   TextTestRunnerR(   (   R+   RQ   t   namesR)   t   stream(    (    s   run_tests.pyR(   Ņ   s    		 c   
      C   s%  d d l  m } |  j   } t j   } zģyt j } | t _ | j d  r± t j j	 d |  t
 j   j |  } | j   d k rõ t | |  } t
 j | g  } qõ nD t j j	 d |  t | d  d  | j d   } t j |  } ~ | j |  | j   r"t j j	 d  t St j j	 d	  | j   t SWnĻ | k
 rk}	 t j j	 d
 |	  t St k
 r}	 t j j	 d  | j j	 | j d  | j j	 d |  | j j	 | j d  y | j j	 t j    Wn% t  k
 r| j j	 d |	  n Xt SXWd  | t _ Xd  S(   Ni’’’’(   t   MissingExternalDependencyErrorRO   s   %s ... i    s   %s docstring test ... R    s   ok
s   FAIL
s   skipping. %s
s   ERROR
s   
s
   ERROR: %s
s   %s
(!   t   BioR_   t   _makeResultRZ   R[   R   RF   t
   startswithRW   RE   R&   t
   TestLoadert   loadTestsFromNamet   countTestCasesR   t	   TestSuiteRG   RM   t   splitR   t   DocTestSuiteR   t   wasSuccessfulR8   t   printErrorst   Falset	   ExceptionR^   t
   separator1t
   separator2t	   tracebackt
   format_exct   AttributeError(
   R+   R)   R_   t   resultR*   RF   t   suiteR"   t   moduleR   (    (    s   run_tests.pyRC   ź   sL    		
	c         C   sž   d } t  j    } x3 |  j D]( } |  j |  } | s | d 7} q q Wt |  j  } t  j    } | | } t j j |  j j    t j j d d d  t j j d | | d k ræ d pĀ d | f  t j j d  | rś t j j d	 |  n  d  S(
   Ni    i   t   -iF   s   
s   Ran %d test%s in %.3f seconds
t   sR.   s   FAILED (failures = %d)
(	   t   timeRQ   RC   R   R   RW   RE   R^   t   getvalue(   R+   t   failurest	   startTimeR"   t   okt   totalt   stopTimet	   timeTaken(    (    s   run_tests.pyR      s     
#(   RK   RL   R   R   t   filet   __file__R   R   t   dirnamet   curdirR0   R(   RC   R   (    (    (    s   run_tests.pyR   Ź   s   	6RN   i   (   R   RY   t   numpyRX   t   ImportErrorR   RZ   R   R9   R   Rw   Ro   R&   R   t   distutils.utilR
   R%   R'   R   R\   R   RK   R   (    (    (    s   run_tests.pyt   <module>   sB   		9Wi