commit 07addc1b4e1c60ede34b166a5d1208f183f47530 Author: Arturo Filastò arturo@filasto.net Date: Wed Oct 24 15:32:31 2012 +0000
Refactor runner and ooni * Remove unused imports * Fix import indentation (please do not align from modules) --- ooni/__init__.py | 15 ++++++- ooni/runner.py | 121 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 84 insertions(+), 52 deletions(-)
diff --git a/ooni/__init__.py b/ooni/__init__.py index a03c856..e730eb5 100644 --- a/ooni/__init__.py +++ b/ooni/__init__.py @@ -1 +1,14 @@ -__all__ = ['plugoo', 'utils', 'config', 'logo', 'lib', 'log'] +from . import config +from . import input +from . import inputunit +from . import kit +from . import lib +from . import nettest +from . import oonicli +from . import reporter +from . import runner +from . import template +from . import utils +__all__ = ['config', 'input', 'inputunit', 'kit', + 'lib', 'nettest', 'oonicli', 'reporter', + 'runner', 'templates', 'utils'] diff --git a/ooni/runner.py b/ooni/runner.py index f352dfb..dd5f8a9 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -10,40 +10,21 @@ # :version: 0.1.0-pre-alpha #
- -import os -import sys -import types -import time import inspect -import yaml - -from pprint import pprint
-from twisted.internet import defer, reactor -from twisted.python import reflect, failure, usage -from twisted.python import log as tlog +from twisted.python import reflect, usage
-from twisted.trial import unittest -from twisted.trial.runner import TrialRunner, TestLoader -from twisted.trial.runner import isPackage, isTestCase, ErrorHolder -from twisted.trial.runner import filenameToModule, _importFromFile +from twisted.trial.runner import isTestCase +from twisted.trial.runner import filenameToModule
-from ooni import nettest -from ooni.inputunit import InputUnitFactory -from ooni.nettest import InputTestSuite -from ooni.plugoo import tests as oonitests -from ooni.reporter import ReporterFactory -from ooni.utils import log, geodata, date +from ooni.inputunit import InputUnitFactory +from ooni.nettest import InputTestSuite +from ooni.plugoo import tests as oonitests +from ooni.reporter import ReporterFactory +from ooni.utils import log, date from ooni.utils.legacy import LegacyOONITest from ooni.utils.legacy import start_legacy_test, adapt_legacy_test
-def isTestCase(thing): - try: - return issubclass(thing, unittest.TestCase) - except TypeError: - return False - def isLegacyTest(obj): """ Returns True if the test in question is written using the OONITest legacy @@ -55,6 +36,45 @@ def isLegacyTest(obj): except TypeError: return False
+def processTest(obj, config): + """ + Process the parameters and :class:`twisted.python.usage.Options` of a + :class:`ooni.nettest.Nettest`. + + :param obj: + An uninstantiated old test, which should be a subclass of + :class:`ooni.plugoo.tests.OONITest`. + :param config: + A configured and instantiated :class:`twisted.python.usage.Options` + class. + """ + + input_file = obj.inputFile + + if obj.optParameters or input_file: + if not obj.optParameters: + obj.optParameters = [] + + if input_file: + obj.optParameters.append(input_file) + + class Options(usage.Options): + optParameters = obj.optParameters + + options = Options() + options.parseOptions(config['subArgs']) + obj.localOptions = options + + if input_file: + obj.inputFile = options[input_file[0]] + try: + tmp_obj = obj() + tmp_obj.getOptions() + except usage.UsageError: + options.opt_help() + + return obj + def findTestClassesFromConfig(config): """ Takes as input the command line config parameters and returns the test @@ -82,14 +102,14 @@ def findTestClassesFromConfig(config): classes.append(adapt_legacy_test(val, config)) return classes
-def makeTestCases(klass, tests, methodPrefix): +def makeTestCases(klass, tests, method_prefix): """ - Takes a class some tests and returns the test cases. methodPrefix is how + Takes a class some tests and returns the test cases. method_prefix is how the test case functions should be prefixed with. """ cases = [] for test in tests: - cases.append(klass(methodPrefix+test)) + cases.append(klass(method_prefix+test)) return cases
def processTestOptions(cls, config): @@ -144,10 +164,10 @@ def loadTestsAndOptions(classes, config): Takes a list of test classes and returns their testcases and options. Legacy tests will be adapted. """ - methodPrefix = 'test' - suiteFactory = InputTestSuite + + method_prefix = 'test' options = [] - testCases = [] + test_cases = [] names = []
_old_class_type = LegacyOONITest @@ -155,8 +175,12 @@ def loadTestsAndOptions(classes, config): for cls in classes: if isinstance(cls, _old_class_type): try: - cases = start_legacy_test(cls) - testCases.append(cases) + cases = start_legacy_test(klass) + if cases: + log.debug("Processing cases") + log.debug(str(cases)) + return [], [] + test_cases.append(cases) except Exception, e: log.err(e) else: @@ -170,10 +194,10 @@ def loadTestsAndOptions(classes, config): print cases return [], [] else: - tests = reflect.prefixedMethodNames(cls, methodPrefix) + tests = reflect.prefixedMethodNames(klass, method_prefix) if tests: - cases = makeTestCases(cls, tests, methodPrefix) - testCases.append(cases) + cases = makeTestCases(klass, tests, method_prefix) + test_cases.append(cases) try: #c = cls() #cls, opts = processTestOptions(cls, config) @@ -191,8 +215,7 @@ def loadTestsAndOptions(classes, config): opts.update(inputs) options.append(opts)
- log.debug("runner.loadTestsAndOptions: OPTIONS: %s" % options) - return testCases, options + return test_cases, options
class ORunner(object): """ @@ -201,7 +224,7 @@ class ORunner(object): them in input units. I also create all the report instances required to run the tests. """ - def __init__(self, cases, options=None, config=None, *arg, **kw): + def __init__(self, cases, options=None, config=None): self.baseSuite = InputTestSuite self.cases = cases self.options = options @@ -232,21 +255,17 @@ class ORunner(object): reportFile, testSuite=self.baseSuite(self.cases) )
- def runWithInputUnit(self, inputUnit): + def runWithInputUnit(self, input_unit): idx = 0 result = self.reporterFactory.create() - log.debug("Running test with input unit %s" % inputUnit) - for inputs in inputUnit: + log.debug("Running test with input unit %s" % input_unit) + for inputs in input_unit: result.reporterFactory = self.reporterFactory
log.debug("Running with %s" % inputs) suite = self.baseSuite(self.cases) suite.input = inputs - try: - suite(result, idx) - except Exception, e: - log.err("Error in running test!") - log.err(e) + suite(result, idx)
# XXX refactor all of this index bullshit to avoid having to pass # this index around. Probably what I want to do is go and make @@ -262,5 +281,5 @@ class ORunner(object):
def run(self): self.reporterFactory.options = self.options - for inputUnit in InputUnitFactory(self.inputs): - self.runWithInputUnit(inputUnit) + for input_unit in InputUnitFactory(self.inputs): + self.runWithInputUnit(input_unit)
tor-commits@lists.torproject.org