Changeset - 163d1c4f2b8b
[Not reviewed]
default
1 4 0
Mads Kiilerich - 8 years ago 2017-09-14 02:08:07
mads@kiilerich.com
tests: generate test.ini directly from template

Get rid of the committed generated test.ini
5 files changed with 24 insertions and 475 deletions:
0 comments (0 inline, 0 general)
MANIFEST.in
Show inline comments
 
include           .coveragerc
 
include           Apache-License-2.0.txt
 
include           CONTRIBUTORS
 
include           COPYING
 
include           Jenkinsfile
 
include           LICENSE-MERGELY.html
 
include           LICENSE.md
 
include           MIT-Permissive-License.txt
 
include           README.rst
 
include           dev_requirements.txt
 
include           development.ini
 
include           pytest.ini
 
include           requirements.txt
 
include           tox.ini
 
recursive-include docs *
 
recursive-include init.d *
 
recursive-include kallithea/alembic *
 
include           kallithea/bin/ldap_sync.conf
 
include           kallithea/lib/paster_commands/template.ini.mako
 
recursive-include kallithea/i18n *
 
recursive-include kallithea/public *
 
recursive-include kallithea/templates *
 
recursive-include kallithea/tests/fixtures *
 
recursive-include kallithea/tests/scripts *
 
include           kallithea/tests/models/test_dump_html_mails.ref.html
 
include           kallithea/tests/performance/test_vcs.py
 
include           kallithea/tests/test.ini
 
include           kallithea/tests/vcs/aconfig
 
recursive-include scripts *
kallithea/tests/conftest.py
Show inline comments
 
import os
 
import re
 
import sys
 
import logging
 
import pkg_resources
 
import time
 

	
 
import formencode
 
from paste.deploy import loadwsgi
 
from routes.util import URLGenerator
 
import pytest
 
from pytest_localserver.http import WSGIServer
 

	
 
from kallithea.controllers.root import RootController
 
from kallithea.lib import inifile
 
from kallithea.lib.utils import repo2db_mapper
 
from kallithea.model.user import UserModel
 
from kallithea.model.meta import Session
 
from kallithea.model.db import Setting, User, UserIpMap
 
from kallithea.model.scm import ScmModel
 
from kallithea.tests.base import invalidate_all_caches, TEST_USER_REGULAR_LOGIN, TESTS_TMP_PATH, \
 
    TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS
 
import kallithea.tests.base # FIXME: needed for setting testapp instance!!!
 

	
 
from tg.util.webtest import test_context
 

	
 

	
 
def pytest_configure():
 
    os.environ['TZ'] = 'UTC'
 
    if not kallithea.is_windows:
 
        time.tzset() # only available on Unix
 

	
 
    path = os.getcwd()
 
    sys.path.insert(0, path)
 
    pkg_resources.working_set.add_entry(path)
 

	
 
    # Disable INFO logging of test database creation, restore with NOTSET
 
    logging.disable(logging.INFO)
 

	
 
    with open(os.path.join(path, 'kallithea/tests/test.ini'), 'r') as input_file:
 
        test_ini = input_file.read()
 

	
 
    ini_settings = {
 
        '[server:main]': {
 
            'port': '4999',
 
        },
 
        '[app:main]': {
 
            'app_instance_uuid': 'test',
 
            'show_revision_number': 'true',
 
            'beaker.cache.sql_cache_short.expire': '1',
 
            'beaker.session.secret': '{74e0cd75-b339-478b-b129-07dd221def1f}',
 
        },
 
        '[handler_console]': {
 
            'formatter': 'color_formatter',
 
        },
 
        # The 'handler_console_sql' block is very similar to the one in
 
        # development.ini, but without the explicit 'level=DEBUG' setting:
 
        # it causes duplicate sqlalchemy debug logs, one through
 
        # handler_console_sql and another through another path.
 
        '[handler_console_sql]': {
 
            'formatter': 'color_formatter_sql',
 
        },
 
    }
 
    if 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)
 
        ini_settings['[app:main]']['sqlalchemy.url'] = os.environ.get('TEST_DB')
 

	
 
    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)
 
    inifile.create(test_ini_file, None, ini_settings)
 

	
 
    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
 
    if not int(os.environ.get('KALLITHEA_NO_TMP_PATH', 0)):
 
        create_test_env(TESTS_TMP_PATH, context.config())
 

	
 
    # set KALLITHEA_WHOOSH_TEST_DISABLE=1 to disable whoosh index during tests
 
    if not int(os.environ.get('KALLITHEA_WHOOSH_TEST_DISABLE', 0)):
 
        create_test_index(TESTS_TMP_PATH, context.config(), True)
 

	
 
    kallithea.tests.base.testapp = context.create()
 
    # do initial repo scan
 
    repo2db_mapper(ScmModel().repo_scan(TESTS_TMP_PATH))
 

	
 
    logging.disable(logging.NOTSET)
 

	
 
    kallithea.tests.base.url = URLGenerator(RootController().mapper, {'HTTP_HOST': 'example.com'})
 

	
 
    # set fixed language for form messages, regardless of environment settings
 
    formencode.api.set_stdtranslation(languages=[])
 

	
 

	
 
