Skip to content

Commit e109d96

Browse files
committed
test: Move inline to freezer
1 parent 0a52a69 commit e109d96

File tree

7 files changed

+137
-131
lines changed

7 files changed

+137
-131
lines changed

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ If you need an internal API stabilized please [file an issue](https://github.com
131131
```
132132

133133
```{eval-rst}
134-
.. automethod:: tmuxp.workspace.config.inline
134+
.. automethod:: tmuxp.workspace.freezer.inline
135135
```
136136

137137
## Exceptions

src/tmuxp/cli/freeze.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from tmuxp.exc import TmuxpException
1010

1111
from .. import util
12-
from ..workspace import config
13-
from ..workspace.freezer import freeze
12+
from ..workspace import freezer
1413
from .utils import get_config_dir, prompt, prompt_choices, prompt_yes_no
1514

1615
if t.TYPE_CHECKING:
@@ -110,8 +109,8 @@ def command_freeze(
110109
print(e)
111110
return
112111

113-
sconf = freeze(session)
114-
newconfig = config.inline(sconf)
112+
sconf = freezer.freeze(session)
113+
newconfig = freezer.inline(sconf)
115114
configparser = ConfigReader(newconfig)
116115

117116
if not args.quiet:

src/tmuxp/workspace/config.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -133,49 +133,6 @@ def expandshell(_path):
133133
return os.path.expandvars(os.path.expanduser(_path))
134134

135135

136-
def inline(workspace_dict):
137-
"""
138-
Return config in inline form, opposite of :meth:`config.expand`.
139-
140-
Parameters
141-
----------
142-
workspace_dict : dict
143-
144-
Returns
145-
-------
146-
dict
147-
configuration with optional inlined configs.
148-
"""
149-
150-
if (
151-
"shell_command" in workspace_dict
152-
and isinstance(workspace_dict["shell_command"], list)
153-
and len(workspace_dict["shell_command"]) == 1
154-
):
155-
workspace_dict["shell_command"] = workspace_dict["shell_command"][0]
156-
157-
if len(workspace_dict.keys()) == int(1):
158-
workspace_dict = workspace_dict["shell_command"]
159-
if (
160-
"shell_command_before" in workspace_dict
161-
and isinstance(workspace_dict["shell_command_before"], list)
162-
and len(workspace_dict["shell_command_before"]) == 1
163-
):
164-
workspace_dict["shell_command_before"] = workspace_dict["shell_command_before"][
165-
0
166-
]
167-
168-
# recurse into window and pane config items
169-
if "windows" in workspace_dict:
170-
workspace_dict["windows"] = [
171-
inline(window) for window in workspace_dict["windows"]
172-
]
173-
if "panes" in workspace_dict:
174-
workspace_dict["panes"] = [inline(pane) for pane in workspace_dict["panes"]]
175-
176-
return workspace_dict
177-
178-
179136
def expand_cmd(p: Dict) -> Dict:
180137
if isinstance(p, str):
181138
p = {"shell_command": [p]}

src/tmuxp/workspace/freezer.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
1+
def inline(workspace_dict):
2+
"""
3+
Return config in inline form, opposite of :meth:`config.expand`.
4+
5+
Parameters
6+
----------
7+
workspace_dict : dict
8+
9+
Returns
10+
-------
11+
dict
12+
configuration with optional inlined configs.
13+
"""
14+
15+
if (
16+
"shell_command" in workspace_dict
17+
and isinstance(workspace_dict["shell_command"], list)
18+
and len(workspace_dict["shell_command"]) == 1
19+
):
20+
workspace_dict["shell_command"] = workspace_dict["shell_command"][0]
21+
22+
if len(workspace_dict.keys()) == int(1):
23+
workspace_dict = workspace_dict["shell_command"]
24+
if (
25+
"shell_command_before" in workspace_dict
26+
and isinstance(workspace_dict["shell_command_before"], list)
27+
and len(workspace_dict["shell_command_before"]) == 1
28+
):
29+
workspace_dict["shell_command_before"] = workspace_dict["shell_command_before"][
30+
0
31+
]
32+
33+
# recurse into window and pane config items
34+
if "windows" in workspace_dict:
35+
workspace_dict["windows"] = [
36+
inline(window) for window in workspace_dict["windows"]
37+
]
38+
if "panes" in workspace_dict:
39+
workspace_dict["panes"] = [inline(pane) for pane in workspace_dict["panes"]]
40+
41+
return workspace_dict
42+
43+
144
def freeze(session):
245
"""
346
Freeze live tmux session and Return session config :py:obj:`dict`.

tests/workspace/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import types
2+
3+
import pytest
4+
5+
from ..fixtures.structures import WorkspaceTestData
6+
7+
8+
@pytest.fixture
9+
def config_fixture():
10+
"""Deferred import of tmuxp.tests.fixtures.*
11+
12+
pytest setup (conftest.py) patches os.environ["HOME"], delay execution of
13+
os.path.expanduser until here.
14+
"""
15+
from ..fixtures import workspace as test_workspace_data
16+
17+
return WorkspaceTestData(
18+
**{
19+
k: v
20+
for k, v in test_workspace_data.__dict__.items()
21+
if isinstance(v, types.ModuleType)
22+
}
23+
)

tests/workspace/test_config.py

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Test for tmuxp configuration import, inlining, expanding and export."""
22
import os
33
import pathlib
4-
import types
54
import typing
65
from typing import Union
76

@@ -17,25 +16,6 @@
1716
from ..fixtures.structures import WorkspaceTestData
1817

1918

20-
@pytest.fixture
21-
def config_fixture():
22-
"""Deferred import of tmuxp.tests.fixtures.*
23-
24-
pytest setup (conftest.py) patches os.environ["HOME"], delay execution of
25-
os.path.expanduser until here.
26-
"""
27-
from ..fixtures import workspace as test_workspace_data
28-
from ..fixtures.structures import WorkspaceTestData
29-
30-
return WorkspaceTestData(
31-
**{
32-
k: v
33-
for k, v in test_workspace_data.__dict__.items()
34-
if isinstance(v, types.ModuleType)
35-
}
36-
)
37-
38-
3919
def load_yaml(path: Union[str, pathlib.Path]) -> str:
4020
return ConfigReader._from_file(
4121
pathlib.Path(path) if isinstance(path, str) else path
@@ -61,22 +41,6 @@ def test_export_json(tmp_path: pathlib.Path, config_fixture: "WorkspaceTestData"
6141
assert config_fixture.sample_workspace.sample_workspace_dict == new_workspace_data
6242

6343

64-
def test_export_yaml(tmp_path: pathlib.Path, config_fixture: "WorkspaceTestData"):
65-
yaml_workspace_file = tmp_path / "config.yaml"
66-
67-
sample_workspace = config.inline(
68-
config_fixture.sample_workspace.sample_workspace_dict
69-
)
70-
configparser = ConfigReader(sample_workspace)
71-
72-
yaml_workspace_data = configparser.dump("yaml", indent=2, default_flow_style=False)
73-
74-
yaml_workspace_file.write_text(yaml_workspace_data, encoding="utf-8")
75-
76-
new_workspace_data = load_workspace(str(yaml_workspace_file))
77-
assert config_fixture.sample_workspace.sample_workspace_dict == new_workspace_data
78-
79-
8044
def test_scan_workspace(tmp_path: pathlib.Path):
8145
configs = []
8246

@@ -123,49 +87,6 @@ def test_workspace_expand2(config_fixture: "WorkspaceTestData"):
12387
assert config.expand(unexpanded_dict) == expanded_dict
12488

12589

126-
"""Tests for :meth:`config.inline()`."""
127-
128-
ibefore_workspace = { # inline config
129-
"session_name": "sample workspace",
130-
"start_directory": "~",
131-
"windows": [
132-
{
133-
"shell_command": ["top"],
134-
"window_name": "editor",
135-
"panes": [{"shell_command": ["vim"]}, {"shell_command": ['cowsay "hey"']}],
136-
"layout": "main-verticle",
137-
},
138-
{
139-
"window_name": "logging",
140-
"panes": [{"shell_command": ["tail -F /var/log/syslog"]}],
141-
},
142-
{"options": {"automatic-rename": True}, "panes": [{"shell_command": ["htop"]}]},
143-
],
144-
}
145-
146-
iafter_workspace = {
147-
"session_name": "sample workspace",
148-
"start_directory": "~",
149-
"windows": [
150-
{
151-
"shell_command": "top",
152-
"window_name": "editor",
153-
"panes": ["vim", 'cowsay "hey"'],
154-
"layout": "main-verticle",
155-
},
156-
{"window_name": "logging", "panes": ["tail -F /var/log/syslog"]},
157-
{"options": {"automatic-rename": True}, "panes": ["htop"]},
158-
],
159-
}
160-
161-
162-
def test_inline_workspace():
163-
""":meth:`config.inline()` shell commands list to string."""
164-
165-
test_workspace = config.inline(ibefore_workspace)
166-
assert test_workspace == iafter_workspace
167-
168-
16990
"""Test config inheritance for the nested 'start_command'."""
17091

17192
inheritance_workspace_before = {

tests/workspace/test_freezer.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
"""Tests for freezing tmux sessions with tmuxp."""
2+
import pathlib
23
import time
4+
import typing
35

46
from tmuxp.config_reader import ConfigReader
5-
from tmuxp.workspace import config
7+
from tmuxp.workspace import config, freezer
68
from tmuxp.workspace.builder import WorkspaceBuilder
7-
from tmuxp.workspace.freezer import freeze
89

910
from ..fixtures import utils as test_utils
1011

12+
if typing.TYPE_CHECKING:
13+
from ..fixtures.structures import WorkspaceTestData
14+
1115

1216
def test_freeze_config(session):
1317
session_config = ConfigReader._from_file(
@@ -21,7 +25,7 @@ def test_freeze_config(session):
2125
time.sleep(0.50)
2226

2327
session = session
24-
new_config = freeze(session)
28+
new_config = freezer.freeze(session)
2529

2630
config.validate_schema(new_config)
2731

@@ -30,7 +34,66 @@ def test_freeze_config(session):
3034
ConfigReader._dump(format="yaml", content=new_config)
3135

3236
# Inline configs should also dump without an error
33-
compact_config = config.inline(new_config)
37+
compact_config = freezer.inline(new_config)
3438

3539
ConfigReader._dump(format="json", content=compact_config)
3640
ConfigReader._dump(format="yaml", content=compact_config)
41+
42+
43+
"""Tests for :meth:`freezer.inline()`."""
44+
45+
ibefore_workspace = { # inline config
46+
"session_name": "sample workspace",
47+
"start_directory": "~",
48+
"windows": [
49+
{
50+
"shell_command": ["top"],
51+
"window_name": "editor",
52+
"panes": [{"shell_command": ["vim"]}, {"shell_command": ['cowsay "hey"']}],
53+
"layout": "main-verticle",
54+
},
55+
{
56+
"window_name": "logging",
57+
"panes": [{"shell_command": ["tail -F /var/log/syslog"]}],
58+
},
59+
{"options": {"automatic-rename": True}, "panes": [{"shell_command": ["htop"]}]},
60+
],
61+
}
62+
63+
iafter_workspace = {
64+
"session_name": "sample workspace",
65+
"start_directory": "~",
66+
"windows": [
67+
{
68+
"shell_command": "top",
69+
"window_name": "editor",
70+
"panes": ["vim", 'cowsay "hey"'],
71+
"layout": "main-verticle",
72+
},
73+
{"window_name": "logging", "panes": ["tail -F /var/log/syslog"]},
74+
{"options": {"automatic-rename": True}, "panes": ["htop"]},
75+
],
76+
}
77+
78+
79+
def test_inline_workspace():
80+
""":meth:`freezer.inline()` shell commands list to string."""
81+
82+
test_workspace = freezer.inline(ibefore_workspace)
83+
assert test_workspace == iafter_workspace
84+
85+
86+
def test_export_yaml(tmp_path: pathlib.Path, config_fixture: "WorkspaceTestData"):
87+
yaml_workspace_file = tmp_path / "config.yaml"
88+
89+
sample_workspace = freezer.inline(
90+
config_fixture.sample_workspace.sample_workspace_dict
91+
)
92+
configparser = ConfigReader(sample_workspace)
93+
94+
yaml_workspace_data = configparser.dump("yaml", indent=2, default_flow_style=False)
95+
96+
yaml_workspace_file.write_text(yaml_workspace_data, encoding="utf-8")
97+
98+
new_workspace_data = ConfigReader._from_file(yaml_workspace_file)
99+
assert config_fixture.sample_workspace.sample_workspace_dict == new_workspace_data

0 commit comments

Comments
 (0)