Commits

Michael Foord  committed 41bfed7

Removing configs and userConfig parameters from main and allowing a single extra config instead

  • Participants
  • Parent commits 3f50b80
  • Branches plugins

Comments (0)

Files changed (4)

File description.txt

 useful in some situations. This can be fixed with the use of the optparse
 epilogue.
 
-`main` gains two new parameters: `configs` and `userConfig`. If configs is supplied it can be a string or sequence of strings. It will be used *instead* of loading a config file from the current project directory. If userConfig is False then the userConfig file won't be loaded. If userConfig is False and configs is not None (it can be an empty list) then the --no-plugins option is not available at the command line as the loading of plugins is being completely controlled from the `main(...)` call.
+`main` gains a new parameter `config`. If supplied this should be a config file location that will be loaded in *addition* to the standard plugins. This allows programmatic setup of some plugins.
 
 The verbosity argument to the main function and the TextTestRunner can be passed in as strings ('quiet', 'normal' or 'verbose', which correspond to 0, 1, and 2) instead of just integers.
 

File unittest2/config.py

     return parser, plugins, excludedPlugins
 
 
-def loadConfig(noUserConfig=False, configLocations=None):
+def loadConfig(noUserConfig=False, configLocations=None, extraConfig=None):
     global _config
     
     configs = []
         userParser, userPlugins, userExcludedPlugins = loadPluginsConfigFile(cfgPath)
         configs.append((userPlugins, userParser, userExcludedPlugins))
     
-    
     if configLocations is None:
         cfgPath = os.path.join(os.getcwd(), CFG_NAME)
         localParser, localPlugins, localExcludedPlugins = loadPluginsConfigFile(cfgPath)
         configs.append((localPlugins, localParser, localExcludedPlugins))
-    else:
-        for entry in configLocations:
-            path = entry
+        configLocations = []
+    
+    if extraConfig is not None:
+        configLocations.append(extraConfig)
+        
+    for entry in configLocations:
+        path = entry
+        if not os.path.isfile(path):
+            path = os.path.join(path, CFG_NAME)
             if not os.path.isfile(path):
-                path = os.path.join(path, CFG_NAME)
-                if not os.path.isfile(path):
-                    # exception type?
-                    raise Exception('Config file location %r could not be found'
-                                    % entry)
-            
-            parser, plugins, excludedPlugins = loadPluginsConfigFile(path)
-            configs.append((plugins, parser, excludedPlugins))
+                # exception type?
+                raise Exception('Config file location %r could not be found'
+                                % entry)
+        
+        parser, plugins, excludedPlugins = loadPluginsConfigFile(path)
+        configs.append((plugins, parser, excludedPlugins))
                     
 
     plugins = set(sum([plugin for plugin, _, __ in configs], []))

File unittest2/events.py

 
 
 def loadPlugins(pluginsDisabled=False, noUserConfig=False, 
-                configLocations=None):
-    allPlugins = loadConfig(noUserConfig, configLocations)
+                configLocations=None, extraConfig=None):
+    allPlugins = loadConfig(noUserConfig, configLocations, extraConfig)
     
     if not pluginsDisabled:
         for plugin in set(allPlugins):

File unittest2/main.py

     
     # defaults for testing
     failfast = catchbreak = buffer = progName = module = defaultTest = None
-    pluginsLoaded = verbosity = configs = None
-    userConfig = True
+    pluginsLoaded = verbosity = extraConfig = None
 
     def __init__(self, module='__main__', defaultTest=None,
                  argv=None, testRunner=None,
                  testLoader=loader.defaultTestLoader, exit=True,
                  verbosity=None, failfast=None, catchbreak=None, buffer=None,
-                 configs=None, userConfig=True):
+                 config=None):
         if isinstance(module, basestring):
             __import__(module)
             self.module = sys.modules[module]
         else:
             self.module = module
 
-        if isinstance(configs, basestring):
-            configs = (configs,)
-        self.configs = configs
-        self.userConfig = userConfig
-
+        self.extraConfig = config
         if isinstance(verbosity, basestring):
             # allow string verbosities not in the dictionary
             # for backwards compatibility
         if isinstance(testLoader, type):
             testLoader = testLoader()
         self.testLoader = testLoader
-
-        # allow argv[0] to be None
-        self.progName = os.path.basename(argv[0] or '')
+        self.progName = os.path.basename(argv[0])
 
         self.parseArgs(argv)
         self.runTests()
         # an initial pass over command line options to load config files
         # and plugins
         parser = _ImperviousOptionParser()
-        if self.configs is None:
-            parser.add_option('--config', dest='configLocations', action='append')
-        if self.userConfig:
-            parser.add_option('--no-user-config', dest='noUserConfig', default=False,
-                              action='store_true')
-        if self.configs is None or self.userConfig:
-            parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
-                              action='store_true')
+        parser.add_option('--config', dest='configLocations', action='append')
+        parser.add_option('--no-user-config', dest='noUserConfig', default=False,
+                          action='store_true')
+        parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
+                          action='store_true')
 
         if TestProgram.pluginsLoaded:
             # only needed because we call several times during tests
 
         # we catch any optparse errors here as they will be
         # reraised on the second pass through
-        noUserConfig = not self.userConfig
-        configLocations = self.configs
-        pluginsDisabled = False
-        if self.configs is None or self.userConfig:
-            try:
-                options, _ = parser.parse_args(argv)
-            except optparse.OptionError:
-                pluginsDisabled = False
-                configLocations = self.configs or []
-            else:
-                pluginsDisabled = options.pluginsDisabled
-                if not noUserConfig:
-                    noUserConfig = options.noUserConfig
-                if configLocations is None:
-                    configLocations = options.configLocations or None
+        try:
+            options, _ = parser.parse_args(argv)
+        except optparse.OptionError:
+            pluginsDisabled = False
+            noUserConfig = False
+            configLocations = []
+        else:
+            pluginsDisabled = options.pluginsDisabled
+            noUserConfig = options.noUserConfig
+            configLocations = options.configLocations
 
-        loadPlugins(pluginsDisabled, noUserConfig, configLocations)
+        loadPlugins(pluginsDisabled, noUserConfig, configLocations,
+                    self.extraConfig)
         TestProgram.pluginsLoaded = True
         return pluginsDisabled
 
         parser.add_option('-q', '--quiet', dest='quiet', default=False,
                           help='Quiet output', action='store_true')
         
-        if self.configs is None:
-            parser.add_option('--config', dest='configLocations', action='append',
-                              help='Specify local config file location')
-        if self.userConfig:
-            parser.add_option('--no-user-config', dest='noUserConfig', default=False,
-                              action='store_true',
-                              help="Don't use user config file")
-        if self.configs is None or self.userConfig:
-            parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
-                              action='store_true', help="Disable all plugins")
+        parser.add_option('--config', dest='configLocations', action='append',
+                          help='Specify local config file location')
+        parser.add_option('--no-user-config', dest='noUserConfig', default=False,
+                          action='store_true',
+                          help="Don't use user config file")
+        parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
+                          action='store_true', help="Disable all plugins")
         
         if self.failfast != False:
             parser.add_option('-f', '--failfast', dest='failfast', default=None,