[test] make test independant from the working directory
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 25 Oct 2010 17:15:46 +0200
changeset 6631 26c303c3f1aa
parent 6630 f516ee1ef36c
child 6632 78878f5a8166
[test] make test independant from the working directory
devtools/__init__.py
devtools/testlib.py
server/test/unittest_checkintegrity.py
server/test/unittest_msplanner.py
server/test/unittest_multisources.py
server/test/unittest_querier.py
server/test/unittest_rql2sql.py
server/test/unittest_rqlannotation.py
server/test/unittest_schemaserial.py
server/test/unittest_ssplanner.py
test/data/scripts/script1.py
test/data/scripts/script2.py
test/data/scripts/script3.py
test/unittest_cwconfig.py
test/unittest_cwctl.py
test/unittest_migration.py
web/test/unittest_formfields.py
--- a/devtools/__init__.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/devtools/__init__.py	Mon Oct 25 17:15:46 2010 +0200
@@ -224,10 +224,10 @@
 
 # test database handling #######################################################
 
-def init_test_database(config=None, configdir='data'):
+def init_test_database(config=None, appid='data', apphome=None):
     """init a test database for a specific driver"""
     from cubicweb.dbapi import in_memory_cnx
-    config = config or TestServerConfiguration(configdir)
+    config = config or TestServerConfiguration(appid, apphome=apphome)
     sources = config.sources()
     driver = sources['system']['db-driver']
     if config.db_require_setup:
@@ -335,12 +335,13 @@
 def init_test_database_sqlite(config):
     """initialize a fresh sqlite databse used for testing purpose"""
     # remove database file if it exists
+    dbfile = join(config.apphome, config.sources()['system']['db-name'])
+    config.sources()['system']['db-name'] = dbfile
     if not reset_test_database_sqlite(config):
         # initialize the database
         import shutil
         from cubicweb.server import init_repository
         init_repository(config, interactive=False)
-        dbfile = config.sources()['system']['db-name']
         shutil.copy(dbfile, '%s-template' % dbfile)
 
 def install_sqlite_patch(querier):
--- a/devtools/testlib.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/devtools/testlib.py	Mon Oct 25 17:15:46 2010 +0200
@@ -25,7 +25,7 @@
 import sys
 import re
 import urlparse
-from os.path import dirname, join
+from os.path import dirname, join, abspath
 from urllib import unquote
 from math import log
 from contextlib import contextmanager
@@ -199,7 +199,7 @@
         try:
             return cls.__dict__['_config']
         except KeyError:
-            home = join(dirname(sys.modules[cls.__module__].__file__), cls.appid)
+            home = abspath(join(dirname(sys.modules[cls.__module__].__file__), cls.appid))
             config = cls._config = cls.configcls(cls.appid, apphome=home)
             config.mode = 'test'
             return config
--- a/server/test/unittest_checkintegrity.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_checkintegrity.py	Mon Oct 25 17:15:46 2010 +0200
@@ -26,7 +26,7 @@
 
 class CheckIntegrityTC(TestCase):
     def setUp(self):
-        self.repo, self.cnx = init_test_database()
+        self.repo, self.cnx = init_test_database(apphome=self.datadir)
         self.execute = self.cnx.cursor().execute
         self.session = self.repo._sessions[self.cnx.sessionid]
         sys.stderr = sys.stdout = StringIO()
--- a/server/test/unittest_msplanner.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_msplanner.py	Mon Oct 25 17:15:46 2010 +0200
@@ -75,7 +75,9 @@
 
 
 # keep cnx so it's not garbage collected and the associated session is closed
-repo, cnx = init_test_database()
+def setup_module(*args):
+    global repo, cnx
+    repo, cnx = init_test_database(apphome=BaseMSPlannerTC.datadir)
 
 def teardown_module(*args):
     global repo, cnx
@@ -89,9 +91,9 @@
     * ldap source supporting CWUser
     * rql source supporting Card
     """
-    repo = repo
 
     def setUp(self):
+        self.__class__.repo = repo
         #_QuerierTC.setUp(self)
         self.setup()
         # hijack Affaire security
@@ -2243,9 +2245,9 @@
 
     * 2 rql sources supporting Card
     """
