verbose subprocess exception handle annd quite git

This commit is contained in:
2023-08-21 01:35:43 +03:00
parent 6530afbfc7
commit 598af7d9db
9 changed files with 137 additions and 41 deletions

View File

@ -56,9 +56,9 @@ def test_fetch_existing(remote_source: RemoteSource, mocker: MockerFixture) -> N
local = Path("local")
Sources.fetch(local, remote_source)
check_output_mock.assert_has_calls([
MockCall("git", "fetch", "origin", remote_source.branch, cwd=local, logger=pytest.helpers.anyvar(int)),
MockCall("git", "fetch", "--quiet", "origin", remote_source.branch, cwd=local, logger=pytest.helpers.anyvar(int)),
MockCall("git", "checkout", "--force", remote_source.branch, cwd=local, logger=pytest.helpers.anyvar(int)),
MockCall("git", "reset", "--hard", f"origin/{remote_source.branch}",
MockCall("git", "reset", "--quiet", "--hard", f"origin/{remote_source.branch}",
cwd=local, logger=pytest.helpers.anyvar(int)),
])
move_mock.assert_called_once_with(local.resolve(), local)
@ -75,10 +75,10 @@ def test_fetch_new(remote_source: RemoteSource, mocker: MockerFixture) -> None:
local = Path("local")
Sources.fetch(local, remote_source)
check_output_mock.assert_has_calls([
MockCall("git", "clone", "--branch", remote_source.branch, "--single-branch",
MockCall("git", "clone", "--quiet", "--branch", remote_source.branch, "--single-branch",
remote_source.git_url, str(local), cwd=local.parent, logger=pytest.helpers.anyvar(int)),
MockCall("git", "checkout", "--force", remote_source.branch, cwd=local, logger=pytest.helpers.anyvar(int)),
MockCall("git", "reset", "--hard", f"origin/{remote_source.branch}",
MockCall("git", "reset", "--quiet", "--hard", f"origin/{remote_source.branch}",
cwd=local, logger=pytest.helpers.anyvar(int))
])
move_mock.assert_called_once_with(local.resolve(), local)
@ -96,7 +96,7 @@ def test_fetch_new_without_remote(mocker: MockerFixture) -> None:
Sources.fetch(local, RemoteSource(source=PackageSource.Archive))
check_output_mock.assert_has_calls([
MockCall("git", "checkout", "--force", Sources.DEFAULT_BRANCH, cwd=local, logger=pytest.helpers.anyvar(int)),
MockCall("git", "reset", "--hard", f"origin/{Sources.DEFAULT_BRANCH}",
MockCall("git", "reset", "--quiet", "--hard", f"origin/{Sources.DEFAULT_BRANCH}",
cwd=local, logger=pytest.helpers.anyvar(int))
])
move_mock.assert_called_once_with(local.resolve(), local)
@ -144,7 +144,7 @@ def test_init(mocker: MockerFixture) -> None:
local = Path("local")
Sources.init(local)
check_output_mock.assert_called_once_with("git", "init", "--initial-branch", Sources.DEFAULT_BRANCH,
check_output_mock.assert_called_once_with("git", "init", "--quiet", "--initial-branch", Sources.DEFAULT_BRANCH,
cwd=local, logger=pytest.helpers.anyvar(int))
add_mock.assert_called_once_with(local, "PKGBUILD", ".SRCINFO", "local")
commit_mock.assert_called_once_with(local)
@ -241,7 +241,7 @@ def test_push(package_ahriman: Package, mocker: MockerFixture) -> None:
add_mock.assert_called_once_with(local, "glob")
commit_mock.assert_called_once_with(local, commit_author=commit_author)
check_output_mock.assert_called_once_with(
"git", "push", package_ahriman.remote.git_url, package_ahriman.remote.branch,
"git", "push", "--quiet", package_ahriman.remote.git_url, package_ahriman.remote.branch,
cwd=local, logger=pytest.helpers.anyvar(int))
@ -310,7 +310,7 @@ def test_commit(sources: Sources, mocker: MockerFixture) -> None:
user, email = sources.DEFAULT_COMMIT_AUTHOR
assert sources.commit(local, message=message)
check_output_mock.assert_called_once_with(
"git", "commit", "--message", message,
"git", "commit", "--quiet", "--message", message,
cwd=local, logger=pytest.helpers.anyvar(int), environment={
"GIT_AUTHOR_NAME": user,
"GIT_AUTHOR_EMAIL": email,
@ -343,7 +343,7 @@ def test_commit_author(sources: Sources, mocker: MockerFixture) -> None:
user, email = author = ("commit author", "user@host")
assert sources.commit(Path("local"), message=message, commit_author=author)
check_output_mock.assert_called_once_with(
"git", "commit", "--message", message,
"git", "commit", "--quiet", "--message", message,
cwd=local, logger=pytest.helpers.anyvar(int), environment={
"GIT_AUTHOR_NAME": user,
"GIT_AUTHOR_EMAIL": email,
@ -364,7 +364,7 @@ def test_commit_autogenerated_message(sources: Sources, mocker: MockerFixture) -
assert sources.commit(Path("local"))
user, email = sources.DEFAULT_COMMIT_AUTHOR
check_output_mock.assert_called_once_with(
"git", "commit", "--message", pytest.helpers.anyvar(str, strict=True),
"git", "commit", "--quiet", "--message", pytest.helpers.anyvar(str, strict=True),
cwd=local, logger=pytest.helpers.anyvar(int), environment={
"GIT_AUTHOR_NAME": user,
"GIT_AUTHOR_EMAIL": email,

View File

@ -0,0 +1,19 @@
from ahriman.core.exceptions import BuildError, CalledProcessError
def test_from_process() -> None:
"""
must correctly generate exception instance from subprocess
"""
instance = BuildError.from_process("ahriman")(0, [], "out", "err")
assert isinstance(instance, BuildError)
assert instance.args == ("Package ahriman build failed,\nprocess stderr:\nerr\ncheck logs for details",)
def test_str() -> None:
"""
must correctly transform CalledProcessError to string
"""
instance = CalledProcessError(1, ["cmd"], "error")
message = "Command '['cmd']' returned non-zero exit status 1.\nProcess stderr:\nerror"
assert str(instance) == message

View File

@ -3,14 +3,13 @@ import logging
import os
import pytest
import requests
import subprocess
from pathlib import Path
from pytest_mock import MockerFixture
from typing import Any
from unittest.mock import MagicMock, call as MockCall
from ahriman.core.exceptions import BuildError, OptionError, UnsafeRunError
from ahriman.core.exceptions import BuildError, CalledProcessError, OptionError, UnsafeRunError
from ahriman.core.util import check_output, check_user, dataclass_view, enum_values, exception_response_text, \
extract_user, filter_json, full_version, package_like, parse_version, partition, pretty_datetime, pretty_size, \
safe_filename, srcinfo_property, srcinfo_property_list, trim_package, utcnow, walk
@ -107,10 +106,10 @@ def test_check_output_failure(mocker: MockerFixture) -> None:
"""
mocker.patch("subprocess.Popen.wait", return_value=1)
with pytest.raises(subprocess.CalledProcessError):
with pytest.raises(CalledProcessError):
check_output("echo", "hello")
with pytest.raises(subprocess.CalledProcessError):
with pytest.raises(CalledProcessError):
check_output("echo", "hello", logger=logging.getLogger(""))
@ -128,6 +127,20 @@ def test_check_output_failure_exception(mocker: MockerFixture) -> None:
check_output("echo", "hello", exception=exception, logger=logging.getLogger(""))
def test_check_output_failure_exception_callable(mocker: MockerFixture) -> None:
"""
must raise exception from callable provided instead of default
"""
mocker.patch("subprocess.Popen.wait", return_value=1)
exception = BuildError.from_process("")
with pytest.raises(BuildError):
check_output("echo", "hello", exception=exception)
with pytest.raises(BuildError):
check_output("echo", "hello", exception=exception, logger=logging.getLogger(""))
def test_check_output_empty_line(mocker: MockerFixture) -> None:
"""
must correctly process empty lines in command output