mirror of
https://gerrit.googlesource.com/git-repo
synced 2026-11-13 15:57:35 +00:00
Align with Google style guides and `import git_command` as a module rather than import symbols out of it. Change-Id: If80d95203e240dd6591e465ef75500c72401d2bf Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/648503 Commit-Queue: Mike Frysinger <vapier@google.com> Reviewed-by: Gavin Mak <gavinmak@google.com> Tested-by: Mike Frysinger <vapier@google.com>
86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
# Copyright (C) 2026 The Android Open Source Project
|
|
#
|
|
# 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.
|
|
|
|
"""Unittests for subcmds/cherry_pick.py."""
|
|
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
|
|
import error
|
|
import git_command
|
|
from subcmds import cherry_pick
|
|
|
|
|
|
def test_resolve_reference_uses_one_typed_batch_request(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
oid = "1" * 40
|
|
commit = "tree " + "2" * 40 + "\n\nSubject 🚀\n\nBody\n"
|
|
output = oid + " commit " + str(len(commit.encode("utf-8"))) + "\n"
|
|
output += commit + "\n"
|
|
command = mock.create_autospec(git_command.GitCommand, instance=True)
|
|
command.stdout = output
|
|
command.stderr = ""
|
|
command.Wait.return_value = 0
|
|
run_git = mock.create_autospec(git_command.GitCommand, return_value=command)
|
|
monkeypatch.setattr(git_command, "GitCommand", run_git)
|
|
|
|
resolved, contents = cherry_pick.CherryPick()._ResolveReference("topic")
|
|
|
|
assert resolved == oid
|
|
assert contents == commit
|
|
run_git.assert_called_once_with(
|
|
None,
|
|
["cat-file", "--batch"],
|
|
input="topic^{commit}\n",
|
|
capture_stdout=True,
|
|
capture_stderr=True,
|
|
verify_command=True,
|
|
)
|
|
|
|
|
|
def test_resolve_reference_rejects_missing_object(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
command = mock.create_autospec(git_command.GitCommand, instance=True)
|
|
command.stdout = "topic^{commit} missing\n"
|
|
command.stderr = ""
|
|
command.Wait.return_value = 0
|
|
monkeypatch.setattr(
|
|
git_command,
|
|
"GitCommand",
|
|
mock.create_autospec(git_command.GitCommand, return_value=command),
|
|
)
|
|
|
|
with pytest.raises(error.GitError, match="commit topic not found"):
|
|
cherry_pick.CherryPick()._ResolveReference("topic")
|
|
|
|
|
|
def test_resolve_reference_rejects_ambiguous_object(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
command = mock.create_autospec(git_command.GitCommand, instance=True)
|
|
command.stdout = "topic^{commit} ambiguous\n"
|
|
command.stderr = ""
|
|
command.Wait.return_value = 0
|
|
monkeypatch.setattr(
|
|
git_command,
|
|
"GitCommand",
|
|
mock.create_autospec(git_command.GitCommand, return_value=command),
|
|
)
|
|
|
|
with pytest.raises(error.GitError, match="commit topic not found"):
|
|
cherry_pick.CherryPick()._ResolveReference("topic")
|