-    repo = repo
 
     def setUp(self):
+        self.__class__.repo = repo
         self.setup()
         self.add_source(FakeCardSource, 'cards')
         self.add_source(FakeCardSource, 'cards2')
@@ -2452,9 +2454,9 @@
         return []
 
 class MSPlannerVCSSource(BasePlannerTC):
-    repo = repo
 
     def setUp(self):
+        self.__class__.repo = repo
         self.setup()
         self.add_source(FakeVCSSource, 'vcs')
         self.planner = MSPlanner(self.o.schema, self.repo.vreg.rqlhelper)
--- a/server/test/unittest_multisources.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_multisources.py	Mon Oct 25 17:15:46 2010 +0200
@@ -48,8 +48,10 @@
 
 def setup_module(*args):
     global repo2, cnx2, repo3, cnx3
-    repo2, cnx2 = init_test_database(config=ExternalSource1Configuration('data'))
-    repo3, cnx3 = init_test_database(config=ExternalSource2Configuration('data'))
+    cfg1 = ExternalSource1Configuration('data', apphome=TwoSourcesTC.datadir)
+    repo2, cnx2 = init_test_database(config=cfg1)
+    cfg2 = ExternalSource2Configuration('data', apphome=TwoSourcesTC.datadir)
+    repo3, cnx3 = init_test_database(config=cfg2)
     cnx3.request().create_entity('CWSource', name=u'extern', type=u'pyrorql',
                                  config=EXTERN_SOURCE_CFG)
     cnx3.commit()
--- a/server/test/unittest_querier.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_querier.py	Mon Oct 25 17:15:46 2010 +0200
@@ -62,7 +62,9 @@
                           ('C0 text,C1 integer', {'A': 'table0.C0', 'B': 'table0.C1'}))
 
 
-repo, cnx = init_test_database()
+def setup_module(*args):
+    global repo, cnx
+    repo, cnx = init_test_database(apphome=UtilsTC.datadir)
 
 def teardown_module(*args):
     global repo, cnx
@@ -72,7 +74,9 @@
 
 
 class UtilsTC(BaseQuerierTC):
-    repo = repo
+    def setUp(self):
+        self.__class__.repo = repo
+        super(UtilsTC, self).setUp()
 
     def get_max_eid(self):
         # no need for cleanup here
@@ -225,7 +229,9 @@
 
 
 class QuerierTC(BaseQuerierTC):
-    repo = repo
+    def setUp(self):
+        self.__class__.repo = repo
+        super(QuerierTC, self).setUp()
 
     def test_encoding_pb(self):
         self.assertRaises(RQLSyntaxError, self.execute,
--- a/server/test/unittest_rql2sql.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_rql2sql.py	Mon Oct 25 17:15:46 2010 +0200
@@ -38,12 +38,14 @@
     pass # already registered
 
 
-config = TestServerConfiguration('data')
-config.bootstrap_cubes()
-schema = config.load_schema()
-schema['in_state'].inlined = True
-schema['state_of'].inlined = False
-schema['comments'].inlined = False
+def setup_module(*args):
+    global config, schema
+    config = TestServerConfiguration('data', apphome=CWRQLTC.datadir)
+    config.bootstrap_cubes()
+    schema = config.load_schema()
+    schema['in_state'].inlined = True
+    schema['state_of'].inlined = False
+    schema['comments'].inlined = False
 
 def teardown_module(*args):
     global config, schema
@@ -1076,8 +1078,12 @@
 
     ]
 class CWRQLTC(RQLGeneratorTC):
-    schema = schema
     backend = 'sqlite'
+
+    def setUp(self):
+        self.__class__.schema = schema
+        super(CWRQLTC, self).setUp()
+
     def test_nonregr_sol(self):
         delete = self.rqlhelper.parse(
             'DELETE X read_permission READ_PERMISSIONSUBJECT,X add_permission ADD_PERMISSIONSUBJECT,'
@@ -1105,9 +1111,12 @@
     return '\n'.join(l.strip() for l in text.strip().splitlines())
 
 class PostgresSQLGeneratorTC(RQLGeneratorTC):
-    schema = schema
     backend = 'postgres'
 
+    def setUp(self):
+        self.__class__.schema = schema
+        super(PostgresSQLGeneratorTC, self).setUp()
+
     def _norm_sql(self, sql):
         return sql.strip()
 
--- a/server/test/unittest_rqlannotation.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_rqlannotation.py	Mon Oct 25 17:15:46 2010 +0200
@@ -22,7 +22,10 @@
 from cubicweb.devtools import init_test_database
 from cubicweb.devtools.repotest import BaseQuerierTC
 
-repo, cnx = init_test_database()
+
+def setup_module(*args):
+    global repo, cnx
+    repo, cnx = init_test_database(apphome=SQLGenAnnotatorTC.datadir)
 
 def teardown_module(*args):
     global repo, cnx
@@ -30,7 +33,10 @@
 
 
 class SQLGenAnnotatorTC(BaseQuerierTC):
-    repo = repo
+
+    def setUp(self):
+        self.__class__.repo = repo
+        super(SQLGenAnnotatorTC, self).setUp()
 
     def get_max_eid(self):
         # no need for cleanup here
--- a/server/test/unittest_schemaserial.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_schemaserial.py	Mon Oct 25 17:15:46 2010 +0200
@@ -26,14 +26,16 @@
 from cubicweb.schema import CubicWebSchemaLoader
 from cubicweb.devtools import TestServerConfiguration
 
-loader = CubicWebSchemaLoader()
-config = TestServerConfiguration('data')
-config.bootstrap_cubes()
-schema = loader.load(config)
+def setup_module(*args):
+    global schema, config
+    loader = CubicWebSchemaLoader()
+    config = TestServerConfiguration('data', apphome=Schema2RQLTC.datadir)
+    config.bootstrap_cubes()
+    schema = loader.load(config)
 
 def teardown_module(*args):
-    global schema, config, loader
-    del schema, config, loader
+    global schema, config
+    del schema, config
 
 from cubicweb.server.schemaserial import *
 from cubicweb.server.schemaserial import _erperms2rql as erperms2rql
--- a/server/test/unittest_ssplanner.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/server/test/unittest_ssplanner.py	Mon Oct 25 17:15:46 2010 +0200
@@ -23,17 +23,19 @@
 from cubicweb.server.ssplanner import SSPlanner
 
 # keep cnx so it's not garbage collected and the associated session closed
-repo, cnx = init_test_database()
+def setup_module(*args):
+    global repo, cnx
+    repo, cnx = init_test_database(apphome=SSPlannerTC.datadir)
 
 def teardown_module(*args):
     global repo, cnx
     del repo, cnx
 
 class SSPlannerTC(BasePlannerTC):
-    repo = repo
     _test = test_plan
 
     def setUp(self):
+        self.__class__.repo = repo
         BasePlannerTC.setUp(self)
         self.planner = SSPlanner(self.o.schema, self.repo.vreg.rqlhelper)
         self.system = self.o._repo.system_source
--- a/test/data/scripts/script1.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/test/data/scripts/script1.py	Mon Oct 25 17:15:46 2010 +0200
@@ -1,3 +1,4 @@
-assert 'data/scripts/script1.py' == __file__
-assert '__main__' == __name__
+from os.path import join
+assert __file__.endswith(join('scripts', 'script1.py')), __file__
+assert '__main__' == __name__, __name__
 assert [] == __args__, __args__
--- a/test/data/scripts/script2.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/test/data/scripts/script2.py	Mon Oct 25 17:15:46 2010 +0200
@@ -1,3 +1,4 @@
-assert 'data/scripts/script2.py' == __file__
-assert '__main__' == __name__
+from os.path import join
+assert __file__.endswith(join('scripts', 'script2.py')), __file__
+assert '__main__' == __name__, __name__
 assert ['-v'] == __args__, __args__
--- a/test/data/scripts/script3.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/test/data/scripts/script3.py	Mon Oct 25 17:15:46 2010 +0200
@@ -1,3 +1,4 @@
-assert 'data/scripts/script3.py' == __file__
-assert '__main__' == __name__
+from os.path import join
+assert __file__.endswith(join('scripts', 'script3.py')), __file__
+assert '__main__' == __name__, __name__
 assert ['-vd', '-f', 'FILE.TXT'] == __args__, __args__
--- a/test/unittest_cwconfig.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/test/unittest_cwconfig.py	Mon Oct 25 17:15:46 2010 +0200
@@ -43,7 +43,7 @@
 class CubicWebConfigurationTC(TestCase):
     def setUp(self):
         cleanup_sys_modules([CUSTOM_CUBES_DIR, ApptestConfiguration.CUBES_DIR])
-        self.config = ApptestConfiguration('data')
+        self.config = ApptestConfiguration('data', apphome=self.datadir)
         self.config._cubes = ('email', 'file')
 
     def tearDown(self):
--- a/test/unittest_cwctl.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/test/unittest_cwctl.py	Mon Oct 25 17:15:46 2010 +0200
@@ -20,6 +20,7 @@
 """
 import sys
 import os
+from os.path import join
 from cStringIO import StringIO
 from logilab.common.testlib import TestCase, unittest_main
 
@@ -54,9 +55,10 @@
                    'script2.py': ['-v'],
                    'script3.py': ['-vd', '-f', 'FILE.TXT'],
                   }
-        mih.cmd_process_script('data/scripts/script1.py', funcname=None)
+        mih.cmd_process_script(join(self.datadir, 'scripts', 'script1.py'),
+                               funcname=None)
         for script, args in scripts.items():
-            scriptname = os.path.join('data/scripts/', script)
+            scriptname = os.path.join(self.datadir, 'scripts', script)
             self.assert_(os.path.exists(scriptname))
             mih.cmd_process_script(scriptname, None, scriptargs=args)
 
--- a/test/unittest_migration.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/test/unittest_migration.py	Mon Oct 25 17:15:46 2010 +0200
@@ -15,11 +15,9 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""cubicweb.migration unit tests
+"""cubicweb.migration unit tests"""
 
-"""
-
-from os.path import abspath
+from os.path import abspath, dirname, join
 from logilab.common.testlib import TestCase, unittest_main
 
 from cubicweb.devtools import TestServerConfiguration
@@ -32,8 +30,8 @@
     def has_entity(self, e_type):
         return self.has_key(e_type)
 
-SMIGRDIR = abspath('data/server_migration') + '/'
-TMIGRDIR = abspath('data/migration') + '/'
+SMIGRDIR = join(dirname(__file__), 'data', 'server_migration') + '/'
+TMIGRDIR = join(dirname(__file__), 'data', 'migration') + '/'
 
 class MigrTestConfig(TestServerConfiguration):
     verbosity = 0
@@ -105,7 +103,7 @@
 
     def test_db_creation(self):
         """make sure database can be created"""
-        config = ApptestConfiguration('data')
+        config = ApptestConfiguration('data', apphome=self.datadir)
         source = config.sources()['system']
         self.assertEqual(source['db-driver'], 'sqlite')
         cleanup_sqlite(source['db-name'], removetemplate=True)
--- a/web/test/unittest_formfields.py	Mon Oct 25 16:28:05 2010 +0200
+++ b/web/test/unittest_formfields.py	Mon Oct 25 17:15:46 2010 +0200
@@ -29,9 +29,11 @@
 
 from cubes.file.entities import File
 
-config = TestServerConfiguration('data')
-config.bootstrap_cubes()
-schema = config.load_schema()
+def setup_module(*args):
+    global schema
+    config = TestServerConfiguration('data', apphome=GuessFieldTC.datadir)
+    config.bootstrap_cubes()
+    schema = config.load_schema()
 
 class GuessFieldTC(TestCase):