2025-02-05 11:32:35 +01:00
|
|
|
# Copyright (c) 2023-2024 Arista Networks, Inc.
|
|
|
|
# Use of this source code is governed by the Apache License 2.0
|
|
|
|
# that can be found in the LICENSE file.
|
2025-02-05 11:39:50 +01:00
|
|
|
"""Tests for anta.cli._main."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
import sys
|
|
|
|
from importlib import reload
|
|
|
|
from typing import TYPE_CHECKING, Any
|
2025-02-05 11:39:09 +01:00
|
|
|
from unittest.mock import patch
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
import pytest
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
import anta.cli
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
if TYPE_CHECKING:
|
2025-02-05 11:39:50 +01:00
|
|
|
from types import ModuleType
|
2025-02-05 11:39:09 +01:00
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
builtins_import = __import__
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
# 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)
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
def test_cli_error_missing(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)
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
with pytest.raises(SystemExit) as e_info:
|
|
|
|
anta.cli.cli()
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert "The ANTA command line client could not run because the required dependencies were not installed." in captured.out
|
|
|
|
assert "Make sure you've installed everything with: pip install 'anta[cli]'" in captured.out
|
|
|
|
assert e_info.value.code == 1
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
# setting ANTA_DEBUG
|
|
|
|
with pytest.raises(SystemExit) as e_info, patch("anta.cli.__DEBUG__", new=True):
|
|
|
|
anta.cli.cli()
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert "The ANTA command line client could not run because the required dependencies were not installed." in captured.out
|
|
|
|
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
|