# kas - setup tool for bitbake based projects
#
# Copyright (c) Siemens AG, 2019
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest
import shutil
import yaml
import subprocess
import os
from pathlib import Path
from kas import kas
from kas.repos import RepoRefError, Repo
from kas.kasusererror import CommandExecError


def run_cmd(cmd, cwd=None, fail=True):
    """
        Run a command and return the return code and output.
        Replacement for kas internal run_cmd function which
        cannot be used outside of kas as there is no event loop.
    """
    kas_wd = Path(os.environ.get('KAS_WORK_DIR', '.'))
    _cwd = Path(cwd or '.')
    if not _cwd.is_absolute():
        _cwd = kas_wd / _cwd
    try:
        output = subprocess.check_output(cmd, cwd=_cwd)
        return (0, output.decode('utf-8'))
    except subprocess.CalledProcessError as e:
        if fail:
            raise e
        return (e.returncode, e.output.decode('utf-8'))


@pytest.mark.online
def test_refspec_switch(monkeykas, tmpdir):
    """
        Test that the local git clone is correctly updated when switching
        between a commit hash refspec and a branch refspec.
    """
    tdir = str(tmpdir / 'test_refspec_switch')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    kas.kas(['shell', 'test.yml', '-c', 'true'])
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas',
                           fail=False)
    assert rc != 0
    assert output.strip() == ''
    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas',
                           fail=False)
    assert rc == 0
    assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas2',
                           fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'], cwd='kas3',
                           fail=False)
    assert rc == 0
    assert output.strip() == '3.0.1'

    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas',
                           fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas2',
                           fail=False)
    assert rc != 0
    assert output.strip() == ''
    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas2',
                           fail=False)
    assert rc == 0
    assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas3',
                           fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'], cwd='kas4',
                           fail=False)
    assert rc == 0
    assert output.strip() == '2.6.3'


@pytest.mark.online
def test_refspec_absolute(monkeykas, tmpdir):
    """
        Test that the local git clone works when a absolute refspec
        is given.
    """
    tdir = str(tmpdir / 'test_refspec_absolute')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    kas.kas(['shell', 'test3.yml', '-c', 'true'])
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'],
                           cwd='kas_abs', fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output_kas_abs) = run_cmd(['git', 'rev-parse', 'HEAD'],
                                   cwd='kas_abs', fail=False)
    assert rc == 0
    (rc, output_kas_rel) = run_cmd(['git', 'rev-parse', 'HEAD'],
                                   cwd='kas_rel', fail=False)
    assert rc == 0
    assert output_kas_abs.strip() == output_kas_rel.strip()
    (rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'],
                           cwd='kas_tag_abs', fail=False)
    assert rc == 0
    assert output.strip() == '3.0.1'


def test_url_no_refspec(monkeykas, tmpdir):
    """
        Test that a repository with url but no refspec raises an error.
    """
    tdir = str(tmpdir / 'test_url_no_refspec')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test4.yml', '-c', 'true'])


def test_commit_refspec_mix(monkeykas, tmpdir):
    """
        Test that mixing legacy refspec with commit/branch raises errors.
    """
    tdir = str(tmpdir / 'test_commit_refspec_mix')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test5.yml', '-c', 'true'])
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test6.yml', '-c', 'true'])
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test7.yml', '-c', 'true'])


@pytest.mark.online
def test_tag_commit_do_not_match(monkeykas, tmpdir):
    """
        Test that giving tag and commit that do not match raises an error.
    """
    tdir = str(tmpdir / 'test_tag_commit_do_not_match')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test8.yml', '-c', 'true'])


@pytest.mark.online
def test_unsafe_tag_warning(capsys, monkeykas, tmpdir):
    """
        Test that using tag without commit issues a warning, but only once.
    """
    tdir = str(tmpdir / 'test_unsafe_tag_warning')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    # needs to be reset in case other tests ran before
    Repo.__no_commit_warned__ = []
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    assert capsys.readouterr().err.count(
        'Using tag without commit for repository "kas4" is unsafe as tags '
        'are mutable.') == 1


