| 1 |
"""nose2 test infrastructure.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
import doctest |
| 7 |
import importlib |
| 8 |
|
| 9 |
from nose2.events import Plugin |
| 10 |
|
| 11 |
|
| 12 |
DOT = '.' |
| 13 |
FLAGS = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.REPORT_NDIFF |
| 14 |
|
| 15 |
|
| 16 |
def as_object(path): |
| 17 |
if path is None: |
| 18 |
return None |
| 19 |
# mod.ule.object -> import(mod.ule); mod.ule.object |
| 20 |
module_name, dot, object_name = path.rpartition('.') |
| 21 |
if dot != '.' or len(object_name) == 0: |
| 22 |
return None |
| 23 |
module = importlib.import_module(module_name) |
| 24 |
return getattr(module, object_name, None) |
| 25 |
|
| 26 |
|
| 27 |
class NosePlugin(Plugin): |
| 28 |
configSection = 'flufl.testing' |
| 29 |
|
| 30 |
def __init__(self): |
| 31 |
super().__init__() |
| 32 |
self.patterns = [] |
| 33 |
self.stderr = False |
| 34 |
def set_stderr(ignore): # noqa: E306 |
| 35 |
self.stderr = True |
| 36 |
self.addArgument(self.patterns, 'P', 'pattern', |
| 37 |
'Add a test matching pattern') |
| 38 |
self.addFlag(set_stderr, 'E', 'stderr', |
| 39 |
'Enable stderr logging to sub-runners') |
| 40 |
# Get the topdir out of the plugin configuration file. |
| 41 |
self.package = self.config.as_str('package') |
| 42 |
if self.package is None: |
| 43 |
raise RuntimeError('flufl.nose2 plugin missing "package" setting') |
| 44 |
|
| 45 |
def startTestRun(self, event): |
| 46 |
callback = as_object(self.config.get('start_run')) |
| 47 |
if callback is not None: |
| 48 |
callback(self) |
| 49 |
|
| 50 |
def getTestCaseNames(self, event): |
| 51 |
if len(self.patterns) == 0: |
| 52 |
# No filter patterns, so everything should be tested. |
| 53 |
return |
| 54 |
# Does the pattern match the fully qualified class name? |
| 55 |
for pattern in self.patterns: |
| 56 |
full_class_name = '{}.{}'.format( |
| 57 |
event.testCase.__module__, event.testCase.__name__) |
| 58 |
if re.search(pattern, full_class_name): |
| 59 |
# Don't suppress this test class. |
| 60 |
return |
| 61 |
names = filter(event.isTestMethod, dir(event.testCase)) |
| 62 |
for name in names: |
| 63 |
full_test_name = '{}.{}.{}'.format( |
| 64 |
event.testCase.__module__, |
| 65 |
event.testCase.__name__, |
| 66 |
name) |
| 67 |
for pattern in self.patterns: |
| 68 |
if re.search(pattern, full_test_name): |
| 69 |
break |
| 70 |
else: |
| 71 |
event.excludedNames.append(name) |
| 72 |
|
| 73 |
def handleFile(self, event): |
| 74 |
package = importlib.import_module(self.package) |
| 75 |
path = event.path[len(os.path.dirname(package.__file__))+1:] |
| 76 |
if len(self.patterns) > 0: |
| 77 |
for pattern in self.patterns: |
| 78 |
if re.search(pattern, path): |
| 79 |
break |
| 80 |
else: |
| 81 |
# Skip this doctest. |
| 82 |
return |
| 83 |
base, ext = os.path.splitext(path) |
| 84 |
if ext != '.rst': |
| 85 |
return |
| 86 |
# Look to see if the package defines a test layer, otherwise use the |
| 87 |
# default layer. First turn the file system path into a dotted Python |
| 88 |
# module path. |
| 89 |
parent = os.path.dirname(path) |
| 90 |
dotted = '{}.{}'.format( |
| 91 |
self.package, DOT.join(parent.split(os.path.sep))) |
| 92 |
layer = None |
| 93 |
default_layer = as_object(self.config.get('default_layer')) |
| 94 |
try: |
| 95 |
module = importlib.import_module(dotted) |
| 96 |
except ImportError: |
| 97 |
layer = default_layer |
| 98 |
else: |
| 99 |
layer = getattr(module, 'layer', default_layer) |
| 100 |
setup = as_object(self.config.get('setup')) |
| 101 |
teardown = as_object(self.config.get('teardown')) |
| 102 |
test = doctest.DocFileTest( |
| 103 |
path, package=self.package, |
| 104 |
optionflags=FLAGS, |
| 105 |
setUp=setup, |
| 106 |
tearDown=teardown) |
| 107 |
test.layer = layer |
| 108 |
# Suppress the extra "Doctest: ..." line. |
| 109 |
test.shortDescription = lambda: None |
| 110 |
event.extraTests.append(test) |
| 111 |
|
| 112 |
def startTest(self, event): |
| 113 |
if self.config.as_bool('trace', False): |
| 114 |
print('vvvvv', event.test, file=sys.stderr) |
| 115 |
|
| 116 |
def stopTest(self, event): |
| 117 |
if self.config.as_bool('trace', False): |
| 118 |
print('^^^^^', event.test, file=sys.stderr) |