[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Refactor runner and ooni



commit a67e70e7a41118ea0425369a3ec6aff08900040a
Author: Arturo Filastò <arturo@xxxxxxxxxxx>
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   |   94 ++++++++++++++++++++---------------------------------
 2 files changed, 50 insertions(+), 59 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 40960fe..de90a80 100644
--- a/ooni/runner.py
+++ b/ooni/runner.py
@@ -10,38 +10,21 @@
 # :version: 0.1.0-pre-alpha
 #
 
-
-import os
-import sys
-import types
-import time
 import inspect
-import yaml
-
-from twisted.internet import defer, reactor
-from twisted.python   import reflect, failure, usage
-from twisted.python   import log as tlog
-
-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 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 twisted.python import reflect, usage
+
+from twisted.trial.runner import isTestCase
+from twisted.trial.runner import filenameToModule
+
+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
@@ -66,14 +49,14 @@ def processTest(obj, config):
         class.
     """
 
-    inputFile = obj.inputFile
+    input_file = obj.inputFile
 
-    if obj.optParameters or inputFile:
+    if obj.optParameters or input_file:
         if not obj.optParameters:
             obj.optParameters = []
 
-        if inputFile:
-            obj.optParameters.append(inputFile)
+        if input_file:
+            obj.optParameters.append(input_file)
 
         class Options(usage.Options):
             optParameters = obj.optParameters
@@ -82,8 +65,8 @@ def processTest(obj, config):
         options.parseOptions(config['subArgs'])
         obj.localOptions = options
 
-        if inputFile:
-            obj.inputFile = options[inputFile[0]]
+        if input_file:
+            obj.inputFile = options[input_file[0]]
         try:
             tmp_obj = obj()
             tmp_obj.getOptions()
@@ -120,15 +103,15 @@ 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 loadTestsAndOptions(classes, config):
@@ -137,10 +120,9 @@ def loadTestsAndOptions(classes, config):
     Legacy tests will be adapted.
     """
 
-    methodPrefix = 'test'
-    suiteFactory = InputTestSuite
+    method_prefix = 'test'
     options = []
-    testCases = []
+    test_cases = []
     names = []
 
     _old_klass_type = LegacyOONITest
@@ -149,11 +131,11 @@ def loadTestsAndOptions(classes, config):
         if isinstance(klass, _old_klass_type):
             try:
                 cases = start_legacy_test(klass)
-                #cases.callback()
                 if cases:
-                    print cases
+                    log.debug("Processing cases")
+                    log.debug(str(cases))
                     return [], []
-                testCases.append(cases)
+                test_cases.append(cases)
             except Exception, e:
                 log.err(e)
             else:
@@ -164,10 +146,10 @@ def loadTestsAndOptions(classes, config):
                     options.append([])
                     log.err(ae)
         else:
-            tests = reflect.prefixedMethodNames(klass, methodPrefix)
+            tests = reflect.prefixedMethodNames(klass, method_prefix)
             if tests:
-                cases = makeTestCases(klass, tests, methodPrefix)
-                testCases.append(cases)
+                cases = makeTestCases(klass, tests, method_prefix)
+                test_cases.append(cases)
             try:
                 k = klass()
                 opts = k.getOptions()
@@ -176,7 +158,7 @@ def loadTestsAndOptions(classes, config):
                 options.append([])
                 log.err(ae)
 
-    return testCases, options
+    return test_cases, options
 
 class ORunner(object):
     """
@@ -185,7 +167,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
@@ -216,21 +198,17 @@ class ORunner(object):
         self.reporterFactory = ReporterFactory(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
@@ -246,5 +224,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 mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits