File: test_yaml.py

package info (click to toggle)
ansible-core 2.19.0~beta6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,628 kB
  • sloc: python: 180,313; cs: 4,929; sh: 4,601; xml: 34; makefile: 21
file content (47 lines) | stat: -rw-r--r-- 1,262 bytes parent folder | download | duplicates (3)
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
from __future__ import annotations

import typing as t

import pytest

from ansible.module_utils.common.yaml import yaml_dump, yaml_dump_all
from ansible.module_utils._internal._datatag import Tripwire
from ansible.module_utils._internal._datatag._tags import Deprecated
from units.mock.custom_types import CustomMapping, CustomSequence

_test_tag = Deprecated(msg="test")


@pytest.mark.parametrize("value, expected", (
    (CustomMapping(dict(a=1)), "a: 1"),
    (CustomSequence([1]), "- 1"),
    (_test_tag.tag(dict(a=1)), "a: 1"),
    (_test_tag.tag([1]), "- 1"),
    (_test_tag.tag(1), "1"),
    (_test_tag.tag("Ansible"), "Ansible"),
))
def test_dump(value: t.Any, expected: str) -> None:
    """Verify supported types can be dumped."""
    result = yaml_dump(value).strip()

    assert result == expected

    result = yaml_dump_all([value]).strip()

    assert result == expected


def test_dump_tripwire() -> None:
    """Verify dumping a tripwire trips it."""
    class Tripped(Exception):
        pass

    class CustomTripwire(Tripwire):
        def trip(self) -> t.NoReturn:
            raise Tripped()

    with pytest.raises(Tripped):
        yaml_dump(CustomTripwire())

    with pytest.raises(Tripped):
        yaml_dump_all([CustomTripwire()])