@pytest.fixture
 
def create_test_user():
 
    """Provide users that automatically disappear after test is over."""
 
    test_user_ids = []
 

	
 
    def _create_test_user(user_form):
 
        user = UserModel().create(user_form)
 
        test_user_ids.append(user.user_id)
 
        return user
 
    yield _create_test_user
 
    for user_id in test_user_ids:
 
        UserModel().delete(user_id)
 
    Session().commit()
 

	
 

	
 
def _set_settings(*kvtseq):
 
    session = Session()
 
    for kvt in kvtseq:
 
        assert len(kvt) in (2, 3)
 
        k = kvt[0]
 
        v = kvt[1]
 
        t = kvt[2] if len(kvt) == 3 else 'unicode'
 
        Setting.create_or_update(k, v, t)
 
    session.commit()
 

	
 

	
 
@pytest.fixture
 
def set_test_settings():
 
    """Restore settings after test is over."""
 
    # Save settings.
 
    settings_snapshot = [
 
        (s.app_settings_name, s.app_settings_value, s.app_settings_type)
 
        for s in Setting.query().all()]
 
    yield _set_settings
 
    # Restore settings.
 
    session = Session()
 
    keys = frozenset(k for (k, v, t) in settings_snapshot)
 
    for s in Setting.query().all():
 
        if s.app_settings_name not in keys:
 
            session.delete(s)
 
    for k, v, t in settings_snapshot:
 
        if t == 'list' and hasattr(v, '__iter__'):
 
            v = ','.join(v) # Quirk: must format list value manually.
 
        Setting.create_or_update(k, v, t)
 
    session.commit()
 

	
 

	
 
@pytest.fixture
 
def auto_clear_ip_permissions():
 
    """Fixture that provides nothing but clearing IP permissions upon test
 
    exit. This clearing is needed to avoid other test failing to make fake http
 
    accesses."""
 
    yield
 
    # cleanup
 
    user_model = UserModel()
 

	
 
    user_ids = []
 
    user_ids.append(User.get_default_user().user_id)
 
    user_ids.append(User.get_by_username(TEST_USER_REGULAR_LOGIN).user_id)
 

	
 
    for user_id in user_ids:
 
        for ip in UserIpMap.query().filter(UserIpMap.user_id == user_id):
 
            user_model.delete_extra_ip(user_id, ip.ip_id)
 

	
 
    # IP permissions are cached, need to invalidate this cache explicitly
 
    invalidate_all_caches()
 

	
 

	
 
@pytest.fixture
 
