Skip to content

Commit

Permalink
test: abstract commits with GitMock
Browse files Browse the repository at this point in the history
Change-Id: Ibbe59fe3142f05896f792d19a45792c8195e043b
  • Loading branch information
DouglasBlackwood authored and jd committed Feb 26, 2024
1 parent debd210 commit f8ecfbd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
39 changes: 21 additions & 18 deletions mergify_cli/tests/test_mergify_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,22 @@ async def test_stack_create(
git_mock: test_utils.GitMock, respx_mock: respx.MockRouter
) -> None:
# Mock 2 commits on branch `current-branch`
git_mock.mock("merge-base --fork-point origin/main", "base_commit_sha")
git_mock.mock(
"log --format='%H' base_commit_sha..current-branch", "commit2_sha\ncommit1_sha"
)
git_mock.mock(
"log -1 --format='%b' commit1_sha",
"Message commit 1\n\nChange-Id: I29617d37762fd69809c255d7e7073cb11f8fbf50",
git_mock.commit(
test_utils.Commit(
sha="commit1_sha",
title="Title commit 1",
message="Message commit 1",
change_id="I29617d37762fd69809c255d7e7073cb11f8fbf50",
)
)
git_mock.mock("log -1 --format='%s' commit1_sha", "Title commit 1")
git_mock.mock(
"log -1 --format='%b' commit2_sha",
"Message commit 2\n\nChange-Id: I29617d37762fd69809c255d7e7073cb11f8fbf51",
git_mock.commit(
test_utils.Commit(
sha="commit2_sha",
title="Title commit 2",
message="Message commit 2",
change_id="I29617d37762fd69809c255d7e7073cb11f8fbf51",
)
)
git_mock.mock("log -1 --format='%s' commit2_sha", "Title commit 2")

# Mock HTTP calls
respx_mock.get("/repos/user/repo/git/matching-refs/heads//current-branch/").respond(
Expand Down Expand Up @@ -207,13 +209,14 @@ async def test_stack_update(
git_mock: test_utils.GitMock, respx_mock: respx.MockRouter
) -> None:
# Mock 1 commits on branch `current-branch`
git_mock.mock("merge-base --fork-point origin/main", "base_commit_sha")
git_mock.mock("log --format='%H' base_commit_sha..current-branch", "commit_sha")
git_mock.mock(
"log -1 --format='%b' commit_sha",
"Message\n\nChange-Id: I29617d37762fd69809c255d7e7073cb11f8fbf50",
git_mock.commit(
test_utils.Commit(
sha="commit_sha",
title="Title",
message="Message",
change_id="I29617d37762fd69809c255d7e7073cb11f8fbf50",
)
)
git_mock.mock("log -1 --format='%s' commit_sha", "Title")

# Mock HTTP calls: the stack already exists but it's out of date, it should
# be updated
Expand Down
29 changes: 28 additions & 1 deletion mergify_cli/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2021-2023 Mergify SAS
# Copyright © 2021-2024 Mergify SAS
#
# 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
Expand All @@ -13,11 +13,20 @@
# License for the specific language governing permissions and limitations
# under the License.
import dataclasses
import typing


class Commit(typing.TypedDict):
sha: str
title: str
message: str
change_id: str


@dataclasses.dataclass
class GitMock:
_mocked: dict[str, str] = dataclasses.field(init=False, default_factory=dict)
_commits: list[Commit] = dataclasses.field(init=False, default_factory=list)

def mock(self, command: str, output: str) -> None:
self._mocked[command] = output
Expand All @@ -27,3 +36,21 @@ async def __call__(self, args: str) -> str:
return self._mocked[args]

raise AssertionError(f"git_mock called with `{args}`, not mocked!")

def commit(self, commit: Commit) -> None:
self._commits.append(commit)

# Base commit SHA
self.mock("merge-base --fork-point origin/main", "base_commit_sha")
# Commit message
self.mock(
f"log -1 --format='%b' {commit['sha']}",
f"{commit['message']}\n\nChange-Id: {commit['change_id']}",
)
# Commit title
self.mock(f"log -1 --format='%s' {commit['sha']}", commit["title"])
# List of commit SHAs
self.mock(
"log --format='%H' base_commit_sha..current-branch",
"\n".join(c["sha"] for c in reversed(self._commits)),
)

0 comments on commit f8ecfbd

Please sign in to comment.