Move setting of TestExecution.{options,execution_of} to a hook (closes #5242517)
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 13 Apr 2015 11:12:49 +0200
changeset 1825 b859e64fc3ee
parent 1824 82ad4a58ea49
child 1826 239805c9132d
Move setting of TestExecution.{options,execution_of} to a hook (closes #5242517) Apart from the 'archive' setting, they're inherited from the ProjectEnvironment and/or TestConfig. Make 'archive' a separate attribute instead.
_apycotlib/atest.py
entities.py
hooks.py
migration/3.4.0_Any.py
schema.py
test/unittest_hooks.py
test/unittest_task.py
wdoc/apycot_quick_start_en.rst
--- a/_apycotlib/atest.py	Mon Jul 20 12:03:04 2015 +0200
+++ b/_apycotlib/atest.py	Mon Apr 13 11:12:49 2015 +0200
@@ -89,7 +89,7 @@
         # flag indicating whether to clean test environment after test execution
         # or if an archive containing it should be uploaded
         self.keep_test_dir = options.get('keep_test_dir', False)
-        self.archive = options.get('archive', False)
+        self.archive = texec['keep_archive']
         # set of preprocessors which have failed
         self._failed_pp = set()
         self.executed_checkers = {}
--- a/entities.py	Mon Jul 20 12:03:04 2015 +0200
+++ b/entities.py	Mon Apr 13 11:12:49 2015 +0200
@@ -322,13 +322,8 @@
             #                 if not dup_arch:
             #                     duplicate.set_attributes(archive=False)
         else:
-            options = self.apycot_configuration(pe)
-            if archive:
-                options['archive'] = archive
-            options_str = u'\n'.join(u"%s=%s" % kv for kv in options.iteritems())
             texec = self._cw.create_entity(
-                'TestExecution', priority=priority,
-                options=options_str, execution_of=self.recipe,
+                'TestExecution', priority=priority, keep_archive=archive,
                 branch=branch, using_environment=pe, using_config=self)
         if start_rev_deps or (start_rev_deps is None and self.start_reverse_dependencies):
             for dpe in pe.reverse_dependencies():
--- a/hooks.py	Mon Jul 20 12:03:04 2015 +0200
+++ b/hooks.py	Mon Apr 13 11:12:49 2015 +0200
@@ -63,6 +63,25 @@
                 tc.start(env, head.branch)
 
 
+class SetOptionsHook(hook.Hook):
+    __regid__ = 'apycot.set_te_options_hook'
+    __select__ = hook.Hook.__select__ & hook.match_rtype('using_config', 'using_environment')
+    events = ('after_add_relation',)
+
+    def __call__(self):
+        te = self._cw.entity_from_eid(self.eidfrom)
+        if not te.using_config:
+            return
+        config = te.using_config[0]
+        if self.rtype == 'using_config' and not te.execution_of:
+            te.cw_set(execution_of=config.recipe)
+        if not te.using_environment or te.options:
+            return
+        pe = te.using_environment[0]
+        options = config.apycot_configuration(pe)
+        options_str = u'\n'.join(u"%s=%s" % kv for kv in options.iteritems())
+        te.cw_set(options=options_str)
+
 
 class ComputeStartModeHook(hook.Hook):
     __regid__ = 'apycot.compute_start_mode'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/migration/3.4.0_Any.py	Mon Apr 13 11:12:49 2015 +0200
@@ -0,0 +1,1 @@
+add_attribute('TestExecution', 'keep_archive')
--- a/schema.py	Mon Jul 20 12:03:04 2015 +0200
+++ b/schema.py	Mon Apr 13 11:12:49 2015 +0200
@@ -280,6 +280,8 @@
                                 _('killed'))
                     )
     branch = String(indexed=True, __permissions__=IMMUTABLE_ATTR_PERMS, required=True)
+    keep_archive = Boolean(required=True, default=False, description=_('keep an archive of the execution environment'))
+
 
 class CheckResult(EntityType):
     """group results of execution of a specific test on a project"""
--- a/test/unittest_hooks.py	Mon Jul 20 12:03:04 2015 +0200
+++ b/test/unittest_hooks.py	Mon Apr 13 11:12:49 2015 +0200
@@ -194,6 +194,48 @@
             te_e.cw_set(using_revision=rev)
         cnx.commit() #change new te status
 
+    def test_setup_te_config_hook(self):
+        """check the apycot.setup_te_config hook works properly"""
+        # ensure options is set from TC and PE
+        # also check execution_if is properly set
+        with self.admin_access.client_cnx() as cnx:
+            tc = cnx.entity_from_eid(self.lgc)
+            pe = cnx.entity_from_eid(self.lgce)
+
+            te = cnx.create_entity('TestExecution', branch=u'toto')
+            te.cw_set(using_environment=pe, using_config=tc)
+            cnx.commit()
+
+            # options from the TestConfig
+            self.assertIn('python_lint_treshold=8', te.options)
+            self.assertIn('pouet=5', te.options)
+            # options from the ProjectEnvironement
+            self.assertIn('env-option=value', te.options)
+            self.assertIn('install=python_setup', te.options)
+            # check execution_of
+            self.assertEqual(tc.recipe.script, te.script)
+            
+        # ensure options is not set from TC and PE if already
+        # specified "by hand"
+        with self.admin_access.client_cnx() as cnx:
+            tc = cnx.entity_from_eid(self.lgc)
+            pe = cnx.entity_from_eid(self.lgce)
+
+            te = cnx.create_entity('TestExecution', branch=u'toto',
+                                   options=u'arg=value')
+            te.cw_set(using_environment=pe, using_config=tc)
+            cnx.commit()
+
+            # check options from the TestConfig are not set
+            self.assertNotIn('python_lint_treshold=8', te.options)
+            self.assertNotIn('pouet=5', te.options)
+            # check options from the ProjectEnvironement are not set
+            self.assertNotIn('env-option=value', te.options)
+            self.assertNotIn('install=python_setup', te.options)
+            # check "manual" options
+            self.assertIn('arg=value', te.options)
+            
+
     def test_new_vc_trigger(self):
         """ check the on new revision start mode. Run all testconfigs, add new
         revision, re-run and check there are new test configs"""
--- a/test/unittest_task.py	Mon Jul 20 12:03:04 2015 +0200
+++ b/test/unittest_task.py	Mon Apr 13 11:12:49 2015 +0200
@@ -44,7 +44,7 @@
     environment = environment or Environment(eid=tconfig['name'], apycot_preprocessors={})
     texec = dict(configuration=tconfig, environment=environment,
                  branch=environment['apycot_configuration'].get('branch'),
-                 options='')
+                 keep_archive=False, options='')
     test = BaseTest(texec, writer)
     if pps is not None:
         test.apycot_preprocessors = lambda x: pps
--- a/wdoc/apycot_quick_start_en.rst	Mon Jul 20 12:03:04 2015 +0200
+++ b/wdoc/apycot_quick_start_en.rst	Mon Apr 13 11:12:49 2015 +0200
@@ -146,7 +146,6 @@
   pylintrc='~/.pylintrc'
   coverage_threshold=0.7
   keep_test_dir=True
-  archive=False
   required=False
   verbose=True