@pytest.mark.online
def test_unsafe_branch_warning(capsys, monkeykas, tmpdir):
    """
        Test that using branch without commit issues a warning, but only once.
    """
    tdir = str(tmpdir / 'test_unsafe_branch_warning')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    # needs to be reset in case other tests ran before
    Repo.__no_commit_warned__ = []
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    assert capsys.readouterr().err.count(
        'Using branch without commit for repository "kas3" is unsafe. Either '
        'add a commit or use a lock file.') == 1


@pytest.mark.online
def test_tag_branch_same_name(capsys, monkeykas, tmpdir):
    """
        Test that kas uses the tag if a branch has the same name as the tag.
    """
    tdir = str(tmpdir / 'test_tag_branch_same_name')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    # Checkout the repositories
    kas.kas(['shell', 'test.yml', '-c', 'true'])

    # In kas3: create a branch named "3.0.1" on master HEAD
    # A tag named "3.0.1" already exists on an old commit from 2022
    (rc, output) = run_cmd(['git', 'switch', 'master'], cwd='kas3',
                           fail=False)
    assert rc == 0
    (rc, output) = run_cmd(['git', 'branch', '3.0.1'], cwd='kas3',
                           fail=False)
    assert rc == 0

    # In kas4: create a tag named "master" on existing 2.6.3 tag
    (rc, output) = run_cmd(['git', 'checkout', '2.6.3'], cwd='kas4',
                           fail=False)
    assert rc == 0
    (rc, output) = run_cmd(['git', 'tag', 'master'], cwd='kas4',
                           fail=False)
    assert rc == 0

    # Checkout the repositories again
    kas.kas(['shell', 'test.yml', '-c', 'true'])

    # Check the commit hashes
    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas3',
                           fail=False)
    assert rc == 0
    assert output.strip() == '229310958b17dc2b505b789c1cc1d0e2fddccc44'

    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas4',
                           fail=False)
    assert rc == 0

    (rc, output2) = run_cmd(['git', 'rev-parse', 'refs/heads/master'],
                            cwd='kas4', fail=False)
    assert rc == 0
    assert output.strip() == output2.strip()


@pytest.mark.online
def test_refspec_warning(capsys, monkeykas, tmpdir):
    """
        Test that using legacy refspec issues a warning, but only once.
    """
    tdir = str(tmpdir / 'test_refspec_warning')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    # needs to be reset in case other tests ran before
    Repo.__legacy_refspec_warned__ = []
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    assert capsys.readouterr().err.count(
        'Using deprecated refspec for repository "kas2".') == 1


@pytest.mark.online
def test_branch_and_tag(monkeykas, tmpdir, mercurial):
    """
        Test if error is raised when branch and tag are set.
    """
    tdir = str(tmpdir / 'test_branch_and_tag')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with mercurial(tmpdir, 'evolve', branch='stable'):
        with pytest.raises(RepoRefError):
            kas.kas(['checkout', 'test9.yml'])

    with pytest.raises(RepoRefError):
        kas.kas(['checkout', 'test10.yml'])

    with pytest.raises(RepoRefError):
        kas.kas(['checkout', 'test11.yml'])


@pytest.mark.online
def test_commit_expand(monkeykas, tmpdir, capsys):
    """
        Test if an abbreviated commit hash is expanded to the full hash.
    """
    tdir = str(tmpdir / 'test_commit_expand')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    kas.kas(['dump', '--resolve-refs', 'test12.yml'])
    rawspec = yaml.safe_load(capsys.readouterr().out)
    assert rawspec['repos']['kas']['commit'] == \
        'abd109469d17b7ff4d958b5aa5ab5f5511cc4d43'


def check_invalid_commit(monkeykas, tdir, variant, commit, exception,
                         expected_commit=None):
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    # Checkout the repositories
    kas.kas(['checkout', 'test13.yml'])

    cmd = ['git', 'tag', commit] if variant == 'tag' \
        else ['git', 'checkout', '-b', commit]
    (rc, output) = run_cmd(cmd, cwd='kas', fail=False)
    assert rc == 0

    with open(f'{tdir}/sha-test.yml', 'w') as f:
        f.write('header:\n')
        f.write('  version: 15\n')
        f.write('repos:\n')
        f.write('  this:\n')
        f.write('  kas-2:\n')
        f.write(f'    url: file://{tdir}/kas\n')
        f.write(f'    commit: {commit}\n')

    if exception:
        with pytest.raises(exception):
            kas.kas(['checkout', 'sha-test.yml'])
    else:
        kas.kas(['checkout', 'sha-test.yml'])
        (rc, output) = run_cmd(['git', 'log', '--pretty=%H', '-1'],
                               cwd='kas-2', fail=False)
        assert rc == 0
        assert output.strip() == expected_commit


