Skip to content

Commit

Permalink
test: adds unit tests for the set_git_provider method for EntrypointB…
Browse files Browse the repository at this point in the history
…ase (#118)

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 authored Dec 14, 2023
1 parent 4ca6958 commit f4609c0
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/trestlebot/entrypoints/test_entrypoint_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/python

# Copyright 2023 Red Hat, Inc.
#
# 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
#
# http://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.

"""Test for Entrypoint Base"""

import argparse
from io import StringIO
from typing import Optional
from unittest.mock import patch

import pytest

from trestlebot.entrypoints.entrypoint_base import (
EntrypointBase,
EntrypointInvalidArgException,
)
from trestlebot.github import GitHub
from trestlebot.gitlab import GitLab
from trestlebot.provider import GitProvider, GitProviderException


@patch.dict("os.environ", {"GITHUB_ACTIONS": "true"})
def test_set_git_provider_with_github() -> None:
"""Test set_git_provider function in Entrypoint Base for GitHub Actions"""
provider: Optional[GitProvider]
fake_token = StringIO("fake_token")
args = argparse.Namespace(target_branch="main", with_token=fake_token)
provider = EntrypointBase.set_git_provider(args=args)
assert isinstance(provider, GitHub)


@patch.dict(
"os.environ",
{
"GITHUB_ACTIONS": "false",
"GITLAB_CI": "true",
"CI_SERVER_PROTOCOL": "https",
"CI_SERVER_HOST": "test-gitlab.com",
},
)
def test_set_git_provider_with_gitlab() -> None:
"""Test set_git_provider function in Entrypoint Base for GitLab CI"""
provider: Optional[GitProvider]
fake_token = StringIO("fake_token")
args = argparse.Namespace(target_branch="main", with_token=fake_token)
provider = EntrypointBase.set_git_provider(args=args)
assert isinstance(provider, GitLab)


@patch.dict("os.environ", {"GITHUB_ACTIONS": "false", "GITLAB_CI": "true"})
def test_set_git_provider_with_gitlab_with_failure() -> None:
"""Trigger error with GitLab provider with insufficient environment variables"""
fake_token = StringIO("fake_token")
args = argparse.Namespace(target_branch="main", with_token=fake_token)
with pytest.raises(
GitProviderException,
match="Set CI_SERVER_PROTOCOL and CI SERVER HOST environment variables",
):
EntrypointBase.set_git_provider(args=args)


@patch.dict("os.environ", {"GITHUB_ACTIONS": "false"})
def test_set_git_provider_with_none() -> None:
"""Test set_git_provider function when no git provider is set"""
fake_token = StringIO("fake_token")
provider: Optional[GitProvider]
args = argparse.Namespace(target_branch="main", with_token=fake_token)

with pytest.raises(
EntrypointInvalidArgException,
match="Invalid args --target-branch: "
"target-branch flag is set with an unset git provider. To test locally, set the "
"GITHUB_ACTIONS or GITLAB_CI environment variable.",
):
EntrypointBase.set_git_provider(args=args)

# Now test with no target branch which is a valid case
args = argparse.Namespace(target_branch=None, with_token=None)
provider = EntrypointBase.set_git_provider(args=args)
assert provider is None

0 comments on commit f4609c0

Please sign in to comment.