Commit 540d181b authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[devtools] Simplify test configuration's init

Specify module's __file__ instead of apphome. __file__ is saved on the class as
it will be used later (eg to find the proper pg cluster without relying on
shared dictionnary modifications).

This will require some tests update, but it should be less common in cubes than
in cubicweb itself.
parent f6938ae0dea8
......@@ -30,7 +30,7 @@ import subprocess
import tempfile
import getpass
from hashlib import sha1 # pylint: disable=E0611
from os.path import abspath, join, exists, split, isabs, isdir
from os.path import abspath, join, exists, split, isdir, dirname
from functools import partial
from six import text_type
......@@ -140,18 +140,15 @@ class TestServerConfiguration(ServerConfiguration):
skip_db_create_and_restore = False
default_sources = DEFAULT_SOURCES
def __init__(self, appid='data', apphome=None, log_threshold=logging.CRITICAL+10):
def __init__(self, appid, test_module_file, log_threshold=logging.CRITICAL + 10):
# must be set before calling parent __init__
if apphome is None:
if exists(appid):
apphome = abspath(appid)
else: # cube test
apphome = abspath('..')
apphome = abspath(join(dirname(test_module_file), appid))
self._apphome = apphome
super(TestServerConfiguration, self).__init__(appid)
self.init_log(log_threshold, force=True)
# need this, usually triggered by cubicweb-ctl
self.load_cwctl_plugins()
self.test_module_file = test_module_file
# By default anonymous login are allow but some test need to deny of to
# change the default user. Set it to None to prevent anonymous login.
......@@ -236,13 +233,6 @@ class ApptestConfiguration(BaseApptestConfiguration):
# considered initialized
skip_db_create_and_restore = False
def __init__(self, appid, apphome=None,
log_threshold=logging.WARNING, sourcefile=None):
BaseApptestConfiguration.__init__(self, appid, apphome,
log_threshold=log_threshold)
self.init_repository = sourcefile is None
self.sourcefile = sourcefile
class PostgresApptestConfiguration(ApptestConfiguration):
default_sources = DEFAULT_PSQL_SOURCES
......
......@@ -56,7 +56,7 @@ class ValueGeneratorTC(TestCase):
return [f.strip() for f in io.open(osp.join(DATADIR, 'firstnames.txt'), encoding='latin1')]
def setUp(self):
config = ApptestConfiguration('data', apphome=DATADIR)
config = ApptestConfiguration('data', __file__)
config.bootstrap_cubes()
schema = config.load_schema()
e_schema = schema.eschema('Person')
......
......@@ -378,8 +378,8 @@ class CubicWebTC(BaseTestCase):
try:
return cls.__dict__['_config']
except KeyError:
home = abspath(join(dirname(sys.modules[cls.__module__].__file__), cls.appid))
config = cls._config = cls.configcls(cls.appid, apphome=home)
test_module_file = sys.modules[cls.__module__].__file__
config = cls._config = cls.configcls(cls.appid, test_module_file)
config.mode = 'test'
return config
......
......@@ -33,7 +33,7 @@ from cubicweb.server.checkintegrity import check, reindex_entities
class CheckIntegrityTC(TestCase):
def setUp(self):
handler = get_test_db_handler(TestServerConfiguration(apphome=self.datadir))
handler = get_test_db_handler(TestServerConfiguration('data', __file__))
handler.build_db_cache()
self.repo, _cnx = handler.get_repo_and_cnx()
sys.stderr = sys.stdout = StringIO()
......
......@@ -57,7 +57,7 @@ class OperationsTC(CubicWebTC):
class HookCalled(Exception): pass
config = TestServerConfiguration('data')
config = TestServerConfiguration('data', __file__)
config.bootstrap_cubes()
schema = config.load_schema()
......
......@@ -66,7 +66,7 @@ SQL_CONNECT_HOOKS['sqlite'].append(init_sqlite_connexion)
def setUpClass(cls, *args):
global repo, cnx
config = TestServerConfiguration(apphome=UtilsTC.datadir)
config = TestServerConfiguration('data', __file__)
handler = get_test_db_handler(config)
handler.build_db_cache()
repo, cnx = handler.get_repo_and_cnx()
......
......@@ -61,7 +61,7 @@ def monkey_patch_import_driver_module(driver, drivers, quiet=True):
def setUpModule():
global config, schema
config = TestServerConfiguration('data', apphome=CWRQLTC.datadir)
config = TestServerConfiguration('data', __file__)
config.bootstrap_cubes()
schema = config.load_schema()
schema['in_state'].inlined = True
......
......@@ -24,8 +24,7 @@ from cubicweb.devtools.repotest import BaseQuerierTC
class SQLGenAnnotatorTC(BaseQuerierTC):
def setUp(self):
handler = get_test_db_handler(TestServerConfiguration(
'data2', apphome=SQLGenAnnotatorTC.datadir))
handler = get_test_db_handler(TestServerConfiguration('data', __file__))
handler.build_db_cache()
repo, _cnx = handler.get_repo_and_cnx()
self.__class__.repo = repo
......
......@@ -38,8 +38,7 @@ def setUpModule(*args):
global schema, config
loader = CubicWebSchemaLoader()
apphome = Schema2RQLTC.datadir + '-schemaserial'
config = TestServerConfiguration('data', apphome=apphome)
config = TestServerConfiguration('data-schemaserial', __file__)
config.bootstrap_cubes()
schema = loader.load(config)
......
......@@ -9,7 +9,7 @@ class ServerCTLTC(testlib.CubicWebTC):
def setUp(self):
super(ServerCTLTC, self).setUp()
self.orig_config_for = ServerConfiguration.config_for
config_for = lambda appid: ApptestConfiguration(appid, apphome=self.datadir)
config_for = lambda appid: ApptestConfiguration(appid, __file__)
ServerConfiguration.config_for = staticmethod(config_for)
def tearDown(self):
......
......@@ -23,8 +23,7 @@ from cubicweb.server.ssplanner import SSPlanner
# keep cnx so it's not garbage collected and the associated session closed
def setUpModule(*args):
global repo, cnx
handler = get_test_db_handler(TestServerConfiguration(
'data', apphome=SSPlannerTC.datadir))
handler = get_test_db_handler(TestServerConfiguration('data', __file__))
handler.build_db_cache()
global repo, cnx
repo, cnx = handler.get_repo_and_cnx()
......
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
......@@ -43,7 +43,7 @@ CUSTOM_CUBES_DIR = abspath(join(dirname(__file__), 'data', 'cubes'))
class CubicWebConfigurationTC(TestCase):
def setUp(self):
cleanup_sys_modules([CUSTOM_CUBES_DIR, ApptestConfiguration.CUBES_DIR])
self.config = ApptestConfiguration('data', apphome=self.datadir)
self.config = ApptestConfiguration('data', __file__)
self.config._cubes = ('email', 'file')
def tearDown(self):
......
......@@ -43,7 +43,7 @@ class MigrTestConfig(TestServerConfiguration):
class MigrationToolsTC(TestCase):
def setUp(self):
self.config = MigrTestConfig('data')
self.config = MigrTestConfig('data', __file__)
from yams.schema import Schema
self.config.load_schema = lambda expand_cubes=False: Schema('test')
self.config.__class__.cubicweb_appobject_path = frozenset()
......@@ -74,7 +74,7 @@ class MigrationToolsTC(TestCase):
((0, 0, 4), TMIGRDIR+'0.0.4_Any.py')])
def test_filter_scripts_for_mode(self):
config = CubicWebConfiguration('data')
config = CubicWebConfiguration('data', __file__)
config.verbosity = 0
config = self.config
config.__class__.name = 'repository'
......@@ -100,7 +100,7 @@ class BaseCreationTC(TestCase):
def test_db_creation(self):
"""make sure database can be created"""
config = ApptestConfiguration('data', apphome=self.datadir)
config = ApptestConfiguration('data', __file__)
source = config.system_source_config
self.assertEqual(source['db-driver'], 'sqlite')
handler = get_test_db_handler(config)
......
......@@ -31,7 +31,7 @@ from cubicweb.devtools import repotest, TestServerConfiguration, BaseApptestConf
def setUpModule(*args):
global rqlhelper, schema
config = TestServerConfiguration(RQLRewriteTC.datapath('rewrite'))
config = TestServerConfiguration('data-rewrite', __file__)
config.bootstrap_cubes()
schema = config.load_schema()
schema.add_relation_def(RelationDefinition(subject='Card', name='in_state',
......@@ -498,12 +498,9 @@ class RQLRewriteTC(TestCase):
'EXISTS(NOT S in_group A, A name "guests", A is CWGroup)')
from cubicweb.devtools.testlib import CubicWebTC
from logilab.common.decorators import classproperty
class RewriteFullTC(CubicWebTC):
@classproperty
def config(cls):
return BaseApptestConfiguration(apphome=cls.datapath('rewrite'))
appid = 'data-rewrite'
def process(self, rql, args=None):
if args is None:
......@@ -784,7 +781,7 @@ class RQLRelationRewriterTC(TestCase):
class RQLRelationRewriterTC(CubicWebTC):
appid = 'data/rewrite'
appid = 'data-rewrite'
def test_base_rule(self):
with self.admin_access.client_cnx() as cnx:
......
......@@ -143,7 +143,7 @@ class CubicWebSchemaTC(TestCase):
self.assertEqual(str(expr), 'Any O,U WHERE U has_update_permission O, O eid %(o)s, U eid %(u)s')
loader = CubicWebSchemaLoader()
config = TestConfiguration('data', apphome=DATADIR)
config = TestConfiguration('data', __file__)
config.bootstrap_cubes()
class SchemaReaderClassTest(TestCase):
......@@ -269,7 +269,7 @@ class SchemaReaderClassTest(TestCase):
def test_relation_perm_overriding(self):
loader = CubicWebSchemaLoader()
config = TestConfiguration('data', apphome=join(dirname(__file__), 'data_schemareader'))
config = TestConfiguration('data_schemareader', __file__)
config.bootstrap_cubes()
schema = loader.load(config)
rdef = next(iter(schema['in_group'].rdefs.values()))
......
......@@ -33,7 +33,7 @@ xy.add_equivalence('Project name', 'doap:Project doap:name')
xy.add_equivalence('Project name', 'doap:Project dc:title')
config = TestServerConfiguration('data')
config = TestServerConfiguration('data', __file__)
config.bootstrap_cubes()
schema = config.load_schema()
......
......@@ -38,7 +38,7 @@ WEBVIEWSDIR = join(BASE, 'web', 'views')
class VRegistryTC(TestCase):
def setUp(self):
config = TestServerConfiguration('data')
config = TestServerConfiguration('data', __file__)
self.vreg = CWRegistryStore(config)
config.bootstrap_cubes()
self.vreg.schema = config.load_schema()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment