changeset 6857:cddff7f0dd08

tests: use temporary copy of test.ini, possibly customized for TEST_DB It was an undocumented feature that if setting the environment variable TEST_DB, that would be used for tests instead of the default kallithea/tests/test.ini sqlalchemy.url . That did however not work for Git hooks and tests would fail. Instead, create a copy of test.ini in the temp folder and use that for testing. If TEST_DB is set, edit the file so the specified DB URL is used. This fixes Git hook related tests if TEST_DB is used. Since this also changes the path of %(here)s to the temporary location, just use the default paths. This also has the advantage that the data folders are now in the temp folder as well. Therefore a broken data folder, from a past run, can no longer influence a test.
author domruf <dominikruf@gmail.com>
date Mon, 14 Aug 2017 22:34:28 +0200
parents dfb31968225d
children cbf524e4c1a3
files kallithea/tests/conftest.py kallithea/tests/test.ini scripts/generate-ini.py
diffstat 3 files changed, 18 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/conftest.py	Sun Jul 30 14:26:10 2017 +0200
+++ b/kallithea/tests/conftest.py	Mon Aug 14 22:34:28 2017 +0200
@@ -1,4 +1,5 @@
 import os
+import re
 import sys
 import logging
 import pkg_resources
@@ -34,12 +35,20 @@
     # Disable INFO logging of test database creation, restore with NOTSET
     logging.disable(logging.INFO)
 
-    context = loadwsgi.loadcontext(loadwsgi.APP, 'config:kallithea/tests/test.ini', relative_to=path)
+    with open(os.path.join(path, 'kallithea/tests/test.ini'), 'r') as input_file:
+        test_ini = input_file.read()
 
     if os.environ.get('TEST_DB'):
-        # swap config if we pass environment variable
-        context.local_conf['sqlalchemy.url'] = os.environ.get('TEST_DB')
+        test_ini = re.sub('^\s*sqlalchemy.url\s*=.*$',
+                           'sqlalchemy.url = %s' % os.environ.get('TEST_DB'),
+                           test_ini,
+                           flags=re.M)
 
+    test_ini_file = os.path.join(TESTS_TMP_PATH, 'test.ini')
+    with open(test_ini_file, 'w') as output_file:
+        output_file.write(test_ini)
+
+    context = loadwsgi.loadcontext(loadwsgi.APP, 'config:%s' % test_ini_file)
     from kallithea.tests.fixture import create_test_env, create_test_index
 
     # set KALLITHEA_NO_TMP_PATH=1 to disable re-creating the database and test repos
--- a/kallithea/tests/test.ini	Sun Jul 30 14:26:10 2017 +0200
+++ b/kallithea/tests/test.ini	Mon Aug 14 22:34:28 2017 +0200
@@ -184,18 +184,15 @@
 ## Fallback language, empty for English (valid values are the names of subdirectories in kallithea/i18n):
 i18n.lang =
 
-#cache_dir = %(here)s/data
-cache_dir = %(here)s/../../data/test/cache
-#index_dir = %(here)s/data/index
-index_dir = %(here)s/../../data/test/index
+cache_dir = %(here)s/data
+index_dir = %(here)s/data/index
 
 ## perform a full repository scan on each server start, this should be
 ## set to false after first startup, to allow faster server restarts.
 initial_repo_scan = false
 
 ## uncomment and set this path to use archive download cache
-#archive_cache_dir = %(here)s/tarballcache
-archive_cache_dir = %(here)s/../../data/test/tarballcache
+archive_cache_dir = %(here)s/tarballcache
 
 ## change this to unique ID for security
 app_instance_uuid = test
@@ -343,10 +340,8 @@
 ###         BEAKER CACHE        ####
 ####################################
 
-#beaker.cache.data_dir = %(here)s/data/cache/data
-beaker.cache.data_dir = %(here)s/../../data/test/cache/data
-#beaker.cache.lock_dir = %(here)s/data/cache/lock
-beaker.cache.lock_dir = %(here)s/../../data/test/cache/lock
+beaker.cache.data_dir = %(here)s/data/cache/data
+beaker.cache.lock_dir = %(here)s/data/cache/lock
 
 beaker.cache.regions = short_term,long_term,sql_cache_short
 
@@ -499,8 +494,7 @@
 #########################################################
 
 # SQLITE [default]
-#sqlalchemy.url = sqlite:///%(here)s/kallithea.db?timeout=60
-sqlalchemy.url = sqlite:///%(here)s/kallithea_test.sqlite
+sqlalchemy.url = sqlite:///%(here)s/kallithea.db?timeout=60
 
 # POSTGRESQL
 #sqlalchemy.url = postgresql://user:pass@localhost/kallithea
--- a/scripts/generate-ini.py	Sun Jul 30 14:26:10 2017 +0200
+++ b/scripts/generate-ini.py	Mon Aug 14 22:34:28 2017 +0200
@@ -41,12 +41,6 @@
                 'show_revision_number': 'true',
                 'beaker.cache.sql_cache_short.expire': '1',
                 'beaker.session.secret': '{74e0cd75-b339-478b-b129-07dd221def1f}',
-                'cache_dir': '%(here)s/../../data/test/cache',
-                'index_dir': '%(here)s/../../data/test/index',
-                'archive_cache_dir': '%(here)s/../../data/test/tarballcache',
-                'beaker.cache.data_dir': '%(here)s/../../data/test/cache/data',
-                'beaker.cache.lock_dir': '%(here)s/../../data/test/cache/lock',
-                'sqlalchemy.url': 'sqlite:///%(here)s/kallithea_test.sqlite',
             },
             '[handler_console]': {
                 'level': 'DEBUG',