git_dag.test.test_cli

src/git_dag/test/test_cli.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""Test ``cli.py``."""

# pylint: disable=missing-function-docstring

from pathlib import Path
from typing import Any
from unittest.mock import patch

import pytest

from git_dag.cli import get_user_defined_cla, main
from git_dag.constants import CONFIG_FILE
from git_dag.parameters import ParamsPublic


def test_cli_main(git_repository_default: Path) -> None:
    repo_path = git_repository_default

    out_filename = "out.gv"
    _p = ("-p", str(repo_path))
    _i = ("-i", "main")
    _m = ("-m", "1")
    _f = ("-f", f"{repo_path / out_filename}")
    _log_level = ("--log-level", "INFO")

    main(
        [
            "-l",
            "-r",
            "-s",
            "-t",
            "-T",
            "-B",
            "-D",
            "-H",
            "-u",
            "--html",
            "--config-ignore",
            *_f,
            *_p,
            *_i,
            *_m,
            *_f,
            *_log_level,
        ]
    )

    assert (repo_path / out_filename).exists()
    assert (repo_path / f"{out_filename}.svg").exists()
    assert (repo_path / f"{out_filename}.svg.html").exists()


def test_cli_main_config_create(tmp_path: Path) -> None:
    config_file = tmp_path / CONFIG_FILE.name

    with patch("git_dag.parameters.CONFIG_FILE", config_file):
        main(["--config-create"])

    assert config_file.exists()


@pytest.mark.parametrize(
    "arg,field,value",
    [
        ("--init-refs", "init_refs", ["main", "topic"]),
        ("-R", "range_expr", "main..topic"),
        ("-p", "path", "/some/path"),
        ("-f", "file", "/some/path/git-dag.gv"),
        ("--format", "format", "png"),
        ("-n", "max_numb_commits", 10),
        ("-m", "commit_message_as_label", 1),
        ("--log-level", "log_level", "INFO"),
        # flags have value None
        ("-u", "show_unreachable_commits", None),
        ("-t", "show_tags", None),
        ("-D", "show_deleted_tags", None),
        ("-s", "show_stash", None),
        ("-H", "show_head", None),
        ("--pr", "show_prs_heads", None),
        ("-T", "show_trees", None),
        ("--trees-standalone", "show_trees_standalone", None),
        ("-B", "show_blobs", None),
        ("--blobs-standalone", "show_blobs_standalone", None),
        ("-o", "xdg_open", None),
        ("-l", "show_local_branches", None),
        ("-r", "show_remote_branches", None),
        ("--html", "html_embed_svg", None),
    ],
)
def test_cli_args(arg: str, field: str, value: Any) -> None:
    if value is None:
        arg_and_value = [arg]  # arg is a flag
    elif isinstance(value, list):
        arg_and_value = [arg] + value  # if value is a list it is a list of string
    else:
        arg_and_value = [arg, str(value)]

    # we need to pass [] to get_cla because of the way pytest sets sys.argv
    default_params = ParamsPublic(**get_user_defined_cla([])).model_dump()
    params_dict = ParamsPublic(**get_user_defined_cla(arg_and_value)).model_dump()

    assert params_dict[field] == (True if value is None else value)
    for default_field, default_value in default_params.items():
        if default_field != field:
            assert params_dict[default_field] == default_value