def test_context_fixture(app_fixture):
 
    """
 
    Encompass the entire test using this fixture in a test_context,
 
    making sure that certain functionality still works even if no call to
 
    self.app.get/post has been made.
 
    The typical error message indicating you need a test_context is:
 
        TypeError: No object (name: context) has been registered for this thread
 

	
 
    The standard way to fix this is simply using the test_context context
 
    manager directly inside your test:
 
        with test_context(self.app):
 
            <actions>
 
    but if test setup code (xUnit-style or pytest fixtures) also needs to be
 
    executed inside the test context, that method is not possible.
 
    Even if there is no such setup code, the fixture may reduce code complexity
 
    if the entire test needs to run inside a test context.
 

	
 
    To apply this fixture (like any other fixture) to all test methods of a
 
    class, use the following class decorator:
 
        @pytest.mark.usefixtures("test_context_fixture")
 
        class TestFoo(TestController):
 
            ...
 
    """
 
    with test_context(app_fixture):
 
        yield
 

	
 

	
 
class MyWSGIServer(WSGIServer):
 
    def repo_url(self, repo_name, username=TEST_USER_ADMIN_LOGIN, password=TEST_USER_ADMIN_PASS):
 
        """Return URL to repo on this web server."""
 
        host, port = self.server_address
 
        proto = 'http' if self._server.ssl_context is None else 'https'
 
        auth = ''
 
        if username is not None:
 
            auth = username
 
            if password is not None:
 
                auth += ':' + password
 
        if auth:
 
            auth += '@'
 
        return '%s://%s%s:%s/%s' % (proto, auth, host, port, repo_name)
 

	
 

	
 
@pytest.yield_fixture(scope="session")
 
def webserver():
 
    """Start web server while tests are running.
 
    Useful for debugging and necessary for vcs operation tests."""
 
    server = MyWSGIServer(application=kallithea.tests.base.testapp)
 
    server.start()
 

	
 
    yield server
 

	
 
    server.stop()
kallithea/tests/test.ini
Show inline comments
 
deleted file
scripts/generate-ini.py
Show inline comments
 
#!/usr/bin/env python2
 
"""
 
Based on kallithea/lib/paster_commands/template.ini.mako, generate
 
  development.ini
 
  kallithea/tests/test.ini
 
"""
 

	
 
import re
 

	
 
from kallithea.lib import inifile
 

	
 
# files to be generated from the mako template
 
ini_files = [
 
    ('kallithea/tests/test.ini',
 
        {
 
            '[server:main]': {
 
                'port': '4999',
 
            },
 
            '[app:main]': {
 
                'app_instance_uuid': 'test',
 
                'show_revision_number': 'true',
 
                'beaker.cache.sql_cache_short.expire': '1',
 
                'beaker.session.secret': '{74e0cd75-b339-478b-b129-07dd221def1f}',
 
            },
 
            '[handler_console]': {
 
                'formatter': 'color_formatter',
 
            },
 
            # The 'handler_console_sql' block is very similar to the one in
 
            # development.ini, but without the explicit 'level=DEBUG' setting:
 
            # it causes duplicate sqlalchemy debug logs, one through
 
            # handler_console_sql and another through another path.
 
            '[handler_console_sql]': {
 
                'formatter': 'color_formatter_sql',
 
            },
 
        },
 
    ),
 
    ('development.ini',
 
        {
 
            '[server:main]': {
 
                'host': '0.0.0.0',
 
            },
 
            '[app:main]': {
 
                'initial_repo_scan': 'true',
 
                'debug': 'true',
 
                'app_instance_uuid': 'development-not-secret',
 
                'beaker.session.secret': 'development-not-secret',
 
            },
 
            '[handler_console]': {
 
                'formatter': 'color_formatter',
 
            },
 
            '[handler_console_sql]': {
 
                'formatter': 'color_formatter_sql',
 
            },
 
        },
 
    ),
 
]
 

	
 

	
 
def main():
 
    # make sure all mako lines starting with '#' (the '##' comments) are marked up as <text>
 
    makofile = inifile.template_file
 
    print 'reading:', makofile
 
    mako_org = open(makofile).read()
 
    mako_no_text_markup = re.sub(r'</?%text>', '', mako_org)
 
    mako_marked_up = re.sub(r'\n(##.*)', r'\n<%text>\1</%text>', mako_no_text_markup, flags=re.MULTILINE)
 
    if mako_marked_up != mako_org:
 
        print 'writing:', makofile
 
        open(makofile, 'w').write(mako_marked_up)
 

	
 
    # create ini files
 
    for fn, settings in ini_files:
 
        print 'updating:', fn
 
        inifile.create(fn, None, settings)
 

	
 

	
 
