/usr/lib/python3/dist-packages/nose2/session.py is in python3-nose2 0.7.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | import logging
import os
import argparse
from six.moves import configparser
from nose2 import config, events, util
log = logging.getLogger(__name__)
__unittest = True
class Session(object):
"""Configuration session.
Encapsulates all configuration for a given test run.
.. attribute :: argparse
An instance of :class:`argparse.ArgumentParser`. Plugins can
use this directly to add arguments and argument groups, but
*must* do so in their ``__init__`` methods.
.. attribute :: pluginargs
The argparse argument group in which plugins (by default) place
their command-line arguments. Plugins can use this directly to
add arguments, but *must* do so in their ``__init__`` methods.
.. attribute :: hooks
The :class:`nose2.events.PluginInterface` instance contains
all available plugin methods and hooks.
.. attribute :: plugins
The list of loaded -- but not necessarily *active* -- plugins.
.. attribute :: verbosity
Current verbosity level. Default: 1.
.. attribute :: startDir
Start directory of test run. Test discovery starts
here. Default: current working directory.
.. attribute :: topLevelDir
Top-level directory of test run. This directory is added to
sys.path. Default: starting directory.
.. attribute :: libDirs
Names of code directories, relative to starting
directory. Default: ['lib', 'src']. These directories are added
to sys.path and discovery if the exist.
.. attribute :: testFilePattern
Pattern used to discover test module files. Default: test*.py
.. attribute :: testMethodPrefix
Prefix used to discover test methods and functions: Default: 'test'.
.. attribute :: unittest
The config section for nose2 itself.
"""
configClass = config.Config
def __init__(self):
self.argparse = argparse.ArgumentParser(prog='nose2', add_help=False)
self.pluginargs = self.argparse.add_argument_group(
'plugin arguments',
'Command-line arguments added by plugins:')
self.config = configparser.ConfigParser()
self.hooks = events.PluginInterface()
self.plugins = []
self.verbosity = 1
self.startDir = None
self.topLevelDir = None
self.testResult = None
self.testLoader = None
self.logLevel = logging.WARN
self.configCache = dict()
def get(self, section):
"""Get a config section.
:param section: The section name to retreive.
:returns: instance of self.configClass.
"""
# If section exists in cache, return cached version
if section in self.configCache:
return self.configCache[section]
# If section doesn't exist in cache, parse config file
# (and cache result)
items = []
if self.config.has_section(section):
items = self.config.items(section)
self.configCache[section] = self.configClass(items)
return self.configCache[section]
def loadConfigFiles(self, *filenames):
"""Load config files.
:param filenames: Names of config files to load.
Loads all names files that exist into ``self.config``.
"""
self.config.read(filenames)
def loadPlugins(self, modules=None, exclude=None):
"""Load plugins.
:param modules: List of module names from which to load plugins.
"""
# plugins set directly
if modules is None:
modules = []
if exclude is None:
exclude = []
# plugins mentioned in config file(s)
cfg = self.unittest
more_plugins = cfg.as_list('plugins', [])
cfg_exclude = cfg.as_list('exclude-plugins', [])
exclude.extend(cfg_exclude)
exclude = set(exclude)
all_ = (set(modules) | set(more_plugins)) - exclude
log.debug("Loading plugin modules: %s", all_)
for module in sorted(all_):
self.loadPluginsFromModule(util.module_from_name(module))
self.hooks.pluginsLoaded(events.PluginsLoadedEvent(self.plugins))
def loadPluginsFromModule(self, module):
"""Load plugins from a module.
:param module: A python module containing zero or more plugin
classes.
"""
avail = []
for entry in dir(module):
try:
item = getattr(module, entry)
except AttributeError:
pass
try:
if issubclass(item, events.Plugin):
avail.append(item)
except TypeError:
pass
for cls in avail:
log.debug("Plugin is available: %s", cls)
plugin = cls(session=self)
self.plugins.append(plugin)
for method in self.hooks.preRegistrationMethods:
if hasattr(plugin, method):
self.hooks.register(method, plugin)
def registerPlugin(self, plugin):
"""Register a plugin.
:param plugin: A `nose2.events.Plugin` instance.
Register the plugin with all methods it implements.
"""
log.debug("Register active plugin %s", plugin)
if plugin not in self.plugins:
self.plugins.append(plugin)
for method in self.hooks.methods:
if hasattr(plugin, method):
log.debug("Register method %s for plugin %s", method, plugin)
self.hooks.register(method, plugin)
def setStartDir(self):
if self.startDir is None:
self.startDir = self.unittest.as_str('start-dir', '.')
def prepareSysPath(self):
"""Add code directories to sys.path"""
tld = self.topLevelDir
sd = self.startDir
if tld is None:
tld = sd
tld = os.path.abspath(tld)
util.ensure_importable(tld)
for libdir in self.libDirs:
libdir = os.path.abspath(os.path.join(tld, libdir))
if os.path.exists(libdir):
util.ensure_importable(libdir)
# convenience properties
@property
def libDirs(self):
return self.unittest.as_list('code-directories', ['lib', 'src'])
@property
def testFilePattern(self):
return self.unittest.as_str('test-file-pattern', 'test*.py')
@property
def testMethodPrefix(self):
return self.unittest.as_str('test-method-prefix', 'test')
@property
def unittest(self):
return self.get('unittest')
def isPluginLoaded(self, pluginName):
"""Returns ``True`` if a given plugin is loaded.
:param pluginName: the name of the plugin module: e.g. "nose2.plugins.layers".
"""
for plugin in self.plugins:
if pluginName == plugin.__class__.__module__:
return True
return False
|