File: test_cli.py

package info (click to toggle)
tmuxp 1.62.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: python: 7,989; makefile: 202; sh: 21
file content (175 lines) | stat: -rw-r--r-- 5,278 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""CLI tests for tmuxp's core shell functionality."""

from __future__ import annotations

import argparse
import contextlib
import pathlib
import typing as t

import libtmux
import pytest

from tests.fixtures import utils as test_utils
from tmuxp import cli
from tmuxp._internal.config_reader import ConfigReader
from tmuxp.cli.import_config import get_teamocil_dir, get_tmuxinator_dir
from tmuxp.cli.load import _reattach, load_plugins
from tmuxp.cli.utils import tmuxp_echo
from tmuxp.workspace import loader
from tmuxp.workspace.builder import WorkspaceBuilder
from tmuxp.workspace.finders import find_workspace_file

if t.TYPE_CHECKING:
    import _pytest.capture
    from libtmux.server import Server


def test_creates_config_dir_not_exists(tmp_path: pathlib.Path) -> None:
    """cli.startup() creates config dir if not exists."""
    cli.startup(tmp_path)
    assert tmp_path.exists()


class HelpTestFixture(t.NamedTuple):
    """Test fixture for help command tests."""

    test_id: str
    cli_args: list[str]


HELP_TEST_FIXTURES: list[HelpTestFixture] = [
    HelpTestFixture(
        test_id="help_long_flag",
        cli_args=["--help"],
    ),
    HelpTestFixture(
        test_id="help_short_flag",
        cli_args=["-h"],
    ),
]


@pytest.mark.parametrize(
    list(HelpTestFixture._fields),
    HELP_TEST_FIXTURES,
    ids=[test.test_id for test in HELP_TEST_FIXTURES],
)
def test_help(
    test_id: str,
    cli_args: list[str],
    tmp_path: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Test tmuxp --help / -h."""
    # In scrunched terminals, prevent width causing differentiation in result.out.
    monkeypatch.setenv("COLUMNS", "100")
    monkeypatch.setenv("LINES", "100")

    with contextlib.suppress(SystemExit):
        cli.cli(cli_args)

    result = capsys.readouterr()

    assert "usage: tmuxp [-h] [--version] [--log-level log-level]" in result.out


def test_resolve_behavior(
    tmp_path: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test resolution of file paths."""
    expect = tmp_path
    monkeypatch.chdir(tmp_path)
    assert pathlib.Path("../").resolve() == expect.parent
    assert pathlib.Path.cwd() == expect
    assert pathlib.Path("./").resolve() == expect
    assert pathlib.Path(expect).resolve() == expect


def test_get_tmuxinator_dir(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test get_tmuxinator_dir() helper function."""
    assert get_tmuxinator_dir() == pathlib.Path("~/.tmuxinator").expanduser()

    monkeypatch.setenv("HOME", "/moo")
    assert get_tmuxinator_dir() == pathlib.Path("/moo/.tmuxinator/")
    assert str(get_tmuxinator_dir()) == "/moo/.tmuxinator"
    assert get_tmuxinator_dir() == pathlib.Path("~/.tmuxinator/").expanduser()


def test_get_teamocil_dir(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Test get_teamocil_dir() helper function."""
    assert get_teamocil_dir() == pathlib.Path("~/.teamocil/").expanduser()

    monkeypatch.setenv("HOME", "/moo")
    assert get_teamocil_dir() == pathlib.Path("/moo/.teamocil/")
    assert str(get_teamocil_dir()) == "/moo/.teamocil"
    assert get_teamocil_dir() == pathlib.Path("~/.teamocil/").expanduser()


def test_pass_config_dir_argparse(
    tmp_path: pathlib.Path,
    monkeypatch: pytest.MonkeyPatch,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Test workspace configurations can be detected via directory."""
    configdir = tmp_path / "myconfigdir"
    configdir.mkdir()
    user_config_name = "myconfig"
    user_config = configdir / f"{user_config_name}.yaml"
    user_config.touch()

    expect = str(user_config)

    parser = argparse.ArgumentParser()
    parser.add_argument("workspace_file", type=str)

    def config_cmd(workspace_file: str) -> None:
        tmuxp_echo(find_workspace_file(workspace_file, workspace_dir=configdir))

    def check_cmd(config_arg: str) -> _pytest.capture.CaptureResult[str]:
        args = parser.parse_args([config_arg])
        config_cmd(workspace_file=args.workspace_file)
        return capsys.readouterr()

    monkeypatch.chdir(configdir)

    assert expect in check_cmd("myconfig").out
    assert expect in check_cmd("myconfig.yaml").out
    assert expect in check_cmd("./myconfig.yaml").out
    assert str(user_config) in check_cmd(str(configdir / "myconfig.yaml")).out

    with pytest.raises(FileNotFoundError):
        assert "FileNotFoundError" in check_cmd(".tmuxp.json").out


def test_reattach_plugins(
    monkeypatch_plugin_test_packages: None,
    server: Server,
) -> None:
    """Test reattach plugin hook."""
    config_plugins = test_utils.read_workspace_file("workspace/builder/plugin_r.yaml")

    session_config = ConfigReader._load(fmt="yaml", content=config_plugins)
    session_config = loader.expand(session_config)

    # open it detached
    builder = WorkspaceBuilder(
        session_config=session_config,
        plugins=load_plugins(session_config),
        server=server,
    )
    builder.build()

    with contextlib.suppress(libtmux.exc.LibTmuxException):
        _reattach(builder)

    assert builder.session is not None
    proc = builder.session.cmd("display-message", "-p", "'#S'")

    assert proc.stdout[0] == "'plugin_test_r'"