2025-03-17 07:33:45 +01:00
|
|
|
# Copyright (c) 2023-2025 Arista Networks, Inc.
|
2025-02-05 11:32:35 +01:00
|
|
|
# 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:42 +01:00
|
|
|
"""Tests for anta.cli._main."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-05 11:39:42 +01:00
|
|
|
import sys
|
2025-05-15 09:34:27 +02:00
|
|
|
from typing import Any
|
2025-02-05 11:38:32 +01:00
|
|
|
from unittest.mock import patch
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
import pytest
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-05-15 09:34:27 +02:00
|
|
|
# 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:
|
2025-02-05 11:39:42 +01:00
|
|
|
"""Test ANTA errors out when anta[cli] was not installed."""
|
2025-05-15 09:34:27 +02:00
|
|
|
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
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:39:42 +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:42 +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:42 +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:42 +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
|
2025-05-15 09:34:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
# 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()
|