File: test_prompts.py

package info (click to toggle)
zabbix-cli 3.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,860 kB
  • sloc: python: 18,557; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,275 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
57
58
59
60
61
62
63
64
from __future__ import annotations

import os

import pytest
from zabbix_cli.output.prompts import HEADLESS_VARS_SET
from zabbix_cli.output.prompts import TRUE_ARGS
from zabbix_cli.output.prompts import is_headless


@pytest.mark.parametrize("envvar", HEADLESS_VARS_SET)
@pytest.mark.parametrize("value", TRUE_ARGS)
def test_is_headless_set_true(envvar: str, value: str):
    """Returns True when the envvar is set to a truthy value."""
    _do_test_is_headless(envvar, value, True)


@pytest.mark.parametrize("envvar", HEADLESS_VARS_SET)
@pytest.mark.parametrize("value", ["0", "false", "", None])
def test_is_headless_set_false(envvar: str, value: str):
    """Returns False when the envvar is set to a falsey value or unset"""
    _do_test_is_headless(envvar, value, False)


@pytest.mark.parametrize(
    "envvar, value, expected",
    [
        ("DEBIAN_FRONTEND", "noninteractive", True),
        ("DEBIAN_FRONTEND", "teletype", False),
        ("DEBIAN_FRONTEND", "readline", False),
        ("DEBIAN_FRONTEND", "dialog", False),
        ("DEBIAN_FRONTEND", "gtk", False),
        ("DEBIAN_FRONTEND", "text", False),
        ("DEBIAN_FRONTEND", "anything", False),
        ("DEBIAN_FRONTEND", None, False),
        ("DEBIAN_FRONTEND", "", False),
        ("DEBIAN_FRONTEND", "0", False),
        ("DEBIAN_FRONTEND", "false", False),
        ("DEBIAN_FRONTEND", "1", False),
        ("DEBIAN_FRONTEND", "true", False),
    ],
)
def test_is_headless_map(envvar: str, value: str, expected: bool) -> None:
    """Returns True when the envvar is set to a specific value."""
    _do_test_is_headless(envvar, value, expected)


def _do_test_is_headless(envvar: str, value: str | None, expected: bool):
    """Helper function for testing is_headless.

    Sets/clears envvar before testing, then clears cache and envvar after test.
    """
    _orig_environ = os.environ.copy()
    os.environ.clear()
    try:
        if value is None:
            os.environ.pop(envvar, None)
        else:
            os.environ[envvar] = value
        assert is_headless() == expected
    finally:
        # IMPORTANT: Remove envvar and clear cache after each test
        os.environ = _orig_environ  # type: ignore  # noqa: B003 # I _think_ this is fine?
        is_headless.cache_clear()