# Copyright (C) 2010-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import pytest
from rhodecode.lib import ext_json


def get_backends_from_metafunc(metafunc):
    requested_backends = set(metafunc.config.getoption('--backends'))
    backend_mark = metafunc.definition.get_closest_marker('backends')
    if backend_mark:
        # Supported backends by this test function, created from
        # pytest.mark.backends
        backends = backend_mark.args
    elif hasattr(metafunc.cls, 'backend_alias'):
        # Support class attribute "backend_alias", this is mainly
        # for legacy reasons for tests not yet using pytest.mark.backends
        backends = [metafunc.cls.backend_alias]
    else:
        backends = metafunc.config.getoption('--backends')
    return requested_backends.intersection(backends)


def pytest_addoption(parser):

    def _parse_json(value):
        return ext_json.str_json(value) if value else None

    def _split_comma(value):
        return value.split(',')

    parser.addoption(
        '--keep-tmp-path', action='store_true',
        help="Keep the test temporary directories")

    parser.addoption(
        '--backends', action='store', type=_split_comma,
        default=['git', 'hg', 'svn'],
        help="Select which backends to test for backend specific tests.")
    parser.addoption(
        '--dbs', action='store', type=_split_comma,
        default=['sqlite'],
        help="Select which database to test for database specific tests. "
             "Possible options are sqlite,postgres,mysql")
    parser.addoption(
        '--appenlight', '--ae', action='store_true',
        help="Track statistics in appenlight.")
    parser.addoption(
        '--appenlight-api-key', '--ae-key',
        help="API key for Appenlight.")
    parser.addoption(
        '--appenlight-url', '--ae-url',
        default="https://ae.rhodecode.com",
        help="Appenlight service URL, defaults to https://ae.rhodecode.com")
    parser.addoption(
        '--sqlite-connection-string', action='store',
        default='', help="Connection string for the dbs tests with SQLite")
    parser.addoption(
        '--postgres-connection-string', action='store',
        default='', help="Connection string for the dbs tests with Postgres")
    parser.addoption(
        '--mysql-connection-string', action='store',
        default='', help="Connection string for the dbs tests with MySQL")
    parser.addoption(
        '--repeat', type=int, default=100,
        help="Number of repetitions in performance tests.")

    parser.addoption(
        '--test-loglevel', dest='test_loglevel',
        help="Set default Logging level for tests, critical(default), error, warn , info, debug")
    group = parser.getgroup('pylons')
    group.addoption(
        '--with-pylons', dest='pyramid_config',
        help="Set up a Pylons environment with the specified config file.")
    group.addoption(
        '--ini-config-override', action='store', type=_parse_json,
        default=None, dest='pyramid_config_override', help=(
            "Overrides the .ini file settings. Should be specified in JSON"
            " format, e.g. '{\"section\": {\"parameter\": \"value\", ...}}'"
        )
    )
    parser.addini(
        'pyramid_config',
        "Set up a Pyramid environment with the specified config file.")

    vcsgroup = parser.getgroup('vcs')
    vcsgroup.addoption(
        '--without-vcsserver', dest='with_vcsserver', action='store_false',
        help="Do not start the VCSServer in a background process.")
    vcsgroup.addoption(
        '--with-vcsserver-http', dest='vcsserver_config_http',
        help="Start the HTTP VCSServer with the specified config file.")
    vcsgroup.addoption(
        '--vcsserver-protocol', dest='vcsserver_protocol',
        help="Start the VCSServer with HTTP protocol support.")
    vcsgroup.addoption(
        '--vcsserver-config-override', action='store', type=_parse_json,
        default=None, dest='vcsserver_config_override', help=(
            "Overrides the .ini file settings for the VCSServer. "
            "Should be specified in JSON "
            "format, e.g. '{\"section\": {\"parameter\": \"value\", ...}}'"
        )
    )
    vcsgroup.addoption(
        '--vcsserver-port', action='store', type=int,
        default=None, help=(
            "Allows to set the port of the vcsserver. Useful when testing "
            "against an already running server and random ports cause "
            "trouble."))
    parser.addini(
        'vcsserver_config_http',
        "Start the HTTP VCSServer with the specified config file.")
    parser.addini(
        'vcsserver_protocol',
        "Start the VCSServer with HTTP protocol support.")


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """
    Adding the remote traceback if the exception has this information.

    VCSServer attaches this information as the attribute `_vcs_server_traceback`
    to the exception instance.
    """
    outcome = yield
    report = outcome.get_result()

    if call.excinfo:
        exc = call.excinfo.value
        vcsserver_traceback = getattr(exc, '_vcs_server_traceback', None)

        if vcsserver_traceback and report.outcome == 'failed':
            section = f'VCSServer remote traceback {report.when}'
            report.sections.append((section, vcsserver_traceback))


def pytest_generate_tests(metafunc):

    # Support test generation based on --backend parameter
    if 'backend_alias' in metafunc.fixturenames:
        backends = get_backends_from_metafunc(metafunc)
        scope = None
        if not backends:
            pytest.skip("Not enabled for any of selected backends")

        metafunc.parametrize('backend_alias', backends, scope=scope)

    backend_mark = metafunc.definition.get_closest_marker('backends')
    if backend_mark:
        backends = get_backends_from_metafunc(metafunc)
        if not backends:
            pytest.skip("Not enabled for any of selected backends")