if __name__ == '__main__':
 
    main()
scripts/manifest
Show inline comments
 
@@ -729,218 +729,217 @@ kallithea/templates/admin/users/user_edi
 
kallithea/templates/admin/users/user_edit_api_keys.html
 
kallithea/templates/admin/users/user_edit_emails.html
 
kallithea/templates/admin/users/user_edit_ips.html
 
kallithea/templates/admin/users/user_edit_perms.html
 
kallithea/templates/admin/users/user_edit_profile.html
 
kallithea/templates/admin/users/users.html
 
kallithea/templates/base/
 
kallithea/templates/base/base.html
 
kallithea/templates/base/default_perms_box.html
 
kallithea/templates/base/flash_msg.html
 
kallithea/templates/base/perms_summary.html
 
kallithea/templates/base/root.html
 
kallithea/templates/bookmarks/
 
kallithea/templates/bookmarks/bookmarks.html
 
kallithea/templates/bookmarks/bookmarks_data.html
 
kallithea/templates/branches/
 
kallithea/templates/branches/branches.html
 
kallithea/templates/branches/branches_data.html
 
kallithea/templates/changelog/
 
kallithea/templates/changelog/changelog.html
 
kallithea/templates/changelog/changelog_details.html
 
kallithea/templates/changelog/changelog_summary_data.html
 
kallithea/templates/changeset/
 
kallithea/templates/changeset/changeset.html
 
kallithea/templates/changeset/changeset_comment_block.html
 
kallithea/templates/changeset/changeset_file_comment.html
 
kallithea/templates/changeset/changeset_range.html
 
kallithea/templates/changeset/diff_block.html
 
kallithea/templates/changeset/patch_changeset.html
 
kallithea/templates/compare/
 
kallithea/templates/compare/compare_cs.html
 
kallithea/templates/compare/compare_diff.html
 
kallithea/templates/data_table/
 
kallithea/templates/data_table/_dt_elements.html
 
kallithea/templates/email_templates/
 
kallithea/templates/email_templates/changeset_comment.html
 
kallithea/templates/email_templates/changeset_comment.txt
 
kallithea/templates/email_templates/default.html
 
kallithea/templates/email_templates/default.txt
 
kallithea/templates/email_templates/main.html
 
kallithea/templates/email_templates/main.txt
 
kallithea/templates/email_templates/password_reset.html
 
kallithea/templates/email_templates/password_reset.txt
 
kallithea/templates/email_templates/pull_request.html
 
kallithea/templates/email_templates/pull_request.txt
 
kallithea/templates/email_templates/pull_request_comment.html
 
kallithea/templates/email_templates/pull_request_comment.txt
 
kallithea/templates/email_templates/registration.html
 
kallithea/templates/email_templates/registration.txt
 
kallithea/templates/errors/
 
kallithea/templates/errors/error_document.html
 
kallithea/templates/files/
 
kallithea/templates/files/diff_2way.html
 
kallithea/templates/files/file_diff.html
 
kallithea/templates/files/files.html
 
kallithea/templates/files/files_add.html
 
kallithea/templates/files/files_browser.html
 
kallithea/templates/files/files_delete.html
 
kallithea/templates/files/files_edit.html
 
kallithea/templates/files/files_history_box.html
 
kallithea/templates/files/files_source.html
 
kallithea/templates/files/files_ypjax.html
 
kallithea/templates/followers/
 
kallithea/templates/followers/followers.html
 
kallithea/templates/followers/followers_data.html
 
kallithea/templates/forks/
 
kallithea/templates/forks/fork.html
 
kallithea/templates/forks/forks.html
 
kallithea/templates/forks/forks_data.html
 
kallithea/templates/index.html
 
kallithea/templates/index_base.html
 
kallithea/templates/journal/
 
kallithea/templates/journal/journal.html
 
