# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

from __future__ import annotations

from PB.recipe_modules.pigweed.bazel_roll.tests.full import InputProperties
from recipe_engine import post_process

DEPS = [
    "pigweed/bazel_roll",
    "pigweed/checkout",
    "pigweed/roll_util",
    "recipe_engine/properties",
    "recipe_engine/step",
]

PROPERTIES = InputProperties


def RunSteps(  # pylint: disable=invalid-name
    api: recipe_api.RecipeScriptApi,
    props: InputProperties,
):
    checkout = api.checkout.fake_context()

    assert len(props.git_repositories) == 1

    rolls = api.bazel_roll.update_git_repository(
        checkout=checkout,
        git_repository=props.git_repositories[0],
    )

    if not rolls:
        with api.step.nest('nothing to roll, exiting'):
            return


def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]:
    """Create tests."""

    def _url(x: str = 'pigweed/pigweed'):
        assert ':' not in x
        return f'https://pigweed.googlesource.com/{x}'

    def trigger(url, **kwargs):
        return api.checkout.ci_test_data(git_repo=_url(url), **kwargs)

    def properties(repo: GitRepository, **kwargs):
        props = InputProperties(**kwargs)
        props.git_repositories.append(repo)
        return api.properties(props)

    def commit_data(name, **kwargs):
        return api.roll_util.commit_data(
            name,
            api.roll_util.commit('a' * 40, 'foo\nbar\n\nChange-Id: I1111'),
            **kwargs,
        )

    yield api.test(
        'success',
        properties(api.bazel_roll.git_repo()),
        api.roll_util.properties(commit_divider='--divider--'),
        trigger('pigweed/pigweed'),
        api.roll_util.forward_roll(),
        commit_data('pigweed', prefix=''),
    )

    yield api.test(
        'unrecognized-remote',
        properties(api.bazel_roll.git_repo(remote=_url('unrecognized-remote'))),
        api.roll_util.properties(commit_divider='--divider--'),
        trigger('unrecognized-remote'),
        api.post_process(post_process.MustRun, 'failed to update commit hash'),
        status='FAILURE',
    )

    yield api.test(
        'bad-trigger',
        properties(
            api.bazel_roll.git_repo(
                remote=_url('foo'),
                workspace_path='bar/WORKSPACE',
            ),
        ),
        trigger('bar'),
        status='FAILURE',
    )

    yield api.test(
        'name-not-found',
        properties(api.bazel_roll.git_repo(remote=_url('third/repo'))),
        trigger('third/repo'),
        api.roll_util.forward_roll(),
        api.post_process(post_process.MustRunRE, r'could not find name.*'),
        api.post_process(post_process.DropExpectation),
        status='FAILURE',
    )

    yield api.test(
        'no-trigger',
        properties(api.bazel_roll.git_repo()),
        api.roll_util.forward_roll(),
        commit_data('pigweed', prefix=''),
    )

    yield api.test(
        'backwards',
        properties(api.bazel_roll.git_repo()),
        api.roll_util.backward_roll(),
    )
