File: test_freezer.py

package info (click to toggle)
tmuxp 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,688 kB
  • sloc: python: 8,049; makefile: 202; sh: 14
file content (108 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download
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
"""Tests tmux session freezing functionality for tmuxp."""

from __future__ import annotations

import time
import typing

from tests.fixtures import utils as test_utils
from tmuxp._internal.config_reader import ConfigReader
from tmuxp.workspace import freezer, validation
from tmuxp.workspace.builder import WorkspaceBuilder

if typing.TYPE_CHECKING:
    import pathlib

    from libtmux.session import Session

    from tests.fixtures.structures import WorkspaceTestData


def test_freeze_config(session: Session) -> None:
    """Test freezing a tmux session."""
    session_config = ConfigReader._from_file(
        test_utils.get_workspace_file("workspace/freezer/sample_workspace.yaml"),
    )

    builder = WorkspaceBuilder(session_config=session_config, server=session.server)
    builder.build(session=session)
    assert session == builder.session

    time.sleep(0.50)

    session = session
    new_config = freezer.freeze(session)

    validation.validate_schema(new_config)

    # These should dump without an error
    ConfigReader._dump(fmt="json", content=new_config)
    ConfigReader._dump(fmt="yaml", content=new_config)

    # Inline configs should also dump without an error
    compact_config = freezer.inline(new_config)

    ConfigReader._dump(fmt="json", content=compact_config)
    ConfigReader._dump(fmt="yaml", content=compact_config)


"""Tests for :meth:`freezer.inline()`."""

ibefore_workspace = {  # inline config
    "session_name": "sample workspace",
    "start_directory": "~",
    "windows": [
        {
            "shell_command": ["top"],
            "window_name": "editor",
            "panes": [{"shell_command": ["vim"]}, {"shell_command": ['cowsay "hey"']}],
            "layout": "main-vertical",
        },
        {
            "window_name": "logging",
            "panes": [{"shell_command": ["tail -F /var/log/syslog"]}],
        },
        {"options": {"automatic-rename": True}, "panes": [{"shell_command": ["htop"]}]},
    ],
}

iafter_workspace = {
    "session_name": "sample workspace",
    "start_directory": "~",
    "windows": [
        {
            "shell_command": "top",
            "window_name": "editor",
            "panes": ["vim", 'cowsay "hey"'],
            "layout": "main-vertical",
        },
        {"window_name": "logging", "panes": ["tail -F /var/log/syslog"]},
        {"options": {"automatic-rename": True}, "panes": ["htop"]},
    ],
}


def test_inline_workspace() -> None:
    """:meth:`freezer.inline()` shell commands list to string."""
    test_workspace = freezer.inline(ibefore_workspace)
    assert test_workspace == iafter_workspace


def test_export_yaml(
    tmp_path: pathlib.Path,
    config_fixture: WorkspaceTestData,
) -> None:
    """Test exporting a frozen tmux session to YAML."""
    yaml_workspace_file = tmp_path / "config.yaml"

    sample_workspace = freezer.inline(
        config_fixture.sample_workspace.sample_workspace_dict,
    )
    configparser = ConfigReader(sample_workspace)

    yaml_workspace_data = configparser.dump("yaml", indent=2, default_flow_style=False)

    yaml_workspace_file.write_text(yaml_workspace_data, encoding="utf-8")

    new_workspace_data = ConfigReader._from_file(yaml_workspace_file)
    assert config_fixture.sample_workspace.sample_workspace_dict == new_workspace_data