Debian Codegraph @

tests/units/cli/test__init__.py

raw symbols SCIP
1 # Copyright (c) 2023-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """Tests for anta.cli._main."""
5
6 from __future__ import annotations
7
8 import sys
9 from typing import Any
10 from unittest.mock import patch
11
12 import pytest
13
14
15 # https://github.com/python/cpython/issues/88852
16 @pytest.mark.skipif(sys.version_info <= (3, 11), reason="Unreliable behavior patching sys.modules before 3.11")
17 def test_cli_error_missing_click(capsys: pytest.CaptureFixture[Any]) -> None:
18 """Test ANTA errors out when anta[cli] was not installed."""
19 with patch.dict(sys.modules, {"click": None}) as sys_modules:
20 for k in list(sys_modules.keys()):
21 if k.startswith("anta."):
22 del sys_modules[k]
23 import anta.cli
24
25 with pytest.raises(SystemExit) as e_info:
26 anta.cli.cli()
27
28 captured = capsys.readouterr()
29 assert "The ANTA command line client could not run because the required dependencies were not installed." in captured.out
30 assert "Make sure you've installed everything with: pip install 'anta[cli]'" in captured.out
31 assert e_info.value.code == 1
32
33 # setting ANTA_DEBUG
34 with pytest.raises(SystemExit) as e_info, patch("anta.cli.__DEBUG__", new=True):
35 anta.cli.cli()
36
37 captured = capsys.readouterr()
38 assert "The ANTA command line client could not run because the required dependencies were not installed." in captured.out
39 assert "Make sure you've installed everything with: pip install 'anta[cli]'" in captured.out
40 assert "The caught exception was:" in captured.out
41 assert e_info.value.code == 1
42
43
44 # https://github.com/python/cpython/issues/88852
45 @pytest.mark.skipif(sys.version_info <= (3, 11), reason="Unreliable behavior patching sys.modules before 3.11")
46 def test_cli_error_missing_other() -> None:
47 """Test ANTA errors out when anta[cli] was not installed."""
48 with patch.dict(sys.modules, {"httpx": None}) as sys_modules:
49 # Need to clean up from previous runs a path that will trigger reimporting httpx
50 for k in list(sys_modules.keys()):
51 if k.startswith("anta."):
52 del sys_modules[k]
53 import anta.cli
54
55 with pytest.raises(ImportError, match="httpx"):
56 anta.cli.cli()