Skip to content

Commit

Permalink
Add CI workflow for running unittests. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZolotukhinM authored Feb 24, 2025
1 parent dda58b1 commit 4e64a48
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/check_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Check Tests
on: [ pull_request ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x' # Specify the Python version you want to use
cache: pip
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Run tests
run: pytest tests/

13 changes: 13 additions & 0 deletions src/stack_pr/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class GitError(Exception):
pass


username_override = None


def override_username(username: str):
"""Override username for testing purposes. Call with None to reset."""
global username_override
username_override = username


def fetch_checkout_commit(
repo_dir: Path, ref: str, quiet: bool, remote: str = "origin"
):
Expand Down Expand Up @@ -182,12 +191,16 @@ def check_gh_installed():
def get_gh_username() -> str:
"""Return the current github username.
If username_override is set, it will be used instead of the actual username.
Returns:
Current github username as a string.
Raises:
GitError: if called outside a git repo, or.
"""
if username_override is not None:
return username_override

user_query = get_command_output(
[
Expand Down
41 changes: 24 additions & 17 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

sys.path.append(str(Path(__file__).parent.parent / "src"))

from stack_pr.git import override_username
from stack_pr.cli import (
get_branch_id,
generate_branch_name,
Expand All @@ -16,6 +17,7 @@

@pytest.fixture(scope="module")
def username():
override_username("TestBot")
return get_gh_username()


Expand Down Expand Up @@ -51,33 +53,38 @@ def test_generate_branch_name():


def test_get_taken_branch_ids():
template = "User/stack/$ID"
template = "$USERNAME/stack/$ID"
refs = [
"refs/remotes/origin/User/stack/104",
"refs/remotes/origin/User/stack/105",
"refs/remotes/origin/User/stack/134",
"refs/remotes/origin/TestBot/stack/104",
"refs/remotes/origin/TestBot/stack/105",
"refs/remotes/origin/TestBot/stack/134",
]
assert get_taken_branch_ids(refs, template) == [104, 105, 134]
refs = ["User/stack/104", "User/stack/105", "User/stack/134"]
refs = ["TestBot/stack/104", "TestBot/stack/105", "TestBot/stack/134"]
assert get_taken_branch_ids(refs, template) == [104, 105, 134]
refs = ["User/stack/104", "AAAA/stack/105", "User/stack/134", "User/stack/bbb"]
refs = [
"TestBot/stack/104",
"AAAA/stack/105",
"TestBot/stack/134",
"TestBot/stack/bbb",
]
assert get_taken_branch_ids(refs, template) == [104, 134]


def test_generate_available_branch_name():
template = "User/stack/$ID"
template = "$USERNAME/stack/$ID"
refs = [
"refs/remotes/origin/User/stack/104",
"refs/remotes/origin/User/stack/105",
"refs/remotes/origin/User/stack/134",
"refs/remotes/origin/TestBot/stack/104",
"refs/remotes/origin/TestBot/stack/105",
"refs/remotes/origin/TestBot/stack/134",
]
assert generate_available_branch_name(refs, template) == "User/stack/135"
assert generate_available_branch_name(refs, template) == "TestBot/stack/135"
refs = []
assert generate_available_branch_name(refs, template) == "User/stack/1"
template = "User-stack-$ID"
assert generate_available_branch_name(refs, template) == "TestBot/stack/1"
template = "$USERNAME-stack-$ID"
refs = [
"refs/remotes/origin/User-stack-104",
"refs/remotes/origin/User-stack-105",
"refs/remotes/origin/User-stack-134",
"refs/remotes/origin/TestBot-stack-104",
"refs/remotes/origin/TestBot-stack-105",
"refs/remotes/origin/TestBot-stack-134",
]
assert generate_available_branch_name(refs, template) == "User-stack-135"
assert generate_available_branch_name(refs, template) == "TestBot-stack-135"

0 comments on commit 4e64a48

Please sign in to comment.