# Copyright (C) 2010-2024 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/

"""
Test suite for making push/pull operations, on specially modified INI files
"""

import pytest

from rhodecode.tests import GIT_REPO, SVN_REPO, HG_REPO

from rhodecode.tests.vcs_operations import Command, _check_proper_clone
from rhodecode.tests.vcs_operations.test_vcs_operations_svn import get_cli_flags


@pytest.mark.usefixtures(
    "init_pyramid_app",
    "repo_group_repos",
    "disable_anonymous_user",
    "disable_locking",
)
class TestVCSOperationsClone:
    def test_clone_git_repo_by_admin(self, rcstack, tmpdir):
        clone_url = rcstack.repo_clone_url(GIT_REPO)
        cmd = Command(tmpdir.strpath)
        stdout, stderr = cmd.execute("git clone", clone_url, tmpdir.strpath)
        _check_proper_clone(stdout, stderr, "git")
        cmd.assert_returncode_success()

    def test_clone_hg_repo_by_admin(self, rcstack, tmpdir):
        clone_url = rcstack.repo_clone_url(HG_REPO)
        cmd = Command(tmpdir.strpath)
        stdout, stderr = cmd.execute("hg clone", clone_url, tmpdir.strpath)
        _check_proper_clone(stdout, stderr, "hg")
        cmd.assert_returncode_success()

    @pytest.mark.xfail(reason="Lack of proper SVN support of cloning")
    def test_clone_svn_repo_by_admin(self, rcstack, tmpdir):
        clone_url = rcstack.repo_clone_url(SVN_REPO)
        username, password = rcstack.repo_clone_credentials()
        flags, auth = get_cli_flags(username, password)
        cmd = Command(tmpdir.strpath)
        stdout, stderr = cmd.execute(f"svn checkout {flags} {auth}", clone_url, tmpdir.strpath)
        _check_proper_clone(stdout, stderr, "svn")
        cmd.assert_returncode_success()
