Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"skipIf",
"GIT_REPO",
"GIT_DAEMON_PORT",
"xfail_if_raises",
]

import contextlib
Expand All @@ -35,8 +36,10 @@
import time
import unittest
import venv
from typing import Union, Type, Tuple

import gitdb
import pytest

from git.util import rmtree, cwd

Expand Down Expand Up @@ -465,3 +468,27 @@ def _executable(self, basename):
if osp.isfile(path) or osp.islink(path):
return path
raise RuntimeError(f"no regular file or symlink {path!r}")


@contextlib.contextmanager
def xfail_if_raises(
condition: bool,
*,
raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]],
reason: str = "",
strict: bool = False,
):
"""Approximates the behavior of @pytest.mark.xfail(..., raises=...) as a context
manager that can be used within a test, such as when the condition is complex or has
side effects

One difference is it will not report XPASS if the test passes, but setting `strict`
simulates it by raising an exception"""
try:
yield
except raises:
if condition:
pytest.xfail(reason)
raise
if strict and condition:
pytest.fail("[XPASS(strict)] " + reason)
Loading
Loading