kallithea/templates/journal/journal_data.html
 
kallithea/templates/journal/public_journal.html
 
kallithea/templates/login.html
 
kallithea/templates/password_reset.html
 
kallithea/templates/password_reset_confirmation.html
 
kallithea/templates/pullrequests/
 
kallithea/templates/pullrequests/pullrequest.html
 
kallithea/templates/pullrequests/pullrequest_data.html
 
kallithea/templates/pullrequests/pullrequest_show.html
 
kallithea/templates/pullrequests/pullrequest_show_all.html
 
kallithea/templates/pullrequests/pullrequest_show_my.html
 
kallithea/templates/register.html
 
kallithea/templates/search/
 
kallithea/templates/search/search.html
 
kallithea/templates/search/search_commit.html
 
kallithea/templates/search/search_content.html
 
kallithea/templates/search/search_path.html
 
kallithea/templates/search/search_repository.html
 
kallithea/templates/summary/
 
kallithea/templates/summary/statistics.html
 
kallithea/templates/summary/summary.html
 
kallithea/templates/switch_to_list.html
 
kallithea/templates/tags/
 
kallithea/templates/tags/tags.html
 
kallithea/templates/tags/tags_data.html
 
kallithea/tests/
 
kallithea/tests/__init__.py
 
kallithea/tests/api/
 
kallithea/tests/api/__init__.py
 
kallithea/tests/api/api_base.py
 
kallithea/tests/api/test_api_git.py
 
kallithea/tests/api/test_api_hg.py
 
kallithea/tests/conftest.py
 
kallithea/tests/fixture.py
 
kallithea/tests/fixtures/
 
kallithea/tests/fixtures/diff_with_diff_data.diff
 
kallithea/tests/fixtures/git_diff_binary_and_normal.diff
 
kallithea/tests/fixtures/git_diff_chmod.diff
 
kallithea/tests/fixtures/git_diff_mod_single_binary_file.diff
 
kallithea/tests/fixtures/git_diff_modify_binary_file.diff
 
kallithea/tests/fixtures/git_diff_rename_file.diff
 
kallithea/tests/fixtures/git_node_history_response.json
 
kallithea/tests/fixtures/hg_diff_add_single_binary_file.diff
 
kallithea/tests/fixtures/hg_diff_binary_and_normal.diff
 
kallithea/tests/fixtures/hg_diff_chmod.diff
 
kallithea/tests/fixtures/hg_diff_chmod_and_mod_single_binary_file.diff
 
kallithea/tests/fixtures/hg_diff_copy_and_chmod_file.diff
 
kallithea/tests/fixtures/hg_diff_copy_and_modify_file.diff
 
kallithea/tests/fixtures/hg_diff_copy_chmod_and_edit_file.diff
 
kallithea/tests/fixtures/hg_diff_copy_file.diff
 
kallithea/tests/fixtures/hg_diff_del_single_binary_file.diff
 
kallithea/tests/fixtures/hg_diff_mod_file_and_rename.diff
 
kallithea/tests/fixtures/hg_diff_mod_single_binary_file.diff
 
kallithea/tests/fixtures/hg_diff_mod_single_file_and_rename_and_chmod.diff
 
kallithea/tests/fixtures/hg_diff_rename_and_chmod_file.diff
 
kallithea/tests/fixtures/hg_diff_rename_file.diff
 
kallithea/tests/fixtures/hg_diff_rename_space_cr.diff
 
kallithea/tests/fixtures/hg_node_history_response.json
 
kallithea/tests/fixtures/journal_dump.csv
 
kallithea/tests/fixtures/markuptest.diff
 
kallithea/tests/fixtures/vcs_test_git.tar.gz
 
kallithea/tests/fixtures/vcs_test_hg.tar.gz
 
kallithea/tests/functional/
 
kallithea/tests/functional/__init__.py
 
kallithea/tests/functional/test_admin.py
 
kallithea/tests/functional/test_admin_auth_settings.py
 
kallithea/tests/functional/test_admin_defaults.py
 
kallithea/tests/functional/test_admin_gists.py
 
