File: test__init__.py

package info (click to toggle)
anta 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,048 kB
  • sloc: python: 48,164; sh: 28; javascript: 9; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 2,367 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Tests for anta.cli._main."""

from __future__ import annotations

import sys
from typing import Any
from unittest.mock import patch

import pytest


# 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, {"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()

        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

        # setting ANTA_DEBUG
        with pytest.raises(SystemExit) as e_info, patch("anta.cli.__DEBUG__", new=True):
            anta.cli.cli()

        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


# 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()