test/unittest_preprocessors.py
author David Douard <david.douard@logilab.fr>
Wed, 27 Sep 2017 12:39:06 +0200
changeset 1871 980e20892562
parent 1674 7e66dbf04a09
child 1681 8282a3ca8a79
permissions -rw-r--r--
[checker/pypi] allow to configure the pypi repo to use and defaults to 'pypi'.

# -*- coding: utf-8 -*-
import os
import json

from logilab.common.testlib import unittest_main
import cubicweb.devtools

from cubes.apycot.testutils import ApycotBaseTC
from apycotlib import atest, writer, REGISTRY
import preprocessors.apycot.distutils
from cubicweb.utils import json_dumps

os.environ['HGRCPATH'] = os.devnull

class FakeCnxh(object):
    def __init__(self, msgcfg, msgdep):
        self.msgcfg = msgcfg
        self.msgdep = msgdep
        self.instance_url = 'http://gna'
    def http_post(self, *args, **kwargs):
        if 'vid' in kwargs and kwargs['vid'] == 'apycot.get_configuration':
            return self.msgcfg
        elif 'vid' in kwargs and kwargs['vid'] == 'apycot.get_dependencies':
            return self.msgdep
        else:
            return []

    def http_get(*args, **kwargs):
        return [{'deps': 'a'}]

REGISTRY['preprocessor']['python_setup'] = preprocessors.apycot.distutils.DistutilsProcessor

class FakeWriter(object):
    def debug(self, msg, *args, **kwargs):
        pass
    def info(self, msg, *args, **kwargs):
        pass
    def fatal(self, msg, *args, **kwargs):
        pass

    def __init__(self, msgcfg, msgdep):
        self.cnxh = FakeCnxh(msgcfg, msgdep)

class FileCheckerTest(ApycotBaseTC):
    def start_lgc_tests(self, cnx):
        with self.admin_access.client_cnx() as cnx:
            lgc = cnx.entity_from_eid(self.lgc)
            lgce = cnx.entity_from_eid(self.lgce)
            plan = lgc.start(lgce)
            cnx.commit()
            return plan.eid

    def test_exec_status_change(self):
        with self.admin_access.client_cnx() as cnx:
            tceid = cnx.create_entity('TestConfig',
                                      name=u'TC',
                                      label=u'full python tests',
                                      start_mode=u'manual',
                                      check_config=u"""pylint_threshold=7
    pycoverage_threshold=70
    install=python_setup""").eid
            peeid = cnx.create_entity('ProjectEnvironment',
                                      name=u'gna',
                                      reverse_use_environment=tceid,
                                      local_repository=self.vcsrepo).eid

            self.vcsrepo2 = cnx.create_entity('Repository', type=u'mercurial',
                    # use path to avoid clone attempt when using url
                    source_url=u'file://' + unicode(self.datapath('project1')),
                    reverse_local_repository=self.lgce)

            pe_dep1 = cnx.create_entity('ProjectEnvironment',
                                        name=u'regna',
                                        reverse_use_environment=tceid,
                                        local_repository=self.vcsrepo2)
            cnx.commit()

        with self.admin_access.web_request(environment=peeid) as req_env:
            tc = req_env.entity_from_eid(tceid)
            pe = req_env.entity_from_eid(peeid)
            tconf = json.loads(req_env.view('apycot.get_configuration', rset=tc.as_rset()))
            penv = json.loads(req_env.view('ejsonexport', rset=pe.as_rset()))[0]
            penv_dep = json.loads(req_env.view('ejsonexport', rset=pe.as_rset()))
            penv['title'] = 'gna'
            planeid = self.start_lgc_tests(req_env)

        with self.new_access('narval').client_cnx() as cnx:
            plan = json.loads(json_dumps(cnx.entity_from_eid(planeid)))
            test = atest.Test(plan, FakeWriter(tconf, penv_dep))
            test.call_preprocessor('install', penv)


if __name__ == '__main__':
    unittest_main()