summaryrefslogtreecommitdiffstats
path: root/test/test_formatter_base.py
blob: 462fc625bdbc55ee650f3afaa26c052595c639b6 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Tests related to base formatter."""

from __future__ import annotations

from pathlib import Path
from typing import Any

import pytest

from ansiblelint.formatters import BaseFormatter


@pytest.mark.parametrize(
    ("base_dir", "relative_path"),
    (
        pytest.param(None, True, id="0"),
        pytest.param("/whatever", False, id="1"),
        pytest.param(Path("/whatever"), False, id="2"),
    ),
)
@pytest.mark.parametrize(
    "path",
    (
        pytest.param("/whatever/string", id="a"),
        pytest.param(Path("/whatever/string"), id="b"),
    ),
)
def test_base_formatter_when_base_dir(
    base_dir: Any,
    relative_path: bool,
    path: str,
) -> None:
    """Check that base formatter accepts relative pathlib and str."""
    # Given
    base_formatter = BaseFormatter(base_dir, relative_path)  # type: ignore[var-annotated]

    # When
    output_path = base_formatter._format_path(  # noqa: SLF001
        path,
    )

    # Then
    assert isinstance(output_path, str | Path)
    assert base_formatter.base_dir is None or isinstance(
        base_formatter.base_dir,
        str | Path,
    )
    assert output_path == path


@pytest.mark.parametrize(
    "base_dir",
    (
        pytest.param(Path("/whatever"), id="0"),
        pytest.param("/whatever", id="1"),
    ),
)
@pytest.mark.parametrize(
    "path",
    (
        pytest.param("/whatever/string", id="a"),
        pytest.param(Path("/whatever/string"), id="b"),
    ),
)
def test_base_formatter_when_base_dir_is_given_and_relative_is_true(
    path: str | Path,
    base_dir: str | Path,
) -> None:
    """Check that the base formatter equally accepts pathlib and str."""
    # Given
    base_formatter = BaseFormatter(base_dir, True)  # type: ignore[var-annotated]

    # When
    output_path = base_formatter._format_path(path)  # noqa: SLF001

    # Then
    assert isinstance(output_path, str | Path)
    assert isinstance(base_formatter.base_dir, str | Path)
    assert output_path == Path(path).name


# vim: et:sw=4:syntax=python:ts=4: