-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moves auto-detect logic to entrypoint base
All reusable input/output logic should be defined in entrypoint_base.py for easier management and readability Signed-off-by: Jennifer Power <[email protected]>
- Loading branch information
Showing
4 changed files
with
118 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,12 @@ | |
|
||
import argparse | ||
from io import StringIO | ||
from typing import Optional | ||
from typing import Dict, Optional | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from tests.testutils import args_dict_to_list | ||
from trestlebot.entrypoints.entrypoint_base import ( | ||
EntrypointBase, | ||
EntrypointInvalidArgException, | ||
|
@@ -20,35 +21,46 @@ | |
from trestlebot.provider import GitProvider, GitProviderException | ||
|
||
|
||
@pytest.fixture | ||
def valid_args_dict() -> Dict[str, str]: | ||
return { | ||
"branch": "main", | ||
"committer-name": "test", | ||
"committer-email": "[email protected]", | ||
"working-dir": ".", | ||
"file-patterns": ".", | ||
"target-branch": "main", | ||
} | ||
|
||
|
||
def setup_base_cli() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser(description="Test parser") | ||
EntrypointBase(parser=parser) | ||
return parser.parse_args() | ||
|
||
|
||
@patch.dict("os.environ", {"GITHUB_ACTIONS": "true"}) | ||
def test_set_git_provider_with_github() -> None: | ||
def test_base_cli_with_github(valid_args_dict: Dict[str, str]) -> None: | ||
"""Test set_git_provider function in Entrypoint Base for GitHub Actions""" | ||
provider: Optional[GitProvider] | ||
args = argparse.Namespace( | ||
target_branch="main", | ||
with_token=StringIO("fake_token"), | ||
git_provider_type="", | ||
git_server_url="", | ||
) | ||
provider = EntrypointBase.set_git_provider(args=args) | ||
assert isinstance(provider, GitHub) | ||
with patch("sys.argv", ["trestlebot", *args_dict_to_list(valid_args_dict)]): | ||
args = setup_base_cli() | ||
vars(args)["with_token"] = StringIO("fake_token") | ||
provider = EntrypointBase.set_git_provider(args=args) | ||
assert isinstance(provider, GitHub) | ||
|
||
|
||
@patch.dict( | ||
"os.environ", | ||
{"GITHUB_ACTIONS": "true", "TRESTLEBOT_REPO_ACCESS_TOKEN": "fake_token"}, | ||
) | ||
def test_set_git_provider_with_github_no_stdin() -> None: | ||
def test_base_cli_with_github_no_stdin(valid_args_dict: Dict[str, str]) -> None: | ||
"""Test set_git_provider function in Entrypoint Base for GitHub Actions""" | ||
provider: Optional[GitProvider] | ||
args = argparse.Namespace( | ||
target_branch="main", | ||
with_token=None, | ||
git_provider_type="", | ||
git_server_url="", | ||
) | ||
provider = EntrypointBase.set_git_provider(args=args) | ||
assert isinstance(provider, GitHub) | ||
with patch("sys.argv", ["trestlebot", *args_dict_to_list(valid_args_dict)]): | ||
args = setup_base_cli() | ||
provider = EntrypointBase.set_git_provider(args=args) | ||
assert isinstance(provider, GitHub) | ||
|
||
|
||
@patch.dict( | ||
|
@@ -60,33 +72,38 @@ def test_set_git_provider_with_github_no_stdin() -> None: | |
"CI_SERVER_HOST": "test-gitlab.com", | ||
}, | ||
) | ||
def test_set_git_provider_with_gitlab() -> None: | ||
def test_base_cli_with_gitlab(valid_args_dict: Dict[str, str]) -> None: | ||
"""Test set_git_provider function in Entrypoint Base for GitLab CI""" | ||
provider: Optional[GitProvider] | ||
args = argparse.Namespace( | ||
target_branch="main", | ||
with_token=StringIO("fake_token"), | ||
git_provider_type="", | ||
git_server_url="", | ||
) | ||
provider = EntrypointBase.set_git_provider(args=args) | ||
assert isinstance(provider, GitLab) | ||
with patch("sys.argv", ["trestlebot", *args_dict_to_list(valid_args_dict)]): | ||
args = setup_base_cli() | ||
vars(args)["with_token"] = StringIO("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: | ||
def test_base_cli_with_gitlab_with_failure(valid_args_dict: Dict[str, str]) -> None: | ||
"""Trigger error with GitLab provider with insufficient environment variables""" | ||
args = argparse.Namespace( | ||
target_branch="main", | ||
with_token=StringIO("fake_token"), | ||
git_provider_type="", | ||
git_server_url="", | ||
) | ||
with pytest.raises( | ||
GitProviderException, | ||
match="Set CI_SERVER_PROTOCOL and CI SERVER HOST environment variables", | ||
): | ||
EntrypointBase.set_git_provider(args=args) | ||
with patch("sys.argv", ["trestlebot", *args_dict_to_list(valid_args_dict)]): | ||
with pytest.raises( | ||
GitProviderException, | ||
match="Set CI_SERVER_PROTOCOL and CI SERVER HOST environment variables", | ||
): | ||
setup_base_cli() | ||
|
||
|
||
@patch.dict("os.environ", {"GITHUB_ACTIONS": "false", "GITLAB_CI": "false"}) | ||
def test_base_cli_with_provide_type_set(valid_args_dict: Dict[str, str]) -> None: | ||
"""Trigger error with GitLab provider with insufficient environment variables""" | ||
args_dict = valid_args_dict | ||
args_dict["git-provider-type"] = "gitlab" | ||
args_dict["git-server-url"] = "https://mygitlab.com" | ||
with patch("sys.argv", ["trestlebot", *args_dict_to_list(valid_args_dict)]): | ||
args = setup_base_cli() | ||
vars(args)["with_token"] = StringIO("fake_token") | ||
provider = EntrypointBase.set_git_provider(args=args) | ||
assert isinstance(provider, GitLab) | ||
|
||
|
||
@patch.dict("os.environ", {"GITHUB_ACTIONS": "false"}) | ||
|
@@ -103,7 +120,7 @@ def test_set_git_provider_with_none() -> None: | |
with pytest.raises( | ||
EntrypointInvalidArgException, | ||
match="Invalid args --target-branch, --git-provider-type: " | ||
"Could not detect Git provider from environment or inputs", | ||
"Could not determine Git provider from inputs", | ||
): | ||
EntrypointBase.set_git_provider(args=args) | ||
|
||
|
@@ -156,6 +173,19 @@ def test_set_provider_with_input() -> None: | |
) | ||
with pytest.raises( | ||
EntrypointInvalidArgException, | ||
match="Invalid args --server-url: GitHub provider does not support custom server URLs", | ||
match="Invalid args --git-server-url: GitHub provider does not support custom server URLs", | ||
): | ||
EntrypointBase.set_git_provider(args=args) | ||
|
||
args = argparse.Namespace( | ||
target_branch="main", | ||
with_token=StringIO("fake_token"), | ||
git_provider_type="", | ||
git_server_url="https://github.com", | ||
) | ||
with pytest.raises( | ||
EntrypointInvalidArgException, | ||
match="Invalid args --git-provider-type: git-provider-type must be set when using " | ||
"git-server-url", | ||
): | ||
EntrypointBase.set_git_provider(args=args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,3 +51,4 @@ | |
# Git Provider Types | ||
GITHUB = "github" | ||
GITLAB = "gitlab" | ||
GITHUB_SERVER_URL = "https://github.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters