Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 483

Show
Ignore:
Timestamp:
07/18/05 18:01:35
Author:
fumanchu
Message:

1. Pulled dict_from_config_file out of config._load to make it reusable.
2. Made test.test.main() reusable by CP apps.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/config.py

    r474 r483  
    174174            fp.close() 
    175175 
    176 def _load(configFile, override=True): 
    177     """ Convert an INI file to a dictionary. If override is false, 
    178         values already in the configMap will not be changed. 
    179     """ 
     176def dict_from_config_file(configFile): 
     177    """ Convert an INI file to a dictionary. """ 
    180178     
    181179    # Parse config file 
     
    186184        configParser.read(configFile) 
    187185     
    188     # Load INI file into cherrypy.configMap 
     186    # Load INI file into a dict 
     187    result = {} 
    189188    for section in configParser.sections(): 
    190         if section not in configMap
    191             configMap[section] = {} 
     189        if section not in result
     190            result[section] = {} 
    192191        for option in configParser.options(section): 
    193192            value = configParser.get(section, option) 
     
    198197                       (repr(section), repr(option), repr(value))) 
    199198                raise _cperror.WrongConfigValue, msg 
     199            result[section][option] = value 
     200    return result 
     201 
     202 
     203def _load(configFile, override=True): 
     204    """ Merge an INI file into configMap. If override is false, 
     205        values already in the configMap will not be changed. 
     206    """ 
     207     
     208    conf = dict_from_config_file(configFile) 
     209     
     210    # Load new conf into cherrypy.configMap 
     211    for section, options in conf.iteritems(): 
     212        bucket = configMap.setdefault(section, {}) 
     213        for key, value in options.iteritems(): 
    200214            if override: 
    201                 configMap[section][option] = value 
     215                bucket[key] = value 
    202216            else: 
    203                 configMap[section].setdefault(option, value) 
     217                bucket.setdefault(key, value) 
     218 
    204219 
    205220def outputConfigMap(): 
  • trunk/cherrypy/lib/covercp.py

    r425 r483  
    2929"""Code-coverage tools for CherryPy. 
    3030 
     31To use this module, or the coverage tools in the test suite, 
     32you need to download 'coverage.py', either Gareth Rees' original 
     33implementation: 
     34http://www.garethrees.org/2001/12/04/python-coverage/ 
     35 
     36or Ned Batchelder's enhanced version: 
     37http://www.nedbatchelder.com/code/modules/coverage.html 
     38 
    3139Set "cherrypy.codecoverage = True" to turn on coverage tracing. 
    3240Then, use the serve() function to browse the results in a web browser. 
     
    5159     
    5260except ImportError: 
    53     # This will raise errors that need to be trapped downstream 
     61    # Setting coverage to None will raise errors 
     62    # that need to be trapped downstream. 
    5463    coverage = None 
     64     
    5565    import warnings 
    5666    warnings.warn("No code coverage will be performed; coverage.py could not be imported.") 
  • trunk/cherrypy/test/helper.py

    r482 r483  
    194194    if conf is None: 
    195195        conf = {} 
    196 ##        conf = {'global': {'server.socketHost': HOST, 
    197 ##                                  'server.socketPort': PORT, 
    198 ##                                  'server.protocolVersion': "HTTP/1.1", 
    199 ##                                  'server.threadPool': 10, 
    200 ##                                  'server.logToScreen': False, 
    201 ##                                  'server.environment': "production", 
    202 ##                                  } 
    203 ##                       } 
    204 ##    if server is None: 
    205 ##        server = "cherrypy._cphttpserver.embedded_server" 
    206196    if isinstance(conf, basestring): 
    207197        # assume it's a filename 
  • trunk/cherrypy/test/test.py

    r480 r483  
    3737 
    3838 
    39 testDict = { 
    40     'baseurlFilter'          : 'test_baseurl_filter', 
    41     'cacheFilter'            : 'test_cache_filter', 
    42     'combinedFilters'        : 'test_combinedfilters', 
    43     'core'                   : 'test_core', 
    44     'decodingEncodingFilter' : 'test_decodingencoding_filter', 
    45     'gzipFilter'             : 'test_gzip_filter', 
    46     'logDebugInfoFilter'     : 'test_logdebuginfo_filter', 
    47     'objectMapping'          : 'test_objectmapping', 
    48     'staticFilter'           : 'test_static_filter', 
    49     'tutorials'              : 'test_tutorials', 
    50     'virtualHostFilter'      : 'test_virtualhost_filter', 
    51 ##    'sessionFilter'          : 'test_session_filter' 
    52 
    53  
    54  
    55 def help(): 
     39def help(testList): 
    5640    print """CherryPy Test Program 
    5741    Usage:  
    58         test.py -mode testName1 testName2 testName... 
    59      
    60     modes: wsgi, severless, native, all 
    61       default: wsgi 
     42        test.py -mode -cover -profile -1.1 testName1 testName2 testName... 
     43     
     44    modes: wsgi, severless, native, all (default is wsgi) 
     45     
     46    cover: turns on code-coverage tool 
     47     
     48    profile: turns on profiling tool 
     49     
     50    1.1: use HTTP/1.1 servers instead of default HTTP/1.0 
    6251    """ 
    6352     
    6453    print '    tests:' 
    65     for testString in testDict: 
    66         print '        ', testString 
     54    for name in testList: 
     55        print '        ', name 
    6756 
    6857 
     
    7665class Options: 
    7766     
    78     def __init__(self, args): 
     67    def __init__(self, args, testList): 
    7968        argSet = set([arg.lower() for arg in args]) 
    8069         
     
    10998        # All remaining args should be test names. 
    11099        tests = [] 
    111         for testString, test in testDict.iteritems()
    112             if testString.lower() in argSet: 
    113                 tests.append(testDict[testString]
    114                 argSet.discard(testString.lower()
     100        for name in testList
     101            if ("-" + name) in argSet: 
     102                tests.append(name
     103                argSet.discard("-" + name
    115104        if not tests: 
    116             tests = testDict.values() 
     105            tests = testList 
    117106        self.tests = tests 
    118107         
     
    122111                    raise BadArgument('Bad Argument: %s is not a valid option.' % arg) 
    123112 
    124  
    125 def main(opts): 
    126     # Place our current directory's parent (cherrypy/) at the beginning 
    127     # of sys.path, so that all imports are from our current directory. 
    128     localDir = os.path.dirname(__file__) 
    129     curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) 
    130     sys.path.insert(0, os.path.normpath(os.path.join(curpath, '../../'))) 
    131      
     113def get_coverage(): 
     114    """get_coverage() -> a coverage instance. 
     115     
     116    To use this feature, or the coverage server in cherrypy/lib/covercp, 
     117    you need to download 'coverage.py', either Gareth Rees' original 
     118    implementation: 
     119    http://www.garethrees.org/2001/12/04/python-coverage/ 
     120 
     121    or Ned Batchelder's enhanced version: 
     122    http://www.nedbatchelder.com/code/modules/coverage.html 
     123     
     124    If neither module is found in PYTHONPATH, this module returns None. 
     125    """ 
     126    try: 
     127        from coverage import the_coverage as coverage 
     128        c = os.path.join(os.path.dirname(__file__), "../lib/coverage.cache") 
     129        coverage.cache_default = c 
     130        if c and os.path.exists(c): 
     131            os.remove(c) 
     132        coverage.start() 
     133    except ImportError: 
     134        coverage = None 
     135    return coverage 
     136 
     137 
     138def main(opts, conf=None, includeNotReady=False): 
    132139    if opts.cover: 
    133140        # Start the coverage tool before importing cherrypy, 
    134141        # so module-level global statements are covered. 
    135         try: 
    136             from coverage import the_coverage as coverage 
    137             coverage.cache_default = c = os.path.join(os.path.dirname(__file__), 
    138                                                       "../lib/coverage.cache") 
    139             if c and os.path.exists(c): 
    140                 os.remove(c) 
    141             coverage.start() 
    142         except ImportError: 
    143             coverage = None 
     142        coverage = get_coverage() 
    144143     
    145144    import cherrypy 
     
    150149    from cherrypy.test import helper 
    151150     
    152     class NotReadyTest(helper.CPWebCase): 
    153         def testNotReadyError(self): 
    154             # Without having called "cherrypy.server.start()", we should 
    155             # get a NotReady error 
    156             class Root: pass 
    157             cherrypy.root = Root() 
    158             self.assertRaises(cherrypy.NotReady, self.getPage, "/") 
    159     helper.CPTestRunner.run(NotReadyTest("testNotReadyError")) 
    160      
    161     server_conf = {'global': {'server.socketHost': helper.HOST, 
    162                               'server.socketPort': helper.PORT, 
    163                               'server.protocolVersion': opts.protocol, 
    164                               'server.threadPool': 10, 
    165                               'server.logToScreen': False, 
    166                               'server.environment': "production", 
    167                               } 
    168                    } 
     151    if includeNotReady: 
     152        class NotReadyTest(helper.CPWebCase): 
     153            def testNotReadyError(self): 
     154                # Without having called "cherrypy.server.start()", we should 
     155                # get a NotReady error 
     156                class Root: pass 
     157                cherrypy.root = Root() 
     158                self.assertRaises(cherrypy.NotReady, self.getPage, "/") 
     159        helper.CPTestRunner.run(NotReadyTest("testNotReadyError")) 
     160     
     161    if conf is None: 
     162        conf = {'global': {'server.socketHost': helper.HOST, 
     163                           'server.socketPort': helper.PORT, 
     164                           'server.threadPool': 10, 
     165                           'server.logToScreen': False, 
     166                           'server.environment': "production", 
     167                           } 
     168                } 
     169    elif isinstance(conf, basestring): 
     170        conf = cherrypy.config.dict_from_config_file(conf) 
     171     
     172    conf['server.protocolVersion'] = opts.protocol 
    169173     
    170174    if opts.cover: 
     
    172176     
    173177    if opts.profile: 
    174         server_conf['profiling.on'] = True 
     178        conf['profiling.on'] = True 
    175179     
    176180    if 'serverless' in opts.servers: 
    177181        print 
    178182        print "Running tests: Serverless" 
    179         helper.run_test_suite(opts.tests, None, server_conf) 
     183        helper.run_test_suite(opts.tests, None, conf) 
    180184     
    181185    if 'native' in opts.servers: 
     
    184188        helper.run_test_suite(opts.tests, 
    185189                              "cherrypy._cphttpserver.embedded_server", 
    186                               server_conf) 
     190                              conf) 
    187191     
    188192    if 'wsgi' in opts.servers: 
     
    191195        helper.run_test_suite(opts.tests, 
    192196                              "cherrypy._cpwsgi.WSGIServer", 
    193                               server_conf) 
     197                              conf) 
    194198     
    195199    if opts.profile or opts.cover: 
     
    197201     
    198202    if opts.profile: 
    199         del server_conf['profiling.on'] 
     203        del conf['profiling.on'] 
    200204        print "run /cherrypy/lib/profiler.py as a script to serve profiling results on port 8080" 
    201205     
     
    212216 
    213217if __name__ == '__main__': 
     218     
     219    testList = [ 
     220        'test_baseurl_filter', 
     221        'test_cache_filter', 
     222        'test_combinedfilters', 
     223        'test_core', 
     224        'test_decodingencoding_filter', 
     225        'test_gzip_filter', 
     226        'test_logdebuginfo_filter', 
     227        'test_objectmapping', 
     228        'test_static_filter', 
     229        'test_tutorials', 
     230        'test_virtualhost_filter', 
     231##        'test_session_filter', 
     232    ] 
     233     
    214234    try: 
    215         opts = Options(sys.argv[1:]
     235        opts = Options(sys.argv[1:], testList
    216236    except DisplayHelp: 
    217         help(
     237        help(testList
    218238    except BadArgument, argError: 
    219239        print argError 
    220240    else: 
    221         main(opts) 
     241        # Place our current directory's parent (cherrypy/) at the beginning 
     242        # of sys.path, so that all imports are from our current directory. 
     243        localDir = os.path.dirname(__file__) 
     244        curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) 
     245        sys.path.insert(0, os.path.normpath(os.path.join(curpath, '../../'))) 
     246         
     247        main(opts, includeNotReady=True) 

Hosted by WebFaction

Log in as guest/cpguest to create tickets