Debian Codegraph @

tests/units/cli/test_main.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 from typing import TYPE_CHECKING
9 from unittest.mock import patch
10
11 import pytest
12
13 from anta.cli._main import anta, cli
14 from anta.cli.utils import ExitCode
15
16 if TYPE_CHECKING:
17 from click.testing import CliRunner
18
19
20 def test_anta(click_runner: CliRunner) -> None:
21 """Test anta main entrypoint."""
22 result = click_runner.invoke(anta)
23 assert result.exit_code == ExitCode.USAGE_ERROR
24 assert "Usage" in result.output
25
26
27 def test_anta_help(click_runner: CliRunner) -> None:
28 """Test anta --help."""
29 result = click_runner.invoke(anta, ["--help"])
30 assert result.exit_code == ExitCode.OK
31 assert "Usage" in result.output
32
33
34 def test_anta_exec_help(click_runner: CliRunner) -> None:
35 """Test anta exec --help."""
36 result = click_runner.invoke(anta, ["exec", "--help"])
37 assert result.exit_code == ExitCode.OK
38 assert "Usage: anta exec" in result.output
39
40
41 def test_anta_debug_help(click_runner: CliRunner) -> None:
42 """Test anta debug --help."""
43 result = click_runner.invoke(anta, ["debug", "--help"])
44 assert result.exit_code == ExitCode.OK
45 assert "Usage: anta debug" in result.output
46
47
48 def test_anta_get_help(click_runner: CliRunner) -> None:
49 """Test anta get --help."""
50 result = click_runner.invoke(anta, ["get", "--help"])
51 assert result.exit_code == ExitCode.OK
52 assert "Usage: anta get" in result.output
53
54
55 def test_uncaught_failure_anta(caplog: pytest.LogCaptureFixture) -> None:
56 """Test uncaught failure when running ANTA cli."""
57 with (
58 pytest.raises(SystemExit) as e_info,
59 patch("anta.cli._main.anta", side_effect=ZeroDivisionError()),
60 ):
61 cli()
62 assert "CRITICAL" in caplog.text
63 assert "Uncaught Exception when running ANTA CLI" in caplog.text
64 assert e_info.value.code == 1