Adding upstream version 1.4.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
dc7df702ea
commit
7996c81031
166 changed files with 13787 additions and 11959 deletions
|
@ -6,35 +6,21 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from importlib import reload
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
import anta.cli
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from types import ModuleType
|
||||
|
||||
builtins_import = __import__
|
||||
|
||||
|
||||
# Tried to achieve this with mock
|
||||
# http://materials-scientist.com/blog/2021/02/11/mocking-failing-module-import-python/
|
||||
def import_mock(name: str, *args: Any) -> ModuleType: # noqa: ANN401
|
||||
"""Mock."""
|
||||
if name == "click":
|
||||
msg = "No module named 'click'"
|
||||
raise ModuleNotFoundError(msg)
|
||||
return builtins_import(name, *args)
|
||||
|
||||
|
||||
def test_cli_error_missing(capsys: pytest.CaptureFixture[Any]) -> None:
|
||||
# https://github.com/python/cpython/issues/88852
|
||||
@pytest.mark.skipif(sys.version_info <= (3, 11), reason="Unreliable behavior patching sys.modules before 3.11")
|
||||
def test_cli_error_missing_click(capsys: pytest.CaptureFixture[Any]) -> None:
|
||||
"""Test ANTA errors out when anta[cli] was not installed."""
|
||||
with patch.dict(sys.modules) as sys_modules, patch("builtins.__import__", import_mock):
|
||||
del sys_modules["anta.cli._main"]
|
||||
reload(anta.cli)
|
||||
with patch.dict(sys.modules, {"click": None}) as sys_modules:
|
||||
for k in list(sys_modules.keys()):
|
||||
if k.startswith("anta."):
|
||||
del sys_modules[k]
|
||||
import anta.cli
|
||||
|
||||
with pytest.raises(SystemExit) as e_info:
|
||||
anta.cli.cli()
|
||||
|
@ -53,3 +39,18 @@ def test_cli_error_missing(capsys: pytest.CaptureFixture[Any]) -> None:
|
|||
assert "Make sure you've installed everything with: pip install 'anta[cli]'" in captured.out
|
||||
assert "The caught exception was:" in captured.out
|
||||
assert e_info.value.code == 1
|
||||
|
||||
|
||||
# https://github.com/python/cpython/issues/88852
|
||||
@pytest.mark.skipif(sys.version_info <= (3, 11), reason="Unreliable behavior patching sys.modules before 3.11")
|
||||
def test_cli_error_missing_other() -> None:
|
||||
"""Test ANTA errors out when anta[cli] was not installed."""
|
||||
with patch.dict(sys.modules, {"httpx": None}) as sys_modules:
|
||||
# Need to clean up from previous runs a path that will trigger reimporting httpx
|
||||
for k in list(sys_modules.keys()):
|
||||
if k.startswith("anta."):
|
||||
del sys_modules[k]
|
||||
import anta.cli
|
||||
|
||||
with pytest.raises(ImportError, match="httpx"):
|
||||
anta.cli.cli()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue