<s> '''<STR_LIT>''' <EOL> __author__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" [ <NUM_LIT:11> : - <NUM_LIT:2> ] <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __url__ = "<STR_LIT>" <EOL> import sys <EOL> import os <EOL> import unittest <EOL> import time <EOL> import types <EOL> import logging <EOL> import cgi <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import datastore_file_stub <EOL> from google . appengine . ext . webapp . util import run_wsgi_app <EOL> import kay <EOL> kay . setup ( ) <EOL> import simplejson <EOL> from kay . conf import settings <EOL> from werkzeug . utils import import_string <EOL> _cached_suite = None <EOL> _WEB_TEST_DIR = '<STR_LIT>' <EOL> class MainTestPageHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> unknown_args = [ arg for arg in self . request . arguments ( ) <EOL> if arg not in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:name>" ) ] <EOL> if len ( unknown_args ) > <NUM_LIT:0> : <EOL> errors = [ ] <EOL> for arg in unknown_args : <EOL> errors . append ( _log_error ( "<STR_LIT>" % arg ) ) <EOL> self . error ( <NUM_LIT> ) <EOL> self . response . out . write ( "<STR_LIT:U+0020>" . join ( errors ) ) <EOL> return <EOL> format = self . request . get ( "<STR_LIT>" , "<STR_LIT:html>" ) <EOL> if format == "<STR_LIT:html>" : <EOL> self . _render_html ( ) <EOL> elif format == "<STR_LIT>" : <EOL> self . _render_plain ( ) <EOL> else : <EOL> error = _log_error ( "<STR_LIT>" % cgi . escape ( format ) ) <EOL> self . error ( <NUM_LIT> ) <EOL> self . response . out . write ( error ) <EOL> def _render_html ( self ) : <EOL> suite , error = _create_suite ( self . request ) <EOL> if not error : <EOL> self . response . out . write ( _MAIN_PAGE_CONTENT % ( _test_suite_to_json ( suite ) , _WEB_TEST_DIR , __version__ ) ) <EOL> else : <EOL> self . error ( <NUM_LIT> ) <EOL> self . response . out . write ( error ) <EOL> self . response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> def _render_plain ( self ) : <EOL> self . response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> runner = unittest . TextTestRunner ( self . response . out ) <EOL> suite , error = _create_suite ( self . request ) <EOL> if not error : <EOL> self . response . out . write ( "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" ) <EOL> _run_test_suite ( runner , suite ) <EOL> else : <EOL> self . error ( <NUM_LIT> ) <EOL> self . response . out . write ( error ) <EOL> class JsonTestResult ( unittest . TestResult ) : <EOL> def __init__ ( self ) : <EOL> unittest . TestResult . __init__ ( self ) <EOL> self . testNumber = <NUM_LIT:0> <EOL> def render_to ( self , stream ) : <EOL> result = { <EOL> '<STR_LIT>' : self . testsRun , <EOL> '<STR_LIT>' : self . testNumber , <EOL> '<STR_LIT>' : self . _list ( self . errors ) , <EOL> '<STR_LIT>' : self . _list ( self . failures ) , <EOL> } <EOL> stream . write ( simplejson . dumps ( result ) ) <EOL> def _list ( self , list ) : <EOL> dict = [ ] <EOL> for test , err in list : <EOL> d = { <EOL> '<STR_LIT>' : test . shortDescription ( ) or str ( test ) , <EOL> '<STR_LIT>' : err , <EOL> } <EOL> dict . append ( d ) <EOL> return dict <EOL> class JsonTestRunner : <EOL> def run ( self , test ) : <EOL> self . result = JsonTestResult ( ) <EOL> self . result . testNumber = test . countTestCases ( ) <EOL> startTime = time . time ( ) <EOL> test ( self . result ) <EOL> stopTime = time . time ( ) <EOL> timeTaken = stopTime - startTime <EOL> return self . result <EOL> class JsonTestRunHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> self . response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> test_name = self . request . get ( "<STR_LIT:name>" ) <EOL> _load_default_test_modules ( ) <EOL> suite = unittest . defaultTestLoader . loadTestsFromName ( test_name ) <EOL> runner = JsonTestRunner ( ) <EOL> _run_test_suite ( runner , suite ) <EOL> runner . result . render_to ( self . response . out ) <EOL> class JsonTestListHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> self . response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> suite , error = _create_suite ( self . request ) <EOL> if not error : <EOL> self . response . out . write ( _test_suite_to_json ( suite ) ) <EOL> else : <EOL> self . error ( <NUM_LIT> ) <EOL> self . response . out . write ( error ) <EOL> def _create_suite ( request ) : <EOL> global _cached_suite <EOL> package_name = request . get ( "<STR_LIT>" ) <EOL> test_name = request . get ( "<STR_LIT:name>" ) <EOL> loader = unittest . defaultTestLoader <EOL> suite = unittest . TestSuite ( ) <EOL> error = None <EOL> try : <EOL> if not package_name and not test_name : <EOL> if _cached_suite : <EOL> return ( _cached_suite , None ) <EOL> modules = _load_default_test_modules ( ) <EOL> for module in modules : <EOL> suite . addTest ( loader . loadTestsFromModule ( module ) ) <EOL> elif test_name : <EOL> _load_default_test_modules ( ) <EOL> suite . addTest ( loader . loadTestsFromName ( test_name ) ) <EOL> elif package_name : <EOL> package = reload ( __import__ ( package_name ) ) <EOL> module_names = package . __all__ <EOL> for module_name in module_names : <EOL> suite . addTest ( loader . loadTestsFromName ( <EOL> '<STR_LIT>' % ( package_name , module_name ) ) ) <EOL> if suite . countTestCases ( ) == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except Exception , e : <EOL> import sys <EOL> import traceback <EOL> error = '<STR_LIT:\n>' . join ( traceback . format_exception ( * ( sys . exc_info ( ) ) ) ) <EOL> error = str ( error ) <EOL> _log_error ( error ) <EOL> if error is None : <EOL> _cached_suite = suite <EOL> return ( suite , error ) <EOL> def _load_default_test_modules ( ) : <EOL> modules = [ ] <EOL> if settings . GAEUNIT_INCLUDES_KAY : <EOL> import kay . tests <EOL> modules . append ( reload ( kay . tests ) ) <EOL> for app_name in settings . INSTALLED_APPS : <EOL> if app_name . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> try : <EOL> modules . append ( reload ( import_string ( "<STR_LIT>" % app_name ) ) ) <EOL> except ( ImportError , AttributeError ) , e : <EOL> logging . warn ( "<STR_LIT>" % <EOL> ( app_name , e ) ) <EOL> pass <EOL> return modules <EOL> def _get_tests_from_suite ( suite , tests ) : <EOL> for test in suite : <EOL> if isinstance ( test , unittest . TestSuite ) : <EOL> _get_tests_from_suite ( test , tests ) <EOL> else : <EOL> tests . append ( test ) <EOL> def _test_suite_to_json ( suite ) : <EOL> tests = [ ] <EOL> _get_tests_from_suite ( suite , tests ) <EOL> test_tuples = [ ( type ( test ) . __module__ , type ( test ) . __name__ , test . _testMethodName ) for test in tests ] <EOL> test_dict = { } <EOL> for test_tuple in test_tuples : <EOL> module_name , class_name , method_name = test_tuple <EOL> if module_name not in test_dict : <EOL> mod_dict = { } <EOL> method_list = [ ] <EOL> method_list . append ( method_name ) <EOL> mod_dict [ class_name ] = method_list <EOL> test_dict [ module_name ] = mod_dict <EOL> else : <EOL> mod_dict = test_dict [ module_name ] <EOL> if class_name not in mod_dict : <EOL> method_list = [ ] <EOL> method_list . append ( method_name ) <EOL> mod_dict [ class_name ] = method_list <EOL> else : <EOL> method_list = mod_dict [ class_name ] <EOL> method_list . append ( method_name ) <EOL> return simplejson . dumps ( test_dict ) <EOL> def _run_test_suite ( runner , suite ) : <EOL> """<STR_LIT>""" <EOL> original_apiproxy = apiproxy_stub_map . apiproxy <EOL> try : <EOL> apiproxy_stub_map . apiproxy = apiproxy_stub_map . APIProxyStubMap ( ) <EOL> temp_stub = datastore_file_stub . DatastoreFileStub ( '<STR_LIT>' , None , None , trusted = True ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( '<STR_LIT>' , temp_stub ) <EOL> for name in [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( name , original_apiproxy . GetStub ( name ) ) <EOL> runner . run ( suite ) <EOL> finally : <EOL> apiproxy_stub_map . apiproxy = original_apiproxy <EOL> def _log_error ( s ) : <EOL> logging . warn ( s ) <EOL> return s <EOL> _MAIN_PAGE_CONTENT = """<STR_LIT>""" <EOL> application = webapp . WSGIApplication ( [ ( '<STR_LIT:%s>' % _WEB_TEST_DIR , MainTestPageHandler ) , <EOL> ( '<STR_LIT>' % _WEB_TEST_DIR , JsonTestRunHandler ) , <EOL> ( '<STR_LIT>' % _WEB_TEST_DIR , JsonTestListHandler ) ] , <EOL> debug = True ) <EOL> def main ( ) : <EOL> logging . getLogger ( ) . setLevel ( logging . DEBUG ) <EOL> from kay . utils import local <EOL> from werkzeug . local import release_local <EOL> release_local ( local ) <EOL> run_wsgi_app ( application ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