kallithea/tests/functional/test_admin_notifications.py
 
kallithea/tests/functional/test_admin_permissions.py
 
kallithea/tests/functional/test_admin_repo_groups.py
 
kallithea/tests/functional/test_admin_repos.py
 
kallithea/tests/functional/test_admin_settings.py
 
kallithea/tests/functional/test_admin_user_groups.py
 
kallithea/tests/functional/test_admin_users.py
 
kallithea/tests/functional/test_branches.py
 
kallithea/tests/functional/test_changelog.py
 
kallithea/tests/functional/test_changeset.py
 
kallithea/tests/functional/test_changeset_comments.py
 
kallithea/tests/functional/test_compare.py
 
kallithea/tests/functional/test_compare_local.py
 
kallithea/tests/functional/test_feed.py
 
kallithea/tests/functional/test_files.py
 
kallithea/tests/functional/test_followers.py
 
kallithea/tests/functional/test_forks.py
 
kallithea/tests/functional/test_home.py
 
kallithea/tests/functional/test_journal.py
 
kallithea/tests/functional/test_login.py
 
kallithea/tests/functional/test_my_account.py
 
kallithea/tests/functional/test_pullrequests.py
 
kallithea/tests/functional/test_repo_groups.py
 
kallithea/tests/functional/test_search.py
 
kallithea/tests/functional/test_summary.py
 
kallithea/tests/functional/test_tags.py
 
kallithea/tests/models/
 
kallithea/tests/models/__init__.py
 
kallithea/tests/models/common.py
 
kallithea/tests/models/test_changeset_status.py
 
kallithea/tests/models/test_diff_parsers.py
 
kallithea/tests/models/test_notifications.py
 
kallithea/tests/models/test_permissions.py
 
kallithea/tests/models/test_repo_groups.py
 
kallithea/tests/models/test_repos.py
 
kallithea/tests/models/test_user_group_permissions_on_repo_groups.py
 
kallithea/tests/models/test_user_groups.py
 
kallithea/tests/models/test_user_permissions_on_repo_groups.py
 
kallithea/tests/models/test_user_permissions_on_repos.py
 
kallithea/tests/models/test_users.py
 
kallithea/tests/other/
 
kallithea/tests/other/__init__.py
 
kallithea/tests/other/manual_test_vcs_operations.py
 
kallithea/tests/other/test_libs.py
 
kallithea/tests/other/test_mail.py
 
kallithea/tests/other/test_validators.py
 
kallithea/tests/scripts/
 
kallithea/tests/scripts/create_rc.sh
 
kallithea/tests/scripts/manual_test_concurrency.py
 
kallithea/tests/scripts/manual_test_crawler.py
 
kallithea/tests/scripts/mem_watch
 
kallithea/tests/test.ini
 
kallithea/tests/vcs/
 
kallithea/tests/vcs/__init__.py
 
kallithea/tests/vcs/aconfig
 
kallithea/tests/vcs/base.py
 
kallithea/tests/vcs/conf.py
 
kallithea/tests/vcs/test_archives.py
 
kallithea/tests/vcs/test_branches.py
 
kallithea/tests/vcs/test_changesets.py
 
kallithea/tests/vcs/test_filenodes_unicode_path.py
 
kallithea/tests/vcs/test_getitem.py
 
kallithea/tests/vcs/test_getslice.py
 
kallithea/tests/vcs/test_git.py
 
kallithea/tests/vcs/test_hg.py
 
kallithea/tests/vcs/test_inmemchangesets.py
 
kallithea/tests/vcs/test_nodes.py
 
kallithea/tests/vcs/test_repository.py
 
kallithea/tests/vcs/test_tags.py
 
kallithea/tests/vcs/test_utils.py
 
kallithea/tests/vcs/test_utils_filesize.py
 
kallithea/tests/vcs/test_vcs.py
 
kallithea/tests/vcs/test_workdirs.py
 
kallithea/tests/vcs/utils.py
 
kallithea/websetup.py
 
setup.cfg
 
setup.py
0 comments (0 inline, 0 general)