@pytest.mark.online
def test_sha_like_branch_nonexist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_sha_like_branch_nonexist')
    check_invalid_commit(monkeykas, tdir, 'branch',
                         '123456789abcdef0123456789abcdef012345678',
                         CommandExecError)


@pytest.mark.online
def test_sha_like_branch_exist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_sha_like_branch_exist')
    check_invalid_commit(monkeykas, tdir, 'branch',
                         '8a82d22f123ea9242eabb91334b654dcf7410b7f',
                         None, '8a82d22f123ea9242eabb91334b654dcf7410b7f')


@pytest.mark.online
def test_sha_like_tag_nonexist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_sha_like_tag_nonexist')
    check_invalid_commit(monkeykas, tdir, 'tag',
                         '123456789abcdef0123456789abcdef012345679',
                         CommandExecError)


@pytest.mark.online
def test_sha_like_tag_exist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_sha_like_tag_exist')
    check_invalid_commit(monkeykas, tdir, 'tag',
                         '8a82d22f123ea9242eabb91334b654dcf7410b7f',
                         None, '8a82d22f123ea9242eabb91334b654dcf7410b7f')


@pytest.mark.online
def test_abbrev_sha_like_branch_nonexist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_abbrev_sha_like_branch_nonexist')
    check_invalid_commit(monkeykas, tdir, 'branch',
                         '123456789abc', RepoRefError)


@pytest.mark.online
def test_abbrev_sha_like_branch_exist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_abbrev_sha_like_branch_exist')
    check_invalid_commit(monkeykas, tdir, 'branch',
                         '8a82d22f123e', RepoRefError)


@pytest.mark.online
def test_abbrev_sha_like_tag_nonexist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_abbrev_sha_like_tag_nonexist')
    check_invalid_commit(monkeykas, tdir, 'tag',
                         '123456789abc', RepoRefError)


@pytest.mark.online
def test_abbrev_sha_like_tag_exist(monkeykas, tmpdir, capsys):
    tdir = str(tmpdir / 'test_abbrev_sha_like_tag_exist')
    check_invalid_commit(monkeykas, tdir, 'tag',
                         '8a82d22f123e', RepoRefError)


@pytest.mark.online
def test_abbreviated_commit(monkeykas, tmpdir, capsys):
    """
        Test if an abbreviated sha1 hash is correctly detected and flagged.
    """
    def test_abbreviated(format):
        tdir = str(tmpdir / format)
        os.mkdir(tdir)
        monkeykas.chdir(tdir)

        (rc, _) = run_cmd(['git', 'init', f'--object-format={format}', 'test'],
                          fail=False)
        assert rc == 0

        with open(f'{tdir}/test/oe-init-build-env', 'w') as f:
            f.write('/bin/true')

        (rc, _) = run_cmd(['git', 'add', 'oe-init-build-env'],
                          cwd='test', fail=False)
        assert rc == 0
        (rc, _) = run_cmd(['git', 'commit', '-m commit'],
                          cwd='test', fail=False)
        assert rc == 0
        (rc, commit) = run_cmd(['git', 'log', '--pretty=%H'],
                               cwd='test', fail=False)
        assert rc == 0

        len = 12 if format == 'sha1' else 40
        with open(f'{tdir}/abbreviated.yml', 'w') as f:
            f.write('header:\n')
            f.write('  version: 23\n')
            f.write('repos:\n')
            f.write('  checkout:\n')
            f.write(f'    url: file://{tdir}/test\n')
            f.write(f'    commit: {commit[:len]}\n')

        kas.kas(['checkout', 'abbreviated.yml'])
        assert capsys.readouterr().err.count(
            'is not a full-length hash for repo "checkout".') == 1

    test_abbreviated('sha1')

    test_abbreviated('sha256')
