[test] move test/utils.py to testutils.py (closes #3241611)
authorDavid Douard <david.douard@logilab.fr>
Mon, 28 Oct 2013 11:22:08 +0100
changeset 1456 c7e5156479d1
parent 1455 4adba1b659f9
child 1457 d0021a81d8fc
[test] move test/utils.py to testutils.py (closes #3241611) so it can be used from other cubes; also make the executed script an attribute of the ApycotBaseTC class (to easy reuse by other cubes).
test/test_functional.py
test/unittest_apycot.py
test/unittest_checkers.py
test/unittest_checkers_pyunit.py
test/unittest_entities.py
test/unittest_hooks.py
test/unittest_parser.py
test/unittest_repositories.py
test/unittest_task.py
test/unittest_writer.py
test/utils.py
testutils.py
--- a/test/test_functional.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/test_functional.py	Mon Oct 28 11:22:08 2013 +0100
@@ -2,7 +2,7 @@
 import subprocess
 import shutil
 
-import utils
+import cubes.apycot.testutils as utils
 
 from cubes.apycot.recipes import full_script
 
--- a/test/unittest_apycot.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_apycot.py	Mon Oct 28 11:22:08 2013 +0100
@@ -1,6 +1,6 @@
 from __future__ import with_statement
 
-from utils import ApycotBaseTC
+from cubes.apycot.testutils import ApycotBaseTC
 
 from apycotlib.writer import TestDataWriter
 from narvalbot import HTTPConnectionHandler
--- a/test/unittest_checkers.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_checkers.py	Mon Oct 28 11:22:08 2013 +0100
@@ -11,10 +11,8 @@
 
 from logilab.common.testlib import unittest_main, TestCase, TestSuite
 
-from utils import MockTest, MockRepository, MockCheckWriter, input_path
+from cubes.apycot.testutils import MockTest, MockRepository, MockCheckWriter, input_path
 
-zope_path = os.environ.get('SOFTWARE_HOME', '/usr/lib/zope/lib/python')
-sys.path.insert(1, zope_path)
 
 from apycotlib import SUCCESS, FAILURE, PARTIAL, NODATA, ERROR
 from checkers.apycot.python import *
--- a/test/unittest_checkers_pyunit.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_checkers_pyunit.py	Mon Oct 28 11:22:08 2013 +0100
@@ -7,7 +7,7 @@
 
 from logilab.common.testlib import unittest_main, TestCase, mock_object
 
-from utils import MockCheckWriter, input_path
+from cubes.apycot.testutils import MockCheckWriter, input_path
 
 from apycotlib import SUCCESS, FAILURE, ERROR, PARTIAL, NODATA
 from apycotlib.checkers import python
--- a/test/unittest_entities.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_entities.py	Mon Oct 28 11:22:08 2013 +0100
@@ -4,7 +4,7 @@
 from cubicweb import devtools # setup import machinery
 from cubes.apycot.views.reports import all_check_results
 
-from utils import ApycotBaseTC
+from cubes.apycot.testutils import ApycotBaseTC
 
 class MockWriter(object):
     """fake apycot.IWriter class, ignore every thing"""
--- a/test/unittest_hooks.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_hooks.py	Mon Oct 28 11:22:08 2013 +0100
@@ -7,7 +7,7 @@
 from cubicweb import Binary
 
 
-from utils import INPUTS_DIR, ApycotBaseTC
+from cubes.apycot.testutils import INPUTS_DIR, ApycotBaseTC
 
 
 def clean_str(string):
--- a/test/unittest_parser.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_parser.py	Mon Oct 28 11:22:08 2013 +0100
@@ -2,7 +2,7 @@
 
 from logilab.common.testlib import TestCase, unittest_main
 
-import utils # import this first
+from cubes.apycot import testutils # import this first
 
 from apycotlib import SimpleOutputParser
 from apycotlib.writer import AbstractLogWriter
--- a/test/unittest_repositories.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_repositories.py	Mon Oct 28 11:22:08 2013 +0100
@@ -5,7 +5,7 @@
 import os
 from copy import copy
 
-from utils import MockVCSFile as VCSFile
+from cubes.apycot.testutils import MockVCSFile as VCSFile
 
 from apycotlib.repositories import *
 
--- a/test/unittest_task.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_task.py	Mon Oct 28 11:22:08 2013 +0100
@@ -7,7 +7,7 @@
 from logilab.common.testlib import TestCase, unittest_main, mock_object, within_tempdir
 
 # import this first will set import machinery on
-from utils import MockTestWriter, MockRepository, MockConnection, MockVCSFile, input_path
+from cubes.apycot.testutils import MockTestWriter, MockRepository, MockConnection, MockVCSFile, input_path
 
 from apycotlib import SetupException
 from apycotlib import SUCCESS, FAILURE, PARTIAL, SKIPPED
--- a/test/unittest_writer.py	Mon Oct 14 16:53:54 2013 +0200
+++ b/test/unittest_writer.py	Mon Oct 28 11:22:08 2013 +0100
@@ -2,7 +2,7 @@
 import os
 from logilab.common.testlib import TestCase, unittest_main
 
-import utils
+import cubes.apycot.testutils
 
 from apycotlib.writer import BaseDataWriter
 
--- a/test/utils.py	Mon Oct 14 16:53:54 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,179 +0,0 @@
-import shutil
-import os
-from datetime import datetime
-from os.path import join, dirname, abspath
-
-# MUST be done BEFORE importing narvalbot
-INPUTS_DIR = abspath(join(dirname(__file__), 'data'))
-def input_path(file=''):
-    return join(INPUTS_DIR, file)
-os.environ['NARVALSOURCES'] = input_path('narval-cw-sources.ini')
-
-from logilab.common.testlib import mock_object
-
-from cubicweb.devtools.testlib import CubicWebTC
-
-from cubes.narval.testutils import NarvalBaseTC
-from cubes.apycot.recipes import quick_script
-
-from apycotlib.writer import CheckDataWriter, BaseDataWriter
-
-
-
-class DummyStack(object):
-
-    def __init__(self):
-        self.msg = None
-        self.clear()
-
-    def __getitem__(self, idx):
-        return self
-
-    def __len__(self):
-        return 0
-
-    def clear(self):
-        self.msg = []
-        self.append = self.msg.append
-
-
-class MockBaseWriter(BaseDataWriter):
-
-    def __init__(self):
-        super(MockBaseWriter, self).__init__( MockConnection, None)
-
-    def skip(self, *args, **kwargs):
-        pass
-
-    def _debug(self, *args, **kwargs):
-        print args, kwargs
-
-    def set_exec_status(self, status):
-        self._logs.append('<internal> SETTING EXEC STATUS: %s' % status)
-
-    raw = execution_info = skip
-    close = skip
-
-
-class MockTestWriter(MockBaseWriter):
-    """fake apycot.IWriter class, ignore every thing"""
-
-    def make_check_writer(self):
-        return MockCheckWriter()
-
-    link_to_revision = MockBaseWriter.skip
-
-
-class MockCheckWriter(MockBaseWriter):
-    """fake apycot.IWriter class, ignore every thing"""
-
-    def start(self, checker):
-        self._logs.append('<internal>STARTING %s' % checker.id)
-
-    def clear_writer(self):
-        self._log_stack = DummyStack()
-
-
-class MockTest(object):
-    """fake apycot.Test.Test class"""
-    def __init__(self, repo=None):
-        self.repo = repo
-        self.tmpdir = 'data'
-        self.environ = {}
-        self.checkers = []
-        self._apycot_config = {}
-
-    def project_path(self, subpath=False):
-        return self.repo.co_path
-
-    @property
-    def tconfig(self):
-        return mock_object(testconfig={}, name='bob', subpath=None)
-
-    def apycot_config(self, something=None):
-        return self._apycot_config
-
-
-class MockVCSFile(dict):
-    def __init__(self, _type, source_url=None, path=None):
-        super(MockVCSFile, self).__init__(
-                type=_type, source_url=source_url, path=path, local_cache=None)
-
-
-class MockRepository:
-    """fake apycot.IRepository class"""
-    branch = None
-    def __init__(self, attrs=None, **kwargs):
-        self.__dict__.update(kwargs)
-        self.co_path = self.path
-
-    def co_command(self):
-        return self.command
-
-    def co_move_to_branch_command(self):
-        return None
-
-    def __repr__(self):
-        return '<MockRepository %r>' % self.__dict__
-
-    def revision(self):
-        pass
-
-
-class MockConnection(object):
-    """fake pyro connexion"""
-    def close(self):
-        pass
-    def execute(*args, **kwargs):
-        pass
-    def commit(self):
-        pass
-
-
-class ApycotBaseTC(NarvalBaseTC):
-
-    def setup_database(self):
-        req = self.request()
-        self.lgce = req.create_entity(
-            'ProjectEnvironment', name=u'lgce',
-            check_config=u'install=python_setup\nenv-option=value',
-            check_environment=u'SETUPTOOLS=1\nDISPLAY=:2.0'
-            )
-        self.vcsrepo = req.create_entity('Repository', type=u'mercurial',
-                                         # use path to avoid clone attempt when using url
-                                         path=unicode(self.datapath('project')),
-                                         reverse_local_repository=self.lgce)
-        self.pyp = req.create_entity('TestConfig', name=u'PYTHONPACKAGE',
-                                     check_config=u'python_lint_treshold=7\n'
-                                     'python_lint_ignore=thirdparty\n'
-                                     'python_test_coverage_treshold=70\n',
-                                     check_environment=u'NO_SETUPTOOLS=1\nDISPLAY=:1.0')
-        self.recipe = req.execute('Recipe X WHERE X name "apycot.recipe.quick"').get_entity(0, 0)
-        # reset recipe content
-        self.recipe.set_attributes(script=quick_script)
-        self.lgc = self.add_test_config(u'lgc', env=self.lgce, group=self.pyp, use_recipe=self.recipe)
-
-        self.repo.threaded_task = lambda func: func() # XXX move to cw
-
-    def add_test_config(self, name,
-                        check_config=u'python_lint_treshold=8\npouet=5',
-                        env=None, group=None, **kwargs):
-        """add a TestConfig instance"""
-        req = self.request()
-        if group is not None:
-            kwargs['refinement_of'] = group
-        if env is not None:
-            kwargs['use_environment'] = env
-        return req.create_entity('TestConfig', name=name,
-                                 check_config=check_config, **kwargs)
-
-    def dumb_execution(self, ex, check_defs, setend=True):
-        """add a TestExecution instance"""
-        req = self.request()
-        for name, status in check_defs:
-            cr = req.create_entity('CheckResult', name=unicode(name), status=unicode(status))
-            req.execute('SET X during_execution Y WHERE X eid %(x)s, Y eid %(e)s',
-                        {'x': cr.eid, 'e': ex.eid})
-        if setend:
-            req.execute('SET X status "success" '
-                        'WHERE X eid %(x)s', {'x': ex.eid})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testutils.py	Mon Oct 28 11:22:08 2013 +0100
@@ -0,0 +1,181 @@
+import shutil
+import os
+from datetime import datetime
+from os.path import join, dirname, abspath
+
+# MUST be done BEFORE importing narvalbot
+INPUTS_DIR = abspath(join(dirname(__file__), 'test', 'data'))
+def input_path(file=''):
+    return join(INPUTS_DIR, file)
+os.environ['NARVALSOURCES'] = input_path('narval-cw-sources.ini')
+
+from logilab.common.testlib import mock_object
+
+from cubicweb.devtools.testlib import CubicWebTC
+
+from cubes.narval.testutils import NarvalBaseTC
+from cubes.apycot.recipes import quick_script
+
+from apycotlib.writer import CheckDataWriter, BaseDataWriter
+
+
+
+class DummyStack(object):
+
+    def __init__(self):
+        self.msg = None
+        self.clear()
+
+    def __getitem__(self, idx):
+        return self
+
+    def __len__(self):
+        return 0
+
+    def clear(self):
+        self.msg = []
+        self.append = self.msg.append
+
+
+class MockBaseWriter(BaseDataWriter):
+
+    def __init__(self):
+        super(MockBaseWriter, self).__init__( MockConnection, None)
+
+    def skip(self, *args, **kwargs):
+        pass
+
+    def _debug(self, *args, **kwargs):
+        print args, kwargs
+
+    def set_exec_status(self, status):
+        self._logs.append('<internal> SETTING EXEC STATUS: %s' % status)
+
+    raw = execution_info = skip
+    close = skip
+
+
+class MockTestWriter(MockBaseWriter):
+    """fake apycot.IWriter class, ignore every thing"""
+
+    def make_check_writer(self):
+        return MockCheckWriter()
+
+    link_to_revision = MockBaseWriter.skip
+
+
+class MockCheckWriter(MockBaseWriter):
+    """fake apycot.IWriter class, ignore every thing"""
+
+    def start(self, checker):
+        self._logs.append('<internal>STARTING %s' % checker.id)
+
+    def clear_writer(self):
+        self._log_stack = DummyStack()
+
+
+class MockTest(object):
+    """fake apycot.Test.Test class"""
+    def __init__(self, repo=None):
+        self.repo = repo
+        self.tmpdir = 'data'
+        self.environ = {}
+        self.checkers = []
+        self._apycot_config = {}
+
+    def project_path(self, subpath=False):
+        return self.repo.co_path
+
+    @property
+    def tconfig(self):
+        return mock_object(testconfig={}, name='bob', subpath=None)
+
+    def apycot_config(self, something=None):
+        return self._apycot_config
+
+
+class MockVCSFile(dict):
+    def __init__(self, _type, source_url=None, path=None):
+        super(MockVCSFile, self).__init__(
+                type=_type, source_url=source_url, path=path, local_cache=None)
+
+
+class MockRepository:
+    """fake apycot.IRepository class"""
+    branch = None
+    def __init__(self, attrs=None, **kwargs):
+        self.__dict__.update(kwargs)
+        self.co_path = self.path
+
+    def co_command(self):
+        return self.command
+
+    def co_move_to_branch_command(self):
+        return None
+
+    def __repr__(self):
+        return '<MockRepository %r>' % self.__dict__
+
+    def revision(self):
+        pass
+
+
+class MockConnection(object):
+    """fake pyro connexion"""
+    def close(self):
+        pass
+    def execute(*args, **kwargs):
+        pass
+    def commit(self):
+        pass
+
+
+class ApycotBaseTC(NarvalBaseTC):
+
+    recipescript = quick_script
+
+    def setup_database(self):
+        req = self.request()
+        self.lgce = req.create_entity(
+            'ProjectEnvironment', name=u'lgce',
+            check_config=u'install=python_setup\nenv-option=value',
+            check_environment=u'SETUPTOOLS=1\nDISPLAY=:2.0'
+            )
+        self.vcsrepo = req.create_entity('Repository', type=u'mercurial',
+                                         # use path to avoid clone attempt when using url
+                                         path=unicode(self.datapath('project')),
+                                         reverse_local_repository=self.lgce)
+        self.pyp = req.create_entity('TestConfig', name=u'PYTHONPACKAGE',
+                                     check_config=u'python_lint_treshold=7\n'
+                                     'python_lint_ignore=thirdparty\n'
+                                     'python_test_coverage_treshold=70\n',
+                                     check_environment=u'NO_SETUPTOOLS=1\nDISPLAY=:1.0')
+        self.recipe = req.execute('Recipe X WHERE X name "apycot.recipe.quick"').get_entity(0, 0)
+        # reset recipe content
+        self.recipe.set_attributes(script=self.recipescript)
+        self.lgc = self.add_test_config(u'lgc', env=self.lgce, group=self.pyp, use_recipe=self.recipe)
+
+        self.repo.threaded_task = lambda func: func() # XXX move to cw
+
+    def add_test_config(self, name,
+                        check_config=u'python_lint_treshold=8\npouet=5',
+                        env=None, group=None, **kwargs):
+        """add a TestConfig instance"""
+        req = self.request()
+        if group is not None:
+            kwargs['refinement_of'] = group
+        if env is not None:
+            kwargs['use_environment'] = env
+        return req.create_entity('TestConfig', name=name,
+                                 check_config=check_config, **kwargs)
+
+    def dumb_execution(self, ex, check_defs, setend=True):
+        """add a TestExecution instance"""
+        req = self.request()
+        for name, status in check_defs:
+            cr = req.create_entity('CheckResult', name=unicode(name), status=unicode(status))
+            req.execute('SET X during_execution Y WHERE X eid %(x)s, Y eid %(e)s',
+                        {'x': cr.eid, 'e': ex.eid})
+        if setend:
+            req.execute('SET X status "success" '
+                        'WHERE X eid %(x)s', {'x': ex.eid})